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 "utils/llist.h"
29 #include "libsysdb_test.h"
31 #include <check.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 fail_unless(list != NULL,
60 "sdb_llist_create() = NULL; expected list object");
61 } /* setup */
63 static void
64 teardown(void)
65 {
66 sdb_llist_destroy(list);
67 list = NULL;
68 } /* teardown */
70 /* populate the list with the golden data in the specified order */
71 static void
72 populate(void)
73 {
74 size_t i;
75 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
76 int check = sdb_llist_append(list, &golden_data[i]);
77 fail_unless(check == 0,
78 "sdb_llist_append(%s) = %i; expected: 0",
79 golden_data[i].name, check);
80 }
81 } /* populate */
83 START_TEST(test_sdb_llist_clone)
84 {
85 sdb_llist_t *clone;
86 size_t i;
88 populate();
90 clone = sdb_llist_clone(list);
91 fail_unless(clone != NULL,
92 "sdb_llist_clone() = NULL; expected: cloned list object");
94 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
95 fail_unless(golden_data[i].ref_cnt == 3,
96 "sdb_llist_clone() did not take ownership");
97 }
99 sdb_llist_destroy(clone);
100 }
101 END_TEST
103 START_TEST(test_sdb_llist_destroy)
104 {
105 size_t i;
106 populate();
107 sdb_llist_destroy(list);
108 list = NULL;
110 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
111 fail_unless(golden_data[i].ref_cnt == 1,
112 "sdb_llist_destroy() did not deref element %s",
113 golden_data[i].name);
114 }
115 }
116 END_TEST
118 START_TEST(test_sdb_llist_append)
119 {
120 size_t i;
121 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
122 int check = sdb_llist_append(list, &golden_data[i]);
123 fail_unless(check == 0,
124 "sdb_llist_append(%s) = %i; expected: 0",
125 golden_data[i].name, check);
126 fail_unless(golden_data[i].ref_cnt == 2,
127 "sdb_llist_append(%s) did not take ownership",
128 golden_data[i].name);
129 }
130 }
131 END_TEST
133 START_TEST(test_sdb_llist_insert)
134 {
135 size_t i;
136 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
137 int check = sdb_llist_insert(list, &golden_data[i], 0);
138 fail_unless(check == 0,
139 "sdb_llist_insert(%s, 0) = %i; expected: 0",
140 golden_data[i].name, check);
141 fail_unless(golden_data[i].ref_cnt == 2,
142 "sdb_llist_insert(%s, 0) did not take ownership",
143 golden_data[i].name);
144 }
145 }
146 END_TEST
148 START_TEST(test_validate_insert)
149 {
150 size_t i;
151 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
152 /* none of these operations will succeed
153 * => 1 is invalid for each case */
154 int check = sdb_llist_insert(list, &golden_data[i], 1);
155 fail_unless(check == -1,
156 "sdb_llist_insert(%s, 1) = %i; expected: -1",
157 golden_data[i].name, check);
158 fail_unless(golden_data[i].ref_cnt == 1,
159 "sdb_llist_insert(%s, 1) took ownership",
160 golden_data[i].name);
161 }
162 }
163 END_TEST
165 START_TEST(test_sdb_llist_search)
166 {
167 size_t i;
168 populate();
169 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
170 sdb_object_t *check = sdb_llist_search_by_name(list,
171 golden_data[i].name);
172 fail_unless(check == &golden_data[i],
173 "sdb_llist_search_by_name(%s) = NULL; expected: %p",
174 golden_data[i].name, &golden_data[i]);
175 }
177 for (i = 0; i < SDB_STATIC_ARRAY_LEN(unused_names); ++i) {
178 sdb_object_t *check = sdb_llist_search_by_name(list,
179 unused_names[i]);
180 fail_unless(check == NULL,
181 "sdb_llist_search_by_name(%s) = %p; expected: NULL",
182 unused_names[i], check);
183 }
184 }
185 END_TEST
187 START_TEST(test_sdb_llist_shift)
188 {
189 size_t i;
190 populate();
191 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
192 sdb_object_t *check = sdb_llist_shift(list);
193 fail_unless(check == &golden_data[i],
194 "sdb_llist_shift() = NULL; expected: %p",
195 &golden_data[i]);
196 fail_unless(check->ref_cnt == 2,
197 "sdb_llist_shift() changed reference count; got: %i; "
198 "expected: 2", check->ref_cnt);
199 }
201 /* must be empty now */
202 fail_unless(sdb_llist_shift(list) == NULL,
203 "sdb_llist_shift() returned value; expected: NULL");
204 }
205 END_TEST
207 START_TEST(test_sdb_llist_iter)
208 {
209 sdb_llist_iter_t *iter;
210 size_t i;
212 populate();
214 iter = sdb_llist_get_iter(list);
215 fail_unless(iter != NULL,
216 "sdb_llist_get_iter() did not return an iterator");
218 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
219 sdb_object_t *check;
220 fail_unless(sdb_llist_iter_has_next(iter),
221 "sdb_llist_iter_has_next() = FALSE; expected: TRUE");
222 check = sdb_llist_iter_get_next(iter);
223 fail_unless(check == &golden_data[i],
224 "sdb_llist_iter_get_next() = %p; expected: %p",
225 check, &golden_data[i]);
226 }
228 fail_unless(!sdb_llist_iter_has_next(iter),
229 "sdb_llist_iter_has_next() = TRUE; expected: FALSE");
230 fail_unless(sdb_llist_iter_get_next(iter) == NULL,
231 "sdb_llist_iter_get_next() returned value; expected: NULL");
232 sdb_llist_iter_destroy(iter);
233 }
234 END_TEST
236 Suite *
237 util_llist_suite(void)
238 {
239 Suite *s = suite_create("utils::llist");
240 TCase *tc;
242 tc = tcase_create("core");
243 tcase_add_checked_fixture(tc, setup, teardown);
244 tcase_add_test(tc, test_sdb_llist_clone);
245 tcase_add_test(tc, test_sdb_llist_destroy);
246 tcase_add_test(tc, test_sdb_llist_append);
247 tcase_add_test(tc, test_sdb_llist_insert);
248 tcase_add_test(tc, test_validate_insert);
249 tcase_add_test(tc, test_sdb_llist_search);
250 tcase_add_test(tc, test_sdb_llist_shift);
251 tcase_add_test(tc, test_sdb_llist_iter);
252 suite_add_tcase(s, tc);
254 return s;
255 } /* util_llist_suite */
257 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */