From 2a0475a7e4ff0890d3290c258db032d051ec13bd Mon Sep 17 00:00:00 2001 From: Kristian Nielsen Date: Wed, 4 Feb 2009 20:06:11 +0100 Subject: [PATCH] Add testing of pack() and unpack(). --- test/bitop.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/test/bitop.cc b/test/bitop.cc index 4a72b45..17bd6fc 100644 --- a/test/bitop.cc +++ b/test/bitop.cc @@ -28,6 +28,7 @@ #include "packed.h" #include "format_macros.h" +/* Test putbits() and getbits(). */ void test1(beedb_tester *t) { @@ -47,13 +48,13 @@ test1(beedb_tester *t) uint64_t used_bits= p1.used_bits(); if (total_bits != used_bits) - return t->nok(NOK_IDENT "wrong number of bits used, %" PRIu64 " instead of %" PRIu64, - used_bits, total_bits); + return t->nok(NOK_IDENT "wrong number of bits used, %" PRIu64 + " instead of %" PRIu64, used_bits, total_bits); uint64_t used_words= p1.used_words(); uint64_t total_words= (total_bits+63)>>6; if (total_words != used_words) - return t->nok(NOK_IDENT "wrong number of words used, %" PRIu64 " instead of %" PRIu64, - used_words, total_bits); + return t->nok(NOK_IDENT "wrong number of words used, %" PRIu64 + " instead of %" PRIu64, used_words, total_bits); pack_ptr p2(buf); for (uint64_t i= 1; i <= 64; i++) @@ -66,7 +67,8 @@ test1(beedb_tester *t) return t->nok(NOK_IDENT "getbits() error (i=%" PRIu64 ")", i); x= p2.getbits(i); if (x != (((uint64_t)1 << (i-1)) - i)) - return t->nok(NOK_IDENT "getbits() error (i=%" PRIu64 " expected=%" PRIu64 ", got=%" PRIu64 ")", i, x, ((uint64_t)1 << (i-1)) - i); + return t->nok(NOK_IDENT "getbits() error (i=%" PRIu64 " expected=%" PRIu64 + ", got=%" PRIu64 ")", i, x, ((uint64_t)1 << (i-1)) - i); x= p2.getbits(1); if (x != 1) return t->nok(NOK_IDENT "getbits() error (i=%" PRIu64 ")", i); @@ -75,11 +77,50 @@ test1(beedb_tester *t) t->ok(); } +/* Test pack() and unpack(). */ +void +test2(beedb_tester *t) +{ + uint64_t buf[2000]; + pack_overwriteptr p1(buf); + + uint64_t total_bits= 0; + for (uint64_t i= 1; i <= 64; i++) + { + p1.pack5(i); + p1.pack5(0); + p1.pack5(((uint64_t)1 << (i-1)) - i); + p1.pack5(1); + } + p1.flush(); + + pack_ptr p2(buf); + for (uint64_t i= 1; i <= 64; i++) + { + uint64_t x= p2.unpack5(); + if (x != i) + return t->nok(NOK_IDENT "unpack() error (i=%" PRIu64 ")", i); + x= p2.unpack5(); + if (x != 0) + return t->nok(NOK_IDENT "unpack() error (i=%" PRIu64 ")", i); + x= p2.unpack5(); + if (x != (((uint64_t)1 << (i-1)) - i)) + return t->nok(NOK_IDENT "unpack() error (i=%" PRIu64 " expected=%" PRIu64 + ", got=%" PRIu64 ")", i, x, ((uint64_t)1 << (i-1)) - i); + x= p2.unpack5(); + if (x != 1) + return t->nok(NOK_IDENT "unpack() error (i=%" PRIu64 ")", i); + } + + t->ok(); +} + int main(int argc, char *argv) { - beedb_tester t(1); + beedb_tester t(2); t.run(test1); + t.run(test2); return 0; } -- 2.11.4.GIT