poulpy/poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/testlib/test_commons.h
poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/test/testlib/test_commons.hRaw
1#ifndef SPQLIOS_TEST_COMMONS_H
2#define SPQLIOS_TEST_COMMONS_H
3
4#include <iostream>
5#include <random>
6
7#include "../../spqlios/commons.h"
8
9/** @brief macro that crashes if the condition are not met */
10#define REQUIRE_DRAMATICALLY(req_contition, error_msg) \
11 do { \
12 if (!(req_contition)) { \
13 std::cerr << "REQUIREMENT FAILED at " << __FILE__ << ":" << __LINE__ << ": " << error_msg << std::endl; \
14 abort(); \
15 } \
16 } while (0)
17
18typedef std::default_random_engine test_rng;
19/** @brief reference to the default test rng */
20test_rng& randgen();
21/** @brief uniformly random 64-bit uint */
22uint64_t uniform_u64();
23/** @brief uniformly random number <= 2^nbits-1 */
24uint64_t uniform_u64_bits(uint64_t nbits);
25/** @brief uniformly random signed 64-bit number */
26int64_t uniform_i64();
27/** @brief uniformly random signed |number| <= 2^nbits */
28int64_t uniform_i64_bits(uint64_t nbits);
29/** @brief uniformly random signed lb <= number <= ub */
30int64_t uniform_i64_bounds(const int64_t lb, const int64_t ub);
31/** @brief uniformly random signed lb <= number <= ub */
32__int128_t uniform_i128_bounds(const __int128_t lb, const __int128_t ub);
33/** @brief uniformly random gaussian float64 */
34double random_f64_gaussian(double stdev = 1);
35/** @brief uniformly random signed lb <= number <= ub */
36double uniform_f64_bounds(const double lb, const double ub);
37/** @brief uniformly random float64 in [0,1] */
38double uniform_f64_01();
39/** @brief random gaussian float64 */
40double random_f64_gaussian(double stdev);
41
42bool is_pow2(uint64_t n);
43
44void* alloc64(uint64_t size);
45
46typedef __uint128_t thash;
47/** @brief returns some pseudorandom hash of a contiguous content */
48thash test_hash(const void* data, uint64_t size);
49/** @brief class to return a pseudorandom hash of a piecewise-defined content */
50class test_hasher {
51 void* md;
52
53 public:
54 test_hasher();
55 test_hasher(const test_hasher&) = delete;
56 void operator=(const test_hasher&) = delete;
57 /**
58 * @brief append input bytes.
59 * The final hash only depends on the concatenation of bytes, not on the
60 * way the content was split into multiple calls to update.
61 */
62 void update(const void* data, uint64_t size);
63 /**
64 * @brief returns the final hash.
65 * no more calls to update(...) shall be issued after this call.
66 */
67 thash hash();
68 ~test_hasher();
69};
70
71// not included by default, since it makes some versions of gtest not compile
72// std::ostream& operator<<(std::ostream& out, __int128_t x);
73// std::ostream& operator<<(std::ostream& out, __uint128_t x);
74
75#endif // SPQLIOS_TEST_COMMONS_H