Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / xbt / random_test.cpp
1 /* Copyright (c) 2019. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/include/catch.hpp"
7 #include "xbt/log.h"
8 #include "xbt/random.hpp"
9 #include <random>
10 #include <cmath>
11
12 #define EpsilonApprox(a) Catch::Matchers::WithinAbs(a, 100 * std::numeric_limits<double>::epsilon())
13
14 TEST_CASE("xbt::random: Random Number Generation")
15 {
16   SECTION("Using XBT_RNG_xbt")
17   {
18     simgrid::xbt::random::set_mersenne_seed(12345);
19     REQUIRE_THAT(simgrid::xbt::random::exponential(25), EpsilonApprox(0.00291934351538427348));
20     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4);
21     REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(0.31637556043369124970));
22     REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(1.62746784745133976635));
23   }
24
25   SECTION("Using XBT_RNG_std")
26   {
27     std::mt19937 gen;
28     gen.seed(12345);
29
30     simgrid::xbt::random::set_mersenne_seed(12345);
31     simgrid::xbt::random::set_implem_std();
32
33     std::exponential_distribution<> distA(25);
34     std::uniform_int_distribution<> distB(1, 6);
35     std::uniform_real_distribution<> distC(0, 1);
36     std::normal_distribution<> distD(0, 2);
37
38     REQUIRE_THAT(simgrid::xbt::random::exponential(25), EpsilonApprox(distA(gen)));
39     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == distB(gen));
40     REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(distC(gen)));
41     REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(distD(gen)));
42   }
43 }