2019-07-09 22:11:09 +03:00
|
|
|
import bf
|
|
|
|
|
|
|
|
import rand
|
2019-07-13 19:17:45 +03:00
|
|
|
import time
|
2019-07-09 22:11:09 +03:00
|
|
|
|
|
|
|
fn test_bf_new_size() {
|
2019-07-12 21:46:37 +03:00
|
|
|
instance := bf.new(75)
|
|
|
|
assert instance.getsize() == 75
|
2019-07-09 22:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bf_set_clear_toggle_get() {
|
2019-07-12 21:46:37 +03:00
|
|
|
mut instance := bf.new(75)
|
|
|
|
instance.setbit(47)
|
|
|
|
assert instance.getbit(47) == 1
|
|
|
|
instance.clearbit(47)
|
|
|
|
assert instance.getbit(47) == 0
|
|
|
|
instance.togglebit(47)
|
|
|
|
assert instance.getbit(47) == 1
|
2019-07-09 22:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bf_and_not_or_xor() {
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-07-09 22:11:09 +03:00
|
|
|
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
|
|
|
|
}
|
2019-07-12 21:46:37 +03:00
|
|
|
|
|
|
|
fn test_clone_cmp() {
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-07-12 21:46:37 +03:00
|
|
|
len := 80
|
|
|
|
mut input := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input.setbit(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output := bf.clone(input)
|
|
|
|
assert output.getsize() == len
|
|
|
|
assert bf.cmp(input, output) == true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_slice_join() {
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-07-12 21:46:37 +03:00
|
|
|
len := 80
|
|
|
|
mut input := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input.setbit(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut result := 1
|
|
|
|
for point := 1; point < (len - 1); point++ {
|
|
|
|
// divide a bitfield into two subfields
|
|
|
|
chunk1 := input.slice(0, point)
|
|
|
|
chunk2 := input.slice(point, input.getsize())
|
|
|
|
// concatenate them back into one and compare to the original
|
|
|
|
output := bf.join(chunk1, chunk2)
|
|
|
|
if !bf.cmp(input, output) {
|
|
|
|
result = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_popcount() {
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-07-12 21:46:37 +03:00
|
|
|
len := 80
|
|
|
|
mut count0 := 0
|
|
|
|
mut input := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input.setbit(i)
|
|
|
|
count0++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count1 := input.popcount()
|
|
|
|
assert count0 == count1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_hamming() {
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-07-12 21:46:37 +03:00
|
|
|
len := 80
|
|
|
|
mut count := 0
|
|
|
|
mut input1 := bf.new(len)
|
|
|
|
mut input2 := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
switch rand.next(4) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
input1.setbit(i)
|
|
|
|
count++
|
|
|
|
case 2:
|
|
|
|
input2.setbit(i)
|
|
|
|
count++
|
|
|
|
case 3:
|
|
|
|
input1.setbit(i)
|
|
|
|
input2.setbit(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert count == bf.hamming(input1, input2)
|
|
|
|
}
|
2019-07-14 18:56:48 +03:00
|
|
|
|
|
|
|
fn test_bf_str2bf() {
|
|
|
|
rand.seed(time.now().uni)
|
|
|
|
len := 80
|
|
|
|
mut input := ''
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input = input + '1'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input = input + '0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output := bf.str2bf(input)
|
|
|
|
mut result := 1
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if input[i] != output.getbit(i) + 48 {
|
|
|
|
result = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bf_bf2str() {
|
|
|
|
rand.seed(time.now().uni)
|
|
|
|
len := 80
|
|
|
|
mut input := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input.setbit(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut check := ''
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if input.getbit(i) == 1 {
|
|
|
|
check = check + '1'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
check = check + '0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output := input.string()
|
|
|
|
mut result := 1
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if check[i] != output[i] {
|
|
|
|
result = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bf_setall() {
|
|
|
|
rand.seed(time.now().uni)
|
|
|
|
len := 80
|
|
|
|
mut input := bf.new(len)
|
|
|
|
input.setall()
|
|
|
|
mut result := 1
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if input.getbit(i) != 1 {
|
|
|
|
result = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_bf_clearall() {
|
|
|
|
rand.seed(time.now().uni)
|
|
|
|
len := 80
|
|
|
|
mut input := bf.new(len)
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if rand.next(2) == 1 {
|
|
|
|
input.setbit(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input.clearall()
|
|
|
|
mut result := 1
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
if input.getbit(i) != 0 {
|
|
|
|
result = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert result == 1
|
|
|
|
}
|