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