Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++11 <random> instead of rand().
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 28 Feb 2019 14:00:06 +0000 (15:00 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 28 Feb 2019 14:36:03 +0000 (15:36 +0100)
src/mc/sosp/mc_snapshot_test.cpp
src/xbt/dict_test.cpp
teshsuite/mc/dwarf-expression/dwarf-expression.cpp

index 7cef004..50cd1a2 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <cstdlib>
 #include <cstring>
 
 #include <cstdlib>
 #include <cstring>
+#include <random>
 
 #include <sys/mman.h>
 
 
 #include <sys/mman.h>
 
@@ -41,11 +42,13 @@ public:
     mc_model_checker = nullptr;
   }
 
     mc_model_checker = nullptr;
   }
 
+  static std::default_random_engine rnd_engine;
   static bool sparse_checkpoint;
   static std::unique_ptr<simgrid::mc::RemoteClient> process;
 };
 
 // static member variables init.
   static bool sparse_checkpoint;
   static std::unique_ptr<simgrid::mc::RemoteClient> process;
 };
 
 // static member variables init.
+std::default_random_engine snap_test_helper::rnd_engine;
 bool snap_test_helper::sparse_checkpoint                             = 0;
 std::unique_ptr<simgrid::mc::RemoteClient> snap_test_helper::process = nullptr;
 
 bool snap_test_helper::sparse_checkpoint                             = 0;
 std::unique_ptr<simgrid::mc::RemoteClient> 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) {
 {
   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) {
     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));
       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) {
     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,
 
       INFO("Mismatch in MC_snapshot_region_memcmp()");
       REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset,
index dd2fc82..5facbd8 100644 (file)
@@ -10,6 +10,7 @@
 #include "simgrid/Exception.hpp"
 #include <cstdio>
 #include <cstring>
 #include "simgrid/Exception.hpp"
 #include <cstdio>
 #include <cstring>
+#include <random>
 
 #include "catch.hpp"
 
 
 #include "catch.hpp"
 
@@ -295,7 +296,8 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
   SECTION("Crash test")
   {
 
   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)");
 
     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++) {
 
         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);
           }
           key[SIZEOFKEY - 1] = '\0';
           data               = (char*)xbt_dict_get_or_null(head, key);
index 8dfb552..a3c143f 100644 (file)
@@ -11,6 +11,7 @@
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
 #include <cassert>
 #include <cstdlib>
 #include <cstring>
+#include <random>
 
 #include "src/mc/mc_private.hpp"
 
 
 #include "src/mc/mc_private.hpp"
 
@@ -19,6 +20,8 @@
 #include "src/mc/Variable.hpp"
 #include "src/mc/remote/RemoteClient.hpp"
 
 #include "src/mc/Variable.hpp"
 #include "src/mc/remote/RemoteClient.hpp"
 
+static std::default_random_engine rnd_engine;
+
 static simgrid::mc::RemoteClient* process;
 
 static
 static simgrid::mc::RemoteClient* process;
 
 static
@@ -51,8 +54,8 @@ void basic_test(simgrid::dwarf::ExpressionContext const& state) {
 
   Dwarf_Op ops[60];
 
 
   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;
 
 
   simgrid::dwarf::ExpressionStack stack;
 
@@ -161,26 +164,26 @@ int main(int argc, char** argv) {
   basic_test(state);
 
   for(int i=0; i!=100; ++i) {
   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) {
     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) {
     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) {
     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));
   }
 
     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
   }