arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/spqlios/lib/test/testlib/test_commons.h
spqlios/lib/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 public:
53  test_hasher();
54  test_hasher(const test_hasher&) = delete;
55  void operator=(const test_hasher&) = delete;
56  /**
57   * @brief append input bytes.
58   * The final hash only depends on the concatenation of bytes, not on the
59   * way the content was split into multiple calls to update.
60   */
61  void update(const void* data, uint64_t size);
62  /**
63   * @brief returns the final hash.
64   * no more calls to update(...) shall be issued after this call.
65   */
66  thash hash();
67  ~test_hasher();
68};
69
70// not included by default, since it makes some versions of gtest not compile
71// std::ostream& operator<<(std::ostream& out, __int128_t x);
72// std::ostream& operator<<(std::ostream& out, __uint128_t x);
73
74#endif  // SPQLIOS_TEST_COMMONS_H