/* 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; }