Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / xbt / dict_test.cpp
index ed105ea..4e76137 100644 (file)
@@ -1,6 +1,6 @@
 /* dict - a generic dictionary, variation over hash table                   */
 
-/* Copyright (c) 2004-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -8,11 +8,11 @@
 #include "xbt/dict.h"
 
 #include "simgrid/Exception.hpp"
+#include "xbt/random.hpp"
 #include <cstdio>
 #include <cstring>
-#include <random>
 
-#include "catch.hpp"
+#include "src/3rd-party/catch.hpp"
 
 #define STR(str) ((str) ? (str) : "(null)")
 
@@ -52,7 +52,7 @@ static xbt_dict_t new_fixture()
 static void search_ext(const_xbt_dict_t head, const char* key, const char* data)
 {
   INFO("Search " << STR(key));
-  char* found = (char*)xbt_dict_get_or_null(head, key);
+  auto* found = static_cast<char*>(xbt_dict_get_or_null(head, key));
   INFO("Found " << STR(found));
   if (data) {
     REQUIRE(found); // data do not match expectations: found null while searching for data
@@ -93,21 +93,6 @@ static void search_not_found(const_xbt_dict_t head, const char* data)
   REQUIRE(xbt_dict_get_or_null(head, data) == nullptr);
 }
 
-static void count(const_xbt_dict_t dict, int length)
-{
-  INFO("Count elements (expecting " << length << ")");
-  REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
-
-  xbt_dict_cursor_t cursor;
-  char* key;
-  void* data;
-  int effective = 0;
-  xbt_dict_foreach (dict, cursor, key, data)
-    effective++;
-
-  REQUIRE(effective == length); // Effective length differs
-}
-
 static int countelems(const_xbt_dict_t head)
 {
   xbt_dict_cursor_t cursor;
@@ -121,6 +106,13 @@ static int countelems(const_xbt_dict_t head)
   return res;
 }
 
+static void count(const_xbt_dict_t dict, int length)
+{
+  INFO("Count elements (expecting " << length << ")");
+  REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
+  REQUIRE(countelems(dict) == length);      // Effective length differs
+}
+
 TEST_CASE("xbt::dict: dict data container", "dict")
 {
   SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
@@ -264,9 +256,6 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
   SECTION("Crash test")
   {
-    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)");
       INFO("Fill the struct, count its elems and frees the structure");
@@ -274,11 +263,11 @@ TEST_CASE("xbt::dict: dict data container", "dict")
       xbt_dict_t head = xbt_dict_new_homogeneous(free);
       for (int j = 0; j < 1000; j++) {
         const char* data = nullptr;
-        char* key  = (char*)xbt_malloc(SIZEOFKEY);
+        auto* key        = static_cast<char*>(xbt_malloc(SIZEOFKEY));
 
         do {
           for (int k = 0; k < SIZEOFKEY - 1; k++) {
-            key[k] = rnd_engine() % ('z' - 'a') + 'a';
+            key[k] = simgrid::xbt::random::uniform_int('a', 'z');
           }
           key[SIZEOFKEY - 1] = '\0';
           data               = (char*)xbt_dict_get_or_null(head, key);
@@ -298,9 +287,9 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     xbt_dict_t head = xbt_dict_new_homogeneous(&free);
     INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
     for (int j = 0; j < NB_ELM; j++) {
-      char* key = (char*)xbt_malloc(10);
+      auto* key = static_cast<char*>(xbt_malloc(12));
 
-      snprintf(key, 10, "%d", j);
+      snprintf(key, 12, "%d", j);
       xbt_dict_set(head, key, key);
     }
 
@@ -308,10 +297,10 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     INFO("There is " << countelems(head) << " elements");
 
     INFO("Search my " << NB_ELM << " elements 20 times");
-    char* key = (char*)xbt_malloc(10);
+    auto* key = static_cast<char*>(xbt_malloc(12));
     for (int i = 0; i < 20; i++) {
       for (int j = 0; j < NB_ELM; j++) {
-        snprintf(key, 10, "%d", j);
+        snprintf(key, 12, "%d", j);
         void* data = xbt_dict_get_or_null(head, key);
         REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
         data = xbt_dict_get_or_null_ext(head, key, strlen(key));
@@ -321,9 +310,9 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     xbt_free(key);
 
     INFO("Remove my " << NB_ELM << " elements");
-    key = (char*)xbt_malloc(10);
+    key = (char*)xbt_malloc(12);
     for (int j = 0; j < NB_ELM; j++) {
-      snprintf(key, 10, "%d", j);
+      snprintf(key, 12, "%d", j);
       xbt_dict_remove_ext(head, key, strlen(key));
     }
     xbt_free(key);