Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / xbt / dict_test.cpp
index f24acad..e06ed08 100644 (file)
@@ -1,6 +1,6 @@
 /* dict - a generic dictionary, variation over hash table                   */
 
-/* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2021. 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,9 +8,9 @@
 #include "xbt/dict.h"
 
 #include "simgrid/Exception.hpp"
+#include "xbt/random.hpp"
 #include <cstdio>
 #include <cstring>
-#include <random>
 
 #include "catch.hpp"
 
@@ -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(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
@@ -71,7 +71,7 @@ static void search(const_xbt_dict_t head, const char* key)
 static void debugged_remove(xbt_dict_t head, const char* key)
 {
   INFO("Remove '" << STR(key) << "'");
-  xbt_dict_remove(head, key);
+  xbt_dict_remove_ext(head, key, strlen(key));
 }
 
 static void traverse(const_xbt_dict_t head)
@@ -90,7 +90,7 @@ static void traverse(const_xbt_dict_t head)
 static void search_not_found(const_xbt_dict_t head, const char* data)
 {
   INFO("Search " << STR(data) << " (expected not to be found)");
-  REQUIRE_THROWS_AS(xbt_dict_get(head, data), std::out_of_range);
+  REQUIRE(xbt_dict_get_or_null(head, data) == nullptr);
 }
 
 static void count(const_xbt_dict_t dict, int length)
@@ -108,25 +108,6 @@ static void count(const_xbt_dict_t dict, int length)
   REQUIRE(effective == length); // Effective length differs
 }
 
-static void count_check_get_key(const_xbt_dict_t dict, int length)
-{
-  xbt_dict_cursor_t cursor;
-  char* key;
-  void* data;
-  int effective = 0;
-
-  INFO("Count elements (expecting " << length << "), and test the getkey function");
-  REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
-
-  xbt_dict_foreach (dict, cursor, key, data) {
-    effective++;
-    char* key2 = xbt_dict_get_key(dict, data);
-    xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
-  }
-
-  REQUIRE(effective == length); // Effective length differs
-}
-
 static int countelems(const_xbt_dict_t head)
 {
   xbt_dict_cursor_t cursor;
@@ -155,7 +136,7 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     INFO("Traverse the full dictionary");
     head = new_fixture();
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     debugged_add_ext(head, "toto", "tutu");
     search_ext(head, "toto", "tutu");
@@ -170,29 +151,29 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     /* CHANGING */
     head = new_fixture();
-    count_check_get_key(head, 7);
+    count(head, 7);
     INFO("Change 123 to 'Changed 123'");
     xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 123 back to '123'");
     xbt_dict_set(head, "123", xbt_strdup("123"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 12a to 'Dummy 12a'");
     xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Change 12a to '12a'");
     xbt_dict_set(head, "12a", xbt_strdup("12a"));
-    count_check_get_key(head, 7);
+    count(head, 7);
 
     INFO("Traverse the resulting dictionary");
     traverse(head);
 
     /* RETRIEVE */
     INFO("Search 123");
-    const char* data = (char*)xbt_dict_get(head, "123");
+    const char* data = (char*)xbt_dict_get_or_null(head, "123");
     REQUIRE((data && strcmp("123", data) == 0));
 
     search_not_found(head, "Can't be found");
@@ -252,13 +233,6 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
     traverse(head);
 
-    INFO("Free dict, create new fresh one, and then reset the dict");
-    xbt_dict_free(&head);
-    head = new_fixture();
-    xbt_dict_reset(head);
-    count(head, 0);
-    traverse(head);
-
     INFO("Free the dictionary twice");
     xbt_dict_free(&head);
     xbt_dict_free(&head);
@@ -290,9 +264,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");
@@ -300,19 +271,19 @@ 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);
         } while (data != nullptr);
 
         xbt_dict_set(head, key, key);
-        data = (char*)xbt_dict_get(head, key);
-        REQUIRE(not strcmp(key, data)); // Retrieved value != Injected value
+        data = (char*)xbt_dict_get_or_null(head, key);
+        REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
 
         count(head, j + 1);
       }
@@ -324,7 +295,7 @@ 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(10));
 
       snprintf(key, 10, "%d", j);
       xbt_dict_set(head, key, key);
@@ -334,14 +305,14 @@ 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(10));
     for (int i = 0; i < 20; i++) {
       for (int j = 0; j < NB_ELM; j++) {
         snprintf(key, 10, "%d", j);
-        void* data = xbt_dict_get(head, key);
-        REQUIRE(not strcmp(key, (char*)data)); // with get, key != data
-        data = xbt_dict_get_ext(head, key, strlen(key));
-        REQUIRE(not strcmp(key, (char*)data)); // with get_ext, key != data
+        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));
+        REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
       }
     }
     xbt_free(key);
@@ -350,7 +321,7 @@ TEST_CASE("xbt::dict: dict data container", "dict")
     key = (char*)xbt_malloc(10);
     for (int j = 0; j < NB_ELM; j++) {
       snprintf(key, 10, "%d", j);
-      xbt_dict_remove(head, key);
+      xbt_dict_remove_ext(head, key, strlen(key));
     }
     xbt_free(key);
 
@@ -371,8 +342,8 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
     INFO("Check elements");
     for (int i = 0; i < count; ++i) {
-      xbt_dict_get_ext(dict, (char*)&i, sizeof(i));
-      REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Unexpected value at index i
+      void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
+      REQUIRE((intptr_t)data == i); // Unexpected value at index i
     }
 
     INFO("Free the array");