QMCPACK
test_FakeRandom.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2023 QMCPACK developers.
6 //
7 // File developed by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National lab
8 //
9 // File created by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National lab
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "Utilities/FakeRandom.h"
16 
17 namespace qmcplusplus
18 {
19 TEST_CASE("FakeRandom determinism", "[utilities]")
20 {
21  double expected = 0.5;
22  FakeRandom<double> our_rng;
23  for (auto i = 0; i < 3; ++i)
24  CHECK(our_rng() == Approx(expected));
25 }
26 
27 TEST_CASE("FakeRandom set_value", "[utilities]")
28 {
29  FakeRandom<double> our_rng;
30  double expected = 0.25;
31  our_rng.set_value(expected);
32  for (auto i = 0; i < 3; ++i)
33  CHECK(our_rng() == Approx(expected));
34 }
35 
36 TEST_CASE("FakeRandom read write", "[utilities]")
37 {
38  using DoubleRNG = FakeRandom<double>;
39  DoubleRNG rng;
40 
41  rng.set_value(1.0);
42 
43  std::stringstream stream;
44  rng.write(stream);
45 
46  DoubleRNG rng2;
47 
48  rng2.read(stream);
49 
50  for (auto i = 0; i < 3; ++i)
51  CHECK(rng2() == rng());
52 }
53 
54 TEST_CASE("FakeRandom noops", "[utilities]")
55 {
56  using DoubleRNG = FakeRandom<double>;
57  DoubleRNG rng;
58  double expected{2.5};
59 
60  rng.set_value(expected);
61  CHECK(rng() == Approx(expected));
62 
63  rng.init(0);
64  CHECK(rng() == Approx(expected));
65 
66  rng.seed(1);
67  CHECK(rng() == Approx(expected));
68 
69  std::vector<DoubleRNG::uint_type> state;
70  rng.load(state);
71  CHECK(state.empty());
72  CHECK(rng() == Approx(expected));
73 
74  rng.save(state);
75  CHECK(rng() == Approx(expected));
76 }
77 
78 TEST_CASE("FakeRandom clone", "[utilities]")
79 {
80  using DoubleRNG = FakeRandom<double>;
81  DoubleRNG rng;
82 
83  std::stringstream stream1;
84  rng.write(stream1);
85 
86  auto rng2 = rng.makeClone();
87 
88  std::stringstream stream2;
89  rng2->write(stream2);
90 
91  CHECK(stream1.str() == stream2.str());
92 
93  for (auto i = 0; i < 3; ++i)
94  CHECK((*rng2)() == rng());
95 }
96 
97 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
void write(std::ostream &rout) const override
Definition: FakeRandom.h:34
void set_value(double val)
Definition: FakeRandom.cpp:21
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))