Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2019-09-05-v2' into staging
[qemu.git] / tests / check-qlit.c
blobbd6798d91224ba5e9b9d4f5c71c7432a1c87f91c
1 /*
2 * QLit unit-tests.
4 * Copyright (C) 2017 Red Hat Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
12 #include "qapi/qmp/qbool.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qlist.h"
15 #include "qapi/qmp/qlit.h"
16 #include "qapi/qmp/qnum.h"
17 #include "qapi/qmp/qstring.h"
19 static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
20 { "foo", QLIT_QNUM(42) },
21 { "bar", QLIT_QSTR("hello world") },
22 { "baz", QLIT_QNULL },
23 { "bee", QLIT_QLIST(((QLitObject[]) {
24 QLIT_QNUM(43),
25 QLIT_QNUM(44),
26 QLIT_QBOOL(true),
27 { },
28 }))},
29 { },
30 }));
32 static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
33 { "foo", QLIT_QNUM(42) },
34 { },
35 }));
37 static QObject *make_qobject(void)
39 QDict *qdict = qdict_new();
40 QList *list = qlist_new();
42 qdict_put_int(qdict, "foo", 42);
43 qdict_put_str(qdict, "bar", "hello world");
44 qdict_put_null(qdict, "baz");
46 qlist_append_int(list, 43);
47 qlist_append_int(list, 44);
48 qlist_append_bool(list, true);
49 qdict_put(qdict, "bee", list);
51 return QOBJECT(qdict);
54 static void qlit_equal_qobject_test(void)
56 QObject *qobj = make_qobject();
58 g_assert(qlit_equal_qobject(&qlit, qobj));
60 g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
62 qdict_put(qobject_to(QDict, qobj), "bee", qlist_new());
63 g_assert(!qlit_equal_qobject(&qlit, qobj));
65 qobject_unref(qobj);
68 static void qobject_from_qlit_test(void)
70 QObject *obj, *qobj = qobject_from_qlit(&qlit);
71 QDict *qdict;
72 QList *bee;
74 qdict = qobject_to(QDict, qobj);
75 g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
76 g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
77 g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
79 bee = qdict_get_qlist(qdict, "bee");
80 obj = qlist_pop(bee);
81 g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 43);
82 qobject_unref(obj);
83 obj = qlist_pop(bee);
84 g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 44);
85 qobject_unref(obj);
86 obj = qlist_pop(bee);
87 g_assert(qbool_get_bool(qobject_to(QBool, obj)));
88 qobject_unref(obj);
90 qobject_unref(qobj);
93 int main(int argc, char **argv)
95 g_test_init(&argc, &argv, NULL);
97 g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
98 g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
100 return g_test_run();