xref: /openbmc/qemu/tests/unit/check-qnull.c (revision 1be5a765c08cee3a9587c8a8d3fc2ea247b13f9c)
1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth  * QNull unit-tests.
3*da668aa1SThomas Huth  *
4*da668aa1SThomas Huth  * Copyright (C) 2016 Red Hat Inc.
5*da668aa1SThomas Huth  *
6*da668aa1SThomas Huth  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7*da668aa1SThomas Huth  * See the COPYING.LIB file in the top-level directory.
8*da668aa1SThomas Huth  */
9*da668aa1SThomas Huth #include "qemu/osdep.h"
10*da668aa1SThomas Huth 
11*da668aa1SThomas Huth #include "qapi/qmp/qnull.h"
12*da668aa1SThomas Huth #include "qapi/qobject-input-visitor.h"
13*da668aa1SThomas Huth #include "qapi/qobject-output-visitor.h"
14*da668aa1SThomas Huth #include "qapi/error.h"
15*da668aa1SThomas Huth 
16*da668aa1SThomas Huth /*
17*da668aa1SThomas Huth  * Public Interface test-cases
18*da668aa1SThomas Huth  *
19*da668aa1SThomas Huth  * (with some violations to access 'private' data)
20*da668aa1SThomas Huth  */
21*da668aa1SThomas Huth 
qnull_ref_test(void)22*da668aa1SThomas Huth static void qnull_ref_test(void)
23*da668aa1SThomas Huth {
24*da668aa1SThomas Huth     QObject *obj;
25*da668aa1SThomas Huth 
26*da668aa1SThomas Huth     g_assert(qnull_.base.refcnt == 1);
27*da668aa1SThomas Huth     obj = QOBJECT(qnull());
28*da668aa1SThomas Huth     g_assert(obj);
29*da668aa1SThomas Huth     g_assert(obj == QOBJECT(&qnull_));
30*da668aa1SThomas Huth     g_assert(qnull_.base.refcnt == 2);
31*da668aa1SThomas Huth     g_assert(qobject_type(obj) == QTYPE_QNULL);
32*da668aa1SThomas Huth     qobject_unref(obj);
33*da668aa1SThomas Huth     g_assert(qnull_.base.refcnt == 1);
34*da668aa1SThomas Huth }
35*da668aa1SThomas Huth 
qnull_visit_test(void)36*da668aa1SThomas Huth static void qnull_visit_test(void)
37*da668aa1SThomas Huth {
38*da668aa1SThomas Huth     QObject *obj;
39*da668aa1SThomas Huth     Visitor *v;
40*da668aa1SThomas Huth     QNull *null;
41*da668aa1SThomas Huth 
42*da668aa1SThomas Huth     /*
43*da668aa1SThomas Huth      * Most tests of interactions between QObject and visitors are in
44*da668aa1SThomas Huth      * test-qmp-*-visitor; but these tests live here because they
45*da668aa1SThomas Huth      * depend on layering violations to check qnull_ refcnt.
46*da668aa1SThomas Huth      */
47*da668aa1SThomas Huth 
48*da668aa1SThomas Huth     g_assert(qnull_.base.refcnt == 1);
49*da668aa1SThomas Huth     obj = QOBJECT(qnull());
50*da668aa1SThomas Huth     v = qobject_input_visitor_new(obj);
51*da668aa1SThomas Huth     qobject_unref(obj);
52*da668aa1SThomas Huth     visit_type_null(v, NULL, &null, &error_abort);
53*da668aa1SThomas Huth     g_assert(obj == QOBJECT(&qnull_));
54*da668aa1SThomas Huth     qobject_unref(null);
55*da668aa1SThomas Huth     visit_free(v);
56*da668aa1SThomas Huth 
57*da668aa1SThomas Huth     null = NULL;
58*da668aa1SThomas Huth     v = qobject_output_visitor_new(&obj);
59*da668aa1SThomas Huth     visit_type_null(v, NULL, &null, &error_abort);
60*da668aa1SThomas Huth     visit_complete(v, &obj);
61*da668aa1SThomas Huth     g_assert(obj == QOBJECT(&qnull_));
62*da668aa1SThomas Huth     qobject_unref(null);
63*da668aa1SThomas Huth     qobject_unref(obj);
64*da668aa1SThomas Huth     visit_free(v);
65*da668aa1SThomas Huth 
66*da668aa1SThomas Huth     g_assert(qnull_.base.refcnt == 1);
67*da668aa1SThomas Huth }
68*da668aa1SThomas Huth 
main(int argc,char ** argv)69*da668aa1SThomas Huth int main(int argc, char **argv)
70*da668aa1SThomas Huth {
71*da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
72*da668aa1SThomas Huth 
73*da668aa1SThomas Huth     g_test_add_func("/public/qnull_ref", qnull_ref_test);
74*da668aa1SThomas Huth     g_test_add_func("/public/qnull_visit", qnull_visit_test);
75*da668aa1SThomas Huth 
76*da668aa1SThomas Huth     return g_test_run();
77*da668aa1SThomas Huth }
78