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, nullptr);
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")
146 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
148 INFO("Traversal the null dictionary");
151 INFO("Traversal and search the empty dictionary");
152 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
154 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
155 xbt_dict_free(&head);
157 INFO("Traverse the full dictionary");
158 head = new_fixture();
159 count_check_get_key(head, 7);
161 debugged_add_ext(head, "toto", "tutu");
162 search_ext(head, "toto", "tutu");
163 debugged_remove(head, "toto");
168 INFO("Free the dictionary (twice)");
169 xbt_dict_free(&head);
170 xbt_dict_free(&head);
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);
179 INFO("Change 123 back to '123'");
180 xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
181 count_check_get_key(head, 7);
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);
187 INFO("Change 12a to '12a'");
188 xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
189 count_check_get_key(head, 7);
191 INFO("Traverse the resulting dictionary");
196 char* data = (char*)xbt_dict_get(head, "123");
197 REQUIRE((data && strcmp("123", data) == 0));
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");
206 search(head, "123456");
207 search(head, "1234");
208 search(head, "123457");
210 INFO("Traverse the resulting dictionary");
213 INFO("Free the dictionary twice");
214 xbt_dict_free(&head);
215 xbt_dict_free(&head);
217 INFO("Traverse the resulting dictionary");
221 SECTION("Removing some values from homogeneous dicts")
223 xbt_dict_t head = new_fixture();
225 INFO("Remove non existing data");
226 REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
229 xbt_dict_free(&head);
231 INFO("Remove each data manually (traversing the resulting dictionary each time)");
232 head = new_fixture();
233 debugged_remove(head, "12a");
236 debugged_remove(head, "12b");
239 debugged_remove(head, "12");
242 debugged_remove(head, "123456");
245 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
247 debugged_remove(head, "1234");
249 debugged_remove(head, "123457");
251 debugged_remove(head, "123");
253 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
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);
263 INFO("Free the dictionary twice");
264 xbt_dict_free(&head);
265 xbt_dict_free(&head);
268 SECTION("nullptr data management")
270 xbt_dict_t head = new_fixture();
272 INFO("Store nullptr under 'null'");
273 xbt_dict_set(head, "null", nullptr, nullptr);
274 search_ext(head, "null", nullptr);
276 INFO("Check whether I see it while traversing...");
277 xbt_dict_cursor_t cursor = nullptr;
282 xbt_dict_foreach (head, cursor, key, data) {
283 INFO("Seen: " << STR(key) << "->" << STR(data));
284 if (key && strcmp(key, "null") == 0)
287 REQUIRE(found); // the key 'null', associated to nullptr is not found
289 xbt_dict_free(&head);
292 SECTION("Crash test")
294 std::random_device rd;
295 std::default_random_engine rnd_engine(rd());
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);
307 for (int k = 0; k < SIZEOFKEY - 1; k++) {
308 key[k] = rnd_engine() % ('z' - 'a') + 'a';
310 key[SIZEOFKEY - 1] = '\0';
311 data = (char*)xbt_dict_get_or_null(head, key);
312 } while (data != nullptr);
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
321 xbt_dict_free(&head);
322 xbt_dict_free(&head);
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);
330 snprintf(key, 10, "%d", j);
331 xbt_dict_set(head, key, key, nullptr);
334 INFO("Count the elements (retrieving the key and data for each)");
335 INFO("There is " << countelems(head) << " elements");
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
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);
358 INFO("Free the object (twice)");
359 xbt_dict_free(&head);
360 xbt_dict_free(&head);
363 SECTION("Test dictionnary with int keys")
365 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
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 dictionnary
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
379 INFO("Free the array");
380 xbt_dict_free(&dict);