-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
149 lines (129 loc) · 3.17 KB
/
uuid_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
142
143
144
145
146
147
148
149
package uuid
import (
"testing"
goog "github.com/google/uuid"
)
func TestNilParse(t *testing.T) {
var nilUUID UUID
u, err := Parse("00000000-0000-0000-0000-000000000000")
if err != nil {
t.Fatal(err)
}
if u != nilUUID {
t.Fatal("parse nil uuid failed")
}
}
func TestParseToString(t *testing.T) {
str := "15588635-a45e-4867-aadb-dbf0385ade95"
u, err := Parse(str)
if err != nil {
t.Fatal(err)
}
res := u.String()
if res != str {
t.Errorf("parse/string roundtrip failed, expected %s, got %s", str, res)
}
}
func TestParseToAppend(t *testing.T) {
str := "15588635-a45e-4867-aadb-dbf0385ade95"
u, err := Parse(str)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 36)
res := u.AppendFormatted(buf[0:0])
if string(res) != str {
t.Errorf("parse/string roundtrip failed, expected %s, got %s", str, res)
}
}
var (
examples = []struct {
namespace string
input string
expected string
}{
{
"15588635-a45e-4867-aadb-dbf0385ade95",
"input 1",
"4c816dc1-9418-502e-9b91-f17b83891bf8",
},
{
"15588635-a45e-4867-aadb-dbf0385ade95",
"input 123456123456123456123456123456123456123456123456123456123456123456123456123456123456",
"0a550dec-ba3e-52c2-a54d-60795ea13c90",
},
}
)
func doTest(t *testing.T, f func(ns, input string) string) {
t.Helper()
for _, example := range examples {
result := f(example.namespace, example.input)
if result != example.expected {
t.Errorf("expected %s with ns %s to output %s but got %s", example.input, example.namespace, example.expected, result)
}
}
}
func TestExamples(t *testing.T) {
doTest(t, func(namespace, input string) string {
ns, err := Parse(namespace)
if err != nil {
t.Fatal(err)
}
var u UUID
NewSHA1Gen(ns).Generate(&u, []byte(input))
return u.String()
})
}
// This is simply to test that the widely used google implementation gets the
// same test results as our implementation.
func TestExamplesGoogle(t *testing.T) {
doTest(t, func(namespace, input string) string {
return goog.NewSHA1(goog.MustParse(namespace), []byte(input)).String()
})
}
func BenchmarkGenerate(b *testing.B) {
for _, input := range []struct {
name string
input []byte
}{
{"short", []byte("example.com")},
{"longer", []byte("benchmark input string 15588635-a45e-4867-aadb-dbf0385ade95 15588635-a45e-4867-aadb-dbf0385ade95 15588635-a45e-4867-aadb-dbf0385ade95 15588635-a45e-4867-aadb-dbf0385ade95")},
} {
b.Run(input.name, func(b *testing.B) {
b.ReportAllocs()
ns, err := Parse("15588635-a45e-4867-aadb-dbf0385ade95")
if err != nil {
b.Fatal(err)
}
gen := NewSHA1Gen(ns)
b.ResetTimer()
var u UUID
for i := 0; i < b.N; i++ {
gen.Generate(&u, input.input)
}
})
}
}
func BenchmarkString(b *testing.B) {
b.ReportAllocs()
example, err := Parse("15588635-a45e-4867-aadb-dbf0385ade95")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = example.String()
}
}
func BenchmarkAppendBytes(b *testing.B) {
b.ReportAllocs()
example, err := Parse("15588635-a45e-4867-aadb-dbf0385ade95")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
buf := make([]byte, 36)
for i := 0; i < b.N; i++ {
buf = example.AppendFormatted(buf[0:0])
}
}