From d1b56c513971c2fcc5314ca919c57fbc614d1a5a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Aug 2018 13:09:05 +0200 Subject: Factor out test utilities --- test/test_bitvec.cpp | 143 +++++++++++++++++++++++---------------------------- test/test_utils.hpp | 69 +++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 80 deletions(-) create mode 100644 test/test_utils.hpp diff --git a/test/test_bitvec.cpp b/test/test_bitvec.cpp index 8305675..74c2e02 100644 --- a/test/test_bitvec.cpp +++ b/test/test_bitvec.cpp @@ -17,39 +17,20 @@ #undef NDEBUG +#include "test_utils.hpp" + #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() +test_and(Context& ctx) { - const T a = make_random_bitvec(); - const T b = make_random_bitvec(); + const T a = make_random_bitvec(ctx); + const T b = make_random_bitvec(ctx); T r = a; assert((a & b) == (r &= b)); @@ -60,10 +41,10 @@ test_and() template void -test_or() +test_or(Context& ctx) { - const T a = make_random_bitvec(); - const T b = make_random_bitvec(); + const T a = make_random_bitvec(ctx); + const T b = make_random_bitvec(ctx); T r = a; assert((a | b) == (r |= b)); @@ -74,10 +55,10 @@ test_or() template void -test_xor() +test_xor(Context& ctx) { - const T a = make_random_bitvec(); - const T b = make_random_bitvec(); + const T a = make_random_bitvec(ctx); + const T b = make_random_bitvec(ctx); T r = a; assert((a ^ b) == (r ^= b)); @@ -88,9 +69,9 @@ test_xor() template void -test_not() +test_not(Context& ctx) { - const T v = make_random_bitvec(); + const T v = make_random_bitvec(ctx); const T r = ~v; for (size_t i = 0; i < N; ++i) { @@ -100,7 +81,7 @@ test_not() template void -test_flip_one() +test_flip_one(Context&) { T v = make_zero_bitvec(); for (size_t i = 0; i < N; ++i) { @@ -117,9 +98,9 @@ test_flip_one() template void -test_flip_all() +test_flip_all(Context& ctx) { - const T a = make_random_bitvec(); + const T a = make_random_bitvec(ctx); T r = a; r.flip(); for (size_t i = 0; i < N; ++i) { @@ -129,7 +110,7 @@ test_flip_all() template void -test_none() +test_none(Context&) { T v = make_zero_bitvec(); assert(v.none()); @@ -146,7 +127,7 @@ test_none() template void -test_set_reset_one() +test_set_reset_one(Context&) { T v = make_zero_bitvec(); for (size_t i = 0; i < N; ++i) { @@ -163,7 +144,7 @@ test_set_reset_one() template void -test_set_all() +test_set_all(Context&) { T v = make_zero_bitvec(); v.set(); @@ -174,7 +155,7 @@ test_set_all() template void -test_reset_all() +test_reset_all(Context&) { T v = make_zero_bitvec(); v.set(); @@ -186,10 +167,10 @@ test_reset_all() template void -test_left_shift() +test_left_shift(Context& ctx) { for (size_t s = 0; s < N; ++s) { - const T v = make_random_bitvec(); + const T v = make_random_bitvec(ctx); T r = v; assert((v << s) == (r <<= s)); @@ -201,10 +182,10 @@ test_left_shift() template void -test_right_shift() +test_right_shift(Context& ctx) { for (size_t s = 0; s < N; ++s) { - const T v = make_random_bitvec(); + const T v = make_random_bitvec(ctx); T r = v; assert((v >> s) == (r >>= s)); @@ -216,7 +197,7 @@ test_right_shift() template void -test_find_first() +test_find_first(Context&) { T v = make_zero_bitvec(); for (size_t i = 0; i < N; ++i) { @@ -231,9 +212,9 @@ test_find_first() template void -test_gray_code() +test_gray_code(Context& ctx) { - const T v = make_random_bitvec(); + const T v = make_random_bitvec(ctx); T r = v; grayCode(r); @@ -248,7 +229,7 @@ test_gray_code() template void -test_comparison() +test_comparison(Context&) { T a = make_zero_bitvec(); T b = make_zero_bitvec(); @@ -267,45 +248,47 @@ test_comparison() template void -test() +test(Context& ctx) { - test_and(); - test_or(); - test_xor(); - test_not(); - 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(); - test_comparison(); + test_and(ctx); + test_or(ctx); + test_xor(ctx); + test_not(ctx); + test_flip_one(ctx); + test_flip_all(ctx); + test_none(ctx); + test_set_reset_one(ctx); + test_set_all(ctx); + test_reset_all(ctx); + test_left_shift(ctx); + test_right_shift(ctx); + test_find_first(ctx); + test_gray_code(ctx); + test_comparison(ctx); } int main() { - // test(); - test(); - test(); - test(); - test(); - test(); - test(); - - test(); - test(); - test(); - test(); - test(); - test(); - test(); - test(); - test(); + Context ctx; + + // test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); + test(ctx); return 0; } diff --git a/test/test_utils.hpp b/test/test_utils.hpp new file mode 100644 index 0000000..acee5d0 --- /dev/null +++ b/test/test_utils.hpp @@ -0,0 +1,69 @@ +/* + 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 . +*/ + +#ifndef TEST_UTILS_HPP +#define TEST_UTILS_HPP + +#undef NDEBUG + +#include "chilbert/BigBitVec.hpp" +#include "chilbert/FixBitVec.hpp" + +#include +#include +#include + +/// Test context +struct Context +{ + std::random_device rng; + std::uniform_int_distribution dist{0, SIZE_MAX}; +}; + +/// Return a bit vector of type T with N zero bits +template +T +make_zero_bitvec() +{ + T v(N); + v.reset(); + return v; +} + +/// Return a bit vector of type T with N random bits +template +T +make_random_bitvec(Context& ctx) +{ + T v(N); + for (size_t i = 0; i < N; ++i) { + v.set(i, ctx.dist(ctx.rng) & 1); + } + return v; +} + +/// Return a random number in [min, max) +static inline size_t +rand_between(Context& ctx, const size_t min, const size_t max) +{ + assert(max >= min); + const size_t r = (max == min) ? min : ctx.dist(ctx.rng) % (max - min) + min; + assert(r >= min && r < max); + return r; +} + +#endif -- cgit v1.2.1