Code

t/Makefile: Use top_srcdir rather than top_builddir.
[sysdb.git] / t / core / object_test.c
1 /*
2  * SysDB - t/core/object_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 "core/object.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 /*
34  * private data types
35  */
37 static int init_noop_called = 0;
38 static int init_noop_retval = 0;
39 static int
40 obj_init_noop(sdb_object_t *obj, va_list __attribute__((unused)) ap)
41 {
42         ++init_noop_called;
43         fail_unless(obj != NULL, "obj init function: received obj == NULL");
44         return init_noop_retval;
45 } /* obj_init_noop */
47 static int destroy_noop_called = 0;
48 static void
49 obj_destroy_noop(sdb_object_t *obj)
50 {
51         ++destroy_noop_called;
52         fail_unless(obj != NULL, "obj destroy function: received obj == NULL");
53 } /* obj_destroy_noop */
55 struct noop {
56         sdb_object_t super;
57         int data;
58 };
59 static sdb_type_t noop_type = {
60         /* size = */ sizeof(struct noop),
61         /* init = */ obj_init_noop,
62         /* destroy = */ obj_destroy_noop,
63 };
65 static void *wrapped = (void *)0x42;
67 static int destroy_wrapper_called = 0;
68 static void
69 wrapper_destroy(void *obj)
70 {
71         ++destroy_wrapper_called;
72         fail_unless(obj == wrapped,
73                         "wrapper_destroy received unexpected obj %p; expected: %p",
74                         obj, wrapped);
75 } /* wrapper_destroy */
77 START_TEST(test_obj_create)
78 {
79         sdb_object_t *obj;
80         sdb_type_t test_type = noop_type;
82         const char *name = "test-object";
84         init_noop_called = 0;
85         init_noop_retval = 0;
86         destroy_noop_called = 0;
87         obj = sdb_object_create(name, test_type);
88         fail_unless(obj != NULL,
89                         "sdb_object_create() = NULL; expected: a new object");
90         fail_unless(obj->type.size == test_type.size,
91                         "after sdb_object_create(): type size mismatch; got: %zu; "
92                         "expected: %zu", obj->type.size, test_type.size);
93         fail_unless(obj->ref_cnt == 1,
94                         "after sdb_object_create(): obj->ref_cnt = %d; expected: 1",
95                         obj->ref_cnt);
96         fail_unless(!strcmp(obj->name, "test-object"),
97                         "after sdb_object_create(): obj->name = '%s'; expected: '%s'",
98                         obj->name, name);
99         fail_unless(obj->name != name,
100                         "after sdb_object_create(): obj->name was not strdup()'ed");
102         fail_unless(init_noop_called == 1,
103                         "sdb_object_create() did not call object's init function");
104         fail_unless(destroy_noop_called == 0,
105                         "sdb_object_create() called object's destroy function");
106         fail_unless(((struct noop *)obj)->data == 0,
107                         "sdb_object_create() did not initialize data to zero");
109         sdb_object_deref(obj);
110         /* the memory address at 'obj' is no longer valid but usually this check
111          * should still work */
112         fail_unless(obj->ref_cnt == 0,
113                         "after sdb_object_deref(): obj->ref_cnt = %d; expected: 0",
114                         obj->ref_cnt);
115         fail_unless(destroy_noop_called == 1,
116                         "sdb_object_deref() did not call object's destroy function");
118         init_noop_called = 0;
119         init_noop_retval = -1;
120         destroy_noop_called = 0;
121         obj = sdb_object_create(name, test_type);
122         fail_unless(obj == NULL,
123                         "sdb_object_create() = %p; expected NULL (init returned -1)",
124                         obj);
125         fail_unless(init_noop_called == 1,
126                         "sdb_object_create() did not call object's init function");
127         fail_unless(destroy_noop_called == 1,
128                         "sdb_object_create() did not call object's destroy function "
129                         "after init failure");
131         test_type.size = 1;
132         init_noop_called = 0;
133         init_noop_retval = 0;
134         destroy_noop_called = 0;
135         obj = sdb_object_create(name, test_type);
136         fail_unless(obj == NULL,
137                         "sdb_object_create() = %p; expected NULL (type's size too small)",
138                         obj);
139         fail_unless(init_noop_called == 0,
140                         "sdb_object_create() called object's init function "
141                         "when size was too small");
142         fail_unless(destroy_noop_called == 0,
143                         "sdb_object_create() called object's destroy function "
144                         "when size was too small");
146         test_type.size = sizeof(struct noop);
147         init_noop_retval = 0;
148         test_type.init = NULL;
149         obj = sdb_object_create(name, test_type);
150         fail_unless(obj != NULL,
151                         "sdb_object_create() fails without init callback");
152         sdb_object_deref(obj);
154         test_type.destroy = NULL;
155         obj = sdb_object_create(name, test_type);
156         fail_unless(obj != NULL,
157                         "sdb_object_create() fails without destroy callback");
158         sdb_object_deref(obj);
160 END_TEST
162 START_TEST(test_obj_wrapper)
164         sdb_object_t *obj;
165         const char *name = "wrapped-object";
167         destroy_wrapper_called = 0;
168         obj = sdb_object_create_wrapper(name, wrapped, wrapper_destroy);
169         fail_unless(obj != NULL,
170                         "sdb_object_create_wrapper() = NULL; expected: wrapper object");
171         fail_unless(obj->ref_cnt == 1,
172                         "after sdb_object_create_wrapper(); obj->ref_cnt = %d; "
173                         "expected: 1", obj->ref_cnt);
174         fail_unless(!strcmp(obj->name, name),
175                         "after sdb_object_create_wrapper(); obj->name = %s; expected: %s",
176                         obj->name, name);
177         fail_unless(obj->name != name,
178                         "sdb_object_create_wrapper() did not copy object name");
179         fail_unless(SDB_OBJ_WRAPPER(obj)->data == wrapped,
180                         "wrapped object wraps unexpected data %p; expected: %p",
181                         SDB_OBJ_WRAPPER(obj)->data, wrapped);
183         fail_unless(destroy_wrapper_called == 0,
184                         "sdb_object_create_wrapper() called object's destructor");
186         sdb_object_deref(obj);
187         fail_unless(destroy_wrapper_called == 1,
188                         "sdb_object_deref() did not call wrapped object's destructor");
190 END_TEST
192 START_TEST(test_obj_ref)
194         sdb_object_t *obj;
195         sdb_type_t test_type = noop_type;
197         init_noop_called = 0;
198         init_noop_retval = 0;
199         destroy_noop_called = 0;
201         obj = sdb_object_create("test-object", test_type);
202         fail_unless(obj != NULL,
203                         "sdb_object_create() = NULL; expected: valid object");
205         sdb_object_ref(obj);
206         fail_unless(obj->ref_cnt == 2,
207                         "after db_object_ref(): obj->ref_cnt = %d; expected: 2",
208                         obj->ref_cnt);
210         sdb_object_ref(obj);
211         fail_unless(obj->ref_cnt == 3,
212                         "after db_object_ref(): obj->ref_cnt = %d; expected: 3",
213                         obj->ref_cnt);
215         obj->ref_cnt = 42;
216         sdb_object_ref(obj);
217         fail_unless(obj->ref_cnt == 43,
218                         "after db_object_ref(): obj->ref_cnt = %d; expected: 43",
219                         obj->ref_cnt);
221         fail_unless(init_noop_called == 1,
222                         "after some sdb_object_ref(); object's init called %d times; "
223                         "expected: 1", init_noop_called);
224         fail_unless(destroy_noop_called == 0,
225                         "after some sdb_object_ref(); object's destroy called %d time%s; "
226                         "expected: 0", destroy_noop_called == 1 ? "" : "2",
227                         destroy_noop_called);
229         sdb_object_deref(obj);
230         fail_unless(obj->ref_cnt == 42,
231                         "after db_object_deref(): obj->ref_cnt = %d; expected: 42",
232                         obj->ref_cnt);
234         obj->ref_cnt = 23;
235         sdb_object_deref(obj);
236         fail_unless(obj->ref_cnt == 22,
237                         "after db_object_deref(): obj->ref_cnt = %d; expected: 22",
238                         obj->ref_cnt);
240         fail_unless(init_noop_called == 1,
241                         "after some sdb_object_{de,}ref(); object's init called %d times; "
242                         "expected: 1", init_noop_called);
243         fail_unless(destroy_noop_called == 0,
244                         "after some sdb_object_{de,}ref(); object's destroy called "
245                         "%d time%s; expected: 0", destroy_noop_called == 1 ? "" : "2",
246                         destroy_noop_called);
248         /* test_obj_create already checks the ref_cnt == 1 case */
249         obj->ref_cnt = 0;
250         sdb_object_deref(obj);
251         fail_unless(obj->ref_cnt <= 0,
252                         "after db_object_deref(): obj->ref_cnt = %d; expected: <= 0",
253                         obj->ref_cnt);
255         fail_unless(init_noop_called == 1,
256                         "after some sdb_object_{de,}ref(); object's init called %d times; "
257                         "expected: 1", init_noop_called);
258         fail_unless(destroy_noop_called == 1,
259                         "after some sdb_object_{de,}ref(); object's destroy called "
260                         "%d times; expected: 1", destroy_noop_called);
262         /* this should work */
263         sdb_object_deref(NULL);
265 END_TEST
267 START_TEST(test_obj_cmp)
269         sdb_object_t *obj1, *obj2, *obj3, *obj4;
270         int status;
272         obj1 = sdb_object_create("a", noop_type);
273         obj2 = sdb_object_create("b", noop_type);
274         obj3 = sdb_object_create("B", noop_type);
275         obj4 = sdb_object_create("c", noop_type);
277         status = sdb_object_cmp_by_name(obj1, obj2);
278         fail_unless(status == -1,
279                         "sdb_object_cmp_by_name('a', 'b') = %d; expected: -1", status);
280         status = sdb_object_cmp_by_name(obj2, obj3);
281         fail_unless(status == 0,
282                         "sdb_object_cmp_by_name('b', 'B') = %d; expected: 0", status);
283         status = sdb_object_cmp_by_name(obj4, obj3);
284         fail_unless(status == 1,
285                         "sdb_object_cmp_by_name('c', 'B') = %d; expected: 1", status);
286         status = sdb_object_cmp_by_name(obj1, obj1);
287         fail_unless(status == 0,
288                         "sdb_object_cmp_by_name('a', 'a') = %d; expected: 0", status);
290         sdb_object_deref(obj1);
291         sdb_object_deref(obj2);
292         sdb_object_deref(obj3);
293         sdb_object_deref(obj4);
295 END_TEST
297 Suite *
298 core_object_suite(void)
300         Suite *s = suite_create("core::object");
301         TCase *tc;
303         tc = tcase_create("core");
304         tcase_add_test(tc, test_obj_create);
305         tcase_add_test(tc, test_obj_wrapper);
306         tcase_add_test(tc, test_obj_ref);
307         tcase_add_test(tc, test_obj_cmp);
308         suite_add_tcase(s, tc);
310         return s;
311 } /* core_object_suite */
313 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */