xref: /qemu/tests/unit/check-qnull.c (revision 7d5e199ade76c53ec316ab6779800581bb47c50a)
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 
117d7a337eSEric Blake #include "qapi/qmp/qobject.h"
127d7a337eSEric Blake #include "qemu-common.h"
13b3db211fSDaniel P. Berrange #include "qapi/qobject-input-visitor.h"
14b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
153df016f1SEric Blake #include "qapi/error.h"
167d7a337eSEric Blake 
177d7a337eSEric Blake /*
187d7a337eSEric Blake  * Public Interface test-cases
197d7a337eSEric Blake  *
207d7a337eSEric Blake  * (with some violations to access 'private' data)
217d7a337eSEric Blake  */
227d7a337eSEric Blake 
237d7a337eSEric Blake static void qnull_ref_test(void)
247d7a337eSEric Blake {
257d7a337eSEric Blake     QObject *obj;
267d7a337eSEric Blake 
277d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
287d7a337eSEric Blake     obj = qnull();
297d7a337eSEric Blake     g_assert(obj);
307d7a337eSEric Blake     g_assert(obj == &qnull_);
317d7a337eSEric Blake     g_assert(qnull_.refcnt == 2);
327d7a337eSEric Blake     g_assert(qobject_type(obj) == QTYPE_QNULL);
337d7a337eSEric Blake     qobject_decref(obj);
347d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
357d7a337eSEric Blake }
367d7a337eSEric Blake 
377d7a337eSEric Blake static void qnull_visit_test(void)
387d7a337eSEric Blake {
397d7a337eSEric Blake     QObject *obj;
40b70ce101SEric Blake     Visitor *v;
417d7a337eSEric Blake 
427d7a337eSEric Blake     /*
437d7a337eSEric Blake      * Most tests of interactions between QObject and visitors are in
447d7a337eSEric Blake      * test-qmp-*-visitor; but these tests live here because they
457d7a337eSEric Blake      * depend on layering violations to check qnull_ refcnt.
467d7a337eSEric Blake      */
477d7a337eSEric Blake 
487d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
493df016f1SEric Blake     obj = qnull();
5009e68369SDaniel P. Berrange     v = qobject_input_visitor_new(obj, true);
513df016f1SEric Blake     qobject_decref(obj);
52b70ce101SEric Blake     visit_type_null(v, NULL, &error_abort);
53b70ce101SEric Blake     visit_free(v);
543df016f1SEric Blake 
55*7d5e199aSDaniel P. Berrange     v = qobject_output_visitor_new(&obj);
563b098d56SEric Blake     visit_type_null(v, NULL, &error_abort);
573b098d56SEric Blake     visit_complete(v, &obj);
587d7a337eSEric Blake     g_assert(obj == &qnull_);
597d7a337eSEric Blake     qobject_decref(obj);
603b098d56SEric Blake     visit_free(v);
613df016f1SEric Blake 
627d7a337eSEric Blake     g_assert(qnull_.refcnt == 1);
637d7a337eSEric Blake }
647d7a337eSEric Blake 
657d7a337eSEric Blake int main(int argc, char **argv)
667d7a337eSEric Blake {
677d7a337eSEric Blake     g_test_init(&argc, &argv, NULL);
687d7a337eSEric Blake 
697d7a337eSEric Blake     g_test_add_func("/public/qnull_ref", qnull_ref_test);
707d7a337eSEric Blake     g_test_add_func("/public/qnull_visit", qnull_visit_test);
717d7a337eSEric Blake 
727d7a337eSEric Blake     return g_test_run();
737d7a337eSEric Blake }
74