Code

Added unit testing framework based on ‘check’.
[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_append)
82 {
83         int i;
84         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
85                 int check = sdb_llist_append(list, &golden_data[i]);
86                 fail_unless(check == 0,
87                                 "sdb_llist_append(%s) = %i; expected: 0",
88                                 golden_data[i].name, check);
89                 fail_unless(golden_data[i].ref_cnt == 2,
90                                 "sdb_llist_append(%s) did not take ownership",
91                                 golden_data[i].name);
92         }
93 }
94 END_TEST
96 START_TEST(test_insert)
97 {
98         int i;
99         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
100                 int check = sdb_llist_insert(list, &golden_data[i], 0);
101                 fail_unless(check == 0,
102                                 "sdb_llist_insert(%s, 0) = %i; expected: 0",
103                                 golden_data[i].name, check);
104                 fail_unless(golden_data[i].ref_cnt == 2,
105                                 "sdb_llist_insert(%s, 0) did not take ownership",
106                                 golden_data[i].name);
107         }
109 END_TEST
111 START_TEST(test_insert_invalid)
113         int i;
114         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
115                 /* none of these operations will succeed
116                  * => 1 is invalid for each case */
117                 int check = sdb_llist_insert(list, &golden_data[i], 1);
118                 fail_unless(check == -1,
119                                 "sdb_llist_insert(%s, 1) = %i; expected: -1",
120                                 golden_data[i].name, check);
121                 fail_unless(golden_data[i].ref_cnt == 1,
122                                 "sdb_llist_insert(%s, 1) took ownership",
123                                 golden_data[i].name);
124         }
126 END_TEST
128 START_TEST(test_search)
130         int i;
131         populate();
132         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
133                 sdb_object_t *check = sdb_llist_search_by_name(list,
134                                 golden_data[i].name);
135                 fail_unless(check == &golden_data[i],
136                                 "sdb_llist_search_by_name(%s) = NULL; expected: %p",
137                                 golden_data[i].name, &golden_data[i]);
138         }
140         for (i = 0; i < SDB_STATIC_ARRAY_LEN(unused_names); ++i) {
141                 sdb_object_t *check = sdb_llist_search_by_name(list,
142                                 unused_names[i]);
143                 fail_unless(check == NULL,
144                                 "sdb_llist_search_by_name(%s) = %p; expected: NULL",
145                                 unused_names[i], check);
146         }
148 END_TEST
150 START_TEST(test_shift)
152         int i;
153         populate();
154         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
155                 sdb_object_t *check = sdb_llist_shift(list);
156                 fail_unless(check == &golden_data[i],
157                                 "sdb_llist_shift() = NULL; expected: %p",
158                                 &golden_data[i]);
159                 fail_unless(check->ref_cnt == 2,
160                                 "sdb_llist_shift() changed reference count; got: %i; "
161                                 "expected: 2", check->ref_cnt);
162         }
164         /* must be empty now */
165         fail_unless(sdb_llist_shift(list) == NULL,
166                         "sdb_llist_shift() returned value; expected: NULL");
168 END_TEST
170 START_TEST(test_iter)
172         sdb_llist_iter_t *iter;
173         int i;
175         populate();
177         iter = sdb_llist_get_iter(list);
178         fail_unless(iter != NULL,
179                         "sdb_llist_get_iter() did not return an iterator");
181         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
182                 sdb_object_t *check;
183                 fail_unless(sdb_llist_iter_has_next(iter),
184                                 "sdb_llist_iter_has_next() = FALSE; expected: TRUE");
185                 check = sdb_llist_iter_get_next(iter);
186                 fail_unless(check == &golden_data[i],
187                                 "sdb_llist_iter_get_next() = %p; expected: %p",
188                                 check, &golden_data[i]);
189         }
191         fail_unless(!sdb_llist_iter_has_next(iter),
192                         "sdb_llist_iter_has_next() = TRUE; expected: FALSE");
193         fail_unless(sdb_llist_iter_get_next(iter) == NULL,
194                         "sdb_llist_iter_get_next() returned value; expected: NULL");
195         sdb_llist_iter_destroy(iter);
197 END_TEST
199 Suite *
200 util_llist_suite(void)
202         Suite *s = suite_create("utils::llist");
204         TCase *tc_core = tcase_create("core");
205         tcase_add_checked_fixture(tc_core, setup, teardown);
206         tcase_add_test(tc_core, test_append);
207         tcase_add_test(tc_core, test_insert);
208         tcase_add_test(tc_core, test_insert_invalid);
209         tcase_add_test(tc_core, test_search);
210         tcase_add_test(tc_core, test_shift);
211         tcase_add_test(tc_core, test_iter);
212         suite_add_tcase(s, tc_core);
214         return s;
215 } /* util_llist_suite */
217 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */