Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [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 | |
| 22 | struct ht_rec *lyht_get_rec(unsigned char *recs, uint16_t rec_size, uint32_t idx); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 23 | |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 24 | static void |
| 25 | test_invalid_arguments(void **state) |
| 26 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 27 | assert_int_equal(LY_EINVAL, lydict_insert(NULL, NULL, 0, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 28 | CHECK_LOG("Invalid argument ctx (lydict_insert()).", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 30 | assert_int_equal(LY_EINVAL, lydict_insert_zc(NULL, NULL, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 31 | CHECK_LOG("Invalid argument ctx (lydict_insert_zc()).", NULL); |
| 32 | assert_int_equal(LY_EINVAL, lydict_insert_zc(UTEST_LYCTX, NULL, NULL)); |
Michal Vasko | be27c91 | 2021-02-24 17:07:37 +0100 | [diff] [blame] | 33 | CHECK_LOG_CTX("Invalid argument str_p (lydict_insert_zc()).", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static void |
| 37 | test_dict_hit(void **state) |
| 38 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 39 | const char *str1, *str2, *str3; |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 40 | |
| 41 | /* insert 2 strings, one of them repeatedly */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 42 | assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "test1", 0, &str1)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 43 | assert_non_null(str1); |
| 44 | /* via zerocopy we have to get the same pointer as provided */ |
| 45 | assert_non_null(str2 = strdup("test2")); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 46 | 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] | 47 | assert_ptr_equal(str2, str3); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 48 | /* 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] | 49 | assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "test1", 0, &str2)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 50 | assert_non_null(str2); |
| 51 | assert_ptr_equal(str1, str2); |
| 52 | |
| 53 | /* remove strings, but the repeatedly inserted only once */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 54 | lydict_remove(UTEST_LYCTX, "test1"); |
| 55 | lydict_remove(UTEST_LYCTX, "test2"); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 56 | |
| 57 | /* destroy dictionary - should raise warning about data presence */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 58 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 59 | UTEST_LYCTX = NULL; |
| 60 | CHECK_LOG("String \"test1\" not freed from the dictionary, refcount 1", NULL); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 61 | |
| 62 | #ifndef NDEBUG |
| 63 | /* cleanup */ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 64 | free((char *)str1); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 65 | #endif |
| 66 | } |
| 67 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 68 | static uint8_t |
| 69 | 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] | 70 | { |
| 71 | int *v1, *v2; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 72 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 73 | (void)mod; |
| 74 | (void)cb_data; |
| 75 | |
| 76 | v1 = (int *)val1; |
| 77 | v2 = (int *)val2; |
| 78 | |
| 79 | return *v1 == *v2; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | test_ht_basic(void **state) |
| 84 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 85 | uint32_t i; |
| 86 | struct hash_table *ht; |
| 87 | |
| 88 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 0)); |
| 89 | |
| 90 | i = 2; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 91 | assert_int_equal(LY_ENOTFOUND, 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_insert(ht, &i, i, NULL)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 93 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 94 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 95 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 96 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 97 | CHECK_LOG("Invalid argument hash (lyht_remove_with_resize_cb()).", NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 98 | |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 99 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void |
| 103 | test_ht_resize(void **state) |
| 104 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 105 | uint32_t i; |
| 106 | struct ht_rec *rec; |
| 107 | struct hash_table *ht; |
| 108 | |
| 109 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 110 | assert_int_equal(8, ht->size); |
| 111 | |
| 112 | /* insert records into indexes 2-7 */ |
| 113 | for (i = 2; i < 8; ++i) { |
| 114 | assert_int_equal(LY_SUCCESS, lyht_insert(ht, &i, i, NULL)); |
| 115 | } |
| 116 | /* check that table resized */ |
| 117 | assert_int_equal(16, ht->size); |
| 118 | |
| 119 | /* check expected content of the table */ |
| 120 | for (i = 0; i < 16; ++i) { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 121 | if ((i >= 2) && (i < 8)) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 122 | /* inserted data on indexes 2-7 */ |
| 123 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 124 | assert_int_equal(1, rec->hits); |
| 125 | assert_int_equal(i, rec->hash); |
| 126 | } else { |
| 127 | /* nothing otherwise */ |
| 128 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 129 | assert_int_equal(0, rec->hits); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* removing not present data should fail */ |
| 134 | for (i = 0; i < 2; ++i) { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 135 | UTEST_LOG_CLEAN; |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 136 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 137 | CHECK_LOG("Invalid argument hash (lyht_remove_with_resize_cb()).", NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 138 | } |
| 139 | /* removing present data, resize should happened |
| 140 | * 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] | 141 | for ( ; i < 5; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 142 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 143 | } |
| 144 | assert_int_equal(8, ht->size); |
| 145 | |
| 146 | /* remove the rest */ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 147 | for ( ; i < 8; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 148 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 149 | } |
| 150 | |
| 151 | for (i = 0; i < 8; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 152 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* cleanup */ |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 156 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 159 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 160 | test_ht_collisions(void **UNUSED(state)) |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 161 | { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 162 | #define GET_REC_INT(rec) (*((uint32_t *)&(rec)->val)) |
| 163 | |
| 164 | uint32_t i; |
| 165 | struct ht_rec *rec; |
| 166 | struct hash_table *ht; |
| 167 | |
| 168 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 169 | |
| 170 | for (i = 2; i < 6; ++i) { |
| 171 | assert_int_equal(lyht_insert(ht, &i, 2, NULL), 0); |
| 172 | } |
| 173 | |
| 174 | /* check all records */ |
| 175 | for (i = 0; i < 2; ++i) { |
| 176 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 177 | assert_int_equal(rec->hits, 0); |
| 178 | } |
| 179 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 180 | assert_int_equal(rec->hits, 4); |
| 181 | assert_int_equal(GET_REC_INT(rec), i); |
| 182 | ++i; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 183 | for ( ; i < 6; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 184 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 185 | assert_int_equal(rec->hits, 1); |
| 186 | assert_int_equal(GET_REC_INT(rec), i); |
| 187 | } |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 188 | for ( ; i < 8; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 189 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 190 | assert_int_equal(rec->hits, 0); |
| 191 | } |
| 192 | |
| 193 | i = 4; |
| 194 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 195 | |
| 196 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 197 | assert_int_equal(rec->hits, -1); |
| 198 | |
| 199 | i = 2; |
| 200 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 201 | |
| 202 | /* check all records */ |
| 203 | for (i = 0; i < 2; ++i) { |
| 204 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 205 | assert_int_equal(rec->hits, 0); |
| 206 | } |
| 207 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 208 | assert_int_equal(rec->hits, 2); |
| 209 | assert_int_equal(GET_REC_INT(rec), 5); |
| 210 | ++i; |
| 211 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 212 | assert_int_equal(rec->hits, 1); |
| 213 | assert_int_equal(GET_REC_INT(rec), 3); |
| 214 | ++i; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 215 | for ( ; i < 6; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 216 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 217 | assert_int_equal(rec->hits, -1); |
| 218 | } |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 219 | for ( ; i < 8; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 220 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 221 | assert_int_equal(rec->hits, 0); |
| 222 | } |
| 223 | |
| 224 | for (i = 0; i < 3; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 225 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 226 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 227 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_SUCCESS); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 228 | ++i; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 229 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 230 | ++i; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 231 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_SUCCESS); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 232 | ++i; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 233 | for ( ; i < 8; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 234 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | i = 3; |
| 238 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 239 | i = 5; |
| 240 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 241 | |
| 242 | /* check all records */ |
| 243 | for (i = 0; i < 2; ++i) { |
| 244 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 245 | assert_int_equal(rec->hits, 0); |
| 246 | } |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 247 | for ( ; i < 6; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 248 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 249 | assert_int_equal(rec->hits, -1); |
| 250 | } |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 251 | for ( ; i < 8; ++i) { |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 252 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 253 | assert_int_equal(rec->hits, 0); |
| 254 | } |
| 255 | |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 256 | lyht_free(ht, NULL); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 259 | int |
| 260 | main(void) |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 261 | { |
| 262 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 263 | UTEST(test_invalid_arguments), |
| 264 | UTEST(test_dict_hit), |
| 265 | UTEST(test_ht_basic), |
| 266 | UTEST(test_ht_resize), |
| 267 | UTEST(test_ht_collisions), |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 271 | } |