forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray_rand_test.go
131 lines (124 loc) · 2.9 KB
/
bitarray_rand_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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"math/rand"
"testing"
"github.com/tunabay/go-bitarray"
)
func TestRand(t *testing.T) {
ba0, err := bitarray.Rand(0)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ba0.V()
if ba0.Len() != 0 {
t.Errorf("unexpected Len: got %d, want 0", ba0.Len())
t.Logf("data: %s", ba0.D())
}
if !ba0.IsZero() {
t.Errorf("unexpected IsZero: got %t, want true", ba0.IsZero())
t.Logf("data: %s", ba0.D())
}
sizes := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 63, 64, 65, 257}
for _, sz := range sizes {
expUniq := 8192
if sz < 13 {
expUniq = 1 << sz
}
hist := make(map[string]int, expUniq)
for i := 0; i < 8192; i++ {
ba, err := bitarray.Rand(sz)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ba.V()
if ba.Len() != sz {
t.Errorf("unexpected Len: got %d, want %d", ba.Len(), sz)
t.Logf("data: %s", ba.D())
}
s := ba.String()
if cnt, dup := hist[s]; dup {
hist[s] = cnt + 1
} else {
hist[s] = 1
}
}
passUniq := expUniq
switch sz {
case 15:
passUniq = 5734 // -30%
case 16:
passUniq = 6553 // -20%
case 17:
passUniq = 7373 // -10%
}
uniq := len(hist)
if uniq < passUniq {
t.Errorf(
"sz=%d: data not distributed as expected: uniq=%d, threshold=%d, ideal=%d",
sz, uniq, passUniq, expUniq,
)
} else {
// t.Logf("sz=%d: uniq=%d, threshold=%d, ideal=%d", sz, uniq, passUniq, expUniq)
}
}
}
func TestPseudoRand(t *testing.T) {
ba0 := bitarray.PseudoRand(0, nil)
ba0.V()
if ba0.Len() != 0 {
t.Errorf("unexpected Len: got %d, want 0", ba0.Len())
t.Logf("data: %s", ba0.D())
}
if !ba0.IsZero() {
t.Errorf("unexpected IsZero: got %t, want true", ba0.IsZero())
t.Logf("data: %s", ba0.D())
}
myRand := rand.New(rand.NewSource(1234))
sizes := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 63, 64, 65, 257}
for i, sz := range sizes {
randSrc := myRand
if i&1 == 0 {
randSrc = nil
}
expUniq := 8192
if sz < 13 {
expUniq = 1 << sz
}
hist := make(map[string]int, expUniq)
for i := 0; i < 8192; i++ {
ba := bitarray.PseudoRand(sz, randSrc)
ba.V()
if ba.Len() != sz {
t.Errorf("unexpected Len: got %d, want %d", ba.Len(), sz)
t.Logf("data: %s", ba.D())
}
s := ba.String()
if cnt, dup := hist[s]; dup {
hist[s] = cnt + 1
} else {
hist[s] = 1
}
}
passUniq := expUniq
switch sz {
case 15:
passUniq = 5734 // -30%
case 16:
passUniq = 6553 // -20%
case 17:
passUniq = 7373 // -10%
}
uniq := len(hist)
if uniq < passUniq {
t.Errorf(
"sz=%d: data not distributed as expected: uniq=%d, threshold=%d, ideal=%d",
sz, uniq, passUniq, expUniq,
)
} else {
// t.Logf("sz=%d: uniq=%d, threshold=%d, ideal=%d", sz, uniq, passUniq, expUniq)
}
}
}