-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_test.go
192 lines (171 loc) · 3 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package rid
import (
"fmt"
"testing"
"time"
"encoding/binary"
"encoding/json"
)
func TestJsonMarshalUnmarshal(t *testing.T) {
type test struct {
ID ID `json:"id"`
Name string `json:"name"`
}
a := test{
ID: New(),
Name: "Test",
}
d, err := json.Marshal(a)
if err != nil {
fmt.Println(err.Error())
t.Fail()
return
}
var b test
err = json.Unmarshal(d, &b)
if err != nil {
fmt.Println(err.Error())
t.Fail()
return
}
if a.ID != b.ID {
t.Fail()
return
}
}
func TestRandomUint32(t *testing.T) {
if b := randUint32(); b == 0 {
t.Fail()
return
}
}
func TestEpochCapacity(t *testing.T) {
if getEpoch().Add(time.Duration(uint64(0x7FFFFFFFFFFFFFFF))).Year() != 2310 {
t.Fail()
return
}
}
func TestNanosecondsAccuracy(t *testing.T) {
ns := time.Now().Sub(epoch).Nanoseconds()
var b [8]byte
_ = b[7]
b[0] = byte(ns >> 56)
b[1] = byte(ns >> 48)
b[2] = byte(ns >> 40)
b[3] = byte(ns >> 32)
b[4] = byte(ns >> 24)
b[5] = byte(ns >> 16)
b[6] = 0
b[7] = 0
short := int64(binary.BigEndian.Uint64(b[:]))
if (short * 100 / ns) < 90 {
t.Fail()
return
}
}
func TestID_IsNil(t *testing.T) {
id := New()
if id.IsNil() {
t.Fail()
return
}
id = ID{}
if !id.IsNil() {
t.Fail()
return
}
}
func TestNumeralString(t *testing.T) {
src := New()
id, err := FromString(src.NumeralString())
if err != nil {
fmt.Println(err.Error())
t.Fail()
return
}
if id != src {
fmt.Println(id, "!=", src)
t.Fail()
return
}
}
func TestIDInfo(t *testing.T) {
s := GlobalSource()
id, now := New(), time.Now()
if id.Counter() != s.Counter() {
fmt.Println("[ERROR] Invalid counter")
t.Fail()
return
}
if id.Mid() != Mid() {
fmt.Println("[ERROR] Invalid MID")
t.Fail()
return
}
if id.Pid() != uint16(Pid()) {
fmt.Println("[ERROR] Invalid PID")
t.Fail()
return
}
if id.Time().Unix() != now.Unix() {
fmt.Println("[ERROR] Invalid Timestamp")
t.Fail()
return
}
}
func TestEncodeDecode(t *testing.T) {
var id, cid ID
for i := 0; i < 1000; i++ {
id = New()
cid, _ = FromString(id.String())
if id != cid {
fmt.Println("[ERROR]", id[:], " != ", cid[:])
t.Fail()
return
}
}
}
func BenchmarkNew(b *testing.B) {
fmt.Println("BenchmarkNew")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = New()
}
})
}
func BenchmarkNewString(b *testing.B) {
fmt.Println("BenchmarkNewString")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = New().String()
}
})
}
func BenchmarkNewInfo(b *testing.B) {
fmt.Println("BenchmarkNewInfo")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
id := New()
id.Mid()
id.Pid()
id.Time()
id.Counter()
}
})
}
func BenchmarkNewNumeralString(b *testing.B) {
fmt.Println("BenchmarkNewNumeralString")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = New().NumeralString()
}
})
}
func BenchmarkFromString(b *testing.B) {
fmt.Println("BenchmarkFromString")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = FromString("t9eti00dzt0e0hkibo9g")
}
})
}