Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / xbt / dict_test.cpp
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2022. 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 "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 void count(const_xbt_dict_t dict, int length)
97 {
98   INFO("Count elements (expecting " << length << ")");
99   REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
100
101   xbt_dict_cursor_t cursor;
102   char* key;
103   void* data;
104   int effective = 0;
105   xbt_dict_foreach (dict, cursor, key, data)
106     effective++;
107
108   REQUIRE(effective == length); // Effective length differs
109 }
110
111 static int countelems(const_xbt_dict_t head)
112 {
113   xbt_dict_cursor_t cursor;
114   char* key;
115   void* data;
116   int res = 0;
117
118   xbt_dict_foreach (head, cursor, key, data) {
119     res++;
120   }
121   return res;
122 }
123
124 TEST_CASE("xbt::dict: dict data container", "dict")
125 {
126   SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
127   {
128     INFO("Traversal the null dictionary");
129     traverse(nullptr);
130
131     INFO("Traversal and search the empty dictionary");
132     xbt_dict_t head = xbt_dict_new_homogeneous(&free);
133     traverse(head);
134     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
135     xbt_dict_free(&head);
136
137     INFO("Traverse the full dictionary");
138     head = new_fixture();
139     count(head, 7);
140
141     debugged_add_ext(head, "toto", "tutu");
142     search_ext(head, "toto", "tutu");
143     debugged_remove(head, "toto");
144
145     search(head, "12a");
146     traverse(head);
147
148     INFO("Free the dictionary (twice)");
149     xbt_dict_free(&head);
150     xbt_dict_free(&head);
151
152     /* CHANGING */
153     head = new_fixture();
154     count(head, 7);
155     INFO("Change 123 to 'Changed 123'");
156     xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
157     count(head, 7);
158
159     INFO("Change 123 back to '123'");
160     xbt_dict_set(head, "123", xbt_strdup("123"));
161     count(head, 7);
162
163     INFO("Change 12a to 'Dummy 12a'");
164     xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
165     count(head, 7);
166
167     INFO("Change 12a to '12a'");
168     xbt_dict_set(head, "12a", xbt_strdup("12a"));
169     count(head, 7);
170
171     INFO("Traverse the resulting dictionary");
172     traverse(head);
173
174     /* RETRIEVE */
175     INFO("Search 123");
176     const char* data = (char*)xbt_dict_get_or_null(head, "123");
177     REQUIRE((data && strcmp("123", data) == 0));
178
179     search_not_found(head, "Can't be found");
180     search_not_found(head, "123 Can't be found");
181     search_not_found(head, "12345678 NOT");
182
183     search(head, "12a");
184     search(head, "12b");
185     search(head, "12");
186     search(head, "123456");
187     search(head, "1234");
188     search(head, "123457");
189
190     INFO("Traverse the resulting dictionary");
191     traverse(head);
192
193     INFO("Free the dictionary twice");
194     xbt_dict_free(&head);
195     xbt_dict_free(&head);
196
197     INFO("Traverse the resulting dictionary");
198     traverse(head);
199   }
200
201   SECTION("Removing some values from homogeneous dicts")
202   {
203     xbt_dict_t head = new_fixture();
204     count(head, 7);
205     INFO("Remove non existing data");
206     REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
207     traverse(head);
208
209     xbt_dict_free(&head);
210
211     INFO("Remove each data manually (traversing the resulting dictionary each time)");
212     head = new_fixture();
213     debugged_remove(head, "12a");
214     traverse(head);
215     count(head, 6);
216     debugged_remove(head, "12b");
217     traverse(head);
218     count(head, 5);
219     debugged_remove(head, "12");
220     traverse(head);
221     count(head, 4);
222     debugged_remove(head, "123456");
223     traverse(head);
224     count(head, 3);
225     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
226     traverse(head);
227     debugged_remove(head, "1234");
228     traverse(head);
229     debugged_remove(head, "123457");
230     traverse(head);
231     debugged_remove(head, "123");
232     traverse(head);
233     REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
234     traverse(head);
235
236     INFO("Free the dictionary twice");
237     xbt_dict_free(&head);
238     xbt_dict_free(&head);
239   }
240
241   SECTION("nullptr data management")
242   {
243     xbt_dict_t head = new_fixture();
244
245     INFO("Store nullptr under 'null'");
246     xbt_dict_set(head, "null", nullptr);
247     search_ext(head, "null", nullptr);
248
249     INFO("Check whether I see it while traversing...");
250     xbt_dict_cursor_t cursor = nullptr;
251     char* key;
252     bool found = false;
253     char* data;
254
255     xbt_dict_foreach (head, cursor, key, data) {
256       INFO("Seen: " << STR(key) << "->" << STR(data));
257       if (key && strcmp(key, "null") == 0)
258         found = true;
259     }
260     REQUIRE(found); // the key 'null', associated to nullptr is not found
261
262     xbt_dict_free(&head);
263   }
264
265   SECTION("Crash test")
266   {
267     for (int i = 0; i < 10; i++) {
268       INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
269       INFO("Fill the struct, count its elems and frees the structure");
270       INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
271       xbt_dict_t head = xbt_dict_new_homogeneous(free);
272       for (int j = 0; j < 1000; j++) {
273         const char* data = nullptr;
274         auto* key        = static_cast<char*>(xbt_malloc(SIZEOFKEY));
275
276         do {
277           for (int k = 0; k < SIZEOFKEY - 1; k++) {
278             key[k] = simgrid::xbt::random::uniform_int('a', 'z');
279           }
280           key[SIZEOFKEY - 1] = '\0';
281           data               = (char*)xbt_dict_get_or_null(head, key);
282         } while (data != nullptr);
283
284         xbt_dict_set(head, key, key);
285         data = (char*)xbt_dict_get_or_null(head, key);
286         REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
287
288         count(head, j + 1);
289       }
290       traverse(head);
291       xbt_dict_free(&head);
292       xbt_dict_free(&head);
293     }
294
295     xbt_dict_t head = xbt_dict_new_homogeneous(&free);
296     INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
297     for (int j = 0; j < NB_ELM; j++) {
298       auto* key = static_cast<char*>(xbt_malloc(12));
299
300       snprintf(key, 12, "%d", j);
301       xbt_dict_set(head, key, key);
302     }
303
304     INFO("Count the elements (retrieving the key and data for each)");
305     INFO("There is " << countelems(head) << " elements");
306
307     INFO("Search my " << NB_ELM << " elements 20 times");
308     auto* key = static_cast<char*>(xbt_malloc(12));
309     for (int i = 0; i < 20; i++) {
310       for (int j = 0; j < NB_ELM; j++) {
311         snprintf(key, 12, "%d", j);
312         void* data = xbt_dict_get_or_null(head, key);
313         REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
314         data = xbt_dict_get_or_null_ext(head, key, strlen(key));
315         REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
316       }
317     }
318     xbt_free(key);
319
320     INFO("Remove my " << NB_ELM << " elements");
321     key = (char*)xbt_malloc(12);
322     for (int j = 0; j < NB_ELM; j++) {
323       snprintf(key, 12, "%d", j);
324       xbt_dict_remove_ext(head, key, strlen(key));
325     }
326     xbt_free(key);
327
328     INFO("Free the object (twice)");
329     xbt_dict_free(&head);
330     xbt_dict_free(&head);
331   }
332
333   SECTION("Test dictionary with int keys")
334   {
335     xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
336     int count       = 500;
337
338     INFO("Insert elements");
339     for (int i = 0; i < count; ++i)
340       xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i);
341     REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionary
342
343     INFO("Check elements");
344     for (int i = 0; i < count; ++i) {
345       void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
346       REQUIRE((intptr_t)data == i); // Unexpected value at index i
347     }
348
349     INFO("Free the array");
350     xbt_dict_free(&dict);
351   }
352 }