blob: bd6798d91224ba5e9b9d4f5c71c7432a1c87f91c [file] [log] [blame]
Marc-André Lureau382176b2017-08-25 12:59:09 +02001/*
2 * QLit unit-tests.
3 *
4 * Copyright (C) 2017 Red Hat Inc.
5 *
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 */
9
10#include "qemu/osdep.h"
11
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010012#include "qapi/qmp/qbool.h"
Marc-André Lureau382176b2017-08-25 12:59:09 +020013#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010014#include "qapi/qmp/qlist.h"
Marc-André Lureau382176b2017-08-25 12:59:09 +020015#include "qapi/qmp/qlit.h"
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010016#include "qapi/qmp/qnum.h"
Marc-André Lureau382176b2017-08-25 12:59:09 +020017#include "qapi/qmp/qstring.h"
18
19static 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}));
31
Marc-André Lureau6da8a7a2017-08-25 12:59:10 +020032static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
33 { "foo", QLIT_QNUM(42) },
34 { },
35}));
36
Marc-André Lureau382176b2017-08-25 12:59:09 +020037static QObject *make_qobject(void)
38{
39 QDict *qdict = qdict_new();
40 QList *list = qlist_new();
41
42 qdict_put_int(qdict, "foo", 42);
43 qdict_put_str(qdict, "bar", "hello world");
44 qdict_put_null(qdict, "baz");
45
46 qlist_append_int(list, 43);
47 qlist_append_int(list, 44);
48 qlist_append_bool(list, true);
49 qdict_put(qdict, "bee", list);
50
51 return QOBJECT(qdict);
52}
53
54static void qlit_equal_qobject_test(void)
55{
56 QObject *qobj = make_qobject();
57
58 g_assert(qlit_equal_qobject(&qlit, qobj));
59
Marc-André Lureau6da8a7a2017-08-25 12:59:10 +020060 g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
61
Max Reitz7dc847e2018-02-24 16:40:29 +010062 qdict_put(qobject_to(QDict, qobj), "bee", qlist_new());
Marc-André Lureaucbb65402017-08-25 12:59:11 +020063 g_assert(!qlit_equal_qobject(&qlit, qobj));
64
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020065 qobject_unref(qobj);
Marc-André Lureau382176b2017-08-25 12:59:09 +020066}
67
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010068static void qobject_from_qlit_test(void)
69{
70 QObject *obj, *qobj = qobject_from_qlit(&qlit);
71 QDict *qdict;
72 QList *bee;
73
Max Reitz7dc847e2018-02-24 16:40:29 +010074 qdict = qobject_to(QDict, qobj);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010075 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);
78
79 bee = qdict_get_qlist(qdict, "bee");
80 obj = qlist_pop(bee);
Max Reitz7dc847e2018-02-24 16:40:29 +010081 g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 43);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020082 qobject_unref(obj);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010083 obj = qlist_pop(bee);
Max Reitz7dc847e2018-02-24 16:40:29 +010084 g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 44);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020085 qobject_unref(obj);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010086 obj = qlist_pop(bee);
Max Reitz7dc847e2018-02-24 16:40:29 +010087 g_assert(qbool_get_bool(qobject_to(QBool, obj)));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020088 qobject_unref(obj);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010089
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020090 qobject_unref(qobj);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010091}
92
Marc-André Lureau382176b2017-08-25 12:59:09 +020093int main(int argc, char **argv)
94{
95 g_test_init(&argc, &argv, NULL);
96
97 g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
Marc-André Lureau3cf42b82018-03-05 18:29:50 +010098 g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
Marc-André Lureau382176b2017-08-25 12:59:09 +020099
100 return g_test_run();
101}