blob: 982163f858f0c98c9921b2d3f17887d388ac6d8e [file] [log] [blame]
Radek Krejcie84f12f2018-09-18 14:11:50 +02001/*
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 Krejcib7db73a2018-10-24 14:18:40 +020014#include "common.h"
Radek Krejcie84f12f2018-09-18 14:11:50 +020015
Radek Krejcie84f12f2018-09-18 14:11:50 +020016#include <stdarg.h>
17#include <stddef.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include <string.h>
22
23#include "libyang.h"
24#include "../../src/set.c"
25
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28
29static void
30logger(LY_LOG_LEVEL level, const char *msg, const char *path)
31{
32 (void) level; /* unused */
33 (void) path; /* unused */
34
35 strncpy(logbuf, msg, BUFSIZE - 1);
36}
37
38static int
39logger_setup(void **state)
40{
41 (void) state; /* unused */
42
43 ly_set_log_clb(logger, 0);
44
45 return 0;
46}
47
48static void
49test_basics(void **state)
50{
51 (void) state; /* unused */
52
53 struct ly_set *set;
54 char *str;
55 unsigned int u;
56 void *ptr;
57
58 /* creation - everything is empty */
59 set = ly_set_new();
60 assert_non_null(set);
61 assert_int_equal(0, set->count);
62 assert_int_equal(0, set->size);
63 assert_null(set->objs);
64
65 /* add a testing object */
66 str = strdup("test string");
67 assert_non_null(str);
68
69 ly_set_add(set, str, 0);
70 assert_int_not_equal(0, set->size);
71 assert_int_equal(1, set->count);
72 assert_non_null(set->objs);
73 assert_non_null(set->objs[0]);
74
75 /* check the presence of the testing data */
76 assert_int_equal(0, ly_set_contains(set, str));
77 assert_int_equal(-1, ly_set_contains(set, str - 1));
78
79 /* remove data, but keep the set */
80 u = set->size;
81 ptr = set->objs;
82 ly_set_clean(set, free);
83 assert_int_equal(0, set->count);
84 assert_int_equal(u, set->size);
85 assert_ptr_equal(ptr, set->objs);
86
87 /* remove buffer, but keep the set object */
88 ly_set_erase(set, NULL);
89 assert_int_equal(0, set->count);
90 assert_int_equal(0, set->size);
91 assert_ptr_equal(NULL, set->objs);
92
93 /* final cleanup */
94 ly_set_free(set, NULL);
95}
96
97static void
98test_inval(void **state)
99{
100 struct ly_set set;
101 memset(&set, 0, sizeof set);
102
103 ly_set_clean(NULL, NULL);
104 assert_string_equal(logbuf, "Invalid argument set (ly_set_clean()).");
105
106 ly_set_erase(NULL, NULL);
107 assert_string_equal(logbuf, "Invalid argument set (ly_set_erase()).");
108
109 ly_set_free(NULL, NULL);
110 assert_string_equal(logbuf, "Invalid argument set (ly_set_free()).");
111
Radek Krejci519b0432018-09-18 17:04:57 +0200112 assert_null(ly_set_dup(NULL, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200113 assert_string_equal(logbuf, "Invalid argument set (ly_set_dup()).");
114
115 assert_int_equal(-1, ly_set_add(NULL, NULL, 0));
116 assert_string_equal(logbuf, "Invalid argument set (ly_set_add()).");
117 assert_int_equal(-1, ly_set_add(&set, NULL, 0));
118 assert_string_equal(logbuf, "Invalid argument object (ly_set_add()).");
119
Radek Krejci519b0432018-09-18 17:04:57 +0200120 assert_int_equal(-1, ly_set_merge(NULL, NULL, 0, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200121 assert_string_equal(logbuf, "Invalid argument trg (ly_set_merge()).");
Radek Krejci519b0432018-09-18 17:04:57 +0200122 assert_int_equal(0, ly_set_merge(&set, NULL, 0, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200123 assert_string_equal(logbuf, "Invalid argument src (ly_set_merge()).");
124
Radek Krejci820d2262018-09-20 12:15:31 +0200125 assert_int_equal(LY_EINVAL, ly_set_rm_index(NULL, 0, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200126 assert_string_equal(logbuf, "Invalid argument set (ly_set_rm_index()).");
Radek Krejci820d2262018-09-20 12:15:31 +0200127 assert_int_equal(LY_EINVAL, ly_set_rm_index(&set, 1, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200128 assert_string_equal(logbuf, "Invalid argument index (ly_set_rm_index()).");
129
Radek Krejci820d2262018-09-20 12:15:31 +0200130 assert_int_equal(LY_EINVAL, ly_set_rm(NULL, NULL, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200131 assert_string_equal(logbuf, "Invalid argument set (ly_set_rm()).");
Radek Krejci820d2262018-09-20 12:15:31 +0200132 assert_int_equal(LY_EINVAL, ly_set_rm(&set, NULL, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200133 assert_string_equal(logbuf, "Invalid argument object (ly_set_rm()).");
Radek Krejci820d2262018-09-20 12:15:31 +0200134 assert_int_equal(LY_EINVAL, ly_set_rm(&set, &state, NULL));
Radek Krejcie84f12f2018-09-18 14:11:50 +0200135 assert_string_equal(logbuf, "Invalid argument object (ly_set_rm()).");
136}
137
Radek Krejci519b0432018-09-18 17:04:57 +0200138static void
139test_duplication(void **state)
140{
141 (void) state; /* unused */
142
143 struct ly_set *orig, *new;
144 char *str;
145
146 orig = ly_set_new();
147 assert_non_null(orig);
148
149 /* add a testing object */
150 str = strdup("test string");
151 assert_non_null(str);
152 assert_int_equal(0, ly_set_add(orig, str, 0));
153
154 /* duplicate the set - without duplicator, so the new set will point to the same string */
155 new = ly_set_dup(orig, NULL);
156 assert_non_null(new);
157 assert_ptr_not_equal(orig, new);
158 assert_int_equal(orig->count, new->count);
159 assert_ptr_equal(orig->objs[0], new->objs[0]);
160
161 ly_set_free(new, NULL);
162
163 /* duplicate the set - with duplicator, so the new set will point to a different buffer with the same content */
164 new = ly_set_dup(orig, (void*(*)(void*))strdup);
165 assert_non_null(new);
166 assert_ptr_not_equal(orig, new);
167 assert_int_equal(orig->count, new->count);
168 assert_ptr_not_equal(orig->objs[0], new->objs[0]);
169 assert_string_equal(orig->objs[0], new->objs[0]);
170
171 /* cleanup */
172 ly_set_free(new, free);
173 ly_set_free(orig, free);
174}
175
176static void
177test_add(void **state)
178{
179 (void) state; /* unused */
180
181 unsigned int u, i;
182 char *str = "test string";
183 struct ly_set set;
184 memset(&set, 0, sizeof set);
185
186 /* add a testing object */
187 assert_int_equal(0, ly_set_add(&set, str, 0));
188
189 /* test avoiding data duplicities */
190 assert_int_equal(0, ly_set_add(&set, str, 0));
191 assert_int_equal(1, set.count);
192 assert_int_equal(1, ly_set_add(&set, str, LY_SET_OPT_USEASLIST));
193 assert_int_equal(2, set.count);
194
195 /* test array resizing */
196 u = set.size;
197 for (i = 2; i <= u; ++i) {
198 assert_int_equal(i, ly_set_add(&set, str, LY_SET_OPT_USEASLIST));
199 }
200 assert_true(u != set.size);
201
202 /* cleanup */
203 ly_set_erase(&set, NULL);
204}
205
206static void
207test_merge(void **state)
208{
209 (void) state; /* unused */
210
211 char *str1, *str2;
212 struct ly_set one, two;
213 memset(&one, 0, sizeof one);
214 memset(&two, 0, sizeof two);
215
216 str1 = strdup("string1");
217 str2 = strdup("string2");
218
219 /* fill first set
220 * - str1 is the same as in two, so it must not be freed! */
221 assert_int_equal(0, ly_set_add(&one, str1, 0));
222
223 /* fill second set */
224 assert_int_equal(0, ly_set_add(&two, str1, 0));
225 assert_int_equal(1, ly_set_add(&two, str2, 0));
226
227 /* merge with checking duplicities - only one item is added into one;
228 * also without duplicating data, so it must not be freed at the end */
229 assert_int_equal(1, ly_set_merge(&one, &two, 0, NULL));
230 assert_ptr_equal(one.objs[1], two.objs[1]);
231
232 /* clean and re-fill one (now duplicating str1, to allow testing duplicator) */
233 ly_set_clean(&one, NULL);
234 assert_int_equal(0, ly_set_add(&one, strdup(str1), 0));
235
236 /* merge without checking duplicities - two items are added into one;
237 * here also with duplicator */
238 assert_int_equal(2, ly_set_merge(&one, &two, LY_SET_OPT_USEASLIST, (void*(*)(void*))strdup));
239 assert_ptr_not_equal(one.objs[1], two.objs[0]);
240 assert_string_equal(one.objs[1], two.objs[0]);
241
242 /* cleanup */
243 ly_set_erase(&one, free);
244 ly_set_erase(&two, free);
245}
246
247static void
248test_rm(void **state)
249{
250 (void) state; /* unused */
251
252 char *str1, *str2, *str3;
253 struct ly_set set;
254 memset(&set, 0, sizeof set);
255
256 /* fill the set */
257 assert_int_equal(0, ly_set_add(&set, "string1", 0));
Radek Krejci820d2262018-09-20 12:15:31 +0200258 assert_int_equal(1, ly_set_add(&set, strdup("string2"), 0));
Radek Krejci519b0432018-09-18 17:04:57 +0200259 assert_int_equal(2, ly_set_add(&set, "string3", 0));
260
261 /* remove by index ... */
262 /* ... in the middle ... */
Radek Krejci820d2262018-09-20 12:15:31 +0200263 assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, free));
Radek Krejci519b0432018-09-18 17:04:57 +0200264 assert_int_equal(2, set.count);
265 assert_string_not_equal("string2", set.objs[0]);
266 assert_string_not_equal("string2", set.objs[1]);
267 /* ... last .. */
Radek Krejci820d2262018-09-20 12:15:31 +0200268 assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, NULL));
Radek Krejci519b0432018-09-18 17:04:57 +0200269 assert_int_equal(1, set.count);
270 assert_string_not_equal("string3", set.objs[0]);
271 /* ... first .. */
Radek Krejci820d2262018-09-20 12:15:31 +0200272 assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 0, NULL));
Radek Krejci519b0432018-09-18 17:04:57 +0200273 assert_int_equal(0, set.count);
274
275 /* fill the set */
276 assert_int_equal(0, ly_set_add(&set, str1 = "string1", 0));
277 assert_int_equal(1, ly_set_add(&set, str2 = "string2", 0));
Radek Krejci820d2262018-09-20 12:15:31 +0200278 assert_int_equal(2, ly_set_add(&set, str3 = strdup("string3"), 0));
Radek Krejci519b0432018-09-18 17:04:57 +0200279
280 /* remove by pointer ... */
281 /* ... in the middle ... */
Radek Krejci820d2262018-09-20 12:15:31 +0200282 assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str2, NULL));
Radek Krejci519b0432018-09-18 17:04:57 +0200283 assert_int_equal(2, set.count);
284 assert_string_not_equal("string2", set.objs[0]);
285 assert_string_not_equal("string2", set.objs[1]);
Radek Krejci820d2262018-09-20 12:15:31 +0200286 /* ... last (with destructor) .. */
287 assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str3, free));
Radek Krejci519b0432018-09-18 17:04:57 +0200288 assert_int_equal(1, set.count);
289 assert_string_not_equal("string3", set.objs[0]);
290 /* ... first .. */
Radek Krejci820d2262018-09-20 12:15:31 +0200291 assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str1, NULL));
Radek Krejci519b0432018-09-18 17:04:57 +0200292 assert_int_equal(0, set.count);
293
294 /* cleanup */
295 ly_set_erase(&set, NULL);
296}
297
Radek Krejcie84f12f2018-09-18 14:11:50 +0200298int main(void)
299{
300 const struct CMUnitTest tests[] = {
301 cmocka_unit_test(test_basics),
Radek Krejci519b0432018-09-18 17:04:57 +0200302 cmocka_unit_test(test_duplication),
303 cmocka_unit_test(test_add),
304 cmocka_unit_test(test_merge),
305 cmocka_unit_test(test_rm),
Radek Krejcie84f12f2018-09-18 14:11:50 +0200306 cmocka_unit_test_setup(test_inval, logger_setup),
307 };
308
309 return cmocka_run_group_tests(tests, NULL, NULL);
310}