1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
10 #include "simgrid/Exception.hpp"
17 #define STR(str) ((str) ? (str) : "(null)")
19 static constexpr int NB_ELM = 20000;
20 static constexpr int SIZEOFKEY = 1024;
22 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
24 char* data = xbt_strdup(data_to_fill);
26 INFO("Add " << STR(data_to_fill) << " under " << STR(key));
28 xbt_dict_set(head, key, data);
31 static void debugged_add(xbt_dict_t head, const char* key)
33 debugged_add_ext(head, key, key);
36 static xbt_dict_t new_fixture()
38 INFO("Fill in the dictionary");
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");
52 static void search_ext(xbt_dict_t head, const char* key, const char* data)
54 INFO("Search " << STR(key));
55 char* found = (char*)xbt_dict_get(head, key);
56 INFO("Found " << STR(found));
58 REQUIRE(found); // data do not match expectations: found null while searching for data
60 REQUIRE(not strcmp(data, found)); // data do not match expectations: found another string while searching for data
62 REQUIRE(not found); // data do not match expectations: found something while searching for null
66 static void search(xbt_dict_t head, const char* key)
68 search_ext(head, key, key);
71 static void debugged_remove(xbt_dict_t head, const char* key)
73 INFO("Remove '" << STR(key) << "'");
74 xbt_dict_remove(head, key);
77 static void traverse(xbt_dict_t head)
79 xbt_dict_cursor_t cursor = nullptr;
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
90 static void search_not_found(xbt_dict_t head, const char* data)
92 INFO("Search " << STR(data) << " (expected not to be found)");
93 REQUIRE_THROWS_AS(xbt_dict_get(head, data), std::out_of_range);
96 static void count(xbt_dict_t dict, int length)
98 INFO("Count elements (expecting " << length << ")");
99 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
101 xbt_dict_cursor_t cursor;
105 xbt_dict_foreach (dict, cursor, key, data)
108 REQUIRE(effective == length); // Effective length differs
111 static void count_check_get_key(xbt_dict_t dict, int length)
113 xbt_dict_cursor_t cursor;
118 INFO("Count elements (expecting " << length << "), and test the getkey function");
119 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
121 xbt_dict_foreach (dict, cursor, key, data) {
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);
127 REQUIRE(effective == length); // Effective length differs
130 static int countelems(xbt_dict_t head)
132 xbt_dict_cursor_t cursor;
137 xbt_dict_foreach (head, cursor, key, data) {
143 TEST_CASE("xbt::dict: dict data container", "dict")
145 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
147 INFO("Traversal the null dictionary");
150 INFO("Traversal and search the empty dictionary");
151 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
153 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
154 xbt_dict_free(&head);
156 INFO("Traverse the full dictionary");
157 head = new_fixture();
158 count_check_get_key(head, 7);
160 debugged_add_ext(head, "toto", "tutu");
161 search_ext(head, "toto", "tutu");
162 debugged_remove(head, "toto");
167 INFO("Free the dictionary (twice)");
168 xbt_dict_free(&head);
169 xbt_dict_free(&head);
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);
178 INFO("Change 123 back to '123'");
179 xbt_dict_set(head, "123", xbt_strdup("123"));
180 count_check_get_key(head, 7);
182 INFO("Change 12a to 'Dummy 12a'");
183 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
184 count_check_get_key(head, 7);
186 INFO("Change 12a to '12a'");
187 xbt_dict_set(head, "12a", xbt_strdup("12a"));
188 count_check_get_key(head, 7);
190 INFO("Traverse the resulting dictionary");
195 char* data = (char*)xbt_dict_get(head, "123");
196 REQUIRE((data && strcmp("123", data) == 0));
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");
205 search(head, "123456");
206 search(head, "1234");
207 search(head, "123457");
209 INFO("Traverse the resulting dictionary");
212 INFO("Free the dictionary twice");
213 xbt_dict_free(&head);
214 xbt_dict_free(&head);
216 INFO("Traverse the resulting dictionary");
220 SECTION("Removing some values from homogeneous dicts")
222 xbt_dict_t head = new_fixture();
224 INFO("Remove non existing data");
225 REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
228 xbt_dict_free(&head);
230 INFO("Remove each data manually (traversing the resulting dictionary each time)");
231 head = new_fixture();
232 debugged_remove(head, "12a");
235 debugged_remove(head, "12b");
238 debugged_remove(head, "12");
241 debugged_remove(head, "123456");
244 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
246 debugged_remove(head, "1234");
248 debugged_remove(head, "123457");
250 debugged_remove(head, "123");
252 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
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);
262 INFO("Free the dictionary twice");
263 xbt_dict_free(&head);
264 xbt_dict_free(&head);
267 SECTION("nullptr data management")
269 xbt_dict_t head = new_fixture();
271 INFO("Store nullptr under 'null'");
272 xbt_dict_set(head, "null", nullptr);
273 search_ext(head, "null", nullptr);
275 INFO("Check whether I see it while traversing...");
276 xbt_dict_cursor_t cursor = nullptr;
281 xbt_dict_foreach (head, cursor, key, data) {
282 INFO("Seen: " << STR(key) << "->" << STR(data));
283 if (key && strcmp(key, "null") == 0)
286 REQUIRE(found); // the key 'null', associated to nullptr is not found
288 xbt_dict_free(&head);
291 SECTION("Crash test")
293 std::random_device rd;
294 std::default_random_engine rnd_engine(rd());
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 char* data = nullptr;
303 char* key = (char*)xbt_malloc(SIZEOFKEY);
306 for (int k = 0; k < SIZEOFKEY - 1; k++) {
307 key[k] = rnd_engine() % ('z' - 'a') + 'a';
309 key[SIZEOFKEY - 1] = '\0';
310 data = (char*)xbt_dict_get_or_null(head, key);
311 } while (data != nullptr);
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
320 xbt_dict_free(&head);
321 xbt_dict_free(&head);
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);
329 snprintf(key, 10, "%d", j);
330 xbt_dict_set(head, key, key);
333 INFO("Count the elements (retrieving the key and data for each)");
334 INFO("There is " << countelems(head) << " elements");
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
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);
357 INFO("Free the object (twice)");
358 xbt_dict_free(&head);
359 xbt_dict_free(&head);
362 SECTION("Test dictionary with int keys")
364 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
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
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
378 INFO("Free the array");
379 xbt_dict_free(&dict);