aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test/test_bitvec.cpp143
-rw-r--r--test/test_utils.hpp69
2 files changed, 132 insertions, 80 deletions
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 <cassert>
#include <cstddef>
-#include <cstdlib>
-
-template <class T, size_t N>
-T
-make_zero_bitvec()
-{
- T v(N);
- v.reset();
- return v;
-}
-
-template <class T, size_t N>
-T
-make_random_bitvec()
-{
- T v(N);
- for (size_t i = 0; i < N; ++i) {
- v.set(i, rand() & 1);
- }
- return v;
-}
template <class T, size_t N>
void
-test_and()
+test_and(Context& ctx)
{
- const T a = make_random_bitvec<T, N>();
- const T b = make_random_bitvec<T, N>();
+ const T a = make_random_bitvec<T, N>(ctx);
+ const T b = make_random_bitvec<T, N>(ctx);
T r = a;
assert((a & b) == (r &= b));
@@ -60,10 +41,10 @@ test_and()
template <class T, size_t N>
void
-test_or()
+test_or(Context& ctx)
{
- const T a = make_random_bitvec<T, N>();
- const T b = make_random_bitvec<T, N>();
+ const T a = make_random_bitvec<T, N>(ctx);
+ const T b = make_random_bitvec<T, N>(ctx);
T r = a;
assert((a | b) == (r |= b));
@@ -74,10 +55,10 @@ test_or()
template <class T, size_t N>
void
-test_xor()
+test_xor(Context& ctx)
{
- const T a = make_random_bitvec<T, N>();
- const T b = make_random_bitvec<T, N>();
+ const T a = make_random_bitvec<T, N>(ctx);
+ const T b = make_random_bitvec<T, N>(ctx);
T r = a;
assert((a ^ b) == (r ^= b));
@@ -88,9 +69,9 @@ test_xor()
template <class T, size_t N>
void
-test_not()
+test_not(Context& ctx)
{
- const T v = make_random_bitvec<T, N>();
+ const T v = make_random_bitvec<T, N>(ctx);
const T r = ~v;
for (size_t i = 0; i < N; ++i) {
@@ -100,7 +81,7 @@ test_not()
template <class T, size_t N>
void
-test_flip_one()
+test_flip_one(Context&)
{
T v = make_zero_bitvec<T, N>();
for (size_t i = 0; i < N; ++i) {
@@ -117,9 +98,9 @@ test_flip_one()
template <class T, size_t N>
void
-test_flip_all()
+test_flip_all(Context& ctx)
{
- const T a = make_random_bitvec<T, N>();
+ const T a = make_random_bitvec<T, N>(ctx);
T r = a;
r.flip();
for (size_t i = 0; i < N; ++i) {
@@ -129,7 +110,7 @@ test_flip_all()
template <class T, size_t N>
void
-test_none()
+test_none(Context&)
{
T v = make_zero_bitvec<T, N>();
assert(v.none());
@@ -146,7 +127,7 @@ test_none()
template <class T, size_t N>
void
-test_set_reset_one()
+test_set_reset_one(Context&)
{
T v = make_zero_bitvec<T, N>();
for (size_t i = 0; i < N; ++i) {
@@ -163,7 +144,7 @@ test_set_reset_one()
template <class T, size_t N>
void
-test_set_all()
+test_set_all(Context&)
{
T v = make_zero_bitvec<T, N>();
v.set();
@@ -174,7 +155,7 @@ test_set_all()
template <class T, size_t N>
void
-test_reset_all()
+test_reset_all(Context&)
{
T v = make_zero_bitvec<T, N>();
v.set();
@@ -186,10 +167,10 @@ test_reset_all()
template <class T, size_t N>
void
-test_left_shift()
+test_left_shift(Context& ctx)
{
for (size_t s = 0; s < N; ++s) {
- const T v = make_random_bitvec<T, N>();
+ const T v = make_random_bitvec<T, N>(ctx);
T r = v;
assert((v << s) == (r <<= s));
@@ -201,10 +182,10 @@ test_left_shift()
template <class T, size_t N>
void
-test_right_shift()
+test_right_shift(Context& ctx)
{
for (size_t s = 0; s < N; ++s) {
- const T v = make_random_bitvec<T, N>();
+ const T v = make_random_bitvec<T, N>(ctx);
T r = v;
assert((v >> s) == (r >>= s));
@@ -216,7 +197,7 @@ test_right_shift()
template <class T, size_t N>
void
-test_find_first()
+test_find_first(Context&)
{
T v = make_zero_bitvec<T, N>();
for (size_t i = 0; i < N; ++i) {
@@ -231,9 +212,9 @@ test_find_first()
template <class T, size_t N>
void
-test_gray_code()
+test_gray_code(Context& ctx)
{
- const T v = make_random_bitvec<T, N>();
+ const T v = make_random_bitvec<T, N>(ctx);
T r = v;
grayCode(r);
@@ -248,7 +229,7 @@ test_gray_code()
template <class T, size_t N>
void
-test_comparison()
+test_comparison(Context&)
{
T a = make_zero_bitvec<T, N>();
T b = make_zero_bitvec<T, N>();
@@ -267,45 +248,47 @@ test_comparison()
template <class T, size_t N>
void
-test()
+test(Context& ctx)
{
- test_and<T, N>();
- test_or<T, N>();
- test_xor<T, N>();
- test_not<T, N>();
- test_flip_one<T, N>();
- test_flip_all<T, N>();
- test_none<T, N>();
- test_set_reset_one<T, N>();
- test_set_all<T, N>();
- test_reset_all<T, N>();
- test_left_shift<T, N>();
- test_right_shift<T, N>();
- test_find_first<T, N>();
- test_gray_code<T, N>();
- test_comparison<T, N>();
+ test_and<T, N>(ctx);
+ test_or<T, N>(ctx);
+ test_xor<T, N>(ctx);
+ test_not<T, N>(ctx);
+ test_flip_one<T, N>(ctx);
+ test_flip_all<T, N>(ctx);
+ test_none<T, N>(ctx);
+ test_set_reset_one<T, N>(ctx);
+ test_set_all<T, N>(ctx);
+ test_reset_all<T, N>(ctx);
+ test_left_shift<T, N>(ctx);
+ test_right_shift<T, N>(ctx);
+ test_find_first<T, N>(ctx);
+ test_gray_code<T, N>(ctx);
+ test_comparison<T, N>(ctx);
}
int
main()
{
- // test<chilbert::CFixBitVec, 0>();
- test<chilbert::CFixBitVec, 1>();
- test<chilbert::CFixBitVec, 31>();
- test<chilbert::CFixBitVec, 32>();
- test<chilbert::CFixBitVec, 33>();
- test<chilbert::CFixBitVec, 63>();
- test<chilbert::CFixBitVec, 64>();
-
- test<chilbert::CBigBitVec, 0>();
- test<chilbert::CBigBitVec, 1>();
- test<chilbert::CBigBitVec, 31>();
- test<chilbert::CBigBitVec, 32>();
- test<chilbert::CBigBitVec, 33>();
- test<chilbert::CBigBitVec, 63>();
- test<chilbert::CBigBitVec, 64>();
- test<chilbert::CBigBitVec, 65>();
- test<chilbert::CBigBitVec, 997>();
+ Context ctx;
+
+ // test<chilbert::CFixBitVec, 0>(ctx);
+ test<chilbert::CFixBitVec, 1>(ctx);
+ test<chilbert::CFixBitVec, 31>(ctx);
+ test<chilbert::CFixBitVec, 32>(ctx);
+ test<chilbert::CFixBitVec, 33>(ctx);
+ test<chilbert::CFixBitVec, 63>(ctx);
+ test<chilbert::CFixBitVec, 64>(ctx);
+
+ test<chilbert::CBigBitVec, 0>(ctx);
+ test<chilbert::CBigBitVec, 1>(ctx);
+ test<chilbert::CBigBitVec, 31>(ctx);
+ test<chilbert::CBigBitVec, 32>(ctx);
+ test<chilbert::CBigBitVec, 33>(ctx);
+ test<chilbert::CBigBitVec, 63>(ctx);
+ test<chilbert::CBigBitVec, 64>(ctx);
+ test<chilbert::CBigBitVec, 65>(ctx);
+ test<chilbert::CBigBitVec, 997>(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 <d@drobilla.net>
+
+ 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 <https://www.gnu.org/licenses/>.
+*/
+
+#ifndef TEST_UTILS_HPP
+#define TEST_UTILS_HPP
+
+#undef NDEBUG
+
+#include "chilbert/BigBitVec.hpp"
+#include "chilbert/FixBitVec.hpp"
+
+#include <cstddef>
+#include <cstdint>
+#include <random>
+
+/// Test context
+struct Context
+{
+ std::random_device rng;
+ std::uniform_int_distribution<size_t> dist{0, SIZE_MAX};
+};
+
+/// Return a bit vector of type T with N zero bits
+template <class T, size_t N>
+T
+make_zero_bitvec()
+{
+ T v(N);
+ v.reset();
+ return v;
+}
+
+/// Return a bit vector of type T with N random bits
+template <class T, size_t N>
+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