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"
16 #define STR(str) ((str) ? (str) : "(null)")
18 #define REQUIRE_THROWS_XBT_EX(...) \
19 REQUIRE_THROWS_MATCHES((__VA_ARGS__), xbt_ex, Catch::Matchers::Predicate<xbt_ex>( \
20 [](xbt_ex const& e) { return e.category == not_found_error; }, \
21 "category not_found_error"))
23 static constexpr int NB_ELM = 20000;
24 static constexpr int SIZEOFKEY = 1024;
26 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
28 char* data = xbt_strdup(data_to_fill);
30 INFO("Add " << STR(data_to_fill) << " under " << STR(key));
32 xbt_dict_set(head, key, data, nullptr);
35 static void debugged_add(xbt_dict_t head, const char* key)
37 debugged_add_ext(head, key, key);
40 static xbt_dict_t new_fixture()
42 INFO("Fill in the dictionary");
44 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
45 debugged_add(head, "12");
46 debugged_add(head, "12a");
47 debugged_add(head, "12b");
48 debugged_add(head, "123");
49 debugged_add(head, "123456");
50 debugged_add(head, "1234");
51 debugged_add(head, "123457");
56 static void search_ext(xbt_dict_t head, const char* key, const char* data)
58 INFO("Search " << STR(key));
59 char* found = (char*)xbt_dict_get(head, key);
60 INFO("Found " << STR(found));
62 REQUIRE(found); // data do not match expectations: found null while searching for data
64 REQUIRE(not strcmp(data, found)); // data do not match expectations: found another string while searching for data
66 REQUIRE(not found); // data do not match expectations: found something while searching for null
70 static void search(xbt_dict_t head, const char* key)
72 search_ext(head, key, key);
75 static void debugged_remove(xbt_dict_t head, const char* key)
77 INFO("Remove '" << STR(key) << "'");
78 xbt_dict_remove(head, key);
81 static void traverse(xbt_dict_t head)
83 xbt_dict_cursor_t cursor = nullptr;
88 xbt_dict_foreach (head, cursor, key, data) {
89 INFO("Seen #" << ++i << ": " << STR(key) << "->" << STR(data));
90 REQUIRE((key && data && strcmp(key, data) == 0)); // key != value
94 static void search_not_found(xbt_dict_t head, const char* data)
96 INFO("Search " << STR(data) << " (expected not to be found)");
97 REQUIRE_THROWS_XBT_EX(data = (const char*)xbt_dict_get(head, data));
100 static void count(xbt_dict_t dict, int length)
102 INFO("Count elements (expecting " << length << ")");
103 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
105 xbt_dict_cursor_t cursor;
109 xbt_dict_foreach (dict, cursor, key, data)
112 REQUIRE(effective == length); // Effective length differs
115 static void count_check_get_key(xbt_dict_t dict, int length)
117 xbt_dict_cursor_t cursor;
122 INFO("Count elements (expecting " << length << "), and test the getkey function");
123 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
125 xbt_dict_foreach (dict, cursor, key, data) {
127 char* key2 = xbt_dict_get_key(dict, data);
128 xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
131 REQUIRE(effective == length); // Effective length differs
134 static int countelems(xbt_dict_t head)
136 xbt_dict_cursor_t cursor;
141 xbt_dict_foreach (head, cursor, key, data) {
147 TEST_CASE("xbt::dict: dict data container", "dict")
150 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
152 INFO("Traversal the null dictionary");
155 INFO("Traversal and search the empty dictionary");
156 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
158 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
159 xbt_dict_free(&head);
161 INFO("Traverse the full dictionary");
162 head = new_fixture();
163 count_check_get_key(head, 7);
165 debugged_add_ext(head, "toto", "tutu");
166 search_ext(head, "toto", "tutu");
167 debugged_remove(head, "toto");
172 INFO("Free the dictionary (twice)");
173 xbt_dict_free(&head);
174 xbt_dict_free(&head);
177 head = new_fixture();
178 count_check_get_key(head, 7);
179 INFO("Change 123 to 'Changed 123'");
180 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), nullptr);
181 count_check_get_key(head, 7);
183 INFO("Change 123 back to '123'");
184 xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
185 count_check_get_key(head, 7);
187 INFO("Change 12a to 'Dummy 12a'");
188 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), nullptr);
189 count_check_get_key(head, 7);
191 INFO("Change 12a to '12a'");
192 xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
193 count_check_get_key(head, 7);
195 INFO("Traverse the resulting dictionary");
200 char* data = (char*)xbt_dict_get(head, "123");
201 REQUIRE((data && strcmp("123", data) == 0));
203 search_not_found(head, "Can't be found");
204 search_not_found(head, "123 Can't be found");
205 search_not_found(head, "12345678 NOT");
210 search(head, "123456");
211 search(head, "1234");
212 search(head, "123457");
214 INFO("Traverse the resulting dictionary");
217 INFO("Free the dictionary twice");
218 xbt_dict_free(&head);
219 xbt_dict_free(&head);
221 INFO("Traverse the resulting dictionary");
225 SECTION("Removing some values from homogeneous dicts")
227 xbt_dict_t head = new_fixture();
229 INFO("Remove non existing data");
230 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "Does not exist"));
233 xbt_dict_free(&head);
235 INFO("Remove each data manually (traversing the resulting dictionary each time)");
236 head = new_fixture();
237 debugged_remove(head, "12a");
240 debugged_remove(head, "12b");
243 debugged_remove(head, "12");
246 debugged_remove(head, "123456");
249 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
251 debugged_remove(head, "1234");
253 debugged_remove(head, "123457");
255 debugged_remove(head, "123");
257 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
260 INFO("Free dict, create new fresh one, and then reset the dict");
261 xbt_dict_free(&head);
262 head = new_fixture();
263 xbt_dict_reset(head);
267 INFO("Free the dictionary twice");
268 xbt_dict_free(&head);
269 xbt_dict_free(&head);
272 SECTION("nullptr data management")
274 xbt_dict_t head = new_fixture();
276 INFO("Store nullptr under 'null'");
277 xbt_dict_set(head, "null", nullptr, nullptr);
278 search_ext(head, "null", nullptr);
280 INFO("Check whether I see it while traversing...");
281 xbt_dict_cursor_t cursor = nullptr;
286 xbt_dict_foreach (head, cursor, key, data) {
287 INFO("Seen: " << STR(key) << "->" << STR(data));
288 if (key && strcmp(key, "null") == 0)
291 REQUIRE(found); // the key 'null', associated to nullptr is not found
293 xbt_dict_free(&head);
296 SECTION("Crash test")
298 srand((unsigned int)time(nullptr));
300 for (int i = 0; i < 10; i++) {
301 INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
302 INFO("Fill the struct, count its elems and frees the structure");
303 INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
304 xbt_dict_t head = xbt_dict_new_homogeneous(free);
305 for (int j = 0; j < 1000; j++) {
306 char* data = nullptr;
307 char* key = (char*)xbt_malloc(SIZEOFKEY);
310 for (int k = 0; k < SIZEOFKEY - 1; k++) {
311 key[k] = rand() % ('z' - 'a') + 'a';
313 key[SIZEOFKEY - 1] = '\0';
314 data = (char*)xbt_dict_get_or_null(head, key);
315 } while (data != nullptr);
317 xbt_dict_set(head, key, key, nullptr);
318 data = (char*)xbt_dict_get(head, key);
319 REQUIRE(not strcmp(key, data)); // Retrieved value != Injected value
324 xbt_dict_free(&head);
325 xbt_dict_free(&head);
328 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
329 INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
330 for (int j = 0; j < NB_ELM; j++) {
331 char* key = (char*)xbt_malloc(10);
333 snprintf(key, 10, "%d", j);
334 xbt_dict_set(head, key, key, nullptr);
337 INFO("Count the elements (retrieving the key and data for each)");
338 INFO("There is " << countelems(head) << " elements");
340 INFO("Search my " << NB_ELM << " elements 20 times");
341 char* key = (char*)xbt_malloc(10);
342 for (int i = 0; i < 20; i++) {
343 for (int j = 0; j < NB_ELM; j++) {
344 snprintf(key, 10, "%d", j);
345 void* data = xbt_dict_get(head, key);
346 REQUIRE(not strcmp(key, (char*)data)); // with get, key != data
347 data = xbt_dict_get_ext(head, key, strlen(key));
348 REQUIRE(not strcmp(key, (char*)data)); // with get_ext, key != data
353 INFO("Remove my " << NB_ELM << " elements");
354 key = (char*)xbt_malloc(10);
355 for (int j = 0; j < NB_ELM; j++) {
356 snprintf(key, 10, "%d", j);
357 xbt_dict_remove(head, key);
361 INFO("Free the object (twice)");
362 xbt_dict_free(&head);
363 xbt_dict_free(&head);
366 SECTION("Test dictionnary with int keys")
368 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
371 INFO("Insert elements");
372 for (int i = 0; i < count; ++i)
373 xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i, nullptr);
374 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionnary
376 INFO("Check elements");
377 for (int i = 0; i < count; ++i) {
378 xbt_dict_get_ext(dict, (char*)&i, sizeof(i));
379 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Unexpected value at index i
382 INFO("Free the array");
383 xbt_dict_free(&dict);