diff options
Diffstat (limited to 'test/test_utils.hpp')
-rw-r--r-- | test/test_utils.hpp | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/test/test_utils.hpp b/test/test_utils.hpp index 65bd1b1..c3b6edb 100644 --- a/test/test_utils.hpp +++ b/test/test_utils.hpp @@ -26,70 +26,69 @@ #include <random> /// Test context -struct Context -{ - std::random_device rng; - std::uniform_int_distribution<size_t> dist{0, SIZE_MAX}; +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> +template<class T, size_t N> T make_zero_bitvec() { - T v(N); - v.reset(); - return v; + T v(N); + v.reset(); + return v; } /// Return a bit vector of type T with N random bits -template <class T, size_t N> +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) & 1U); - } - return v; + T v(N); + for (size_t i = 0; i < N; ++i) { + v.set(i, ctx.dist(ctx.rng) & 1U); + } + 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; + 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 <size_t N, size_t D, size_t Max = 64> +template<size_t N, size_t D, size_t Max = 64> std::array<size_t, D> make_random_precisions(Context& ctx) { - std::array<size_t, D> ms{}; + std::array<size_t, D> 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]; - } + 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 ms; } /// Return a `D`-dimensional point with `M` bits of precision per dimension -template <size_t M, size_t D> +template<size_t M, size_t D> std::array<uint64_t, D> make_random_point(Context& ctx) { - std::array<uint64_t, D> p{}; - for (size_t i = 0; i < D; ++i) { - p[i] = rand_between(ctx, 0, (1UL << M) - 1); - } - return p; + std::array<uint64_t, D> p{}; + for (size_t i = 0; i < D; ++i) { + p[i] = rand_between(ctx, 0, (1UL << M) - 1); + } + return p; } #endif |