v2 / vlib / bitfield / bitfield_test.v
380 lines · 354 sloc · 8.21 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1import bitfield
2import rand
3
4fn test_bf_new_size() {
5 instance := bitfield.new(75)
6 assert instance.get_size() == 75
7}
8
9fn test_bf_set_clear_toggle_get() {
10 mut instance := bitfield.new(75)
11 instance.set_bit(47)
12 assert instance.get_bit(47) == 1
13 instance.clear_bit(47)
14 assert instance.get_bit(47) == 0
15 instance.toggle_bit(47)
16 assert instance.get_bit(47) == 1
17 instance.set_if(true, 48)
18 assert instance.get_bit(48) == 1
19 instance.set_if(false, 48)
20 assert instance.get_bit(48) == 0
21}
22
23fn test_bf_multiple_flags() {
24 mut instance := bitfield.new(75)
25
26 // 1,3,5,7,9 set
27 instance.set_bits(1, 3, 5, 7, 9)
28 assert instance.all(1, 3, 5, 7, 9)
29 assert instance.all(1, 3, 20) == false
30 assert instance.has(1, 20, 30, 40)
31 assert instance.has(20, 30, 40) == false
32
33 // 1,3 set
34 instance.clear_bits(5, 7, 9)
35 assert instance.all(1, 3, 5, 7, 9) == false
36 assert instance.all(1, 3)
37 assert instance.has(3, 20, 30, 40)
38
39 // 3,5,7 set
40 instance.toggle_bits(1, 5, 7)
41 assert instance.all(3, 5, 7)
42 assert instance.all(1, 3, 5, 7) == false
43 assert instance.has(5, 20, 30, 40)
44}
45
46fn test_bf_insert_extract() {
47 mut instance := bitfield.new(11)
48 instance.set_all()
49 instance.insert(2, 9, 3)
50 assert instance.extract(2, 1) == 0
51 assert instance.extract(2, 8) == 1
52 assert instance.extract(10, 1) == 1
53 instance.set_all()
54 instance.insert_lowest_bits_first(2, 9, 3)
55 assert instance.extract_lowest_bits_first(2, 1) == 1
56 assert instance.extract_lowest_bits_first(2, 8) == 3
57 assert instance.extract_lowest_bits_first(10, 1) == 0
58}
59
60fn test_bf_and_not_or_xor() {
61 len := 80
62 mut input1 := bitfield.new(len)
63 mut input2 := bitfield.new(len)
64 mut i := 0
65 for i < len {
66 if rand.intn(2) or { 0 } == 1 {
67 input1.set_bit(i)
68 }
69 if rand.intn(2) or { 0 } == 1 {
70 input2.set_bit(i)
71 }
72 i++
73 }
74 output1 := bitfield.bf_xor(input1, input2)
75 bf_and := bitfield.bf_and(input1, input2)
76 bf_or := bitfield.bf_or(input1, input2)
77 bf_not := bitfield.bf_not(bf_and)
78 output2 := bitfield.bf_and(bf_or, bf_not)
79 mut result := 1
80 for i < len {
81 if output1.get_bit(i) != output2.get_bit(i) {
82 result = 0
83 }
84 }
85 assert result == 1
86}
87
88fn test_clone_cmp() {
89 len := 80
90 mut input := bitfield.new(len)
91 for i in 0 .. len {
92 if rand.intn(2) or { 0 } == 1 {
93 input.set_bit(i)
94 }
95 }
96 output := input.clone()
97 assert output.get_size() == len
98 assert input == output
99}
100
101fn test_slice_join() {
102 len := 80
103 mut input := bitfield.new(len)
104 for i in 0 .. len {
105 if rand.intn(2) or { 0 } == 1 {
106 input.set_bit(i)
107 }
108 }
109 mut result := 1
110 for point := 1; point < (len - 1); point++ {
111 // divide a bitfield into two subfields
112 chunk1 := input.slice(0, point)
113 chunk2 := input.slice(point, input.get_size())
114 // concatenate them back into one and compare to the original
115 output := bitfield.join(chunk1, chunk2)
116 if input != output {
117 result = 0
118 }
119 }
120 assert result == 1
121}
122
123fn test_pop_count() {
124 len := 80
125 mut count0 := 0
126 mut input := bitfield.new(len)
127 for i in 0 .. len {
128 if rand.intn(2) or { 0 } == 1 {
129 input.set_bit(i)
130 count0++
131 }
132 }
133 count1 := input.pop_count()
134 assert count0 == count1
135}
136
137fn test_pop_count2() {
138 b :=
139 bitfield.from_str('011000110110110000010001000011010011011111011110101001010011011010001100001001101111111011010011')
140 assert b.pop_count() == 50
141}
142
143fn test_hamming() {
144 len := 80
145 mut count := 0
146 mut input1 := bitfield.new(len)
147 mut input2 := bitfield.new(len)
148 for i in 0 .. len {
149 match rand.intn(4) {
150 0, 1 {
151 input1.set_bit(i)
152 count++
153 }
154 2 {
155 input2.set_bit(i)
156 count++
157 }
158 3 {
159 input1.set_bit(i)
160 input2.set_bit(i)
161 }
162 else {}
163 } or { 0 }
164 }
165 assert count == bitfield.hamming(input1, input2)
166}
167
168fn test_bf_from_bytes() {
169 input := [u8(0x01), 0xF0, 0x0F, 0xF0, 0xFF]
170 output := bitfield.from_bytes(input).str()
171 assert output == '00000001' + '11110000' + '00001111' + '11110000' + '11111111'
172 newoutput := bitfield.from_str(output).str()
173 assert newoutput == output
174}
175
176fn test_bf_from_bytes_lowest_bits_first() {
177 input := [u8(0x01), 0xF0]
178 output := bitfield.from_bytes_lowest_bits_first(input).str()
179 assert output == '10000000' + '00001111'
180 newoutput := bitfield.from_str(output).str()
181 assert newoutput == output
182}
183
184fn test_bf_from_str() {
185 len := 80
186 mut input := ''
187 for _ in 0 .. len {
188 if rand.intn(2) or { 0 } == 1 {
189 input = input + '1'
190 } else {
191 input = input + '0'
192 }
193 }
194 output := bitfield.from_str(input)
195 mut result := 1
196 for i in 0 .. len {
197 if input[i] != output.get_bit(i) + 48 {
198 result = 0
199 }
200 }
201 assert result == 1
202}
203
204fn test_bf_bf2str() {
205 len := 80
206 mut input := bitfield.new(len)
207 for i in 0 .. len {
208 if rand.intn(2) or { 0 } == 1 {
209 input.set_bit(i)
210 }
211 }
212 mut check := ''
213 for i in 0 .. len {
214 if input.get_bit(i) == 1 {
215 check = check + '1'
216 } else {
217 check = check + '0'
218 }
219 }
220 output := input.str()
221 mut result := 1
222 for i in 0 .. len {
223 if check[i] != output[i] {
224 result = 0
225 }
226 }
227 assert result == 1
228}
229
230fn test_bf_set_all() {
231 len := 80
232 mut input := bitfield.new(len)
233 input.set_all()
234 mut result := 1
235 for i in 0 .. len {
236 if input.get_bit(i) != 1 {
237 result = 0
238 }
239 }
240 assert result == 1
241}
242
243fn test_bf_clear_all() {
244 len := 80
245 mut input := bitfield.new(len)
246 for i in 0 .. len {
247 if rand.intn(2) or { 0 } == 1 {
248 input.set_bit(i)
249 }
250 }
251 input.clear_all()
252 mut result := 1
253 for i in 0 .. len {
254 if input.get_bit(i) != 0 {
255 result = 0
256 }
257 }
258 assert result == 1
259}
260
261fn test_bf_reverse() {
262 len := 80
263 mut input := bitfield.new(len)
264 for i in 0 .. len {
265 if rand.intn(2) or { 0 } == 1 {
266 input.set_bit(i)
267 }
268 }
269 check := input.clone()
270 output := input.reverse()
271 mut result := 1
272 for i in 0 .. len {
273 if output.get_bit(i) != check.get_bit(len - i - 1) {
274 result = 0
275 }
276 }
277 assert result == 1
278}
279
280fn test_bf_resize() {
281 len := 80
282 mut input := bitfield.new(rand.intn(len) or { 0 } + 1)
283 for _ in 0 .. 100 {
284 input.resize(rand.intn(len) or { 0 } + 1)
285 input.set_bit(input.get_size() - 1)
286 }
287 assert input.get_bit(input.get_size() - 1) == 1
288}
289
290fn test_bf_pos() {
291 /*
292 *
293 * set haystack size to 80
294 * test different sizes of needle, from 1 to 80
295 * test different positions of needle, from 0 to where it fits
296 * all haystacks here contain exactly one instance of needle,
297 * so search should return non-negative-values
298 *
299 */
300 len := 80
301 mut result := 1
302 for i := 1; i < len; i++ { // needle size
303 for j in 0 .. len - i { // needle position in the haystack
304 // create the needle
305 mut needle := bitfield.new(i)
306 // fill the needle with random values
307 for k in 0 .. i {
308 if rand.intn(2) or { 0 } == 1 {
309 needle.set_bit(k)
310 }
311 }
312 // make sure the needle contains at least one set bit, selected randomly
313 r := rand.intn(i) or { 0 }
314 needle.set_bit(r)
315 // create the haystack, make sure it contains the needle
316 mut haystack := needle.clone()
317 // if there is space between the start of the haystack and the sought needle, fill it with zeroes
318 if j > 0 {
319 start := bitfield.new(j)
320 tmp := bitfield.join(start, haystack)
321 haystack = tmp
322 }
323 // if there is space between the sought needle and the end of haystack, fill it with zeroes
324 if j + i < len {
325 end := bitfield.new(len - j - i)
326 tmp2 := bitfield.join(haystack, end)
327 haystack = tmp2
328 }
329 // now let's test
330 // the result should be equal to j
331 if haystack.pos(needle) != j {
332 result = 0
333 }
334 }
335 }
336 assert result == 1
337}
338
339fn test_bf_rotate() {
340 mut result := 1
341 len := 80
342 for i := 1; i < 80 && result == 1; i++ {
343 mut chunk1 := bitfield.new(i)
344 chunk2 := bitfield.new(len - i)
345 chunk1.set_all()
346 input := bitfield.join(chunk1, chunk2)
347 output := input.rotate(i)
348 if output.get_bit(len - i - 1) != 0 || output.get_bit(len - i) != 1 {
349 result = 0
350 }
351 }
352 assert result == 1
353}
354
355fn test_bf_printing() {
356 len := 80
357 mut input := bitfield.new(len)
358 for i in 0 .. len {
359 if rand.intn(2) or { 0 } == 0 {
360 input.set_bit(i)
361 }
362 }
363 // the following should convert the bitfield input into a string automatically
364 println(input)
365 assert true
366}
367
368fn test_bf_shift() {
369 str := '0001001101111111'
370 bf := bitfield.from_str(str)
371 bf_left := bf.shift_left(4)
372 assert bf_left.str() == '0011011111110000'
373 bf_right := bf.shift_right(4)
374 assert bf_right.str() == '0000000100110111'
375
376 bf_large_left := bf.shift_left(100)
377 bf_large_right := bf.shift_right(100)
378 assert bf_large_left.str() == '0000000000000000'
379 assert bf_large_right.str() == '0000000000000000'
380}
381