xref: /openbmc/qemu/tests/unit/check-qlit.c (revision 757acb9a8295e8be4a37b2cfc1cd947e357fd29c)
1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth  * QLit unit-tests.
3*da668aa1SThomas Huth  *
4*da668aa1SThomas Huth  * Copyright (C) 2017 Red Hat Inc.
5*da668aa1SThomas Huth  *
6*da668aa1SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7*da668aa1SThomas Huth  * See the COPYING file in the top-level directory.
8*da668aa1SThomas Huth  */
9*da668aa1SThomas Huth 
10*da668aa1SThomas Huth #include "qemu/osdep.h"
11*da668aa1SThomas Huth 
12*da668aa1SThomas Huth #include "qapi/qmp/qbool.h"
13*da668aa1SThomas Huth #include "qapi/qmp/qdict.h"
14*da668aa1SThomas Huth #include "qapi/qmp/qlist.h"
15*da668aa1SThomas Huth #include "qapi/qmp/qlit.h"
16*da668aa1SThomas Huth #include "qapi/qmp/qnum.h"
17*da668aa1SThomas Huth #include "qapi/qmp/qstring.h"
18*da668aa1SThomas Huth 
19*da668aa1SThomas Huth static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
20*da668aa1SThomas Huth     { "foo", QLIT_QNUM(42) },
21*da668aa1SThomas Huth     { "bar", QLIT_QSTR("hello world") },
22*da668aa1SThomas Huth     { "baz", QLIT_QNULL },
23*da668aa1SThomas Huth     { "bee", QLIT_QLIST(((QLitObject[]) {
24*da668aa1SThomas Huth         QLIT_QNUM(43),
25*da668aa1SThomas Huth         QLIT_QNUM(44),
26*da668aa1SThomas Huth         QLIT_QBOOL(true),
27*da668aa1SThomas Huth         { },
28*da668aa1SThomas Huth     }))},
29*da668aa1SThomas Huth     { },
30*da668aa1SThomas Huth }));
31*da668aa1SThomas Huth 
32*da668aa1SThomas Huth static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
33*da668aa1SThomas Huth     { "foo", QLIT_QNUM(42) },
34*da668aa1SThomas Huth     { },
35*da668aa1SThomas Huth }));
36*da668aa1SThomas Huth 
make_qobject(void)37*da668aa1SThomas Huth static QObject *make_qobject(void)
38*da668aa1SThomas Huth {
39*da668aa1SThomas Huth     QDict *qdict = qdict_new();
40*da668aa1SThomas Huth     QList *list = qlist_new();
41*da668aa1SThomas Huth 
42*da668aa1SThomas Huth     qdict_put_int(qdict, "foo", 42);
43*da668aa1SThomas Huth     qdict_put_str(qdict, "bar", "hello world");
44*da668aa1SThomas Huth     qdict_put_null(qdict, "baz");
45*da668aa1SThomas Huth 
46*da668aa1SThomas Huth     qlist_append_int(list, 43);
47*da668aa1SThomas Huth     qlist_append_int(list, 44);
48*da668aa1SThomas Huth     qlist_append_bool(list, true);
49*da668aa1SThomas Huth     qdict_put(qdict, "bee", list);
50*da668aa1SThomas Huth 
51*da668aa1SThomas Huth     return QOBJECT(qdict);
52*da668aa1SThomas Huth }
53*da668aa1SThomas Huth 
qlit_equal_qobject_test(void)54*da668aa1SThomas Huth static void qlit_equal_qobject_test(void)
55*da668aa1SThomas Huth {
56*da668aa1SThomas Huth     QObject *qobj = make_qobject();
57*da668aa1SThomas Huth 
58*da668aa1SThomas Huth     g_assert(qlit_equal_qobject(&qlit, qobj));
59*da668aa1SThomas Huth 
60*da668aa1SThomas Huth     g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
61*da668aa1SThomas Huth 
62*da668aa1SThomas Huth     qdict_put(qobject_to(QDict, qobj), "bee", qlist_new());
63*da668aa1SThomas Huth     g_assert(!qlit_equal_qobject(&qlit, qobj));
64*da668aa1SThomas Huth 
65*da668aa1SThomas Huth     qobject_unref(qobj);
66*da668aa1SThomas Huth }
67*da668aa1SThomas Huth 
qobject_from_qlit_test(void)68*da668aa1SThomas Huth static void qobject_from_qlit_test(void)
69*da668aa1SThomas Huth {
70*da668aa1SThomas Huth     QObject *obj, *qobj = qobject_from_qlit(&qlit);
71*da668aa1SThomas Huth     QDict *qdict;
72*da668aa1SThomas Huth     QList *bee;
73*da668aa1SThomas Huth 
74*da668aa1SThomas Huth     qdict = qobject_to(QDict, qobj);
75*da668aa1SThomas Huth     g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
76*da668aa1SThomas Huth     g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
77*da668aa1SThomas Huth     g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
78*da668aa1SThomas Huth 
79*da668aa1SThomas Huth     bee = qdict_get_qlist(qdict, "bee");
80*da668aa1SThomas Huth     obj = qlist_pop(bee);
81*da668aa1SThomas Huth     g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 43);
82*da668aa1SThomas Huth     qobject_unref(obj);
83*da668aa1SThomas Huth     obj = qlist_pop(bee);
84*da668aa1SThomas Huth     g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 44);
85*da668aa1SThomas Huth     qobject_unref(obj);
86*da668aa1SThomas Huth     obj = qlist_pop(bee);
87*da668aa1SThomas Huth     g_assert(qbool_get_bool(qobject_to(QBool, obj)));
88*da668aa1SThomas Huth     qobject_unref(obj);
89*da668aa1SThomas Huth 
90*da668aa1SThomas Huth     qobject_unref(qobj);
91*da668aa1SThomas Huth }
92*da668aa1SThomas Huth 
main(int argc,char ** argv)93*da668aa1SThomas Huth int main(int argc, char **argv)
94*da668aa1SThomas Huth {
95*da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
96*da668aa1SThomas Huth 
97*da668aa1SThomas Huth     g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
98*da668aa1SThomas Huth     g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
99*da668aa1SThomas Huth 
100*da668aa1SThomas Huth     return g_test_run();
101*da668aa1SThomas Huth }
102