-
Notifications
You must be signed in to change notification settings - Fork 2
/
set256_test.go
169 lines (160 loc) · 3.24 KB
/
set256_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
package bit
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
func sampleSet256() Set256 {
var s Set256
s.Add(3)
s.Add(63)
s.Add(17)
s.Add(70)
s.Add(200)
s.Add(201)
s.Add(192)
return s
}
func TestBasics256(t *testing.T) {
s := sampleSet256()
want := "{3, 17, 63, 70, 192, 200, 201}"
got := s.String()
if got != want {
t.Errorf("s.String() = %q, want %q", got, want)
}
if !s.Equal(&s) {
t.Fatal("not equal")
}
if !cmp.Equal(naiveElementsUint64(&s), []uint64{3, 17, 63, 70, 192, 200, 201}) {
t.Errorf("%s: wrong elements", s)
}
if s.Size() != 7 {
t.Error("wrong size")
}
if s.Empty() {
t.Error("shouldn't be empty")
}
var z Set256
if !z.Empty() {
t.Error("should be empty")
}
}
func TestElements256(t *testing.T) {
var a [10]uint8
s := sampleSet64()
for _, test := range []struct {
n int
start uint8
want []uint8
}{
{0, 0, []uint8{}},
{0, 10, []uint8{}},
{1, 0, []uint8{3}},
{1, 5, []uint8{17}},
{1, 27, []uint8{63}},
{2, 0, []uint8{3, 17}},
{2, 5, []uint8{17, 63}},
{2, 39, []uint8{63}},
{2, 63, []uint8{63}},
{2, 83, []uint8{}},
{3, 0, []uint8{3, 17, 63}},
{3, 10, []uint8{17, 63}},
{3, 99, []uint8{}},
} {
n := s.Elements(a[:test.n], test.start)
got := a[:n]
if !reflect.DeepEqual(got, test.want) {
t.Errorf("%+v: got %v, want %v", test, got, test.want)
}
}
}
func TestElements64_256(t *testing.T) {
var a [10]uint64
s := sampleSet64()
for _, test := range []struct {
n int
start uint64
high uint64
want []uint64
}{
{0, 0, 0, []uint64{}},
{0, 10, 0, []uint64{}},
{1, 0, 0, []uint64{3}},
{1, 5, 0, []uint64{17}},
{1, 27, 0, []uint64{63}},
{2, 0, 0, []uint64{3, 17}},
{2, 5, 0, []uint64{17, 63}},
{2, 39, 0, []uint64{63}},
{2, 63, 0, []uint64{63}},
{2, 83, 0, []uint64{}},
{3, 0, 0, []uint64{3, 17, 63}},
{3, 10, 0, []uint64{17, 63}},
{3, 99, 0, []uint64{}},
{3, 10, 64, []uint64{64 + 17, 64 + 63}},
} {
n := s.Elements64(a[:test.n], uint8(test.start), test.high)
got := a[:n]
if !reflect.DeepEqual(got, test.want) {
t.Errorf("%+v: got %v, want %v", test, got, test.want)
}
}
}
func TestPosition256(t *testing.T) {
s := sampleSet64()
for _, test := range []struct {
n uint8
pos int
in bool
}{
{0, 0, false},
{1, 0, false},
{2, 0, false},
{3, 0, true},
{4, 1, false},
{10, 1, false},
{16, 1, false},
{17, 1, true},
{20, 2, false},
{62, 2, false},
{63, 2, true},
} {
gotPos, gotIn := s.Position(test.n)
if gotPos != test.pos || gotIn != test.in {
t.Errorf("Position(%d) = (%d, %t), want (%d, %t)", test.n, gotPos, gotIn, test.pos, test.in)
}
}
}
func naiveElementsUint64(s interface {
Capacity() int
Contains(uint8) bool
}) []uint64 {
var els []uint64
for i := 0; i < s.Capacity(); i++ {
u := uint8(i)
if s.Contains(u) {
els = append(els, uint64(u))
}
}
return els
}
func TestIntersectN(t *testing.T) {
var c Set256
b1 := sampleSet256()
b2 := sampleSet256()
c.IntersectN([]*Set256{&b1, &b2})
if !c.Equal(&b1) {
t.Fatal("not equal")
}
c.IntersectN([]*Set256{&b1, &b2, &Set256{}})
if !c.Empty() {
t.Fatal("not empty")
}
var b3 Set256
b3.Add(201)
b3.Add(188)
b3.Add(254)
c.IntersectN([]*Set256{&b1, &b3})
if c.Size() != 1 || !c.Contains(201) {
t.Fatal("bad c")
}
}