From 16ec07372eaa92e361f66687d6da2d7e5541c0aa Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 11 Aug 2018 19:35:53 +0200 Subject: Flesh out bitvec tests --- test/test_bitvec.cpp | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_fsb.cpp | 45 --------- 2 files changed, 278 insertions(+), 45 deletions(-) create mode 100644 test/test_bitvec.cpp delete mode 100644 test/test_fsb.cpp (limited to 'test') diff --git a/test/test_bitvec.cpp b/test/test_bitvec.cpp new file mode 100644 index 0000000..e547122 --- /dev/null +++ b/test/test_bitvec.cpp @@ -0,0 +1,278 @@ +/* + Copyright (C) 2018 David Robillard + + This program is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free Software + Foundation, either version 2 of the License, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + this program. If not, see . +*/ + +#undef NDEBUG + +#include "chilbert/BigBitVec.hpp" +#include "chilbert/FixBitVec.hpp" + +#include +#include +#include + +template +T +make_zero_bitvec() +{ + T v(N); + v.reset(); + return v; +} + +template +T +make_random_bitvec() +{ + T v(N); + for (size_t i = 0; i < N; ++i) { + v.set(i, rand() & 1); + } + return v; +} + +template +void +test_and() +{ + const T a = make_random_bitvec(); + const T b = make_random_bitvec(); + T r = a; + assert((a & b) == (r &= b)); + + for (size_t i = 0; i < N; ++i) { + assert(r.test(i) == (a.test(i) && b.test(i))); + } +} + +template +void +test_or() +{ + const T a = make_random_bitvec(); + const T b = make_random_bitvec(); + T r = a; + assert((a | b) == (r |= b)); + + for (size_t i = 0; i < N; ++i) { + assert(r.test(i) == (a.test(i) || b.test(i))); + } +} + +template +void +test_xor() +{ + const T a = make_random_bitvec(); + const T b = make_random_bitvec(); + T r = a; + assert((a ^ b) == (r ^= b)); + + for (size_t i = 0; i < N; ++i) { + assert(r.test(i) == (a.test(i) != b.test(i))); + } +} + +template +void +test_flip_one() +{ + T v = make_zero_bitvec(); + for (size_t i = 0; i < N; ++i) { + assert(v.none()); + v.flip(i); + for (size_t j = 0; j < N; ++j) { + assert(v.test(j) == (j == i)); + } + + v.flip(i); + assert(v.none()); + } +} + +template +void +test_flip_all() +{ + const T a = make_random_bitvec(); + T r = a; + r.flip(); + for (size_t i = 0; i < N; ++i) { + assert(r.test(i) == !a.test(i)); + } +} + +template +void +test_none() +{ + T v = make_zero_bitvec(); + assert(v.none()); + v.set(); + assert(v.none() == (N == 0)); + if (N > 1) { + v.reset(N / 2); + assert(!v.none()); + v.reset(); + v.set(N / 2); + assert(!v.none()); + } +} + +template +void +test_set_reset_one() +{ + T v = make_zero_bitvec(); + for (size_t i = 0; i < N; ++i) { + assert(v.none()); + v.set(i); + for (size_t j = 0; j < N; ++j) { + assert(v.test(j) == (j == i)); + } + + v.reset(i); + assert(v.none()); + } +} + +template +void +test_set_all() +{ + T v = make_zero_bitvec(); + v.set(); + for (size_t i = 0; i < N; ++i) { + assert(v.test(i)); + } +} + +template +void +test_reset_all() +{ + T v = make_zero_bitvec(); + v.set(); + v.reset(); + for (size_t i = 0; i < N; ++i) { + assert(!v.test(i)); + } +} + +template +void +test_left_shift() +{ + for (size_t s = 0; s < N; ++s) { + const T v = make_random_bitvec(); + T r = v; + assert((v << s) == (r <<= s)); + + for (size_t i = s; i < N - s; ++i) { + assert(r.test(i + s) == v.test(i)); + } + } +} + +template +void +test_right_shift() +{ + for (size_t s = 0; s < N; ++s) { + const T v = make_random_bitvec(); + T r = v; + assert((v >> s) == (r >>= s)); + + for (size_t i = s; i < N - s; ++i) { + assert(r.test(i - s) == v.test(i)); + } + } +} + +template +void +test_find_first() +{ + T v = make_zero_bitvec(); + for (size_t i = 0; i < N; ++i) { + v.reset(); + v.set(i); + for (size_t j = i + 1; j < N; ++j) { + v.set(j, rand() & 1); + } + assert(size_t(v.find_first()) == i + 1); + } +} + +template +void +test_gray_code() +{ + const T v = make_random_bitvec(); + T r = v; + grayCode(r); + + if (N > 0) { + assert(r == (v ^ (v >> 1))); + + T s = r; + grayCodeInv(s); + assert(s == v); + } +} + +template +void +test() +{ + test_and(); + test_or(); + test_xor(); + test_flip_one(); + test_flip_all(); + test_none(); + test_set_reset_one(); + test_set_all(); + test_reset_all(); + test_left_shift(); + test_right_shift(); + test_find_first(); + test_gray_code(); +} + +int +main() +{ + // test(); + test(); + test(); + test(); + test(); + test(); + test(); + + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); + test(); + + return 0; +} diff --git a/test/test_fsb.cpp b/test/test_fsb.cpp deleted file mode 100644 index b059394..0000000 --- a/test/test_fsb.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright (C) 2018 David Robillard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#undef NDEBUG - -#include "chilbert/BigBitVec.hpp" -#include "chilbert/FixBitVec.hpp" - -#include - -using namespace chilbert; - -template -int -test_fsb(const T& empty_field) -{ - assert(empty_field.none()); - for (int i = 0; i < empty_field.size(); ++i) { - T field = empty_field; - field.set(i); - assert(field.find_first() == i + 1); - } - - return 0; -} - -int -main() -{ - return test_fsb(CFixBitVec{}) || test_fsb(CBigBitVec{1024}); -} -- cgit v1.2.1