Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use #include <...> for foreign header files.
[simgrid.git] / src / xbt / dict_test.cpp
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/dict.h"
9
10 #include "simgrid/Exception.hpp"
11 #include "xbt/random.hpp"
12 #include <cstdio>
13 #include <cstring>
14
15 #include "src/3rd-party/catch.hpp"
16
17 #define STR(str) ((str) ? (str) : "(null)")
18
19 static constexpr int NB_ELM    = 20000;
20 static constexpr int SIZEOFKEY = 1024;
21
22 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
23 {
24   char* data = xbt_strdup(data_to_fill);
25
26   INFO("Add " << STR(data_to_fill) << " under " << STR(key));
27
28   xbt_dict_set(head, key, data);
29 }
30
31 static void debugged_add(xbt_dict_t head, const char* key)
32 {
33   debugged_add_ext(head, key, key);
34 }
35
36 static xbt_dict_t new_fixture()
37 {
38   INFO("Fill in the dictionary");
39
40   xbt_dict_t head = xbt_dict_new_homogeneous(&free);
41   debugged_add(head, "12");
42   debugged_add(head, "12a");
43   debugged_add(head, "12b");
44   debugged_add(head, "123");
45   debugged_add(head, "123456");
46   debugged_add(head, "1234");
47   debugged_add(head, "123457");
48
49   return head;
50 }
51
52 static void search_ext(const_xbt_dict_t head, const char* key, const char* data)
53 {
54   INFO("Search " << STR(key));
55   auto* found = static_cast<char*>(xbt_dict_get_or_null(head, key));
56   INFO("Found " << STR(found));
57   if (data) {
58     REQUIRE(found); // data do not match expectations: found null while searching for data
59     if (found)
60       REQUIRE(not strcmp(data, found)); // data do not match expectations: found another string while searching for data
61   } else {
62     REQUIRE(not found); // data do not match expectations: found something while searching for null
63   }
64 }
65
66 static void search(const_xbt_dict_t head, const char* key)
67 {
68   search_ext(head, key, key);
69 }
70
71 static void debugged_remove(xbt_dict_t head, const char* key)
72 {
73   INFO("Remove '" << STR(key) << "'");
74   xbt_dict_remove_ext(head, key, strlen(key));
75 }
76
77 static void traverse(const_xbt_dict_t head)
78 {
79   xbt_dict_cursor_t cursor = nullptr;
80   char* key;
81   char* data;
82   int i = 0;
83
84   xbt_dict_foreach (head, cursor, key, data) {
85     INFO("Seen #" << ++i << ": " << STR(key) << "->" << STR(data));
86     REQUIRE((key && data && strcmp(key, data) == 0)); //  key != value
87   }
88 }
89
90 static void search_not_found(const_xbt_dict_t head, const char* data)
91 {
92   INFO("Search " << STR(data) << " (expected not to be found)");
93   REQUIRE(xbt_dict_get_or_null(head, data) == nullptr);
94 }
95
96 static int countelems(const_xbt_dict_t head)
97 {
98   xbt_dict_cursor_t cursor;
99   char* key;
100   void* data;
101   int res = 0;
102
103   xbt_dict_foreach (head, cursor, key, data) {
104     res++;
105   }
106   return res;
107 }
108
109 static void count(const_xbt_dict_t dict, int length)
110 {
111   INFO("Count elements (expecting " << length << ")");
112   REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
113   REQUIRE(countelems(dict) == length);      // Effective length differs
114 }
115
116 TEST_CASE("xbt::dict: dict data container", "dict")
117 {
118   SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
119   {
120     INFO("Traversal the null dictionary");
121     traverse(nullptr);
122
123     INFO("Traversal and search the empty dictionary");
124     xbt_dict_t head = xbt_dict_new_homogeneous(&free);
125     traverse(head);
126     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
127     xbt_dict_free(&head);
128
129     INFO("Traverse the full dictionary");
130     head = new_fixture();
131     count(head, 7);
132
133     debugged_add_ext(head, "toto", "tutu");
134     search_ext(head, "toto", "tutu");
135     debugged_remove(head, "toto");
136
137     search(head, "12a");
138     traverse(head);
139
140     INFO("Free the dictionary (twice)");
141     xbt_dict_free(&head);
142     xbt_dict_free(&head);
143
144     /* CHANGING */
145     head = new_fixture();
146     count(head, 7);
147     INFO("Change 123 to 'Changed 123'");
148     xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
149     count(head, 7);
150
151     INFO("Change 123 back to '123'");
152     xbt_dict_set(head, "123", xbt_strdup("123"));
153     count(head, 7);
154
155     INFO("Change 12a to 'Dummy 12a'");
156     xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
157     count(head, 7);
158
159     INFO("Change 12a to '12a'");
160     xbt_dict_set(head, "12a", xbt_strdup("12a"));
161     count(head, 7);
162
163     INFO("Traverse the resulting dictionary");
164     traverse(head);
165
166     /* RETRIEVE */
167     INFO("Search 123");
168     const char* data = (char*)xbt_dict_get_or_null(head, "123");
169     REQUIRE((data && strcmp("123", data) == 0));
170
171     search_not_found(head, "Can't be found");
172     search_not_found(head, "123 Can't be found");
173     search_not_found(head, "12345678 NOT");
174
175     search(head, "12a");
176     search(head, "12b");
177     search(head, "12");
178     search(head, "123456");
179     search(head, "1234");
180     search(head, "123457");
181
182     INFO("Traverse the resulting dictionary");
183     traverse(head);
184
185     INFO("Free the dictionary twice");
186     xbt_dict_free(&head);
187     xbt_dict_free(&head);
188
189     INFO("Traverse the resulting dictionary");
190     traverse(head);
191   }
192
193   SECTION("Removing some values from homogeneous dicts")
194   {
195     xbt_dict_t head = new_fixture();
196     count(head, 7);
197     INFO("Remove non existing data");
198     REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
199     traverse(head);
200
201     xbt_dict_free(&head);
202
203     INFO("Remove each data manually (traversing the resulting dictionary each time)");
204     head = new_fixture();
205     debugged_remove(head, "12a");
206     traverse(head);
207     count(head, 6);
208     debugged_remove(head, "12b");
209     traverse(head);
210     count(head, 5);
211     debugged_remove(head, "12");
212     traverse(head);
213     count(head, 4);
214     debugged_remove(head, "123456");
215     traverse(head);
216     count(head, 3);
217     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
218     traverse(head);
219     debugged_remove(head, "1234");
220     traverse(head);
221     debugged_remove(head, "123457");
222     traverse(head);
223     debugged_remove(head, "123");
224     traverse(head);
225     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
226     traverse(head);
227
228     INFO("Free the dictionary twice");
229     xbt_dict_free(&head);
230     xbt_dict_free(&head);
231   }
232
233   SECTION("nullptr data management")
234   {
235     xbt_dict_t head = new_fixture();
236
237     INFO("Store nullptr under 'null'");
238     xbt_dict_set(head, "null", nullptr);
239     search_ext(head, "null", nullptr);
240
241     INFO("Check whether I see it while traversing...");
242     xbt_dict_cursor_t cursor = nullptr;
243     char* key;
244     bool found = false;
245     char* data;
246
247     xbt_dict_foreach (head, cursor, key, data) {
248       INFO("Seen: " << STR(key) << "->" << STR(data));
249       if (key && strcmp(key, "null") == 0)
250         found = true;
251     }
252     REQUIRE(found); // the key 'null', associated to nullptr is not found
253
254     xbt_dict_free(&head);
255   }
256
257   SECTION("Crash test")
258   {
259     for (int i = 0; i < 10; i++) {
260       INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
261       INFO("Fill the struct, count its elems and frees the structure");
262       INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
263       xbt_dict_t head = xbt_dict_new_homogeneous(free);
264       for (int j = 0; j < 1000; j++) {
265         const char* data = nullptr;
266         auto* key        = static_cast<char*>(xbt_malloc(SIZEOFKEY));
267
268         do {
269           for (int k = 0; k < SIZEOFKEY - 1; k++) {
270             key[k] = simgrid::xbt::random::uniform_int('a', 'z');
271           }
272           key[SIZEOFKEY - 1] = '\0';
273           data               = (char*)xbt_dict_get_or_null(head, key);
274         } while (data != nullptr);
275
276         xbt_dict_set(head, key, key);
277         data = (char*)xbt_dict_get_or_null(head, key);
278         REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
279
280         count(head, j + 1);
281       }
282       traverse(head);
283       xbt_dict_free(&head);
284       xbt_dict_free(&head);
285     }
286
287     xbt_dict_t head = xbt_dict_new_homogeneous(&free);
288     INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
289     for (int j = 0; j < NB_ELM; j++) {
290       auto* key = static_cast<char*>(xbt_malloc(12));
291
292       snprintf(key, 12, "%d", j);
293       xbt_dict_set(head, key, key);
294     }
295
296     INFO("Count the elements (retrieving the key and data for each)");
297     INFO("There is " << countelems(head) << " elements");
298
299     INFO("Search my " << NB_ELM << " elements 20 times");
300     auto* key = static_cast<char*>(xbt_malloc(12));
301     for (int i = 0; i < 20; i++) {
302       for (int j = 0; j < NB_ELM; j++) {
303         snprintf(key, 12, "%d", j);
304         void* data = xbt_dict_get_or_null(head, key);
305         REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
306         data = xbt_dict_get_or_null_ext(head, key, strlen(key));
307         REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
308       }
309     }
310     xbt_free(key);
311
312     INFO("Remove my " << NB_ELM << " elements");
313     key = (char*)xbt_malloc(12);
314     for (int j = 0; j < NB_ELM; j++) {
315       snprintf(key, 12, "%d", j);
316       xbt_dict_remove_ext(head, key, strlen(key));
317     }
318     xbt_free(key);
319
320     INFO("Free the object (twice)");
321     xbt_dict_free(&head);
322     xbt_dict_free(&head);
323   }
324
325   SECTION("Test dictionary with int keys")
326   {
327     xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
328     int count       = 500;
329
330     INFO("Insert elements");
331     for (int i = 0; i < count; ++i)
332       xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i);
333     REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionary
334
335     INFO("Check elements");
336     for (int i = 0; i < count; ++i) {
337       void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
338       REQUIRE((intptr_t)data == i); // Unexpected value at index i
339     }
340
341     INFO("Free the array");
342     xbt_dict_free(&dict);
343   }
344 }