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 #define REQUIRE_THROWS_XBT_EX(...) \
20 REQUIRE_THROWS_MATCHES((__VA_ARGS__), xbt_ex, Catch::Matchers::Predicate<xbt_ex>( \
21 [](xbt_ex const& e) { return e.category == not_found_error; }, \
22 "category not_found_error"))
24 static constexpr int NB_ELM = 20000;
25 static constexpr int SIZEOFKEY = 1024;
27 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
29 char* data = xbt_strdup(data_to_fill);
31 INFO("Add " << STR(data_to_fill) << " under " << STR(key));
33 xbt_dict_set(head, key, data, nullptr);
36 static void debugged_add(xbt_dict_t head, const char* key)
38 debugged_add_ext(head, key, key);
41 static xbt_dict_t new_fixture()
43 INFO("Fill in the dictionary");
45 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
46 debugged_add(head, "12");
47 debugged_add(head, "12a");
48 debugged_add(head, "12b");
49 debugged_add(head, "123");
50 debugged_add(head, "123456");
51 debugged_add(head, "1234");
52 debugged_add(head, "123457");
57 static void search_ext(xbt_dict_t head, const char* key, const char* data)
59 INFO("Search " << STR(key));
60 char* found = (char*)xbt_dict_get(head, key);
61 INFO("Found " << STR(found));
63 REQUIRE(found); // data do not match expectations: found null while searching for data
65 REQUIRE(not strcmp(data, found)); // data do not match expectations: found another string while searching for data
67 REQUIRE(not found); // data do not match expectations: found something while searching for null
71 static void search(xbt_dict_t head, const char* key)
73 search_ext(head, key, key);
76 static void debugged_remove(xbt_dict_t head, const char* key)
78 INFO("Remove '" << STR(key) << "'");
79 xbt_dict_remove(head, key);
82 static void traverse(xbt_dict_t head)
84 xbt_dict_cursor_t cursor = nullptr;
89 xbt_dict_foreach (head, cursor, key, data) {
90 INFO("Seen #" << ++i << ": " << STR(key) << "->" << STR(data));
91 REQUIRE((key && data && strcmp(key, data) == 0)); // key != value
95 static void search_not_found(xbt_dict_t head, const char* data)
97 INFO("Search " << STR(data) << " (expected not to be found)");
98 REQUIRE_THROWS_XBT_EX(data = (const char*)xbt_dict_get(head, data));
101 static void count(xbt_dict_t dict, int length)
103 INFO("Count elements (expecting " << length << ")");
104 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
106 xbt_dict_cursor_t cursor;
110 xbt_dict_foreach (dict, cursor, key, data)
113 REQUIRE(effective == length); // Effective length differs
116 static void count_check_get_key(xbt_dict_t dict, int length)
118 xbt_dict_cursor_t cursor;
123 INFO("Count elements (expecting " << length << "), and test the getkey function");
124 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
126 xbt_dict_foreach (dict, cursor, key, data) {
128 char* key2 = xbt_dict_get_key(dict, data);
129 xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
132 REQUIRE(effective == length); // Effective length differs
135 static int countelems(xbt_dict_t head)
137 xbt_dict_cursor_t cursor;
142 xbt_dict_foreach (head, cursor, key, data) {
148 TEST_CASE("xbt::dict: dict data container", "dict")
151 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
153 INFO("Traversal the null dictionary");
156 INFO("Traversal and search the empty dictionary");
157 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
159 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
160 xbt_dict_free(&head);
162 INFO("Traverse the full dictionary");
163 head = new_fixture();
164 count_check_get_key(head, 7);
166 debugged_add_ext(head, "toto", "tutu");
167 search_ext(head, "toto", "tutu");
168 debugged_remove(head, "toto");
173 INFO("Free the dictionary (twice)");
174 xbt_dict_free(&head);
175 xbt_dict_free(&head);
178 head = new_fixture();
179 count_check_get_key(head, 7);
180 INFO("Change 123 to 'Changed 123'");
181 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), nullptr);
182 count_check_get_key(head, 7);
184 INFO("Change 123 back to '123'");
185 xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
186 count_check_get_key(head, 7);
188 INFO("Change 12a to 'Dummy 12a'");
189 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), nullptr);
190 count_check_get_key(head, 7);
192 INFO("Change 12a to '12a'");
193 xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
194 count_check_get_key(head, 7);
196 INFO("Traverse the resulting dictionary");
201 char* data = (char*)xbt_dict_get(head, "123");
202 REQUIRE((data && strcmp("123", data) == 0));
204 search_not_found(head, "Can't be found");
205 search_not_found(head, "123 Can't be found");
206 search_not_found(head, "12345678 NOT");
211 search(head, "123456");
212 search(head, "1234");
213 search(head, "123457");
215 INFO("Traverse the resulting dictionary");
218 INFO("Free the dictionary twice");
219 xbt_dict_free(&head);
220 xbt_dict_free(&head);
222 INFO("Traverse the resulting dictionary");
226 SECTION("Removing some values from homogeneous dicts")
228 xbt_dict_t head = new_fixture();
230 INFO("Remove non existing data");
231 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "Does not exist"));
234 xbt_dict_free(&head);
236 INFO("Remove each data manually (traversing the resulting dictionary each time)");
237 head = new_fixture();
238 debugged_remove(head, "12a");
241 debugged_remove(head, "12b");
244 debugged_remove(head, "12");
247 debugged_remove(head, "123456");
250 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
252 debugged_remove(head, "1234");
254 debugged_remove(head, "123457");
256 debugged_remove(head, "123");
258 REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346"));
261 INFO("Free dict, create new fresh one, and then reset the dict");
262 xbt_dict_free(&head);
263 head = new_fixture();
264 xbt_dict_reset(head);
268 INFO("Free the dictionary twice");
269 xbt_dict_free(&head);
270 xbt_dict_free(&head);
273 SECTION("nullptr data management")
275 xbt_dict_t head = new_fixture();
277 INFO("Store nullptr under 'null'");
278 xbt_dict_set(head, "null", nullptr, nullptr);
279 search_ext(head, "null", nullptr);
281 INFO("Check whether I see it while traversing...");
282 xbt_dict_cursor_t cursor = nullptr;
287 xbt_dict_foreach (head, cursor, key, data) {
288 INFO("Seen: " << STR(key) << "->" << STR(data));
289 if (key && strcmp(key, "null") == 0)
292 REQUIRE(found); // the key 'null', associated to nullptr is not found
294 xbt_dict_free(&head);
297 SECTION("Crash test")
299 std::random_device rd;
300 std::default_random_engine rnd_engine(rd());
302 for (int i = 0; i < 10; i++) {
303 INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
304 INFO("Fill the struct, count its elems and frees the structure");
305 INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
306 xbt_dict_t head = xbt_dict_new_homogeneous(free);
307 for (int j = 0; j < 1000; j++) {
308 char* data = nullptr;
309 char* key = (char*)xbt_malloc(SIZEOFKEY);
312 for (int k = 0; k < SIZEOFKEY - 1; k++) {
313 key[k] = rnd_engine() % ('z' - 'a') + 'a';
315 key[SIZEOFKEY - 1] = '\0';
316 data = (char*)xbt_dict_get_or_null(head, key);
317 } while (data != nullptr);
319 xbt_dict_set(head, key, key, nullptr);
320 data = (char*)xbt_dict_get(head, key);
321 REQUIRE(not strcmp(key, data)); // Retrieved value != Injected value
326 xbt_dict_free(&head);
327 xbt_dict_free(&head);
330 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
331 INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
332 for (int j = 0; j < NB_ELM; j++) {
333 char* key = (char*)xbt_malloc(10);
335 snprintf(key, 10, "%d", j);
336 xbt_dict_set(head, key, key, nullptr);
339 INFO("Count the elements (retrieving the key and data for each)");
340 INFO("There is " << countelems(head) << " elements");
342 INFO("Search my " << NB_ELM << " elements 20 times");
343 char* key = (char*)xbt_malloc(10);
344 for (int i = 0; i < 20; i++) {
345 for (int j = 0; j < NB_ELM; j++) {
346 snprintf(key, 10, "%d", j);
347 void* data = xbt_dict_get(head, key);
348 REQUIRE(not strcmp(key, (char*)data)); // with get, key != data
349 data = xbt_dict_get_ext(head, key, strlen(key));
350 REQUIRE(not strcmp(key, (char*)data)); // with get_ext, key != data
355 INFO("Remove my " << NB_ELM << " elements");
356 key = (char*)xbt_malloc(10);
357 for (int j = 0; j < NB_ELM; j++) {
358 snprintf(key, 10, "%d", j);
359 xbt_dict_remove(head, key);
363 INFO("Free the object (twice)");
364 xbt_dict_free(&head);
365 xbt_dict_free(&head);
368 SECTION("Test dictionnary with int keys")
370 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
373 INFO("Insert elements");
374 for (int i = 0; i < count; ++i)
375 xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i, nullptr);
376 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionnary
378 INFO("Check elements");
379 for (int i = 0; i < count; ++i) {
380 xbt_dict_get_ext(dict, (char*)&i, sizeof(i));
381 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Unexpected value at index i
384 INFO("Free the array");
385 xbt_dict_free(&dict);