Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'framagit/master'
[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 EPSILON (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(simgrid::xbt::random::exponential(25) == Approx(0.00291934351538427348).epsilon(EPSILON));
20     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4);
21     REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == Approx(0.31637556043369124970).epsilon(EPSILON));
22     REQUIRE(simgrid::xbt::random::normal(0, 2) == Approx(1.62746784745133976635).epsilon(EPSILON));
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(simgrid::xbt::random::exponential(25) == Approx(distA(gen)).epsilon(EPSILON));
39     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == distB(gen));
40     REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == Approx(distC(gen)).epsilon(EPSILON));
41     REQUIRE(simgrid::xbt::random::normal(0, 2) == Approx(distD(gen)).epsilon(EPSILON));
42   }
43 }