Logo AND Algorithmique Numérique Distribuée

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