From 8f06d600849ef412cea5d62b39a63dbaa0b2f221 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 4 Apr 2020 18:03:03 +0300 Subject: [PATCH] fix vlib/bitfield/bitfield_test.v --- vlib/bitfield/bitfield.v | 22 +++------------------- vlib/bitfield/bitfield_test.v | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/vlib/bitfield/bitfield.v b/vlib/bitfield/bitfield.v index ad0ff2a64b..8731127e9c 100644 --- a/vlib/bitfield/bitfield.v +++ b/vlib/bitfield/bitfield.v @@ -92,17 +92,17 @@ pub fn from_bytes(input []byte) BitField { pub fn from_string(input string) BitField { mut output := new(input.len) for i in 0..input.len { - if input[i] != 48 { + if input[i] != `0` { output.setbit(i) } } return output } -// string() converts the bit array to a string of characters ('0' and '1') and +// str() converts the bit array to a string of characters ('0' and '1') and // return the string -pub fn (input BitField) string() string { +pub fn (input BitField) str() string { mut output := '' for i in 0..input.size { if input.getbit(i) == 1 { @@ -291,22 +291,6 @@ pub fn join(input1 BitField, input2 BitField) BitField { return output } -// print(instance BitField) send the content of a bit array to stdout as a -// string of characters ('0' and '1'). - -pub fn print(instance BitField) { - mut i := 0 - for i < instance.size { - if instance.getbit(i) == 1 { - print('1') - } - else { - print('0') - } - i++ - } -} - // getsize() returns the number of bits the array can hold pub fn (instance BitField) getsize() int { diff --git a/vlib/bitfield/bitfield_test.v b/vlib/bitfield/bitfield_test.v index 8d2279bf9d..2576b4f198 100644 --- a/vlib/bitfield/bitfield_test.v +++ b/vlib/bitfield/bitfield_test.v @@ -177,7 +177,7 @@ fn test_bf_bf2str() { check = check + '0' } } - output := input.string() + output := input.str() mut result := 1 for i in 0..len { if check[i] != output[i] { @@ -320,3 +320,17 @@ fn test_bf_rotate() { } assert result == 1 } + +fn test_bf_printing(){ + rand.seed(time.now().unix) + len := 80 + mut input := bitfield.new(len) + for i in 0..len { + if rand.next(2) == 0 { + input.setbit(i) + } + } + // the following should convert the bitfield input into a string automatically + println(input) + assert true +}