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