Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file hash_table.c |
| 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 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include <string.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "hash_table.h" |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 28 | |
| 29 | 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] | 30 | |
| 31 | #define BUFSIZE 1024 |
| 32 | char logbuf[BUFSIZE] = {0}; |
| 33 | |
| 34 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 35 | #define ENABLE_LOGGER_CHECKING 1 |
| 36 | |
| 37 | static void |
| 38 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 39 | { |
| 40 | (void) level; /* unused */ |
| 41 | (void) path; /* unused */ |
| 42 | |
| 43 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 44 | } |
| 45 | |
| 46 | static int |
| 47 | logger_setup(void **state) |
| 48 | { |
| 49 | (void) state; /* unused */ |
| 50 | #if ENABLE_LOGGER_CHECKING |
| 51 | ly_set_log_clb(logger, 0); |
| 52 | #endif |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | logbuf_clean(void) |
| 58 | { |
| 59 | logbuf[0] = '\0'; |
| 60 | } |
| 61 | |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 64 | #else |
| 65 | # define logbuf_assert(str) |
| 66 | #endif |
| 67 | |
| 68 | static void |
| 69 | test_invalid_arguments(void **state) |
| 70 | { |
| 71 | (void) state; /* unused */ |
| 72 | struct ly_ctx *ctx; |
| 73 | |
| 74 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 75 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 76 | assert_int_equal(LY_EINVAL, lydict_insert(NULL, NULL, 0, NULL)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 77 | logbuf_assert("Invalid argument ctx (lydict_insert())."); |
| 78 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 79 | assert_int_equal(LY_EINVAL, lydict_insert_zc(NULL, NULL, NULL)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 80 | logbuf_assert("Invalid argument ctx (lydict_insert_zc())."); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 81 | assert_int_equal(LY_EINVAL, lydict_insert_zc(ctx, NULL, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 82 | logbuf_assert("Invalid argument value (lydict_insert_zc())."); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 83 | |
| 84 | ly_ctx_destroy(ctx, NULL); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | test_dict_hit(void **state) |
| 89 | { |
| 90 | (void) state; /* unused */ |
| 91 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 92 | const char *str1, *str2, *str3; |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 93 | struct ly_ctx *ctx; |
| 94 | |
| 95 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 96 | |
| 97 | /* insert 2 strings, one of them repeatedly */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 98 | assert_int_equal(LY_SUCCESS, lydict_insert(ctx, "test1", 0, &str1)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 99 | assert_non_null(str1); |
| 100 | /* via zerocopy we have to get the same pointer as provided */ |
| 101 | assert_non_null(str2 = strdup("test2")); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 102 | assert_int_equal(LY_SUCCESS, lydict_insert_zc(ctx, (char *)str2, &str3)); |
| 103 | assert_ptr_equal(str2, str3); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 104 | /* here we get the same pointer as in case the string was inserted first time */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 105 | assert_int_equal(LY_SUCCESS, lydict_insert(ctx, "test1", 0, &str2)); |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 106 | assert_non_null(str2); |
| 107 | assert_ptr_equal(str1, str2); |
| 108 | |
| 109 | /* remove strings, but the repeatedly inserted only once */ |
| 110 | lydict_remove(ctx, "test1"); |
| 111 | lydict_remove(ctx, "test2"); |
| 112 | |
| 113 | /* destroy dictionary - should raise warning about data presence */ |
| 114 | ly_ctx_destroy(ctx, NULL); |
| 115 | logbuf_assert("String \"test1\" not freed from the dictionary, refcount 1"); |
| 116 | |
| 117 | #ifndef NDEBUG |
| 118 | /* cleanup */ |
| 119 | free((char*)str1); |
| 120 | #endif |
| 121 | } |
| 122 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 123 | static uint8_t |
| 124 | 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] | 125 | { |
| 126 | int *v1, *v2; |
| 127 | (void)mod; |
| 128 | (void)cb_data; |
| 129 | |
| 130 | v1 = (int *)val1; |
| 131 | v2 = (int *)val2; |
| 132 | |
| 133 | return *v1 == *v2; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | test_ht_basic(void **state) |
| 138 | { |
| 139 | (void) state; /* unused */ |
| 140 | |
| 141 | uint32_t i; |
| 142 | struct hash_table *ht; |
| 143 | |
| 144 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 0)); |
| 145 | |
| 146 | i = 2; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 147 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 148 | assert_int_equal(LY_SUCCESS, lyht_insert(ht, &i, i, NULL)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 149 | assert_int_equal(LY_SUCCESS, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 150 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 151 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 152 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 153 | logbuf_assert("Invalid argument hash (lyht_remove_with_resize_cb())."); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 154 | |
| 155 | lyht_free(ht); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | test_ht_resize(void **state) |
| 160 | { |
| 161 | (void) state; /* unused */ |
| 162 | |
| 163 | uint32_t i; |
| 164 | struct ht_rec *rec; |
| 165 | struct hash_table *ht; |
| 166 | |
| 167 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 168 | assert_int_equal(8, ht->size); |
| 169 | |
| 170 | /* insert records into indexes 2-7 */ |
| 171 | for (i = 2; i < 8; ++i) { |
| 172 | assert_int_equal(LY_SUCCESS, lyht_insert(ht, &i, i, NULL)); |
| 173 | } |
| 174 | /* check that table resized */ |
| 175 | assert_int_equal(16, ht->size); |
| 176 | |
| 177 | /* check expected content of the table */ |
| 178 | for (i = 0; i < 16; ++i) { |
| 179 | if (i >=2 && i < 8) { |
| 180 | /* inserted data on indexes 2-7 */ |
| 181 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 182 | assert_int_equal(1, rec->hits); |
| 183 | assert_int_equal(i, rec->hash); |
| 184 | } else { |
| 185 | /* nothing otherwise */ |
| 186 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 187 | assert_int_equal(0, rec->hits); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* removing not present data should fail */ |
| 192 | for (i = 0; i < 2; ++i) { |
| 193 | logbuf_clean(); |
Michal Vasko | 4a4c7ed | 2020-07-17 09:30:12 +0200 | [diff] [blame] | 194 | assert_int_equal(LY_ENOTFOUND, lyht_remove(ht, &i, i)); |
Michal Vasko | 5bcc33b | 2020-10-06 15:33:44 +0200 | [diff] [blame] | 195 | logbuf_assert("Invalid argument hash (lyht_remove_with_resize_cb())."); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 196 | } |
| 197 | /* removing present data, resize should happened |
| 198 | * when we are below 25% of the table filled, so with 3 records left */ |
| 199 | for (; i < 5; ++i) { |
| 200 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 201 | } |
| 202 | assert_int_equal(8, ht->size); |
| 203 | |
| 204 | /* remove the rest */ |
| 205 | for (; i < 8; ++i) { |
| 206 | assert_int_equal(LY_SUCCESS, lyht_remove(ht, &i, i)); |
| 207 | } |
| 208 | |
| 209 | for (i = 0; i < 8; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 210 | assert_int_equal(LY_ENOTFOUND, lyht_find(ht, &i, i, NULL)); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* cleanup */ |
| 214 | lyht_free(ht); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | static void |
| 219 | test_ht_collisions(void **state) |
| 220 | { |
| 221 | (void) state; /* unused */ |
| 222 | #define GET_REC_INT(rec) (*((uint32_t *)&(rec)->val)) |
| 223 | |
| 224 | uint32_t i; |
| 225 | struct ht_rec *rec; |
| 226 | struct hash_table *ht; |
| 227 | |
| 228 | assert_non_null(ht = lyht_new(8, sizeof(int), ht_equal_clb, NULL, 1)); |
| 229 | |
| 230 | for (i = 2; i < 6; ++i) { |
| 231 | assert_int_equal(lyht_insert(ht, &i, 2, NULL), 0); |
| 232 | } |
| 233 | |
| 234 | /* check all records */ |
| 235 | for (i = 0; i < 2; ++i) { |
| 236 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 237 | assert_int_equal(rec->hits, 0); |
| 238 | } |
| 239 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 240 | assert_int_equal(rec->hits, 4); |
| 241 | assert_int_equal(GET_REC_INT(rec), i); |
| 242 | ++i; |
| 243 | for (; i < 6; ++i) { |
| 244 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 245 | assert_int_equal(rec->hits, 1); |
| 246 | assert_int_equal(GET_REC_INT(rec), i); |
| 247 | } |
| 248 | for (; i < 8; ++i) { |
| 249 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 250 | assert_int_equal(rec->hits, 0); |
| 251 | } |
| 252 | |
| 253 | i = 4; |
| 254 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 255 | |
| 256 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 257 | assert_int_equal(rec->hits, -1); |
| 258 | |
| 259 | i = 2; |
| 260 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 261 | |
| 262 | /* check all records */ |
| 263 | for (i = 0; i < 2; ++i) { |
| 264 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 265 | assert_int_equal(rec->hits, 0); |
| 266 | } |
| 267 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 268 | assert_int_equal(rec->hits, 2); |
| 269 | assert_int_equal(GET_REC_INT(rec), 5); |
| 270 | ++i; |
| 271 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 272 | assert_int_equal(rec->hits, 1); |
| 273 | assert_int_equal(GET_REC_INT(rec), 3); |
| 274 | ++i; |
| 275 | for (; i < 6; ++i) { |
| 276 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 277 | assert_int_equal(rec->hits, -1); |
| 278 | } |
| 279 | for (; i < 8; ++i) { |
| 280 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 281 | assert_int_equal(rec->hits, 0); |
| 282 | } |
| 283 | |
| 284 | for (i = 0; i < 3; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 285 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 286 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 287 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_SUCCESS); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 288 | ++i; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 289 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 290 | ++i; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 291 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_SUCCESS); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 292 | ++i; |
| 293 | for (; i < 8; ++i) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 294 | assert_int_equal(lyht_find(ht, &i, 2, NULL), LY_ENOTFOUND); |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | i = 3; |
| 298 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 299 | i = 5; |
| 300 | assert_int_equal(lyht_remove(ht, &i, 2), 0); |
| 301 | |
| 302 | /* check all records */ |
| 303 | for (i = 0; i < 2; ++i) { |
| 304 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 305 | assert_int_equal(rec->hits, 0); |
| 306 | } |
| 307 | for (; i < 6; ++i) { |
| 308 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 309 | assert_int_equal(rec->hits, -1); |
| 310 | } |
| 311 | for (; i < 8; ++i) { |
| 312 | rec = lyht_get_rec(ht->recs, ht->rec_size, i); |
| 313 | assert_int_equal(rec->hits, 0); |
| 314 | } |
| 315 | |
| 316 | lyht_free(ht); |
| 317 | } |
| 318 | |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 319 | int main(void) |
| 320 | { |
| 321 | const struct CMUnitTest tests[] = { |
| 322 | cmocka_unit_test_setup(test_invalid_arguments, logger_setup), |
| 323 | cmocka_unit_test_setup(test_dict_hit, logger_setup), |
Radek Krejci | 0ae092d | 2018-09-20 16:43:19 +0200 | [diff] [blame] | 324 | cmocka_unit_test_setup(test_ht_basic, logger_setup), |
| 325 | cmocka_unit_test_setup(test_ht_resize, logger_setup), |
| 326 | cmocka_unit_test_setup(test_ht_collisions, logger_setup), |
Radek Krejci | aaf6d40 | 2018-09-20 15:14:47 +0200 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 330 | } |