Michal Vasko | f20ad11 | 2023-02-10 15:12:12 +0100 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_hash_table.c |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from hash_table.c |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include "common.h" |
| 20 | #include "hash_table.h" |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 21 | |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 22 | static void |
| 23 | test_invalid_arguments(void **state) |
| 24 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 25 | assert_int_equal(LY_EINVAL, lydict_insert(NULL, NULL, 0, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 26 | CHECK_LOG("Invalid argument ctx (lydict_insert()).", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 28 | assert_int_equal(LY_EINVAL, lydict_insert_zc(NULL, NULL, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 29 | CHECK_LOG("Invalid argument ctx (lydict_insert_zc()).", NULL); |
| 30 | assert_int_equal(LY_EINVAL, lydict_insert_zc(UTEST_LYCTX, NULL, NULL)); |
Michal Vasko | be27c91 | 2021-02-24 17:07:37 +0100 | [diff] [blame] | 31 | CHECK_LOG_CTX("Invalid argument str_p (lydict_insert_zc()).", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void |
| 35 | test_dict_hit(void **state) |
| 36 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 37 | const char *str1, *str2, *str3; |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 38 | |
| 39 | /* insert 2 strings, one of them repeatedly */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 40 | assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "test1", 0, &str1)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 41 | assert_non_null(str1); |
| 42 | /* via zerocopy we have to get the same pointer as provided */ |
| 43 | assert_non_null(str2 = strdup("test2")); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 44 | assert_int_equal(LY_SUCCESS, lydict_insert_zc(UTEST_LYCTX, (char *)str2, &str3)); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 45 | assert_ptr_equal(str2, str3); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 46 | /* here we get the same pointer as in case the string was inserted first time */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 47 | assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "test1", 0, &str2)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 48 | assert_non_null(str2); |
| 49 | assert_ptr_equal(str1, str2); |
| 50 | |
| 51 | /* remove strings, but the repeatedly inserted only once */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 52 | lydict_remove(UTEST_LYCTX, "test1"); |
| 53 | lydict_remove(UTEST_LYCTX, "test2"); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 54 | |
| 55 | /* destroy dictionary - should raise warning about data presence */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 56 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 57 | UTEST_LYCTX = NULL; |
| 58 | CHECK_LOG("String \"test1\" not freed from the dictionary, refcount 1", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 59 | |
| 60 | #ifndef NDEBUG |
| 61 | /* cleanup */ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 62 | free((char *)str1); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 63 | #endif |
| 64 | } |
| 65 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 66 | static uint8_t |
| 67 | ht_equal_clb(void *val1, void *val2, uint8_t mod, void *cb_data) |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 68 | { |
| 69 | int *v1, *v2; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 70 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 71 | (void)mod; |
| 72 | (void)cb_data; |
| 73 | |
| 74 | v1 = (int *)val1; |
| 75 | v2 = (int *)val2; |
| 76 | |
| 77 | return *v1 == *v2; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | test_ht_basic(void **state) |
| 82 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 83 | uint32_t i; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 84 | struct ly_ht *ht; |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 85 | |
| 86 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 0)); |
| 87 | |
| 88 | i = 2; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 89 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 90 | assert_int_equal(LY_SUCCESS, lyht_insert(ht, &i, i, NULL)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 91 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 92 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 93 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 94 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 95 | CHECK_LOG("Invalid argument hash (lyht_remove_with_resize_cb()).", NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 96 | |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 97 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void |
| 101 | test_ht_resize(void **state) |
| 102 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 103 | uint32_t i; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 104 | struct ly_ht *ht; |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 105 | |
| 106 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 107 | assert_int_equal(8, ht->size); |
| 108 | |
| 109 | /* insert records into indexes 2-7 */ |
| 110 | for (i = 2; i < 8; ++i) { |
| 111 | assert_int_equal(LY_SUCCESS, lyht_insert(ht, &i, i, NULL)); |
| 112 | } |
| 113 | /* check that table resized */ |
| 114 | assert_int_equal(16, ht->size); |
| 115 | |
| 116 | /* check expected content of the table */ |
| 117 | for (i = 0; i < 16; ++i) { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 118 | if ((i >= 2) && (i < 8)) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 119 | /* inserted data on indexes 2-7 */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 120 | assert_int_not_equal(UINT32_MAX, ht->hlists[i]); |
| 121 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 122 | } else { |
| 123 | /* nothing otherwise */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 124 | assert_int_equal(UINT32_MAX, ht->hlists[i]); |
| 125 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /* removing not present data should fail */ |
| 130 | for (i = 0; i < 2; ++i) { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 131 | UTEST_LOG_CLEAN; |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 132 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 133 | CHECK_LOG("Invalid argument hash (lyht_remove_with_resize_cb()).", NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 134 | } |
| 135 | /* removing present data, resize should happened |
| 136 | * when we are below 25% of the table filled, so with 3 records left */ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 137 | for ( ; i < 5; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 139 | } |
| 140 | assert_int_equal(8, ht->size); |
| 141 | |
| 142 | /* remove the rest */ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 143 | for ( ; i < 8; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 144 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 145 | } |
| 146 | |
| 147 | for (i = 0; i < 8; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 148 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* cleanup */ |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 152 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 155 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 156 | test_ht_collisions(void **UNUSED(state)) |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 157 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 158 | #define GET_REC_INT(rec) (*((uint32_t *)&(rec)->val)) |
| 159 | |
| 160 | uint32_t i; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 161 | struct ly_ht_rec *rec; |
| 162 | struct ly_ht *ht; |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 163 | uint32_t rec_idx; |
| 164 | int count; |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 165 | |
| 166 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 167 | |
| 168 | for (i = 2; i < 6; ++i) { |
| 169 | assert_int_equal(lyht_insert(ht, &i, 2, NULL), 0); |
| 170 | } |
| 171 | |
| 172 | /* check all records */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 173 | for (i = 0; i < 8; ++i) { |
| 174 | if (i == 2) |
| 175 | assert_int_not_equal(UINT32_MAX, ht->hlists[i]); |
| 176 | else |
| 177 | assert_int_equal(UINT32_MAX, ht->hlists[i]); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 178 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 179 | for (i = 0; i < 8; ++i) { |
| 180 | if (i >= 2 && i < 6) |
| 181 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, 2, NULL)); |
| 182 | else |
| 183 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, 2, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 184 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 185 | rec_idx = ht->hlists[2]; |
| 186 | count = 0; |
| 187 | while (rec_idx != UINT32_MAX) { |
| 188 | rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx); |
| 189 | rec_idx = rec->next; |
| 190 | assert_int_equal(rec->hash, 2); |
| 191 | count++; |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 192 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 193 | assert_int_equal(count, 4); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 194 | |
| 195 | i = 4; |
| 196 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 197 | |
| 198 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 199 | |
| 200 | i = 2; |
| 201 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 202 | |
| 203 | /* check all records */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 204 | for (i = 0; i < 8; ++i) { |
| 205 | if (i == 2) |
| 206 | assert_int_not_equal(UINT32_MAX, ht->hlists[i]); |
| 207 | else |
| 208 | assert_int_equal(UINT32_MAX, ht->hlists[i]); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 209 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 210 | for (i = 0; i < 8; ++i) { |
| 211 | if (i == 3 || i == 5) |
| 212 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, 2, NULL)); |
| 213 | else |
| 214 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, 2, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 215 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 216 | rec_idx = ht->hlists[2]; |
| 217 | count = 0; |
| 218 | while (rec_idx != UINT32_MAX) { |
| 219 | rec = lyht_get_rec(ht->recs, ht->rec_size, rec_idx); |
| 220 | rec_idx = rec->next; |
| 221 | assert_int_equal(rec->hash, 2); |
| 222 | count++; |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 223 | } |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 224 | assert_int_equal(count, 2); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 225 | |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 226 | for (i = 0; i < 8; ++i) { |
| 227 | if (i == 3 || i == 5) |
| 228 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_SUCCESS); |
| 229 | else |
| 230 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | i = 3; |
| 234 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 235 | i = 5; |
| 236 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 237 | |
| 238 | /* check all records */ |
Olivier Matz | 75c0019 | 2023-09-21 14:35:12 +0200 | [diff] [blame^] | 239 | for (i = 0; i < 8; ++i) { |
| 240 | assert_int_equal(UINT32_MAX, ht->hlists[i]); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 243 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 246 | int |
| 247 | main(void) |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 248 | { |
| 249 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 250 | UTEST(test_invalid_arguments), |
| 251 | UTEST(test_dict_hit), |
| 252 | UTEST(test_ht_basic), |
| 253 | UTEST(test_ht_resize), |
| 254 | UTEST(test_ht_collisions), |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 258 | } |