summaryrefslogtreecommitdiff
path: root/tests/check-qnull.c
blob: dc906b116e43f5a9fb7826b2fb0faf7fe4b37a90 (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
/*
 * QNull unit-tests.
 *
 * Copyright (C) 2016 Red Hat Inc.
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 */
#include "qemu/osdep.h"

#include "qapi/qmp/qobject.h"
#include "qemu-common.h"
#include "qapi/qmp-input-visitor.h"
#include "qapi/qmp-output-visitor.h"
#include "qapi/error.h"

/*
 * Public Interface test-cases
 *
 * (with some violations to access 'private' data)
 */

static void qnull_ref_test(void)
{
    QObject *obj;

    g_assert(qnull_.refcnt == 1);
    obj = qnull();
    g_assert(obj);
    g_assert(obj == &qnull_);
    g_assert(qnull_.refcnt == 2);
    g_assert(qobject_type(obj) == QTYPE_QNULL);
    qobject_decref(obj);
    g_assert(qnull_.refcnt == 1);
}

static void qnull_visit_test(void)
{
    QObject *obj;
    Visitor *v;

    /*
     * Most tests of interactions between QObject and visitors are in
     * test-qmp-*-visitor; but these tests live here because they
     * depend on layering violations to check qnull_ refcnt.
     */

    g_assert(qnull_.refcnt == 1);
    obj = qnull();
    v = qmp_input_visitor_new(obj, true);
    qobject_decref(obj);
    visit_type_null(v, NULL, &error_abort);
    visit_free(v);

    v = qmp_output_visitor_new(&obj);
    visit_type_null(v, NULL, &error_abort);
    visit_complete(v, &obj);
    g_assert(obj == &qnull_);
    qobject_decref(obj);
    visit_free(v);

    g_assert(qnull_.refcnt == 1);
}

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

    g_test_add_func("/public/qnull_ref", qnull_ref_test);
    g_test_add_func("/public/qnull_visit", qnull_visit_test);

    return g_test_run();
}