aboutsummaryrefslogtreecommitdiff
path: root/tests/check-qlit.c
blob: 836f4a3090c7402fb610b41f9037c7efc3bfe9d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * QLit unit-tests.
 *
 * Copyright (C) 2017 Red Hat Inc.
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"

#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qlit.h"
#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qstring.h"

static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
    { "foo", QLIT_QNUM(42) },
    { "bar", QLIT_QSTR("hello world") },
    { "baz", QLIT_QNULL },
    { "bee", QLIT_QLIST(((QLitObject[]) {
        QLIT_QNUM(43),
        QLIT_QNUM(44),
        QLIT_QBOOL(true),
        { },
    }))},
    { },
}));

static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
    { "foo", QLIT_QNUM(42) },
    { },
}));

static QObject *make_qobject(void)
{
    QDict *qdict = qdict_new();
    QList *list = qlist_new();

    qdict_put_int(qdict, "foo", 42);
    qdict_put_str(qdict, "bar", "hello world");
    qdict_put_null(qdict, "baz");

    qlist_append_int(list, 43);
    qlist_append_int(list, 44);
    qlist_append_bool(list, true);
    qdict_put(qdict, "bee", list);

    return QOBJECT(qdict);
}

static void qlit_equal_qobject_test(void)
{
    QObject *qobj = make_qobject();

    g_assert(qlit_equal_qobject(&qlit, qobj));

    g_assert(!qlit_equal_qobject(&qlit_foo, qobj));

    qdict_put(qobject_to_qdict(qobj), "bee", qlist_new());
    g_assert(!qlit_equal_qobject(&qlit, qobj));

    qobject_decref(qobj);
}

static void qobject_from_qlit_test(void)
{
    QObject *obj, *qobj = qobject_from_qlit(&qlit);
    QDict *qdict;
    QList *bee;

    qdict = qobject_to_qdict(qobj);
    g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
    g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
    g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);

    bee = qdict_get_qlist(qdict, "bee");
    obj = qlist_pop(bee);
    g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 43);
    qobject_decref(obj);
    obj = qlist_pop(bee);
    g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 44);
    qobject_decref(obj);
    obj = qlist_pop(bee);
    g_assert(qbool_get_bool(qobject_to_qbool(obj)));
    qobject_decref(obj);

    qobject_decref(qobj);
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
    g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);

    return g_test_run();
}