1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2023. 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"
11 #include "xbt/random.hpp"
15 #include "src/3rd-party/catch.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(const_xbt_dict_t head, const char* key, const char* data)
54 INFO("Search " << STR(key));
55 auto* found = static_cast<char*>(xbt_dict_get_or_null(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(const_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_ext(head, key, strlen(key));
77 static void traverse(const_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(const_xbt_dict_t head, const char* data)
92 INFO("Search " << STR(data) << " (expected not to be found)");
93 REQUIRE(xbt_dict_get_or_null(head, data) == nullptr);
96 static int countelems(const_xbt_dict_t head)
98 xbt_dict_cursor_t cursor;
103 xbt_dict_foreach (head, cursor, key, data) {
109 static void count(const_xbt_dict_t dict, int length)
111 INFO("Count elements (expecting " << length << ")");
112 REQUIRE(xbt_dict_length(dict) == length); // Announced length differs
113 REQUIRE(countelems(dict) == length); // Effective length differs
116 TEST_CASE("xbt::dict: dict data container", "dict")
118 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
120 INFO("Traversal the null dictionary");
123 INFO("Traversal and search the empty dictionary");
124 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
126 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
127 xbt_dict_free(&head);
129 INFO("Traverse the full dictionary");
130 head = new_fixture();
133 debugged_add_ext(head, "toto", "tutu");
134 search_ext(head, "toto", "tutu");
135 debugged_remove(head, "toto");
140 INFO("Free the dictionary (twice)");
141 xbt_dict_free(&head);
142 xbt_dict_free(&head);
145 head = new_fixture();
147 INFO("Change 123 to 'Changed 123'");
148 xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
151 INFO("Change 123 back to '123'");
152 xbt_dict_set(head, "123", xbt_strdup("123"));
155 INFO("Change 12a to 'Dummy 12a'");
156 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
159 INFO("Change 12a to '12a'");
160 xbt_dict_set(head, "12a", xbt_strdup("12a"));
163 INFO("Traverse the resulting dictionary");
168 const char* data = (char*)xbt_dict_get_or_null(head, "123");
169 REQUIRE((data && strcmp("123", data) == 0));
171 search_not_found(head, "Can't be found");
172 search_not_found(head, "123 Can't be found");
173 search_not_found(head, "12345678 NOT");
178 search(head, "123456");
179 search(head, "1234");
180 search(head, "123457");
182 INFO("Traverse the resulting dictionary");
185 INFO("Free the dictionary twice");
186 xbt_dict_free(&head);
187 xbt_dict_free(&head);
189 INFO("Traverse the resulting dictionary");
193 SECTION("Removing some values from homogeneous dicts")
195 xbt_dict_t head = new_fixture();
197 INFO("Remove non existing data");
198 REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
201 xbt_dict_free(&head);
203 INFO("Remove each data manually (traversing the resulting dictionary each time)");
204 head = new_fixture();
205 debugged_remove(head, "12a");
208 debugged_remove(head, "12b");
211 debugged_remove(head, "12");
214 debugged_remove(head, "123456");
217 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
219 debugged_remove(head, "1234");
221 debugged_remove(head, "123457");
223 debugged_remove(head, "123");
225 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
228 INFO("Free the dictionary twice");
229 xbt_dict_free(&head);
230 xbt_dict_free(&head);
233 SECTION("nullptr data management")
235 xbt_dict_t head = new_fixture();
237 INFO("Store nullptr under 'null'");
238 xbt_dict_set(head, "null", nullptr);
239 search_ext(head, "null", nullptr);
241 INFO("Check whether I see it while traversing...");
242 xbt_dict_cursor_t cursor = nullptr;
247 xbt_dict_foreach (head, cursor, key, data) {
248 INFO("Seen: " << STR(key) << "->" << STR(data));
249 if (key && strcmp(key, "null") == 0)
252 REQUIRE(found); // the key 'null', associated to nullptr is not found
254 xbt_dict_free(&head);
257 SECTION("Crash test")
259 for (int i = 0; i < 10; i++) {
260 INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
261 INFO("Fill the struct, count its elems and frees the structure");
262 INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
263 xbt_dict_t head = xbt_dict_new_homogeneous(free);
264 for (int j = 0; j < 1000; j++) {
265 const char* data = nullptr;
266 auto* key = static_cast<char*>(xbt_malloc(SIZEOFKEY));
269 for (int k = 0; k < SIZEOFKEY - 1; k++) {
270 key[k] = simgrid::xbt::random::uniform_int('a', 'z');
272 key[SIZEOFKEY - 1] = '\0';
273 data = (char*)xbt_dict_get_or_null(head, key);
274 } while (data != nullptr);
276 xbt_dict_set(head, key, key);
277 data = (char*)xbt_dict_get_or_null(head, key);
278 REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
283 xbt_dict_free(&head);
284 xbt_dict_free(&head);
287 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
288 INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
289 for (int j = 0; j < NB_ELM; j++) {
290 auto* key = static_cast<char*>(xbt_malloc(12));
292 snprintf(key, 12, "%d", j);
293 xbt_dict_set(head, key, key);
296 INFO("Count the elements (retrieving the key and data for each)");
297 INFO("There is " << countelems(head) << " elements");
299 INFO("Search my " << NB_ELM << " elements 20 times");
300 auto* key = static_cast<char*>(xbt_malloc(12));
301 for (int i = 0; i < 20; i++) {
302 for (int j = 0; j < NB_ELM; j++) {
303 snprintf(key, 12, "%d", j);
304 void* data = xbt_dict_get_or_null(head, key);
305 REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
306 data = xbt_dict_get_or_null_ext(head, key, strlen(key));
307 REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
312 INFO("Remove my " << NB_ELM << " elements");
313 key = (char*)xbt_malloc(12);
314 for (int j = 0; j < NB_ELM; j++) {
315 snprintf(key, 12, "%d", j);
316 xbt_dict_remove_ext(head, key, strlen(key));
320 INFO("Free the object (twice)");
321 xbt_dict_free(&head);
322 xbt_dict_free(&head);
325 SECTION("Test dictionary with int keys")
327 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
330 INFO("Insert elements");
331 for (int i = 0; i < count; ++i)
332 xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i);
333 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionary
335 INFO("Check elements");
336 for (int i = 0; i < count; ++i) {
337 void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
338 REQUIRE((intptr_t)data == i); // Unexpected value at index i
341 INFO("Free the array");
342 xbt_dict_free(&dict);