xref: /illumos-kvm-cmd/check-qlist.c (revision 68396ea9)
1 /*
2  * QList unit-tests.
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <lcapitulino@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 #include <check.h>
13 
14 #include "qint.h"
15 #include "qlist.h"
16 
17 /*
18  * Public Interface test-cases
19  *
20  * (with some violations to access 'private' data)
21  */
22 
START_TEST(qlist_new_test)23 START_TEST(qlist_new_test)
24 {
25     QList *qlist;
26 
27     qlist = qlist_new();
28     fail_unless(qlist != NULL);
29     fail_unless(qlist->base.refcnt == 1);
30     fail_unless(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
31 
32     // destroy doesn't exist yet
33     qemu_free(qlist);
34 }
35 END_TEST
36 
START_TEST(qlist_append_test)37 START_TEST(qlist_append_test)
38 {
39     QInt *qi;
40     QList *qlist;
41     QListEntry *entry;
42 
43     qi = qint_from_int(42);
44 
45     qlist = qlist_new();
46     qlist_append(qlist, qi);
47 
48     entry = QTAILQ_FIRST(&qlist->head);
49     fail_unless(entry != NULL);
50     fail_unless(entry->value == QOBJECT(qi));
51 
52     // destroy doesn't exist yet
53     QDECREF(qi);
54     qemu_free(entry);
55     qemu_free(qlist);
56 }
57 END_TEST
58 
START_TEST(qobject_to_qlist_test)59 START_TEST(qobject_to_qlist_test)
60 {
61     QList *qlist;
62 
63     qlist = qlist_new();
64 
65     fail_unless(qobject_to_qlist(QOBJECT(qlist)) == qlist);
66 
67     // destroy doesn't exist yet
68     qemu_free(qlist);
69 }
70 END_TEST
71 
START_TEST(qlist_destroy_test)72 START_TEST(qlist_destroy_test)
73 {
74     int i;
75     QList *qlist;
76 
77     qlist = qlist_new();
78 
79     for (i = 0; i < 42; i++)
80         qlist_append(qlist, qint_from_int(i));
81 
82     QDECREF(qlist);
83 }
84 END_TEST
85 
86 static int iter_called;
87 static const int iter_max = 42;
88 
iter_func(QObject * obj,void * opaque)89 static void iter_func(QObject *obj, void *opaque)
90 {
91     QInt *qi;
92 
93     fail_unless(opaque == NULL);
94 
95     qi = qobject_to_qint(obj);
96     fail_unless(qi != NULL);
97     fail_unless((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max));
98 
99     iter_called++;
100 }
101 
START_TEST(qlist_iter_test)102 START_TEST(qlist_iter_test)
103 {
104     int i;
105     QList *qlist;
106 
107     qlist = qlist_new();
108 
109     for (i = 0; i < iter_max; i++)
110         qlist_append(qlist, qint_from_int(i));
111 
112     iter_called = 0;
113     qlist_iter(qlist, iter_func, NULL);
114 
115     fail_unless(iter_called == iter_max);
116 
117     QDECREF(qlist);
118 }
119 END_TEST
120 
QList_suite(void)121 static Suite *QList_suite(void)
122 {
123     Suite *s;
124     TCase *qlist_public_tcase;
125 
126     s = suite_create("QList suite");
127 
128     qlist_public_tcase = tcase_create("Public Interface");
129     suite_add_tcase(s, qlist_public_tcase);
130     tcase_add_test(qlist_public_tcase, qlist_new_test);
131     tcase_add_test(qlist_public_tcase, qlist_append_test);
132     tcase_add_test(qlist_public_tcase, qobject_to_qlist_test);
133     tcase_add_test(qlist_public_tcase, qlist_destroy_test);
134     tcase_add_test(qlist_public_tcase, qlist_iter_test);
135 
136     return s;
137 }
138 
main(void)139 int main(void)
140 {
141 	int nf;
142 	Suite *s;
143 	SRunner *sr;
144 
145 	s = QList_suite();
146 	sr = srunner_create(s);
147 
148 	srunner_run_all(sr, CK_NORMAL);
149 	nf = srunner_ntests_failed(sr);
150 	srunner_free(sr);
151 
152 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
153 }
154