Logo AND Algorithmique Numérique Distribuée

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