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