-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
92 lines (80 loc) · 2.41 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
88
89
90
91
92
package uid64
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var TestIDs = []struct {
timestamp int64
entropy uint8
generatorID uint8
counter uint8
}{
{time.Now().UnixMilli(), 0xff, 0, 0},
{0, 0x0f, 0, 1},
{1, 0xf0, 1, 2},
{0x7ffffffffffffff, 255, 2, 3},
{0x000000000000000, 0, 3, 4},
{0x8fffffff0000000, 0, 0, 7},
{0x10000000fffffff, 0, 1, 8},
{0x01000000fffffff, 0, 2, 15},
{0x00100000fff00ff, 0, 3, 16},
{0x00010000fffff00, 0, 0, 31},
{0x000010001234567, 1, 1, 32},
{0x0000010089abcde, 255, 2, 63},
{0x00000010fffffff, 255, 3, 63},
{0x0000000100fffff, 15, 0, 63},
}
func TestInitUID(t *testing.T) {
for _, fields := range TestIDs {
c := fields
// InitUID and prepare expected timestamp
id, err := InitUID(c.timestamp, c.entropy, c.generatorID, c.counter)
if err != nil {
fmt.Println(err)
t.FailNow()
}
expectedTS := c.timestamp & 0x0000ffffffffffff
// Confirm value with UID.methods
assert.Equal(t, expectedTS, id.Timestamp())
assert.Equal(t, c.entropy, id.Entropy())
assert.Equal(t, c.counter, id.Counter())
assert.Equal(t, c.generatorID, id.GeneratorID())
}
}
func TestIntConversion(t *testing.T) {
for _, fields := range TestIDs {
f := fields
// original UID
// interger: originals' integer representaion
// uid: recovered UID from integer
original, _ := InitUID(f.timestamp, f.entropy, f.generatorID, f.counter)
integer := original.ToInt()
uid, err := FromInt(integer)
assert.Nil(t, err)
// Confirm restored from int is same to the original one.
assert.Equal(t, integer, uid.ToInt())
assert.Equal(t, original.Timestamp(), uid.Timestamp())
assert.Equal(t, original.Entropy(), uid.Entropy())
assert.Equal(t, original.Counter(), uid.Counter())
assert.Equal(t, original.GeneratorID(), uid.GeneratorID())
}
}
func TestStringConversion(t *testing.T) {
for _, fields := range TestIDs {
f := fields
// prepare values
original, _ := InitUID(f.timestamp, f.entropy, f.generatorID, f.counter)
parsed, err := Parse(original.String())
if !assert.Nil(t, err) {
t.FailNow()
}
// Confirm restored form int is same to the original one.
assert.Equal(t, original.String(), parsed.String())
assert.Equal(t, original.Timestamp(), parsed.Timestamp())
assert.Equal(t, original.Entropy(), parsed.Entropy())
assert.Equal(t, original.Counter(), parsed.Counter())
assert.Equal(t, original.GeneratorID(), parsed.GeneratorID())
}
}