Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from set.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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame^] | 14 | |
| 15 | #define _GNU_SOURCE |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame^] | 19 | #include <stdlib.h> |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 20 | #include <setjmp.h> |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame^] | 23 | #include <cmocka.h> |
| 24 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 25 | #include "../../src/set.h" |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 26 | |
| 27 | #define BUFSIZE 1024 |
| 28 | char logbuf[BUFSIZE] = {0}; |
| 29 | |
| 30 | static void |
| 31 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 32 | { |
| 33 | (void) level; /* unused */ |
| 34 | (void) path; /* unused */ |
| 35 | |
| 36 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | logger_setup(void **state) |
| 41 | { |
| 42 | (void) state; /* unused */ |
| 43 | |
| 44 | ly_set_log_clb(logger, 0); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | test_basics(void **state) |
| 51 | { |
| 52 | (void) state; /* unused */ |
| 53 | |
| 54 | struct ly_set *set; |
| 55 | char *str; |
| 56 | unsigned int u; |
| 57 | void *ptr; |
| 58 | |
| 59 | /* creation - everything is empty */ |
| 60 | set = ly_set_new(); |
| 61 | assert_non_null(set); |
| 62 | assert_int_equal(0, set->count); |
| 63 | assert_int_equal(0, set->size); |
| 64 | assert_null(set->objs); |
| 65 | |
| 66 | /* add a testing object */ |
| 67 | str = strdup("test string"); |
| 68 | assert_non_null(str); |
| 69 | |
| 70 | ly_set_add(set, str, 0); |
| 71 | assert_int_not_equal(0, set->size); |
| 72 | assert_int_equal(1, set->count); |
| 73 | assert_non_null(set->objs); |
| 74 | assert_non_null(set->objs[0]); |
| 75 | |
| 76 | /* check the presence of the testing data */ |
| 77 | assert_int_equal(0, ly_set_contains(set, str)); |
| 78 | assert_int_equal(-1, ly_set_contains(set, str - 1)); |
| 79 | |
| 80 | /* remove data, but keep the set */ |
| 81 | u = set->size; |
| 82 | ptr = set->objs; |
| 83 | ly_set_clean(set, free); |
| 84 | assert_int_equal(0, set->count); |
| 85 | assert_int_equal(u, set->size); |
| 86 | assert_ptr_equal(ptr, set->objs); |
| 87 | |
| 88 | /* remove buffer, but keep the set object */ |
| 89 | ly_set_erase(set, NULL); |
| 90 | assert_int_equal(0, set->count); |
| 91 | assert_int_equal(0, set->size); |
| 92 | assert_ptr_equal(NULL, set->objs); |
| 93 | |
| 94 | /* final cleanup */ |
| 95 | ly_set_free(set, NULL); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | test_inval(void **state) |
| 100 | { |
| 101 | struct ly_set set; |
| 102 | memset(&set, 0, sizeof set); |
| 103 | |
| 104 | ly_set_clean(NULL, NULL); |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 105 | assert_string_equal(logbuf, ""); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 106 | |
| 107 | ly_set_erase(NULL, NULL); |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 108 | assert_string_equal(logbuf, ""); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 109 | |
| 110 | ly_set_free(NULL, NULL); |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 111 | assert_string_equal(logbuf, ""); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 112 | |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 113 | assert_null(ly_set_dup(NULL, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 114 | assert_string_equal(logbuf, "Invalid argument set (ly_set_dup())."); |
| 115 | |
| 116 | assert_int_equal(-1, ly_set_add(NULL, NULL, 0)); |
| 117 | assert_string_equal(logbuf, "Invalid argument set (ly_set_add())."); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 118 | |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 119 | assert_int_equal(-1, ly_set_merge(NULL, NULL, 0, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 120 | assert_string_equal(logbuf, "Invalid argument trg (ly_set_merge())."); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 121 | assert_int_equal(0, ly_set_merge(&set, NULL, 0, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 122 | assert_string_equal(logbuf, "Invalid argument src (ly_set_merge())."); |
| 123 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 124 | assert_int_equal(LY_EINVAL, ly_set_rm_index(NULL, 0, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 125 | assert_string_equal(logbuf, "Invalid argument set (ly_set_rm_index())."); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 126 | assert_int_equal(LY_EINVAL, ly_set_rm_index(&set, 1, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 127 | assert_string_equal(logbuf, "Invalid argument index (ly_set_rm_index())."); |
| 128 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 129 | assert_int_equal(LY_EINVAL, ly_set_rm(NULL, NULL, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 130 | assert_string_equal(logbuf, "Invalid argument set (ly_set_rm())."); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 131 | assert_int_equal(LY_EINVAL, ly_set_rm(&set, NULL, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 132 | assert_string_equal(logbuf, "Invalid argument object (ly_set_rm())."); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 133 | assert_int_equal(LY_EINVAL, ly_set_rm(&set, &state, NULL)); |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 134 | assert_string_equal(logbuf, "Invalid argument object (ly_set_rm())."); |
| 135 | } |
| 136 | |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 137 | static void |
| 138 | test_duplication(void **state) |
| 139 | { |
| 140 | (void) state; /* unused */ |
| 141 | |
| 142 | struct ly_set *orig, *new; |
| 143 | char *str; |
| 144 | |
| 145 | orig = ly_set_new(); |
| 146 | assert_non_null(orig); |
| 147 | |
| 148 | /* add a testing object */ |
| 149 | str = strdup("test string"); |
| 150 | assert_non_null(str); |
| 151 | assert_int_equal(0, ly_set_add(orig, str, 0)); |
| 152 | |
| 153 | /* duplicate the set - without duplicator, so the new set will point to the same string */ |
| 154 | new = ly_set_dup(orig, NULL); |
| 155 | assert_non_null(new); |
| 156 | assert_ptr_not_equal(orig, new); |
| 157 | assert_int_equal(orig->count, new->count); |
| 158 | assert_ptr_equal(orig->objs[0], new->objs[0]); |
| 159 | |
| 160 | ly_set_free(new, NULL); |
| 161 | |
| 162 | /* duplicate the set - with duplicator, so the new set will point to a different buffer with the same content */ |
| 163 | new = ly_set_dup(orig, (void*(*)(void*))strdup); |
| 164 | assert_non_null(new); |
| 165 | assert_ptr_not_equal(orig, new); |
| 166 | assert_int_equal(orig->count, new->count); |
| 167 | assert_ptr_not_equal(orig->objs[0], new->objs[0]); |
| 168 | assert_string_equal(orig->objs[0], new->objs[0]); |
| 169 | |
| 170 | /* cleanup */ |
| 171 | ly_set_free(new, free); |
| 172 | ly_set_free(orig, free); |
| 173 | } |
| 174 | |
| 175 | static void |
| 176 | test_add(void **state) |
| 177 | { |
| 178 | (void) state; /* unused */ |
| 179 | |
| 180 | unsigned int u, i; |
| 181 | char *str = "test string"; |
| 182 | struct ly_set set; |
| 183 | memset(&set, 0, sizeof set); |
| 184 | |
| 185 | /* add a testing object */ |
| 186 | assert_int_equal(0, ly_set_add(&set, str, 0)); |
| 187 | |
| 188 | /* test avoiding data duplicities */ |
| 189 | assert_int_equal(0, ly_set_add(&set, str, 0)); |
| 190 | assert_int_equal(1, set.count); |
| 191 | assert_int_equal(1, ly_set_add(&set, str, LY_SET_OPT_USEASLIST)); |
| 192 | assert_int_equal(2, set.count); |
| 193 | |
| 194 | /* test array resizing */ |
| 195 | u = set.size; |
| 196 | for (i = 2; i <= u; ++i) { |
| 197 | assert_int_equal(i, ly_set_add(&set, str, LY_SET_OPT_USEASLIST)); |
| 198 | } |
| 199 | assert_true(u != set.size); |
| 200 | |
| 201 | /* cleanup */ |
| 202 | ly_set_erase(&set, NULL); |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | test_merge(void **state) |
| 207 | { |
| 208 | (void) state; /* unused */ |
| 209 | |
| 210 | char *str1, *str2; |
| 211 | struct ly_set one, two; |
| 212 | memset(&one, 0, sizeof one); |
| 213 | memset(&two, 0, sizeof two); |
| 214 | |
| 215 | str1 = strdup("string1"); |
| 216 | str2 = strdup("string2"); |
| 217 | |
| 218 | /* fill first set |
| 219 | * - str1 is the same as in two, so it must not be freed! */ |
| 220 | assert_int_equal(0, ly_set_add(&one, str1, 0)); |
| 221 | |
| 222 | /* fill second set */ |
| 223 | assert_int_equal(0, ly_set_add(&two, str1, 0)); |
| 224 | assert_int_equal(1, ly_set_add(&two, str2, 0)); |
| 225 | |
| 226 | /* merge with checking duplicities - only one item is added into one; |
| 227 | * also without duplicating data, so it must not be freed at the end */ |
| 228 | assert_int_equal(1, ly_set_merge(&one, &two, 0, NULL)); |
| 229 | assert_ptr_equal(one.objs[1], two.objs[1]); |
| 230 | |
| 231 | /* clean and re-fill one (now duplicating str1, to allow testing duplicator) */ |
| 232 | ly_set_clean(&one, NULL); |
| 233 | assert_int_equal(0, ly_set_add(&one, strdup(str1), 0)); |
| 234 | |
| 235 | /* merge without checking duplicities - two items are added into one; |
| 236 | * here also with duplicator */ |
| 237 | assert_int_equal(2, ly_set_merge(&one, &two, LY_SET_OPT_USEASLIST, (void*(*)(void*))strdup)); |
| 238 | assert_ptr_not_equal(one.objs[1], two.objs[0]); |
| 239 | assert_string_equal(one.objs[1], two.objs[0]); |
| 240 | |
| 241 | /* cleanup */ |
| 242 | ly_set_erase(&one, free); |
| 243 | ly_set_erase(&two, free); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | test_rm(void **state) |
| 248 | { |
| 249 | (void) state; /* unused */ |
| 250 | |
| 251 | char *str1, *str2, *str3; |
| 252 | struct ly_set set; |
| 253 | memset(&set, 0, sizeof set); |
| 254 | |
| 255 | /* fill the set */ |
| 256 | assert_int_equal(0, ly_set_add(&set, "string1", 0)); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 257 | assert_int_equal(1, ly_set_add(&set, strdup("string2"), 0)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 258 | assert_int_equal(2, ly_set_add(&set, "string3", 0)); |
| 259 | |
| 260 | /* remove by index ... */ |
| 261 | /* ... in the middle ... */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 262 | assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, free)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 263 | assert_int_equal(2, set.count); |
| 264 | assert_string_not_equal("string2", set.objs[0]); |
| 265 | assert_string_not_equal("string2", set.objs[1]); |
| 266 | /* ... last .. */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 267 | assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, NULL)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 268 | assert_int_equal(1, set.count); |
| 269 | assert_string_not_equal("string3", set.objs[0]); |
| 270 | /* ... first .. */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 271 | assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 0, NULL)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 272 | assert_int_equal(0, set.count); |
| 273 | |
| 274 | /* fill the set */ |
| 275 | assert_int_equal(0, ly_set_add(&set, str1 = "string1", 0)); |
| 276 | assert_int_equal(1, ly_set_add(&set, str2 = "string2", 0)); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 277 | assert_int_equal(2, ly_set_add(&set, str3 = strdup("string3"), 0)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 278 | |
| 279 | /* remove by pointer ... */ |
| 280 | /* ... in the middle ... */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 281 | assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str2, NULL)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 282 | assert_int_equal(2, set.count); |
| 283 | assert_string_not_equal("string2", set.objs[0]); |
| 284 | assert_string_not_equal("string2", set.objs[1]); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 285 | /* ... last (with destructor) .. */ |
| 286 | assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str3, free)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 287 | assert_int_equal(1, set.count); |
| 288 | assert_string_not_equal("string3", set.objs[0]); |
| 289 | /* ... first .. */ |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 290 | assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str1, NULL)); |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 291 | assert_int_equal(0, set.count); |
| 292 | |
| 293 | /* cleanup */ |
| 294 | ly_set_erase(&set, NULL); |
| 295 | } |
| 296 | |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 297 | int main(void) |
| 298 | { |
| 299 | const struct CMUnitTest tests[] = { |
| 300 | cmocka_unit_test(test_basics), |
Radek Krejci | 519b043 | 2018-09-18 17:04:57 +0200 | [diff] [blame] | 301 | cmocka_unit_test(test_duplication), |
| 302 | cmocka_unit_test(test_add), |
| 303 | cmocka_unit_test(test_merge), |
| 304 | cmocka_unit_test(test_rm), |
Radek Krejci | e84f12f | 2018-09-18 14:11:50 +0200 | [diff] [blame] | 305 | cmocka_unit_test_setup(test_inval, logger_setup), |
| 306 | }; |
| 307 | |
| 308 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 309 | } |