Fix warning about uninitialized variable
[qemu/kraxel.git] / check-qlist.c
blob58984cbfcc32039f4b445956e579d67a05213b24
1 /*
2 * QList unit-tests.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
12 #include <check.h>
14 #include "qint.h"
15 #include "qlist.h"
18 * Public Interface test-cases
20 * (with some violations to access 'private' data)
23 START_TEST(qlist_new_test)
25 QList *qlist;
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);
32 // destroy doesn't exist yet
33 qemu_free(qlist);
35 END_TEST
37 START_TEST(qlist_append_test)
39 QInt *qi;
40 QList *qlist;
41 QListEntry *entry;
43 qi = qint_from_int(42);
45 qlist = qlist_new();
46 qlist_append(qlist, qi);
48 entry = QTAILQ_FIRST(&qlist->head);
49 fail_unless(entry != NULL);
50 fail_unless(entry->value == QOBJECT(qi));
52 // destroy doesn't exist yet
53 QDECREF(qi);
54 qemu_free(entry);
55 qemu_free(qlist);
57 END_TEST
59 START_TEST(qobject_to_qlist_test)
61 QList *qlist;
63 qlist = qlist_new();
65 fail_unless(qobject_to_qlist(QOBJECT(qlist)) == qlist);
67 // destroy doesn't exist yet
68 qemu_free(qlist);
70 END_TEST
72 START_TEST(qlist_destroy_test)
74 int i;
75 QList *qlist;
77 qlist = qlist_new();
79 for (i = 0; i < 42; i++)
80 qlist_append(qlist, qint_from_int(i));
82 QDECREF(qlist);
84 END_TEST
86 static int iter_called;
87 static const int iter_max = 42;
89 static void iter_func(QObject *obj, void *opaque)
91 QInt *qi;
93 fail_unless(opaque == NULL);
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));
99 iter_called++;
102 START_TEST(qlist_iter_test)
104 int i;
105 QList *qlist;
107 qlist = qlist_new();
109 for (i = 0; i < iter_max; i++)
110 qlist_append(qlist, qint_from_int(i));
112 iter_called = 0;
113 qlist_iter(qlist, iter_func, NULL);
115 fail_unless(iter_called == iter_max);
117 QDECREF(qlist);
119 END_TEST
121 static Suite *QList_suite(void)
123 Suite *s;
124 TCase *qlist_public_tcase;
126 s = suite_create("QList suite");
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);
136 return s;
139 int main(void)
141 int nf;
142 Suite *s;
143 SRunner *sr;
145 s = QList_suite();
146 sr = srunner_create(s);
148 srunner_run_all(sr, CK_NORMAL);
149 nf = srunner_ntests_failed(sr);
150 srunner_free(sr);
152 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;