/* 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 #include "chilbert/DynamicBitVec.hpp" #include "chilbert/SmallBitVec.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; } /// Return an array of D precisions each at most Max and with sum at most N template std::array make_random_precisions(Context& ctx) { std::array ms; size_t bits_left = N; for (size_t i = 0; i < D; ++i) { ms[i] = rand_between(ctx, 1, std::min(Max, bits_left / (D - i) + 1)); bits_left -= ms[i]; } return ms; } /// Return a `D`-dimensional point with `M` bits of precision per dimension template std::array make_random_point(Context& ctx) { std::array p; for (size_t i = 0; i < D; ++i) { p[i] = rand_between(ctx, 0, (1UL << M) - 1); } return p; } #endif