-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_test.go
67 lines (61 loc) · 1.23 KB
/
seq_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
package seq
import (
"strconv"
"sync"
"testing"
)
func TestNewSeq(t *testing.T) {
seq := NewSeq(64)
v := seq.Next()
t.Logf("value: %d, type: %T, binary: %s", v, v, ToBinary(v))
}
func TestRandomSeq(t *testing.T) {
seq := RandomSeq()
v := seq.Next()
t.Logf("value: %d, type: %T, binary: %s", v, v, ToBinary(v))
}
func TestConcurrency(t *testing.T) {
seq := NewSeq(0)
num := 100
group := make([][]int64, num)
wg := sync.WaitGroup{}
wg.Add(num)
for i := 0; i < num; i++ {
group[i] = make([]int64, num)
go func(seq *Seq, ids []int64, i int) {
defer wg.Done()
for j := 0; j < num; j++ {
v := seq.Next()
ids[j] = v
}
}(seq, group[i], i)
}
wg.Wait()
m := make(map[int64]byte)
for i := range group {
for j := range group[i] {
v := group[i][j]
m[v] = 1
t.Logf("v: %d, b: %s", v, ToBinary(v))
}
}
if len(m) != num*num {
t.Error("Conflict", len(m))
} else {
t.Log("OK")
}
}
func TestHex(t *testing.T) {
seq := NewSeq(0)
h := seq.NextHex()
v, _ := strconv.ParseInt(h, 16, 64)
t.Logf("v: %d, b: %s, h: %s", v, ToBinary(v), strconv.FormatInt(v, 16))
}
func ToBinary(n int64) string {
result := ""
for ; n > 0; n /= 2 {
lsb := n % 2
result = strconv.FormatInt(lsb, 10) + result
}
return result
}