xref: /qemu/tests/unit/check-qnull.c (revision 3df016f185521f8dfa5bd89168722887156405c7)
17d7a337eSEric Blake /*
27d7a337eSEric Blake  * QNull unit-tests.
37d7a337eSEric Blake  *
47d7a337eSEric Blake  * Copyright (C) 2016 Red Hat Inc.
57d7a337eSEric Blake  *
67d7a337eSEric Blake  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
77d7a337eSEric Blake  * See the COPYING.LIB file in the top-level directory.
87d7a337eSEric Blake  */
97d7a337eSEric Blake #include "qemu/osdep.h"
107d7a337eSEric Blake #include <glib.h>
117d7a337eSEric Blake 
127d7a337eSEric Blake #include "qapi/qmp/qobject.h"
137d7a337eSEric Blake #include "qemu-common.h"
14*3df016f1SEric Blake #include "qapi/qmp-input-visitor.h"
157d7a337eSEric Blake #include "qapi/qmp-output-visitor.h"
16*3df016f1SEric Blake #include "qapi/error.h"
177d7a337eSEric Blake 
187d7a337eSEric Blake /*
197d7a337eSEric Blake  * Public Interface test-cases
207d7a337eSEric Blake  *
217d7a337eSEric Blake  * (with some violations to access 'private' data)
227d7a337eSEric Blake  */
237d7a337eSEric Blake 
247d7a337eSEric Blake static void qnull_ref_test(void)
257d7a337eSEric Blake {
267d7a337eSEric Blake     QObject *obj;
277d7a337eSEric Blake 
287d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
297d7a337eSEric Blake     obj = qnull();
307d7a337eSEric Blake     g_assert(obj);
317d7a337eSEric Blake     g_assert(obj == &qnull_);
327d7a337eSEric Blake     g_assert(qnull_.refcnt == 2);
337d7a337eSEric Blake     g_assert(qobject_type(obj) == QTYPE_QNULL);
347d7a337eSEric Blake     qobject_decref(obj);
357d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
367d7a337eSEric Blake }
377d7a337eSEric Blake 
387d7a337eSEric Blake static void qnull_visit_test(void)
397d7a337eSEric Blake {
407d7a337eSEric Blake     QObject *obj;
417d7a337eSEric Blake     QmpOutputVisitor *qov;
42*3df016f1SEric Blake     QmpInputVisitor *qiv;
437d7a337eSEric Blake 
447d7a337eSEric Blake     /*
457d7a337eSEric Blake      * Most tests of interactions between QObject and visitors are in
467d7a337eSEric Blake      * test-qmp-*-visitor; but these tests live here because they
477d7a337eSEric Blake      * depend on layering violations to check qnull_ refcnt.
487d7a337eSEric Blake      */
497d7a337eSEric Blake 
507d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
51*3df016f1SEric Blake     obj = qnull();
52*3df016f1SEric Blake     qiv = qmp_input_visitor_new(obj, true);
53*3df016f1SEric Blake     qobject_decref(obj);
54*3df016f1SEric Blake     visit_type_null(qmp_input_get_visitor(qiv), NULL, &error_abort);
55*3df016f1SEric Blake     qmp_input_visitor_cleanup(qiv);
56*3df016f1SEric Blake 
577d7a337eSEric Blake     qov = qmp_output_visitor_new();
58*3df016f1SEric Blake     visit_type_null(qmp_output_get_visitor(qov), NULL, &error_abort);
597d7a337eSEric Blake     obj = qmp_output_get_qobject(qov);
607d7a337eSEric Blake     g_assert(obj == &qnull_);
617d7a337eSEric Blake     qobject_decref(obj);
627d7a337eSEric Blake     qmp_output_visitor_cleanup(qov);
63*3df016f1SEric Blake 
647d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
657d7a337eSEric Blake }
667d7a337eSEric Blake 
677d7a337eSEric Blake int main(int argc, char **argv)
687d7a337eSEric Blake {
697d7a337eSEric Blake     g_test_init(&argc, &argv, NULL);
707d7a337eSEric Blake 
717d7a337eSEric Blake     g_test_add_func("/public/qnull_ref", qnull_ref_test);
727d7a337eSEric Blake     g_test_add_func("/public/qnull_visit", qnull_visit_test);
737d7a337eSEric Blake 
747d7a337eSEric Blake     return g_test_run();
757d7a337eSEric Blake }
76