Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
random-test.cpp
Go to the documentation of this file.
1/*
2 Random(Test) - verify framework for controlled random number generation
3
4 Copyright (C)
5 2024, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
20#include "lib/test/run.hpp"
21#include "lib/random.hpp"
22#include "lib/util.hpp"
24
25using util::isLimited;
26
27namespace lib {
28namespace test {
29
30 /******************************************************************/
35 class Random_test : public Test
36 {
37
38 virtual void
45
46
50 void
52 {
53 seedRand();
54
55 int r1 = rani();
56 CHECK (0 <= r1 and r1 < RAND_MAX);
57
58 int r2 = rani();
59 CHECK (0 <= r2 and r2 < RAND_MAX);
60 CHECK (r1 != r2); // may fail with very low probability
61 }
62
63
69 void
71 {
72 double avg{0.0};
73 const uint N = 1e6;
74 for (uint i=0; i < N; ++i)
75 avg += 1.0/N * rani (1000);
76
77 auto expect = 500;
78 auto error = fabs(avg/expect - 1);
79 CHECK (error < 0.005);
80
81 for (uint i=0; i < N; ++i)
82 CHECK (isLimited(0, rani(5), 4));
83
84 for (uint i=0; i < N; ++i)
85 CHECK (0 != ranHash());
86
87 auto sqr = [](double v){ return v*v; };
88
89 double spread{0.0};
90 for (uint i=0; i < N; ++i)
91 spread += sqr (ranNormal() - 0.5);
92 spread = sqrt (spread/N);
93 CHECK (spread < 1.12);
94 }
95
96
97
106 void
108 {
109 class : public SeedNucleus
110 {
111 uint64_t getSeed() override { return 55; }
112 }
113 coreOfEvil;
114
115 Random src1{coreOfEvil};
116
117 int r1 = src1.i32();
118 uint64_t r2 = src1.u64();
119 double r3 = src1.uni();
120
121 Random src2{coreOfEvil};
122 CHECK (r1 == src2.i32());
123 CHECK (r2 == src2.u64());
124 CHECK (r3 == src2.uni());
125
126 src1.reseed (coreOfEvil);
127 CHECK (src1.u64() != src2.u64());
128
129 src2.reseed (coreOfEvil);
130 CHECK (src1.u64() != src2.u64());
131 (void) src2.u64();
132 CHECK (src1.u64() == src2.u64());
133 CHECK (src1.i32() == src2.i32());
134 CHECK (src1.uni() == src2.uni());
135 }
136 };
137
138 LAUNCHER (Random_test, "unit common");
139
140
141}} // namespace lib::test
int i32()
random number from full integer range (incl. negative values)
Definition random.hpp:216
Establishes a seed point for any instance or performance.
Definition random.hpp:49
virtual void run(Arg)
Helpers typically used while writing tests.
unsigned int uint
Definition integral.hpp:29
Implementation namespace for support and library code.
lib::HashVal ranHash()
Definition random.hpp:155
int rani(uint bound=_iBOUND())
Definition random.hpp:135
double ranNormal(double mean=0.0, double stdev=1.0)
Definition random.hpp:148
Test runner and basic definitions for tests.
constexpr bool isLimited(NB lowerBound, NUM val, NB upperBound)
Definition util.hpp:99
Generating (pseudo) random numbers with controlled seed.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...