1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

bf: add a module implementing operations with bit arrays (#1049)

This commit is contained in:
Vitalie Ciubotaru
2019-07-10 04:11:09 +09:00
committed by Alexander Medvednikov
parent 26af513e1b
commit c4fcfcec88
2 changed files with 224 additions and 0 deletions

45
vlib/bf/bf_test.v Normal file
View File

@ -0,0 +1,45 @@
import bf
import rand
fn test_bf_new_size() {
instance := bf.new(5)
assert instance.getsize() == 5
}
fn test_bf_set_clear_toggle_get() {
mut instance := bf.new(5)
instance.setbit(4)
assert instance.getbit(4) == 1
instance.clearbit(4)
assert instance.getbit(4) == 0
instance.togglebit(4)
assert instance.getbit(4) == 1
}
fn test_bf_and_not_or_xor() {
rand.seed()
len := 80
mut input1 := bf.new(len)
mut input2 := bf.new(len)
mut i := 0
for i < len {
if rand.next(2) == 1 {
input1.setbit(i)
}
if rand.next(2) == 1{
input2.setbit(i)
}
i++
}
output1 := bf.bfxor(input1, input2)
bfand := bf.bfand(input1, input2)
bfor := bf.bfor(input1, input2)
bfnot := bf.bfnot(bfand)
output2 := bf.bfand(bfor, bfnot)
mut result := 1
for i < len {
if output1.getbit(i) != output2.getbit(i) {result = 0}
}
assert result == 1
}