summaryrefslogtreecommitdiffstats
path: root/tests/utests/basic/test_set.c
blob: af39afae4c9c6f624983b8b7dc29b5248331e50f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * @file test_set.c
 * @author: Radek Krejci <rkrejci@cesnet.cz>
 * @brief unit tests for functions from set.c
 *
 * Copyright (c) 2018 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */
#define _POSIX_C_SOURCE 200809L /* strdup */
#define _UTEST_MAIN_
#include "utests.h"

#include <stdlib.h>
#include <string.h>

#include "set.h"

static void
test_basics(void **UNUSED(state))
{
    struct ly_set *set;
    char *str;
    unsigned int u;
    void *ptr;
    uint32_t index;

    /* creation - everything is empty */
    assert_int_equal(LY_SUCCESS, ly_set_new(&set));
    assert_non_null(set);
    assert_int_equal(0, set->count);
    assert_int_equal(0, set->size);
    assert_null(set->objs);

    /* add a testing object */
    str = strdup("test string");
    assert_non_null(str);

    assert_int_equal(LY_SUCCESS, ly_set_add(set, str, 0, NULL));
    assert_int_not_equal(0, set->size);
    assert_int_equal(1, set->count);
    assert_non_null(set->objs);
    assert_non_null(set->objs[0]);

    /* check the presence of the testing data */
    assert_int_equal(1, ly_set_contains(set, str, &index));
    assert_int_equal(0, index);
    assert_int_equal(0, ly_set_contains(set, str - 1, NULL));

    /* remove data, but keep the set */
    u = set->size;
    ptr = set->objs;
    ly_set_clean(set, free);
    assert_int_equal(0, set->count);
    assert_int_equal(u, set->size);
    assert_ptr_equal(ptr, set->objs);

    /* remove buffer, but keep the set object */
    ly_set_erase(set, NULL);
    assert_int_equal(0, set->count);
    assert_int_equal(0, set->size);
    assert_ptr_equal(NULL, set->objs);

    /* final cleanup */
    ly_set_free(set, NULL);
}

static void
test_inval(void **state)
{
    struct ly_set set;

    memset(&set, 0, sizeof set);

    ly_set_clean(NULL, NULL);
    CHECK_LOG(NULL, NULL);

    ly_set_erase(NULL, NULL);
    CHECK_LOG(NULL, NULL);

    ly_set_free(NULL, NULL);
    CHECK_LOG(NULL, NULL);

    assert_int_equal(LY_EINVAL, ly_set_dup(NULL, NULL, NULL));
    CHECK_LOG("Invalid argument set (ly_set_dup()).", NULL);

    assert_int_equal(LY_EINVAL, ly_set_add(NULL, NULL, 0, NULL));
    CHECK_LOG("Invalid argument set (ly_set_add()).", NULL);

    assert_int_equal(LY_EINVAL, ly_set_merge(NULL, NULL, 0, NULL));
    CHECK_LOG("Invalid argument trg (ly_set_merge()).", NULL);
    assert_int_equal(LY_SUCCESS, ly_set_merge(&set, NULL, 0, NULL));

    assert_int_equal(LY_EINVAL, ly_set_rm_index(NULL, 0, NULL));
    CHECK_LOG("Invalid argument set (ly_set_rm_index()).", NULL);
    assert_int_equal(LY_EINVAL, ly_set_rm_index(&set, 1, NULL));
    CHECK_LOG("Invalid argument index (ly_set_rm_index()).", NULL);

    assert_int_equal(LY_EINVAL, ly_set_rm(NULL, NULL, NULL));
    CHECK_LOG("Invalid argument set (ly_set_rm()).", NULL);
    assert_int_equal(LY_EINVAL, ly_set_rm(&set, NULL, NULL));
    CHECK_LOG("Invalid argument object (ly_set_rm()).", NULL);
    assert_int_equal(LY_EINVAL, ly_set_rm(&set, &set, NULL));
    CHECK_LOG("Invalid argument object (ly_set_rm()).", NULL);
}

static void
test_duplication(void **UNUSED(state))
{
    struct ly_set *orig, *new;
    char *str;
    uint32_t index;

    assert_int_equal(LY_SUCCESS, ly_set_new(&orig));
    assert_non_null(orig);

    /* add a testing object */
    str = strdup("test string");
    assert_non_null(str);
    assert_int_equal(LY_SUCCESS, ly_set_add(orig, str, 0, &index));
    assert_int_equal(0, index);

    /* duplicate the set - without duplicator, so the new set will point to the same string */
    assert_int_equal(LY_SUCCESS, ly_set_dup(orig, NULL, &new));
    assert_non_null(new);
    assert_ptr_not_equal(orig, new);
    assert_int_equal(orig->count, new->count);
    assert_ptr_equal(orig->objs[0], new->objs[0]);

    ly_set_free(new, NULL);

    /* duplicate the set - with duplicator, so the new set will point to a different buffer with the same content */
    assert_int_equal(LY_SUCCESS, ly_set_dup(orig, (void *(*)(const void *))strdup, &new));
    assert_non_null(new);
    assert_ptr_not_equal(orig, new);
    assert_int_equal(orig->count, new->count);
    assert_ptr_not_equal(orig->objs[0], new->objs[0]);
    assert_string_equal(orig->objs[0], new->objs[0]);

    /* cleanup */
    ly_set_free(new, free);
    ly_set_free(orig, free);
}

static void
test_add(void **UNUSED(state))
{
    uint32_t u, index;
    char *str = "test string";
    struct ly_set set;

    memset(&set, 0, sizeof set);

    /* add a testing object */
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str, 0, &index));
    assert_int_equal(0, index);

    /* test avoiding data duplicities */
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str, 0, &index));
    assert_int_equal(0, index);
    assert_int_equal(1, set.count);
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str, 1, &index));
    assert_int_equal(1, index);
    assert_int_equal(2, set.count);

    /* test array resizing */
    u = set.size;
    for (uint32_t expected_index = 2; expected_index <= u; ++expected_index) {
        assert_int_equal(LY_SUCCESS, ly_set_add(&set, str, 1, &index));
        assert_int_equal(expected_index, index);
    }
    assert_true(u != set.size);

    /* cleanup */
    ly_set_erase(&set, NULL);
}

static void
test_merge(void **UNUSED(state))
{
    char *str1, *str2;
    struct ly_set one, two;

    memset(&one, 0, sizeof one);
    memset(&two, 0, sizeof two);

    str1 = strdup("string1");
    str2 = strdup("string2");

    /* fill first set
     * - str1 is the same as in two, so it must not be freed! */
    assert_int_equal(LY_SUCCESS, ly_set_add(&one, str1, 0, NULL));

    /* fill second set */
    assert_int_equal(LY_SUCCESS, ly_set_add(&two, str1, 0, NULL));
    assert_int_equal(LY_SUCCESS, ly_set_add(&two, str2, 0, NULL));

    /* merge with checking duplicities - only one item is added into one;
     * also without duplicating data, so it must not be freed at the end */
    assert_int_equal(LY_SUCCESS, ly_set_merge(&one, &two, 0, NULL));
    assert_int_equal(2, one.count);
    assert_ptr_equal(one.objs[1], two.objs[1]);

    /* clean and re-fill one (now duplicating str1, to allow testing duplicator) */
    ly_set_clean(&one, NULL);
    assert_int_equal(LY_SUCCESS, ly_set_add(&one, strdup(str1), 0, NULL));

    /* merge without checking duplicities - two items are added into one;
     * here also with duplicator */
    assert_int_equal(LY_SUCCESS, ly_set_merge(&one, &two, 1, (void *(*)(const void *))strdup));
    assert_int_equal(3, one.count);
    assert_ptr_not_equal(one.objs[1], two.objs[0]);
    assert_string_equal(one.objs[1], two.objs[0]);

    /* cleanup */
    ly_set_erase(&one, free);
    ly_set_erase(&two, free);
}

static void
test_rm(void **UNUSED(state))
{
    char *str1, *str2, *str3;
    struct ly_set set;

    memset(&set, 0, sizeof set);

    /* fill the set */
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, "string1", 0, NULL));
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, strdup("string2"), 0, NULL));
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, "string3", 0, NULL));

    /* remove by index ... */
    /* ... in the middle ... */
    assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, free));
    assert_int_equal(2, set.count);
    assert_string_not_equal("string2", set.objs[0]);
    assert_string_not_equal("string2", set.objs[1]);
    /* ... last .. */
    assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 1, NULL));
    assert_int_equal(1, set.count);
    assert_string_not_equal("string3", set.objs[0]);
    /* ... first .. */
    assert_int_equal(LY_SUCCESS, ly_set_rm_index(&set, 0, NULL));
    assert_int_equal(0, set.count);

    /* fill the set */
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str1 = "string1", 0, NULL));
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str2 = "string2", 0, NULL));
    assert_int_equal(LY_SUCCESS, ly_set_add(&set, str3 = strdup("string3"), 0, NULL));

    /* remove by pointer ... */
    /* ... in the middle ... */
    assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str2, NULL));
    assert_int_equal(2, set.count);
    assert_string_not_equal("string2", set.objs[0]);
    assert_string_not_equal("string2", set.objs[1]);
    /* ... last (with destructor) .. */
    assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str3, free));
    assert_int_equal(1, set.count);
    assert_string_not_equal("string3", set.objs[0]);
    /* ... first .. */
    assert_int_equal(LY_SUCCESS, ly_set_rm(&set, str1, NULL));
    assert_int_equal(0, set.count);

    /* cleanup */
    ly_set_erase(&set, NULL);
}

int
main(void)
{
    const struct CMUnitTest tests[] = {
        UTEST(test_basics),
        UTEST(test_duplication),
        UTEST(test_add),
        UTEST(test_merge),
        UTEST(test_rm),
        UTEST(test_inval),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}