kblib 0.2.3
General utilities library for modern C++
random.cpp
Go to the documentation of this file.
1#define KBLIB_DEBUG_SEED_SEQ 1
2
3#include "kblib/random.h"
4#include "catch.hpp"
5
6#include <iostream>
7#include <sstream>
8
9template <std::size_t>
10struct print;
11
12TEST_CASE("Random engine seeding") {
13 SECTION("Mersenne Twister Engine") {
14 std::clog << "MT: ";
15 KBLIB_UNUSED auto gen = kblib::seeded<std::mt19937>();
16 std::clog << '\n';
17 static_assert(kblib::state_size_v<std::mt19937> == 624, ".");
18 static_assert(kblib::state_size_v<std::mt19937_64> == 624, ".");
19 }
20 SECTION("Linear Congruential Engine") {
21 std::clog << "LCG: ";
22 KBLIB_UNUSED auto gen = kblib::seeded<std::minstd_rand>();
23 std::clog << '\n';
24 static_assert(kblib::state_size_v<std::minstd_rand> == 1, ".");
25 static_assert(kblib::state_size_v<std::minstd_rand0> == 1, ".");
26 std::clog << "LCG(64): ";
27 KBLIB_UNUSED auto gen2 = kblib::seeded<kblib::lcgs::java_rand>();
28 std::clog << '\n';
29 static_assert(kblib::state_size_v<kblib::lcgs::java_rand> == 2, ".");
30 }
31 SECTION("Subtract with Carry Engine") {
32 std::clog << "SwCE 24: ";
33 KBLIB_UNUSED auto gen = kblib::seeded<std::ranlux24_base>();
34 std::clog << "SwCE 48: ";
35 KBLIB_UNUSED auto gen2 = kblib::seeded<std::ranlux24_base>();
36 std::clog << '\n';
37 static_assert(kblib::state_size_v<std::ranlux24_base> == 24, ".");
38 static_assert(kblib::state_size_v<std::ranlux48_base> == 24, ".");
39 }
40 SECTION("Discard Block Engine") {
41 std::clog << "DBE: ";
42 KBLIB_UNUSED auto gen
43 = kblib::seeded<std::discard_block_engine<std::minstd_rand, 16, 8>>();
44 std::clog << '\n';
45 std::clog << "RANLUX 24: ";
46 KBLIB_UNUSED auto gen2 = kblib::seeded<std::ranlux24>();
47 std::clog << '\n';
48 static_assert(
50 std::discard_block_engine<std::minstd_rand, 16, 8>> == 1,
51 ".");
52 static_assert(kblib::state_size_v<
53 std::discard_block_engine<std::mt19937, 16, 8>> == 624,
54 ".");
55 static_assert(kblib::state_size_v<std::ranlux24> == 24, ".");
56 }
57 SECTION("Independent Bits Engine") {
58 std::clog << "IBE: ";
59 KBLIB_UNUSED auto gen
60 = kblib::seeded<std::independent_bits_engine<std::ranlux24, 32,
61 std::uint_fast32_t>>();
62 std::clog << '\n';
63 static_assert(kblib::state_size_v<std::independent_bits_engine<
64 std::ranlux24, 32, std::uint_fast32_t>> == 24,
65 ".");
66 static_assert(kblib::state_size_v<std::independent_bits_engine<
67 std::mt19937, 32, std::uint_fast32_t>> == 624,
68 ".");
69 static_assert(kblib::state_size_v<std::independent_bits_engine<
70 std::minstd_rand, 32, std::uint_fast32_t>> == 1,
71 ".");
72 }
73 SECTION("Shuffle Order Engine") {
74 std::clog << "SOE: ";
75 KBLIB_UNUSED auto gen
76 = kblib::seeded<std::shuffle_order_engine<std::ranlux24, 32>>();
77 std::clog << '\n';
78 static_assert(kblib::state_size_v<
79 std::shuffle_order_engine<std::ranlux24, 32>> == 24,
80 ".");
81 static_assert(kblib::state_size_v<
82 std::shuffle_order_engine<std::mt19937, 32>> == 624,
83 ".");
84 static_assert(kblib::state_size_v<
85 std::shuffle_order_engine<std::minstd_rand, 32>> == 1,
86 ".");
87 }
88}
89
90TEST_CASE("transform_engine") {
91 using E
92 = kblib::transform_engine<std::ranlux24,
94 auto gen = kblib::seeded<E>();
95 static_assert(E::min() == 0, ".");
96 static_assert(E::max() == ((std::ranlux24::max() >> 16u)), ".");
97 SECTION("copying") {
98 auto gen1 = gen;
99 REQUIRE(gen == gen1);
100 }
101 SECTION("i/o") {
102 decltype(gen) gen1, gen2;
103 REQUIRE(gen1 == gen2);
104 REQUIRE(gen != gen1);
105 REQUIRE(gen != gen2);
106 std::stringstream ss;
107 ss << gen;
108 ss >> gen1;
109 REQUIRE(gen == gen1);
110 REQUIRE(gen != gen2);
111 REQUIRE(gen1 != gen2);
112 }
113 std::uniform_int_distribution<int> dist;
114 (void)dist(gen);
115}
116
117TEST_CASE("LCG verification") {
118 //
119}
constexpr struct kblib::nums::min_t min
constexpr struct kblib::nums::max_t max
auto seeded(Source &&s) -> Gen
Definition: random.h:206
constexpr std::size_t state_size_v
Definition: random.h:189
TEST_CASE("Random engine seeding")
Definition: random.cpp:12
Provides utilities to correctly and expressively use C++11's random number generation library,...
Definition: bits.cpp:9
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
Definition: tdecl.h:130