Logo AND Algorithmique Numérique Distribuée

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