1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2020. 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"
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 void count(const_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 int countelems(const_xbt_dict_t head)
113 xbt_dict_cursor_t cursor;
118 xbt_dict_foreach (head, cursor, key, data) {
124 TEST_CASE("xbt::dict: dict data container", "dict")
126 SECTION("Basic usage: change, retrieve and traverse homogeneous dicts")
128 INFO("Traversal the null dictionary");
131 INFO("Traversal and search the empty dictionary");
132 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
134 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
135 xbt_dict_free(&head);
137 INFO("Traverse the full dictionary");
138 head = new_fixture();
141 debugged_add_ext(head, "toto", "tutu");
142 search_ext(head, "toto", "tutu");
143 debugged_remove(head, "toto");
148 INFO("Free the dictionary (twice)");
149 xbt_dict_free(&head);
150 xbt_dict_free(&head);
153 head = new_fixture();
155 INFO("Change 123 to 'Changed 123'");
156 xbt_dict_set(head, "123", xbt_strdup("Changed 123"));
159 INFO("Change 123 back to '123'");
160 xbt_dict_set(head, "123", xbt_strdup("123"));
163 INFO("Change 12a to 'Dummy 12a'");
164 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"));
167 INFO("Change 12a to '12a'");
168 xbt_dict_set(head, "12a", xbt_strdup("12a"));
171 INFO("Traverse the resulting dictionary");
176 const char* data = (char*)xbt_dict_get_or_null(head, "123");
177 REQUIRE((data && strcmp("123", data) == 0));
179 search_not_found(head, "Can't be found");
180 search_not_found(head, "123 Can't be found");
181 search_not_found(head, "12345678 NOT");
186 search(head, "123456");
187 search(head, "1234");
188 search(head, "123457");
190 INFO("Traverse the resulting dictionary");
193 INFO("Free the dictionary twice");
194 xbt_dict_free(&head);
195 xbt_dict_free(&head);
197 INFO("Traverse the resulting dictionary");
201 SECTION("Removing some values from homogeneous dicts")
203 xbt_dict_t head = new_fixture();
205 INFO("Remove non existing data");
206 REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range);
209 xbt_dict_free(&head);
211 INFO("Remove each data manually (traversing the resulting dictionary each time)");
212 head = new_fixture();
213 debugged_remove(head, "12a");
216 debugged_remove(head, "12b");
219 debugged_remove(head, "12");
222 debugged_remove(head, "123456");
225 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
227 debugged_remove(head, "1234");
229 debugged_remove(head, "123457");
231 debugged_remove(head, "123");
233 REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range);
236 INFO("Free the dictionary twice");
237 xbt_dict_free(&head);
238 xbt_dict_free(&head);
241 SECTION("nullptr data management")
243 xbt_dict_t head = new_fixture();
245 INFO("Store nullptr under 'null'");
246 xbt_dict_set(head, "null", nullptr);
247 search_ext(head, "null", nullptr);
249 INFO("Check whether I see it while traversing...");
250 xbt_dict_cursor_t cursor = nullptr;
255 xbt_dict_foreach (head, cursor, key, data) {
256 INFO("Seen: " << STR(key) << "->" << STR(data));
257 if (key && strcmp(key, "null") == 0)
260 REQUIRE(found); // the key 'null', associated to nullptr is not found
262 xbt_dict_free(&head);
265 SECTION("Crash test")
267 for (int i = 0; i < 10; i++) {
268 INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
269 INFO("Fill the struct, count its elems and frees the structure");
270 INFO("using 1000 elements with " << SIZEOFKEY << " chars long randomized keys.");
271 xbt_dict_t head = xbt_dict_new_homogeneous(free);
272 for (int j = 0; j < 1000; j++) {
273 const char* data = nullptr;
274 auto* key = static_cast<char*>(xbt_malloc(SIZEOFKEY));
277 for (int k = 0; k < SIZEOFKEY - 1; k++) {
278 key[k] = simgrid::xbt::random::uniform_int('a', 'z');
280 key[SIZEOFKEY - 1] = '\0';
281 data = (char*)xbt_dict_get_or_null(head, key);
282 } while (data != nullptr);
284 xbt_dict_set(head, key, key);
285 data = (char*)xbt_dict_get_or_null(head, key);
286 REQUIRE((data && not strcmp(key, data))); // Retrieved value != Injected value
291 xbt_dict_free(&head);
292 xbt_dict_free(&head);
295 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
296 INFO("Fill " << NB_ELM << " elements, with keys being the number of element");
297 for (int j = 0; j < NB_ELM; j++) {
298 auto* key = static_cast<char*>(xbt_malloc(10));
300 snprintf(key, 10, "%d", j);
301 xbt_dict_set(head, key, key);
304 INFO("Count the elements (retrieving the key and data for each)");
305 INFO("There is " << countelems(head) << " elements");
307 INFO("Search my " << NB_ELM << " elements 20 times");
308 auto* key = static_cast<char*>(xbt_malloc(10));
309 for (int i = 0; i < 20; i++) {
310 for (int j = 0; j < NB_ELM; j++) {
311 snprintf(key, 10, "%d", j);
312 void* data = xbt_dict_get_or_null(head, key);
313 REQUIRE((data && not strcmp(key, (char*)data))); // with get, key != data
314 data = xbt_dict_get_or_null_ext(head, key, strlen(key));
315 REQUIRE((data && not strcmp(key, (char*)data))); // with get_ext, key != data
320 INFO("Remove my " << NB_ELM << " elements");
321 key = (char*)xbt_malloc(10);
322 for (int j = 0; j < NB_ELM; j++) {
323 snprintf(key, 10, "%d", j);
324 xbt_dict_remove_ext(head, key, strlen(key));
328 INFO("Free the object (twice)");
329 xbt_dict_free(&head);
330 xbt_dict_free(&head);
333 SECTION("Test dictionary with int keys")
335 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
338 INFO("Insert elements");
339 for (int i = 0; i < count; ++i)
340 xbt_dict_set_ext(dict, (char*)&i, sizeof(i), (void*)(intptr_t)i);
341 REQUIRE(xbt_dict_size(dict) == (unsigned)count); // Bad number of elements in the dictionary
343 INFO("Check elements");
344 for (int i = 0; i < count; ++i) {
345 void* data = xbt_dict_get_or_null_ext(dict, (char*)&i, sizeof(i));
346 REQUIRE((intptr_t)data == i); // Unexpected value at index i
349 INFO("Free the array");
350 xbt_dict_free(&dict);