Code

t/utils/llist_test: Added tests for clone and destroy.
[sysdb.git] / t / utils / llist_test.c
1 /*
2  * SysDB - t/utils/llist_test.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include <check.h>
30 #include "libsysdb_test.h"
31 #include "utils/llist.h"
33 /*
34  * private data types
35  */
37 static sdb_object_t golden_data[] = {
38         SSTRING_OBJ("abc"),
39         SSTRING_OBJ("bcd"),
40         SSTRING_OBJ("cde"),
41         SSTRING_OBJ("def"),
42         SSTRING_OBJ("efg"),
43         SSTRING_OBJ("fgh"),
44         SSTRING_OBJ("ghi")
45 };
47 static char *unused_names[] = {
48         "xyz",
49         "yza",
50         "zab"
51 };
53 static sdb_llist_t *list;
55 static void
56 setup(void)
57 {
58         list = sdb_llist_create();
59 } /* setup */
61 static void
62 teardown(void)
63 {
64         sdb_llist_destroy(list);
65         list = NULL;
66 } /* teardown */
68 /* populate the list with the golden data in the specified order */
69 static void
70 populate(void)
71 {
72         int i;
73         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
74                 int check = sdb_llist_append(list, &golden_data[i]);
75                 fail_unless(check == 0,
76                                 "sdb_llist_append(%s) = %i; expected: 0",
77                                 golden_data[i].name, check);
78         }
79 } /* populate */
81 START_TEST(test_clone)
82 {
83         sdb_llist_t *clone;
84         int i;
86         populate();
88         clone = sdb_llist_clone(list);
89         fail_unless(clone != NULL,
90                         "sdb_llist_clone() = NULL; expected: !NULL");
92         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
93                 fail_unless(golden_data[i].ref_cnt == 3,
94                                 "sdb_llist_clone() did not take ownership");
95         }
97         sdb_llist_destroy(clone);
98 }
99 END_TEST
101 START_TEST(test_destroy)
103         int i;
104         populate();
105         sdb_llist_destroy(list);
106         list = NULL;
108         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
109                 fail_unless(golden_data[i].ref_cnt == 1,
110                                 "sdb_llist_destroy() did not deref element %s",
111                                 golden_data[i].name);
112         }
114 END_TEST
116 START_TEST(test_append)
118         int i;
119         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
120                 int check = sdb_llist_append(list, &golden_data[i]);
121                 fail_unless(check == 0,
122                                 "sdb_llist_append(%s) = %i; expected: 0",
123                                 golden_data[i].name, check);
124                 fail_unless(golden_data[i].ref_cnt == 2,
125                                 "sdb_llist_append(%s) did not take ownership",
126                                 golden_data[i].name);
127         }
129 END_TEST
131 START_TEST(test_insert)
133         int i;
134         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
135                 int check = sdb_llist_insert(list, &golden_data[i], 0);
136                 fail_unless(check == 0,
137                                 "sdb_llist_insert(%s, 0) = %i; expected: 0",
138                                 golden_data[i].name, check);
139                 fail_unless(golden_data[i].ref_cnt == 2,
140                                 "sdb_llist_insert(%s, 0) did not take ownership",
141                                 golden_data[i].name);
142         }
144 END_TEST
146 START_TEST(test_insert_invalid)
148         int i;
149         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
150                 /* none of these operations will succeed
151                  * => 1 is invalid for each case */
152                 int check = sdb_llist_insert(list, &golden_data[i], 1);
153                 fail_unless(check == -1,
154                                 "sdb_llist_insert(%s, 1) = %i; expected: -1",
155                                 golden_data[i].name, check);
156                 fail_unless(golden_data[i].ref_cnt == 1,
157                                 "sdb_llist_insert(%s, 1) took ownership",
158                                 golden_data[i].name);
159         }
161 END_TEST
163 START_TEST(test_search)
165         int i;
166         populate();
167         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
168                 sdb_object_t *check = sdb_llist_search_by_name(list,
169                                 golden_data[i].name);
170                 fail_unless(check == &golden_data[i],
171                                 "sdb_llist_search_by_name(%s) = NULL; expected: %p",
172                                 golden_data[i].name, &golden_data[i]);
173         }
175         for (i = 0; i < SDB_STATIC_ARRAY_LEN(unused_names); ++i) {
176                 sdb_object_t *check = sdb_llist_search_by_name(list,
177                                 unused_names[i]);
178                 fail_unless(check == NULL,
179                                 "sdb_llist_search_by_name(%s) = %p; expected: NULL",
180                                 unused_names[i], check);
181         }
183 END_TEST
185 START_TEST(test_shift)
187         int i;
188         populate();
189         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
190                 sdb_object_t *check = sdb_llist_shift(list);
191                 fail_unless(check == &golden_data[i],
192                                 "sdb_llist_shift() = NULL; expected: %p",
193                                 &golden_data[i]);
194                 fail_unless(check->ref_cnt == 2,
195                                 "sdb_llist_shift() changed reference count; got: %i; "
196                                 "expected: 2", check->ref_cnt);
197         }
199         /* must be empty now */
200         fail_unless(sdb_llist_shift(list) == NULL,
201                         "sdb_llist_shift() returned value; expected: NULL");
203 END_TEST
205 START_TEST(test_iter)
207         sdb_llist_iter_t *iter;
208         int i;
210         populate();
212         iter = sdb_llist_get_iter(list);
213         fail_unless(iter != NULL,
214                         "sdb_llist_get_iter() did not return an iterator");
216         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
217                 sdb_object_t *check;
218                 fail_unless(sdb_llist_iter_has_next(iter),
219                                 "sdb_llist_iter_has_next() = FALSE; expected: TRUE");
220                 check = sdb_llist_iter_get_next(iter);
221                 fail_unless(check == &golden_data[i],
222                                 "sdb_llist_iter_get_next() = %p; expected: %p",
223                                 check, &golden_data[i]);
224         }
226         fail_unless(!sdb_llist_iter_has_next(iter),
227                         "sdb_llist_iter_has_next() = TRUE; expected: FALSE");
228         fail_unless(sdb_llist_iter_get_next(iter) == NULL,
229                         "sdb_llist_iter_get_next() returned value; expected: NULL");
230         sdb_llist_iter_destroy(iter);
232 END_TEST
234 Suite *
235 util_llist_suite(void)
237         Suite *s = suite_create("utils::llist");
238         TCase *tc;
240         tc = tcase_create("core");
241         tcase_add_checked_fixture(tc, setup, teardown);
242         tcase_add_test(tc, test_clone);
243         tcase_add_test(tc, test_destroy);
244         tcase_add_test(tc, test_append);
245         tcase_add_test(tc, test_insert);
246         tcase_add_test(tc, test_insert_invalid);
247         tcase_add_test(tc, test_search);
248         tcase_add_test(tc, test_shift);
249         tcase_add_test(tc, test_iter);
250         suite_add_tcase(s, tc);
252         return s;
253 } /* util_llist_suite */
255 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */