...
1
2
3
4
5 package set
6
7 import (
8 "math/rand"
9 "testing"
10 )
11
12 const maxLimit = 1024
13
14 var toSet, toClear [maxLimit]bool
15
16 func init() {
17 r := rand.New(rand.NewSource(0))
18 for i := 0; i < maxLimit; i++ {
19 toSet[i] = r.Intn(2) == 0
20 toClear[i] = r.Intn(2) == 0
21 }
22 }
23
24 func TestInts(t *testing.T) {
25 ns := new(Ints)
26
27
28 wantLen := 0
29 if ns.Len() != wantLen {
30 t.Errorf("init: Len() = %d, want %d", ns.Len(), wantLen)
31 }
32 for i := 0; i < maxLimit; i++ {
33 if ns.Has(uint64(i)) {
34 t.Errorf("init: Has(%d) = true, want false", i)
35 }
36 }
37
38
39 for i, b := range toSet[:maxLimit] {
40 if b {
41 ns.Set(uint64(i))
42 wantLen++
43 }
44 }
45
46
47 if ns.Len() != wantLen {
48 t.Errorf("after Set: Len() = %d, want %d", ns.Len(), wantLen)
49 }
50 for i := 0; i < maxLimit; i++ {
51 if got := ns.Has(uint64(i)); got != toSet[i] {
52 t.Errorf("after Set: Has(%d) = %v, want %v", i, got, !got)
53 }
54 }
55
56
57 for i, b := range toClear[:maxLimit] {
58 if b {
59 ns.Clear(uint64(i))
60 if toSet[i] {
61 wantLen--
62 }
63 }
64 }
65
66
67 if ns.Len() != wantLen {
68 t.Errorf("after Clear: Len() = %d, want %d", ns.Len(), wantLen)
69 }
70 for i := 0; i < maxLimit; i++ {
71 if got := ns.Has(uint64(i)); got != toSet[i] && !toClear[i] {
72 t.Errorf("after Clear: Has(%d) = %v, want %v", i, got, !got)
73 }
74 }
75 }
76
View as plain text