From: Arnaud Giersch Date: Thu, 28 Feb 2019 14:00:06 +0000 (+0100) Subject: Use C++11 instead of rand(). X-Git-Tag: v3_22~210 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/7233106ddd4d22acaefbba0e6167350caaafe65d?hp=54db7dfe5eb2b57f880a9b8abbdf418fb90e1f26 Use C++11 instead of rand(). --- diff --git a/src/mc/sosp/mc_snapshot_test.cpp b/src/mc/sosp/mc_snapshot_test.cpp index 7cef004bb2..50cd1a209f 100644 --- a/src/mc/sosp/mc_snapshot_test.cpp +++ b/src/mc/sosp/mc_snapshot_test.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -41,11 +42,13 @@ public: mc_model_checker = nullptr; } + static std::default_random_engine rnd_engine; static bool sparse_checkpoint; static std::unique_ptr process; }; // static member variables init. +std::default_random_engine snap_test_helper::rnd_engine; bool snap_test_helper::sparse_checkpoint = 0; std::unique_ptr snap_test_helper::process = nullptr; @@ -53,7 +56,7 @@ void snap_test_helper::init_memory(void* mem, size_t size) { char* dest = (char*)mem; for (size_t i = 0; i < size; ++i) { - dest[i] = rand() & 255; + dest[i] = rnd_engine() & 255; } } @@ -118,8 +121,8 @@ void snap_test_helper::read_region_parts() prologue_return ret = prologue(n); for (int j = 0; j != 100; ++j) { - size_t offset = rand() % ret.size; - size_t size = rand() % (ret.size - offset); + size_t offset = rnd_engine() % ret.size; + size_t size = rnd_engine() % (ret.size - offset); const void* read = MC_region_read(&(ret.region), ret.dstn, (const char*)ret.src + offset, size); INFO("Mismatch in MC_region_read()"); REQUIRE(not memcmp((char*)ret.src + offset, read, size)); @@ -150,8 +153,8 @@ void snap_test_helper::compare_region_parts() prologue_return ret = prologue(n); for (int j = 0; j != 100; ++j) { - size_t offset = rand() % ret.size; - size_t size = rand() % (ret.size - offset); + size_t offset = rnd_engine() % ret.size; + size_t size = rnd_engine() % (ret.size - offset); INFO("Mismatch in MC_snapshot_region_memcmp()"); REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset, diff --git a/src/xbt/dict_test.cpp b/src/xbt/dict_test.cpp index dd2fc82a5d..5facbd8a55 100644 --- a/src/xbt/dict_test.cpp +++ b/src/xbt/dict_test.cpp @@ -10,6 +10,7 @@ #include "simgrid/Exception.hpp" #include #include +#include #include "catch.hpp" @@ -295,7 +296,8 @@ TEST_CASE("xbt::dict: dict data container", "dict") SECTION("Crash test") { - srand((unsigned int)time(nullptr)); + std::random_device rd; + std::default_random_engine rnd_engine(rd()); for (int i = 0; i < 10; i++) { INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)"); @@ -308,7 +310,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") do { for (int k = 0; k < SIZEOFKEY - 1; k++) { - key[k] = rand() % ('z' - 'a') + 'a'; + key[k] = rnd_engine() % ('z' - 'a') + 'a'; } key[SIZEOFKEY - 1] = '\0'; data = (char*)xbt_dict_get_or_null(head, key); diff --git a/teshsuite/mc/dwarf-expression/dwarf-expression.cpp b/teshsuite/mc/dwarf-expression/dwarf-expression.cpp index 8dfb5527f9..a3c143ffe3 100644 --- a/teshsuite/mc/dwarf-expression/dwarf-expression.cpp +++ b/teshsuite/mc/dwarf-expression/dwarf-expression.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "src/mc/mc_private.hpp" @@ -19,6 +20,8 @@ #include "src/mc/Variable.hpp" #include "src/mc/remote/RemoteClient.hpp" +static std::default_random_engine rnd_engine; + static simgrid::mc::RemoteClient* process; static @@ -51,8 +54,8 @@ void basic_test(simgrid::dwarf::ExpressionContext const& state) { Dwarf_Op ops[60]; - uintptr_t a = rand(); - uintptr_t b = rand(); + uintptr_t a = rnd_engine(); + uintptr_t b = rnd_engine(); simgrid::dwarf::ExpressionStack stack; @@ -161,26 +164,26 @@ int main(int argc, char** argv) { basic_test(state); for(int i=0; i!=100; ++i) { - uintptr_t a = rand(); - uintptr_t b = rand(); + uintptr_t a = rnd_engine(); + uintptr_t b = rnd_engine(); assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b)); } for(int i=0; i!=100; ++i) { - uintptr_t a = rand(); - uintptr_t b = rand(); + uintptr_t a = rnd_engine(); + uintptr_t b = rnd_engine(); assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b)); } for(int i=0; i!=100; ++i) { - uintptr_t a = rand(); - uintptr_t b = rand(); + uintptr_t a = rnd_engine(); + uintptr_t b = rnd_engine(); assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b)); } for(int i=0; i!=100; ++i) { - uintptr_t a = rand(); - uintptr_t b = rand(); + uintptr_t a = rnd_engine(); + uintptr_t b = rnd_engine(); assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b)); }