1 // SPDX-License-Identifier: GPL-2.0
4 * Roman Kapl, SYSGO, rka@sysgo.com
15 #define ITERATIONS 10000
17 static int htab_fill(struct unit_test_state *uts,
18 struct hsearch_data *htab, size_t size)
21 struct env_entry item;
22 struct env_entry *ritem;
25 for (i = 0; i < size; i++) {
26 sprintf(key, "%d", (int)i);
31 ut_asserteq(1, hsearch_r(item, ENV_ENTER, &ritem, htab, 0));
37 static int htab_check_fill(struct unit_test_state *uts,
38 struct hsearch_data *htab, size_t size)
41 struct env_entry item;
42 struct env_entry *ritem;
45 for (i = 0; i < size; i++) {
46 sprintf(key, "%d", (int)i);
51 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
53 ut_asserteq_str(key, ritem->key);
54 ut_asserteq_str(key, ritem->data);
60 static int htab_create_delete(struct unit_test_state *uts,
61 struct hsearch_data *htab, size_t iterations)
64 struct env_entry item;
65 struct env_entry *ritem;
68 for (i = 0; i < iterations; i++) {
69 sprintf(key, "cd-%d", (int)i);
74 hsearch_r(item, ENV_ENTER, &ritem, htab, 0);
77 hsearch_r(item, ENV_FIND, &ritem, htab, 0);
79 ut_asserteq_str(key, ritem->key);
80 ut_asserteq_str(key, ritem->data);
82 ut_asserteq(1, hdelete_r(key, htab, 0));
88 /* Completely fill up the hash table */
89 static int env_test_htab_fill(struct unit_test_state *uts)
91 struct hsearch_data htab;
93 memset(&htab, 0, sizeof(htab));
94 ut_asserteq(1, hcreate_r(SIZE, &htab));
96 ut_assertok(htab_fill(uts, &htab, SIZE));
97 ut_assertok(htab_check_fill(uts, &htab, SIZE));
98 ut_asserteq(SIZE, htab.filled);
104 ENV_TEST(env_test_htab_fill, 0);
106 /* Fill the hashtable up halfway an repeateadly delete/create elements
107 * and check for corruption
109 static int env_test_htab_deletes(struct unit_test_state *uts)
111 struct hsearch_data htab;
113 memset(&htab, 0, sizeof(htab));
114 ut_asserteq(1, hcreate_r(SIZE, &htab));
116 ut_assertok(htab_fill(uts, &htab, SIZE / 2));
117 ut_assertok(htab_create_delete(uts, &htab, ITERATIONS));
118 ut_assertok(htab_check_fill(uts, &htab, SIZE / 2));
119 ut_asserteq(SIZE / 2, htab.filled);
125 ENV_TEST(env_test_htab_deletes, 0);