-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
141 lines (104 loc) · 2.71 KB
/
generator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package uuid
import (
"errors"
"net"
"testing"
)
type testReader struct{ wasWritten bool }
func (w *testReader) Read(buf []byte) (int, error) {
w.wasWritten = true
return 16, nil
}
type byteReader struct {
index int
done bool
count uint8
}
func (w *byteReader) Read(buf []byte) (int, error) {
if w.count == 0xff {
w.done = true
return 0, nil
}
buf[w.index] = byte(w.count)
w.count++
return 16, nil
}
func testGeneratorFactory(version Version) *Generator {
return &Generator{&testReader{}, version}
}
func byteReaderGeneratorFactory(index int, version Version) (*Generator, *byteReader) {
w := &byteReader{index, false, 0}
gen := &Generator{w, version}
return gen, w
}
func newTestConfiguration(version Version) Configuration {
return Configuration{
version,
[]net.Interface{},
func(bs []byte) (int, error) { return 0, nil },
}
}
func TestNewGeneratorInvalidVersion(t *testing.T) {
config := newTestConfiguration(255)
g, err := NewGenerator(config)
if err != errUnknownVersion {
t.Errorf("Expected error %s with version %d; got %s", errUnknownVersion, -1, err)
}
if g != nil {
t.Errorf("Expected invalid version to return a nil generator")
}
}
func TestNewGeneratorFailedVersion1(t *testing.T) {
expectedErr := errors.New("Failed to read random bytes")
config := Configuration{
1,
[]net.Interface{},
func(bs []byte) (int, error) { return 0, expectedErr },
}
g, err := NewGenerator(config)
if err != expectedErr {
t.Errorf("Expected faulted random source to return an error %s; got %s", expectedErr, err)
}
if g != nil {
t.Errorf("Expected a nil generator when an error is returned")
}
}
func TestNewGeneratorValidVersions(t *testing.T) {
versions := []Version{1, 4}
for _, version := range versions {
config := newTestConfiguration(version)
_, err := NewGenerator(config)
if err != nil {
t.Errorf("Did not expect generator to return error for valid version; got %s", err)
}
}
}
func TestGeneratorVariant(t *testing.T) {
gen, w := byteReaderGeneratorFactory(8, 0)
for !w.done {
uuid := gen.Generate()
varaint := uuid[8] & 0xC0
if varaint != byte(0x80) {
t.Errorf("Expected any value of varaint field to begin with 0x04; Got %x", varaint)
}
}
}
func TestGeneratorVersion(t *testing.T) {
versions := []Version{1, 2, 3, 4, 5}
for _, version := range versions {
gen, w := byteReaderGeneratorFactory(6, version)
for !w.done {
uuid := gen.Generate()
got := uuid.Version()
if got != version {
t.Errorf("Expected any value of version field to equal generator version %d; Got %d", version, got)
}
}
}
}
func BenchmarkGeneratorGenerate(b *testing.B) {
gen := testGeneratorFactory(1)
for i := 0; i < b.N; i++ {
gen.Generate()
}
}