-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitarray_search_test.go
349 lines (336 loc) · 10.4 KB
/
bitarray_search_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// 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 (
"reflect"
"strconv"
"strings"
"testing"
"github.com/tunabay/go-bitarray"
)
func TestBitArray_HasPrefix(t *testing.T) {
tcs := []struct {
b, p string
h bool
}{
{"", "", true},
{"", "0", false},
{"", "1", false},
{"0", "", true},
{"1", "", true},
{"0", "00", false},
{"1", "11", false},
{"0000-0000 0000", "0000-0000 000", true},
{"0000-0000 0000", "0000-0000 0000", true},
{"0000-0000 0000", "0000-0000 0000-0", false},
{"1111-1111 1111", "1111-1111 111", true},
{"1111-1111 1111", "1111-1111 1111-1", false},
{"0111-1111 1111-1111 1111", "1111", false},
{"1000-0000 0000-0000 0000", "0000", false},
{"1111-0000 0000-0000 0000", "1111", true},
{"0000-1111 1111-1111 1111", "0000", true},
{"0000-0011 1111-1111 1111", "0000-00", true},
{"1111-1100 0000-0000 0000", "1111-11", true},
{"0000-0000 0000-1111 1111", "0000-0001", false},
{"0000-0000 0000-1111 1111", "0000-0000 0000", true},
{"1111-1111 1111-1000 0000", "1111-1111 1111", true},
// TODO: more cases
}
chk := func(ba, prefix *bitarray.BitArray, want bool) {
t.Helper()
if got := ba.HasPrefix(prefix); got != want {
t.Errorf("unexpected result: got %t, want %t", got, want)
t.Logf("target: %#b", ba)
t.Logf("prefix: %#b", prefix)
}
}
for _, tc := range tcs {
baO := bitarray.MustParse(tc.b).ZOptimize()
baE := baO.ZExpand()
preO := bitarray.MustParse(tc.p).ZOptimize()
preE := preO.ZExpand()
chk(baO, preO, tc.h)
chk(baO, preE, tc.h)
chk(baE, preO, tc.h)
chk(baE, preE, tc.h)
}
var nilba *bitarray.BitArray
zeroba := bitarray.New()
ba := bitarray.MustParse("0101")
switch {
case !nilba.HasPrefix(nil): // untyped nil
t.Error("unexpected result: got false, want true")
case !nilba.HasPrefix(nilba):
t.Error("unexpected result: got false, want true")
case !nilba.HasPrefix(zeroba):
t.Error("unexpected result: got false, want true")
case !zeroba.HasPrefix(nilba):
t.Error("unexpected result: got false, want true")
case !ba.HasPrefix(nilba):
t.Error("unexpected result: got false, want true")
case nilba.HasPrefix(ba):
t.Error("unexpected result: got true, want false")
}
}
func TestBitArray_HasSuffix(t *testing.T) {
tcs := []struct {
b, s string
h bool
}{
{"", "", true},
{"", "0", false},
{"", "1", false},
{"0", "", true},
{"1", "", true},
{"0", "00", false},
{"1", "11", false},
{"0000-0000 0000", "0000-0000 000", true},
{"0000-0000 0000", "0000-0000 0000", true},
{"0000-0000 0000", "0000-0000 0000-0", false},
{"1111-1111 1111", "1111-1111 111", true},
{"1111-1111 1111", "1111-1111 1111-1", false},
{"1111-1111 1111-1111 1110", "1111", false},
{"0000-0000 0000-0000 0001", "0000", false},
{"0000-0000 0000-0000 1111", "1111", true},
{"1111-1111 1111-1111 0000", "0000", true},
{"1111-1111 1111-1100 0000", "0000-00", true},
// TODO: more cases
}
chk := func(ba, suffix *bitarray.BitArray, want bool) {
t.Helper()
if got := ba.HasSuffix(suffix); got != want {
t.Errorf("unexpected result: got %t, want %t", got, want)
t.Logf("target: %#b", ba)
t.Logf("suffix: %#b", suffix)
}
}
for _, tc := range tcs {
baO := bitarray.MustParse(tc.b).ZOptimize()
baE := baO.ZExpand()
sufO := bitarray.MustParse(tc.s).ZOptimize()
sufE := sufO.ZExpand()
chk(baO, sufO, tc.h)
chk(baO, sufE, tc.h)
chk(baE, sufO, tc.h)
chk(baE, sufE, tc.h)
}
var nilba *bitarray.BitArray
zeroba := bitarray.New()
ba := bitarray.MustParse("0101")
switch {
case !nilba.HasSuffix(nil): // untyped nil
t.Error("unexpected result: got false, want true")
case !nilba.HasSuffix(nilba):
t.Error("unexpected result: got false, want true")
case !nilba.HasSuffix(zeroba):
t.Error("unexpected result: got false, want true")
case !zeroba.HasSuffix(nilba):
t.Error("unexpected result: got false, want true")
case !ba.HasSuffix(nilba):
t.Error("unexpected result: got false, want true")
case nilba.HasSuffix(ba):
t.Error("unexpected result: got true, want false")
}
}
func TestBitArray_Index(t *testing.T) {
tcs := []struct {
h, n string
i int
}{
{"", "", 0},
{"", "0", -1},
{"", "1", -1},
{"0", "", 0},
{"1", "", 0},
{"0", "00", -1},
{"1", "11", -1},
{"0000-0000 0000", "0000-0000 000", 0},
{"0000-0000 0000", "0000-0000 0000", 0},
{"0000-0000 0000", "0000-0000 0000-0", -1},
{"1111-1111 1111", "1111-1111 111", 0},
{"1111-1111 1111", "1111-1111 1111-1", -1},
{"1111-1111 1111-1111 1110", "1111", 0},
{"1000-0000 0000-0000 0001", "0000", 1},
{"0000-1111 0000-0000 1111", "1111", 4},
{"1111-1111 1111-1111 0000", "0000", 16},
{"1111-1111 1111-1100 0000", "0000-00", 14},
// TODO: more cases
}
chk := func(haystack, needle *bitarray.BitArray, want int) {
t.Helper()
if got := haystack.Index(needle); got != want {
t.Errorf("unexpected result: got %d, want %d", got, want)
t.Logf("haystack: %#b", haystack)
t.Logf(" needle: %#b", needle)
}
}
for _, tc := range tcs {
haystackO := bitarray.MustParse(tc.h).ZOptimize()
haystackE := haystackO.ZExpand()
needleO := bitarray.MustParse(tc.n).ZOptimize()
needleE := needleO.ZExpand()
chk(haystackO, needleO, tc.i)
chk(haystackO, needleE, tc.i)
chk(haystackE, needleO, tc.i)
chk(haystackE, needleE, tc.i)
}
var nilba *bitarray.BitArray
zeroba := bitarray.New()
ba := bitarray.MustParse("0101")
if got := nilba.Index(nil); got != 0 { // untyped nil
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.Index(nilba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.Index(zeroba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := zeroba.Index(nilba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := ba.Index(nilba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.Index(ba); got != -1 {
t.Errorf("unexpected result: got %d, want -1", got)
}
}
func TestBitArray_LastIndex(t *testing.T) {
tcs := []struct {
h, n string
i int
}{
{"", "", 0},
{"", "0", -1},
{"", "1", -1},
{"0", "", 1},
{"1", "", 1},
{"0", "00", -1},
{"1", "11", -1},
{"0000-0000 0000", "0000-0000 000", 1},
{"0000-0000 0000", "0000-0000 0000", 0},
{"0000-0000 0000", "0000-0000 0000-0", -1},
{"1111-1111 1111", "1111-1111 111", 1},
{"1111-1111 1111", "1111-1111 1111-1", -1},
{"1111-1111 1111-1111 1110", "1111", 15},
{"1000-0000 0000-0000 0001", "0000", 15},
{"0000-1111 0000-0000 1111", "1111", 16},
{"1111-1111 1111-1111 0000", "0000", 16},
{"1111-1111 1111-1100 0000", "0000-00", 14},
// TODO: more cases
}
chk := func(haystack, needle *bitarray.BitArray, want int) {
t.Helper()
if got := haystack.LastIndex(needle); got != want {
t.Errorf("unexpected result: got %d, want %d", got, want)
t.Logf("haystack: %#b", haystack)
t.Logf(" needle: %#b", needle)
}
}
for _, tc := range tcs {
haystackO := bitarray.MustParse(tc.h).ZOptimize()
haystackE := haystackO.ZExpand()
needleO := bitarray.MustParse(tc.n).ZOptimize()
needleE := needleO.ZExpand()
chk(haystackO, needleO, tc.i)
chk(haystackO, needleE, tc.i)
chk(haystackE, needleO, tc.i)
chk(haystackE, needleE, tc.i)
}
var nilba *bitarray.BitArray
zeroba := bitarray.New()
ba := bitarray.MustParse("0101")
if got := nilba.LastIndex(nil); got != 0 { // untyped nil
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.LastIndex(nilba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.LastIndex(zeroba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := zeroba.LastIndex(nilba); got != 0 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := ba.LastIndex(nilba); got != 4 {
t.Errorf("unexpected result: got %d, want 0", got)
}
if got := nilba.LastIndex(ba); got != -1 {
t.Errorf("unexpected result: got %d, want -1", got)
}
}
func TestBitArray_AllIndex(t *testing.T) {
tdt := []string{
"", "", "0",
"", "0", "",
"", "1", "",
"0", "", "0,1",
"1", "", "0,1",
"0", "00", "",
"1", "11", "",
"1010", "", "0,1,2,3,4",
"0000-0000 0000", "0000-0000 000", "0,1",
"0000-0000 0000", "0000-0000 0000", "0",
"0000-0000 0000", "0000-0000 0000-0", "",
"1111-1111 1111", "1111-1111 111", "0,1",
"1111-1111 1111", "1111-1111 1111-1", "",
"1111-1111 1011-1101 1111", "1111", "0,1,2,3,4,5,10,15,16",
"1110-0000 0000-0000 0111", "00000", "3,4,5,6,7,8,9,10,11,12",
"0000-1111 0000-0000 1111", "1111", "4,16",
"1111-1111 1111-1111 0000", "0000", "16",
"1111-1111 1111-1100 0000", "0000-00", "14",
// TODO: more cases
}
chk := func(haystack, needle *bitarray.BitArray, want []int) {
t.Helper()
if got := haystack.AllIndex(needle); !reflect.DeepEqual(got, want) {
t.Error("unexpected result:")
t.Logf("haystack: %#b", haystack)
t.Logf(" needle: %#b", needle)
t.Logf(" got: %v", got)
t.Logf(" want: %v", want)
}
}
for i := 0; i < len(tdt); i += 3 {
haystackO := bitarray.MustParse(tdt[i]).ZOptimize()
haystackE := haystackO.ZExpand()
needleO := bitarray.MustParse(tdt[i+1]).ZOptimize()
needleE := needleO.ZExpand()
wants := strings.Split(tdt[i+2], ",")
want := make([]int, 0, len(wants))
for _, ss := range wants {
if s := strings.TrimSpace(ss); s != "" {
if v, err := strconv.Atoi(s); err == nil {
want = append(want, v)
}
}
}
chk(haystackO, needleO, want)
chk(haystackO, needleE, want)
chk(haystackE, needleO, want)
chk(haystackE, needleE, want)
}
var nilba *bitarray.BitArray
zeroba := bitarray.New()
ba := bitarray.MustParse("0101")
if got := nilba.AllIndex(nil); !reflect.DeepEqual(got, []int{0}) { // untyped nil
t.Errorf("unexpected result: got %v, want [0]", got)
}
if got := nilba.AllIndex(nilba); !reflect.DeepEqual(got, []int{0}) {
t.Errorf("unexpected result: got %v, want [0]", got)
}
if got := nilba.AllIndex(zeroba); !reflect.DeepEqual(got, []int{0}) {
t.Errorf("unexpected result: got %v, want [0]", got)
}
if got := zeroba.AllIndex(nilba); !reflect.DeepEqual(got, []int{0}) {
t.Errorf("unexpected result: got %v, want [0]", got)
}
if got := ba.AllIndex(nilba); !reflect.DeepEqual(got, []int{0, 1, 2, 3, 4}) {
t.Errorf("unexpected result: got %v, want [0 1 2 3 4]", got)
}
if got := nilba.AllIndex(ba); !reflect.DeepEqual(got, []int{}) {
t.Errorf("unexpected result: got %v, want []", got)
}
}