]>
Commit | Line | Data |
---|---|---|
3aa3dcff LC |
1 | /* |
2 | * QList unit-tests. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * | |
41836a9f LC |
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. | |
3aa3dcff | 11 | */ |
91479dd0 | 12 | #include <glib.h> |
3aa3dcff | 13 | |
7b1b5d19 PB |
14 | #include "qapi/qmp/qint.h" |
15 | #include "qapi/qmp/qlist.h" | |
3aa3dcff LC |
16 | |
17 | /* | |
18 | * Public Interface test-cases | |
19 | * | |
20 | * (with some violations to access 'private' data) | |
21 | */ | |
22 | ||
91479dd0 | 23 | static void qlist_new_test(void) |
3aa3dcff LC |
24 | { |
25 | QList *qlist; | |
26 | ||
27 | qlist = qlist_new(); | |
91479dd0 AL |
28 | g_assert(qlist != NULL); |
29 | g_assert(qlist->base.refcnt == 1); | |
30 | g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST); | |
3aa3dcff LC |
31 | |
32 | // destroy doesn't exist yet | |
7267c094 | 33 | g_free(qlist); |
3aa3dcff | 34 | } |
3aa3dcff | 35 | |
91479dd0 | 36 | static void qlist_append_test(void) |
3aa3dcff LC |
37 | { |
38 | QInt *qi; | |
39 | QList *qlist; | |
40 | QListEntry *entry; | |
41 | ||
42 | qi = qint_from_int(42); | |
43 | ||
44 | qlist = qlist_new(); | |
45 | qlist_append(qlist, qi); | |
46 | ||
47 | entry = QTAILQ_FIRST(&qlist->head); | |
91479dd0 AL |
48 | g_assert(entry != NULL); |
49 | g_assert(entry->value == QOBJECT(qi)); | |
3aa3dcff LC |
50 | |
51 | // destroy doesn't exist yet | |
52 | QDECREF(qi); | |
7267c094 AL |
53 | g_free(entry); |
54 | g_free(qlist); | |
3aa3dcff | 55 | } |
3aa3dcff | 56 | |
91479dd0 | 57 | static void qobject_to_qlist_test(void) |
3aa3dcff LC |
58 | { |
59 | QList *qlist; | |
60 | ||
61 | qlist = qlist_new(); | |
62 | ||
91479dd0 | 63 | g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist); |
3aa3dcff LC |
64 | |
65 | // destroy doesn't exist yet | |
7267c094 | 66 | g_free(qlist); |
3aa3dcff | 67 | } |
3aa3dcff | 68 | |
91479dd0 | 69 | static void qlist_destroy_test(void) |
3aa3dcff LC |
70 | { |
71 | int i; | |
72 | QList *qlist; | |
73 | ||
74 | qlist = qlist_new(); | |
75 | ||
76 | for (i = 0; i < 42; i++) | |
77 | qlist_append(qlist, qint_from_int(i)); | |
78 | ||
79 | QDECREF(qlist); | |
80 | } | |
3aa3dcff LC |
81 | |
82 | static int iter_called; | |
83 | static const int iter_max = 42; | |
84 | ||
85 | static void iter_func(QObject *obj, void *opaque) | |
86 | { | |
87 | QInt *qi; | |
88 | ||
91479dd0 | 89 | g_assert(opaque == NULL); |
3aa3dcff LC |
90 | |
91 | qi = qobject_to_qint(obj); | |
91479dd0 AL |
92 | g_assert(qi != NULL); |
93 | g_assert((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max)); | |
3aa3dcff LC |
94 | |
95 | iter_called++; | |
96 | } | |
97 | ||
91479dd0 | 98 | static void qlist_iter_test(void) |
3aa3dcff LC |
99 | { |
100 | int i; | |
101 | QList *qlist; | |
102 | ||
103 | qlist = qlist_new(); | |
104 | ||
105 | for (i = 0; i < iter_max; i++) | |
106 | qlist_append(qlist, qint_from_int(i)); | |
107 | ||
108 | iter_called = 0; | |
109 | qlist_iter(qlist, iter_func, NULL); | |
110 | ||
91479dd0 | 111 | g_assert(iter_called == iter_max); |
3aa3dcff LC |
112 | |
113 | QDECREF(qlist); | |
114 | } | |
3aa3dcff | 115 | |
91479dd0 | 116 | int main(int argc, char **argv) |
3aa3dcff | 117 | { |
91479dd0 | 118 | g_test_init(&argc, &argv, NULL); |
3aa3dcff | 119 | |
91479dd0 AL |
120 | g_test_add_func("/public/new", qlist_new_test); |
121 | g_test_add_func("/public/append", qlist_append_test); | |
122 | g_test_add_func("/public/to_qlist", qobject_to_qlist_test); | |
123 | g_test_add_func("/public/destroy", qlist_destroy_test); | |
124 | g_test_add_func("/public/iter", qlist_iter_test); | |
3aa3dcff | 125 | |
91479dd0 | 126 | return g_test_run(); |
3aa3dcff | 127 | } |