]> Git Repo - qemu.git/blob - tests/check-qlist.c
qapi: Replace qobject_to_X(o) by qobject_to(X, o)
[qemu.git] / tests / check-qlist.c
1 /*
2  * QList unit-tests.
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <[email protected]>
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 "qemu/osdep.h"
13
14 #include "qapi/qmp/qnum.h"
15 #include "qapi/qmp/qlist.h"
16
17 /*
18  * Public Interface test-cases
19  *
20  * (with some violations to access 'private' data)
21  */
22
23 static void qlist_new_test(void)
24 {
25     QList *qlist;
26
27     qlist = qlist_new();
28     g_assert(qlist != NULL);
29     g_assert(qlist->base.refcnt == 1);
30     g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
31
32     QDECREF(qlist);
33 }
34
35 static void qlist_append_test(void)
36 {
37     QNum *qi;
38     QList *qlist;
39     QListEntry *entry;
40
41     qi = qnum_from_int(42);
42
43     qlist = qlist_new();
44     qlist_append(qlist, qi);
45
46     entry = QTAILQ_FIRST(&qlist->head);
47     g_assert(entry != NULL);
48     g_assert(entry->value == QOBJECT(qi));
49
50     QDECREF(qlist);
51 }
52
53 static void qobject_to_qlist_test(void)
54 {
55     QList *qlist;
56
57     qlist = qlist_new();
58
59     g_assert(qobject_to(QList, QOBJECT(qlist)) == qlist);
60
61     QDECREF(qlist);
62 }
63
64 static int iter_called;
65 static const int iter_max = 42;
66
67 static void iter_func(QObject *obj, void *opaque)
68 {
69     QNum *qi;
70     int64_t val;
71
72     g_assert(opaque == NULL);
73
74     qi = qobject_to(QNum, obj);
75     g_assert(qi != NULL);
76
77     g_assert(qnum_get_try_int(qi, &val));
78     g_assert_cmpint(val, >=, 0);
79     g_assert_cmpint(val, <=, iter_max);
80
81     iter_called++;
82 }
83
84 static void qlist_iter_test(void)
85 {
86     int i;
87     QList *qlist;
88
89     qlist = qlist_new();
90
91     for (i = 0; i < iter_max; i++)
92         qlist_append_int(qlist, i);
93
94     iter_called = 0;
95     qlist_iter(qlist, iter_func, NULL);
96
97     g_assert(iter_called == iter_max);
98
99     QDECREF(qlist);
100 }
101
102 int main(int argc, char **argv)
103 {
104     g_test_init(&argc, &argv, NULL);
105
106     g_test_add_func("/public/new", qlist_new_test);
107     g_test_add_func("/public/append", qlist_append_test);
108     g_test_add_func("/public/to_qlist", qobject_to_qlist_test);
109     g_test_add_func("/public/iter", qlist_iter_test);
110
111     return g_test_run();
112 }
This page took 0.030168 seconds and 4 git commands to generate.