-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test.go
127 lines (110 loc) · 3.35 KB
/
generate_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
// Copyright (c) 2023 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package refid
import (
"math/rand"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestGenerateTime(t *testing.T) {
t.Parallel()
ts, _ := time.Parse(time.RFC3339, "2023-09-14T10:27:21.826305Z")
millis := ts.UTC().UnixMilli()
b := make([]byte, size)
setTime(b, millis)
assert.DeepEqual(t, b, []byte{
0b00001100, 0b01010100, 0b10011001, 0b11011001,
0b00110011, 0b00010000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
// all of these should evaulate to 0 time (epoch)
// eg. you can't go further back in time
setTime(b, 0)
assert.DeepEqual(t, b, []byte{
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
setTime(b, -1)
assert.DeepEqual(t, b, []byte{
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
setTime(b, -1)
assert.DeepEqual(t, b, []byte{
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
// similarly, you can't go too far into the future
setTime(b, maxTime)
assert.DeepEqual(t, b, []byte{
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
setTime(b, maxTime+1)
assert.DeepEqual(t, b, []byte{
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
setTime(b, maxTime+2)
assert.DeepEqual(t, b, []byte{
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
})
}
func TestGenerateRandom(t *testing.T) {
t.Parallel()
b := []byte{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
// use a seeded psuedorandom source for testing
r := rand.New(rand.NewSource(99))
err := setRandom(b[8:], r)
assert.NilError(t, err)
// ensure first 8 bytes remain unmodified
assert.DeepEqual(t, b[:8], []byte{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
})
assert.DeepEqual(t, b[8:], []byte{
0x75, 0xed, 0x18, 0x42,
0x49, 0xe9, 0xbc, 0x19,
})
}
func TestGenerateBoth(t *testing.T) {
t.Parallel()
b := make([]byte, size)
// use a seeded psuedorandom source for testing
r := rand.New(rand.NewSource(99))
ts, _ := time.Parse(time.RFC3339, "2023-09-14T10:27:21.826305Z")
millis := ts.UTC().UnixMilli()
setTime(b, millis)
err := setRandom(b, r)
assert.NilError(t, err)
rID, err := FromBytes(b)
assert.NilError(t, err)
rID.SetTag(3)
assert.DeepEqual(t, rID.Bytes(), []byte{
0x75, 0xed, 0x18, 0x42,
0x49, 0xe9, 0xbc, 0x03,
0x67, 0x5e, 0x4d, 0x1f,
0x76, 0x62, 0x13, 0xda,
})
}