Logo AND Algorithmique Numérique Distribuée

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