Lumiera  0.pre.03
»edit your freedom«
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 
25 using util::isLimited;
26 
27 namespace lib {
28 namespace test {
29 
30  /******************************************************************/
35  class Random_test : public Test
36  {
37 
38  virtual void
39  run (Arg)
40  {
41  simpleUsage();
44  }
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
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
lib::HashVal ranHash()
Definition: random.hpp:155
Helpers typically used while writing tests.
Implementation namespace for support and library code.
Simplistic test class runner.
void reseed(SeedNucleus &)
inject controlled randomisation
Definition: random.hpp:188
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Establishes a seed point for any instance or performance.
Definition: random.hpp:48
Generating (pseudo) random numbers with controlled seed.
int i32()
random number from full integer range (incl. negative values)
Definition: random.hpp:216
void verify_distributionVariants()
Definition: random-test.cpp:70