From 9845fd1cf5d5eede92525a30d881f1efeae1f82b Mon Sep 17 00:00:00 2001 From: penguindark <57967770+penguindark@users.noreply.github.com> Date: Sun, 9 Feb 2020 21:16:03 +0100 Subject: [PATCH] math.bits: tests --- vlib/math/bits/bits_test.v | 140 +++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 vlib/math/bits/bits_test.v diff --git a/vlib/math/bits/bits_test.v b/vlib/math/bits/bits_test.v new file mode 100644 index 0000000000..d808920e94 --- /dev/null +++ b/vlib/math/bits/bits_test.v @@ -0,0 +1,140 @@ +module bits + +fn test_bits(){ + mut i := 0 + mut i1:= u64(0) + // + // --- LeadingZeros --- + // + + // 8 bit + i = 1 + for x in 0..8 { + //C.printf("x:%02x lz: %d cmp: %d\n",i<> 1 + } + //C.printf("x:%02x lz: %llu cmp: %d\n",byte(i),reverse_8(byte(i)), rv) + assert reverse_8(byte(i)) == rv + i = (i << 1) + 1 + } + // 16 bit + i = 0 + for x in 0..17 { + mut rv := u16(0) + mut bc := 0 + mut n := i + for bc < 16 { + rv = (rv << 1) | (n & 0x01) + bc++ + n = n >> 1 + } + //C.printf("x:%04x lz: %llu cmp: %d\n",u16(i),reverse_16(u16(i)), rv) + assert reverse_16(u16(i)) == rv + i = (i << 1) + 1 + } + // 32 bit + i = 0 + for x in 0..33 { + mut rv := u32(0) + mut bc := 0 + mut n := i + for bc < 32 { + rv = (rv << 1) | (n & 0x01) + bc++ + n = n >> 1 + } + //C.printf("x:%08x lz: %llu cmp: %d\n",u32(i),reverse_32(u32(i)), rv) + assert reverse_32(u32(i)) == rv + i = (i << 1) + 1 + } + // 64 bit + i1 = 0 + for x in 0..64 { + mut rv := u64(0) + mut bc := 0 + mut n := i1 + for bc < 64 { + rv = (rv << 1) | (n & 0x01) + bc++ + n = n >> 1 + } + //C.printf("x:%016llx lz: %016llx cmp: %016llx\n",u64(i1),reverse_64(u64(i1)), rv) + assert reverse_64(u64(i1)) == rv + i1 = (i1 << 1) + 1 + } +} \ No newline at end of file