Logo AND Algorithmique Numérique Distribuée

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