forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_matrix_test.go
327 lines (296 loc) · 9.95 KB
/
bit_matrix_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
package rsmt2d
import (
"testing"
)
func Test_bitMatrix_ColRangeIsOne(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
type colRange struct {
c int
start int
end int
}
tests := []struct {
name string
initParams initParams
setBits []int
rangeInCol colRange
want bool
}{
{"empty", initParams{4, 16}, nil, colRange{0, 0, 4}, false},
{"empty", initParams{4, 16}, nil, colRange{1, 0, 4}, false},
{"empty", initParams{4, 16}, nil, colRange{2, 0, 4}, false},
{"empty", initParams{4, 16}, nil, colRange{3, 0, 4}, false},
{"none set in requested range", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, colRange{0, 0, 4}, false},
{"none set in requested range", initParams{4, 16}, []int{0, 4}, colRange{0, 1, 3}, false},
{"all set in requested range", initParams{4, 16}, []int{0, 4, 8}, colRange{0, 0, 1}, true},
{"all set in requested range", initParams{4, 16}, []int{0, 4, 8}, colRange{0, 0, 2}, true},
{"all set in requested range", initParams{4, 16}, []int{0, 4, 8, 16}, colRange{0, 0, 1}, true},
{"all set in requested range", initParams{4, 16}, []int{0, 4, 8, 16}, colRange{0, 0, 2}, true},
{"all set in requested range", initParams{4, 16}, []int{0, 4, 8, 12}, colRange{0, 0, 3}, true},
{"all set in requested range", initParams{4, 16}, []int{1, 5, 9, 13}, colRange{1, 0, 3}, true},
{"all set in requested range", initParams{4, 16}, []int{2, 6, 10, 14}, colRange{2, 0, 3}, true},
{"all set in requested range", initParams{4, 16}, []int{3, 7, 11, 15}, colRange{3, 0, 3}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.ColRangeIsOne(tt.rangeInCol.c, tt.rangeInCol.start, tt.rangeInCol.end); got != tt.want {
t.Errorf("ColRangeIsOne() = %v, want %v", got, tt.want)
}
})
}
}
func Test_bitMatrix_ColIsOne(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
tests := []struct {
name string
initParams initParams
setBits []int
col int
want bool
}{
{"empty", initParams{4, 16}, []int{}, 0, false},
{"empty", initParams{4, 16}, []int{}, 1, false},
{"empty", initParams{4, 16}, []int{}, 2, false},
{"empty", initParams{4, 16}, []int{}, 3, false},
{"col_0 == 1", initParams{4, 16}, []int{0, 4, 8, 12}, 0, true},
{"col_1 != 1", initParams{4, 16}, []int{0, 4, 8, 12}, 1, false},
{"col_2 != 1", initParams{4, 16}, []int{0, 4, 8, 12}, 2, false},
{"col_3 != 1", initParams{4, 16}, []int{0, 4, 8, 12}, 3, false},
{"col_0 != 1", initParams{4, 16}, []int{1, 5, 9, 13}, 0, false},
{"col_1 == 1", initParams{4, 16}, []int{1, 5, 9, 13}, 1, true},
{"col_2 != 1", initParams{4, 16}, []int{1, 5, 9, 13}, 2, false},
{"col_3 != 1", initParams{4, 16}, []int{1, 5, 9, 13}, 3, false},
{"col_0 != 1", initParams{4, 16}, []int{2, 6, 10, 14}, 0, false},
{"col_1 != 1", initParams{4, 16}, []int{2, 6, 10, 14}, 1, false},
{"col_2 == 1", initParams{4, 16}, []int{2, 6, 10, 14}, 2, true},
{"col_3 != 1", initParams{4, 16}, []int{2, 6, 10, 14}, 3, false},
{"col_0 != 1", initParams{4, 16}, []int{3, 7, 11, 15}, 0, false},
{"col_1 != 1", initParams{4, 16}, []int{3, 7, 11, 15}, 1, false},
{"col_2 != 1", initParams{4, 16}, []int{3, 7, 11, 15}, 2, false},
{"col_3 == 1", initParams{4, 16}, []int{3, 7, 11, 15}, 3, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.ColIsOne(tt.col); got != tt.want {
t.Errorf("ColIsOne() = %v, want %v", got, tt.want)
}
})
}
}
type setIdx struct {
row int
col int
}
func Test_bitMatrix_Get(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
tests := []struct {
name string
initParams initParams
setIndices []setIdx
}{
{"empty", initParams{8, 64}, nil},
{"(0,0)", initParams{8, 64}, []setIdx{{0, 0}}},
{"(7,7)", initParams{8, 64}, []setIdx{{7, 7}}},
{"(0,0),(7,7)", initParams{8, 64}, []setIdx{{0, 0}, {7, 7}}},
{"(0,7)", initParams{8, 64}, []setIdx{{0, 0}, {7, 7}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, ind := range tt.setIndices {
bm.Set(ind.row, ind.col)
}
for r := 0; r < tt.initParams.squareSize; r++ {
for c := 0; c < tt.initParams.squareSize; c++ {
want := contained(r, c, tt.setIndices)
if got := bm.Get(r, c); got != want {
t.Errorf("Get() = %v, want %v", got, want)
}
}
}
})
}
}
func contained(r int, c int, indices []setIdx) bool {
for _, idx := range indices {
if r == idx.row && c == idx.col {
return true
}
}
return false
}
func Test_bitMatrix_NumOnesInCol(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
tests := []struct {
name string
initParams initParams
setBits []int
col int
want int
}{
{"empty", initParams{4, 16}, []int{}, 0, 0},
{"empty", initParams{4, 16}, []int{}, 1, 0},
{"empty", initParams{4, 16}, []int{}, 2, 0},
{"empty", initParams{4, 16}, []int{}, 3, 0},
{"1", initParams{4, 16}, []int{0}, 0, 1},
{"2", initParams{4, 16}, []int{0, 4}, 0, 2},
{"3", initParams{4, 16}, []int{0, 4, 8}, 0, 3},
{"col == 1", initParams{4, 16}, []int{0, 4, 8, 12}, 0, 4},
{"1", initParams{4, 16}, []int{0}, 1, 0},
{"1", initParams{4, 16}, []int{0}, 2, 0},
{"1", initParams{4, 16}, []int{0}, 3, 0},
{"1", initParams{4, 16}, []int{3}, 3, 1},
{"2", initParams{4, 16}, []int{3, 7}, 3, 2},
{"3", initParams{4, 16}, []int{3, 7, 11}, 3, 3},
{"4", initParams{4, 16}, []int{3, 7, 11, 15}, 3, 4},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.NumOnesInCol(tt.col); got != tt.want {
t.Errorf("NumOnesInCol() = %v, want %v", got, tt.want)
}
})
}
}
func Test_bitMatrix_NumOnesInRow(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
tests := []struct {
name string
initParams initParams
setBits []int
row int
want int
}{
{"empty", initParams{4, 16}, []int{}, 0, 0},
{"empty", initParams{4, 16}, []int{}, 1, 0},
{"empty", initParams{4, 16}, []int{}, 2, 0},
{"empty", initParams{4, 16}, []int{}, 3, 0},
{"empty", initParams{64, 4096}, []int{}, 0, 0},
{"empty", initParams{128, 128 * 128}, []int{}, 0, 0},
{"empty", initParams{256, 256 * 256}, []int{}, 0, 0},
{"empty", initParams{512, 512 * 512}, []int{}, 0, 0},
{"1", initParams{4, 16}, []int{0}, 0, 1},
{"2", initParams{4, 16}, []int{0, 1}, 0, 2},
{"3", initParams{4, 16}, []int{0, 1, 2}, 0, 3},
{"4", initParams{4, 16}, []int{0, 1, 2, 3}, 0, 4},
{"1", initParams{4, 16}, []int{12}, 3, 1},
{"2", initParams{4, 16}, []int{12, 13}, 3, 2},
{"3", initParams{4, 16}, []int{12, 13, 14}, 3, 3},
{"4", initParams{4, 16}, []int{12, 13, 14, 15}, 3, 4},
{"4", initParams{32, 1024}, []int{0, 1, 2, 3}, 0, 4},
{"1", initParams{32, 1024}, []int{1023}, 31, 1},
{"2", initParams{32, 1024}, []int{992, 1023}, 31, 2},
{"3", initParams{32, 1024}, []int{992, 995, 1023}, 31, 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.NumOnesInRow(tt.row); got != tt.want {
t.Errorf("NumOnesInRow() = %v, want %v", got, tt.want)
}
})
}
}
func Test_bitMatrix_RowIsOne(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
tests := []struct {
name string
initParams initParams
setBits []int
row int
want bool
}{
{"empty", initParams{4, 16}, []int{}, 0, false},
{"empty", initParams{4, 16}, []int{}, 1, false},
{"empty", initParams{4, 16}, []int{}, 2, false},
{"empty", initParams{4, 16}, []int{}, 3, false},
{"all 1", initParams{4, 16}, []int{0, 1, 2, 3}, 0, true},
{"empty", initParams{64, 4096}, []int{}, 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.RowIsOne(tt.row); got != tt.want {
t.Errorf("RowIsOne() = %v, want %v", got, tt.want)
}
})
}
}
func Test_bitMatrix_RowRangeIsOne(t *testing.T) {
type initParams struct {
squareSize int
bits int
}
type rowRange struct {
r int
start int
end int
}
tests := []struct {
name string
initParams initParams
setBits []int
rangeInRow rowRange
want bool
}{
{"empty", initParams{4, 16}, nil, rowRange{0, 0, 4}, false},
{"empty", initParams{4, 16}, nil, rowRange{1, 0, 4}, false},
{"empty", initParams{4, 16}, nil, rowRange{2, 0, 4}, false},
{"empty", initParams{4, 16}, nil, rowRange{3, 0, 4}, false},
{"true", initParams{4, 16}, []int{0, 1, 2, 3}, rowRange{0, 0, 4}, true},
{"true", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{0, 0, 3}, true},
{"true", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{0, 0, 2}, true},
{"true", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{0, 0, 1}, true},
{"true", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{1, 0, 1}, true},
{"true", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{1, 0, 2}, true},
{"false", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{1, 0, 3}, false},
{"false", initParams{4, 16}, []int{0, 1, 2, 3, 4, 5, 6}, rowRange{1, 0, 4}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := newBitMatrix(tt.initParams.squareSize)
for _, flatIdx := range tt.setBits {
bm.SetFlat(flatIdx)
}
if got := bm.RowRangeIsOne(tt.rangeInRow.r, tt.rangeInRow.start, tt.rangeInRow.end); got != tt.want {
t.Errorf("RowRangeIsOne() = %v, want %v", got, tt.want)
}
})
}
}