-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathid_test.go
87 lines (65 loc) · 1.63 KB
/
id_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
package tlog
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"sync"
"testing"
"github.com/nikandfor/assert"
)
func testRandID(seed int64) func() ID {
var mu sync.Mutex
rnd := rand.New(rand.NewSource(seed)) //nolint:gosec
return func() (id ID) {
defer mu.Unlock()
mu.Lock()
for id == (ID{}) {
_, _ = rnd.Read(id[:])
}
return
}
}
func TestIDFromString(tb *testing.T) {
id, err := IDFromString("e6a5d996-99b1-493e-ad74-47382220d1a9")
assert.NoError(tb, err)
assert.Equal(tb, ID{0xe6, 0xa5, 0xd9, 0x96, 0x99, 0xb1, 0x49, 0x3e, 0xad, 0x74, 0x47, 0x38, 0x22, 0x20, 0xd1, 0xa9}, id)
_, err = IDFromString("e6a5d996-99b1-493e-ad74-47382220d1a")
assert.ErrorIs(tb, err, ShortIDError{Bytes: 15})
}
func TestIDJSON(t *testing.T) {
id := testRandID(1)()
data, err := json.Marshal(id)
assert.NoError(t, err)
t.Logf("json encoded id: %s (% x)", data, []byte(id[:]))
var back ID
err = json.Unmarshal(data, &back)
assert.NoError(t, err)
assert.Equal(t, id, back)
}
func BenchmarkIDStringUUID(b *testing.B) {
b.ReportAllocs()
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
for i := 0; i < b.N; i++ {
_ = id.StringUUID()
}
}
func BenchmarkIDFormat(b *testing.B) {
b.ReportAllocs()
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
for i := 0; i < b.N; i++ {
fmt.Fprintf(io.Discard, "%+x", id)
}
}
func BenchmarkIDFormatTo(b *testing.B) {
b.ReportAllocs()
var buf [40]byte
id := ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
for i := 0; i < b.N; i++ {
if i&0xf == 0 {
ID{}.FormatTo(buf[:], 0, 'v')
} else {
id.FormatTo(buf[:], 0, 'v')
}
}
}