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
11*407bc4bfSDaniel P. Berrangé #include "qobject/qnull.h"
12b3db211fSDaniel P. Berrange #include "qapi/qobject-input-visitor.h"
13b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
143df016f1SEric Blake #include "qapi/error.h"
157d7a337eSEric Blake
167d7a337eSEric Blake /*
177d7a337eSEric Blake * Public Interface test-cases
187d7a337eSEric Blake *
197d7a337eSEric Blake * (with some violations to access 'private' data)
207d7a337eSEric Blake */
217d7a337eSEric Blake
qnull_ref_test(void)227d7a337eSEric Blake static void qnull_ref_test(void)
237d7a337eSEric Blake {
247d7a337eSEric Blake QObject *obj;
257d7a337eSEric Blake
26006ca09fSMarkus Armbruster g_assert(qnull_.base.refcnt == 1);
27006ca09fSMarkus Armbruster obj = QOBJECT(qnull());
287d7a337eSEric Blake g_assert(obj);
29006ca09fSMarkus Armbruster g_assert(obj == QOBJECT(&qnull_));
30006ca09fSMarkus Armbruster g_assert(qnull_.base.refcnt == 2);
317d7a337eSEric Blake g_assert(qobject_type(obj) == QTYPE_QNULL);
32cb3e7f08SMarc-André Lureau qobject_unref(obj);
33006ca09fSMarkus Armbruster g_assert(qnull_.base.refcnt == 1);
347d7a337eSEric Blake }
357d7a337eSEric Blake
qnull_visit_test(void)367d7a337eSEric Blake static void qnull_visit_test(void)
377d7a337eSEric Blake {
387d7a337eSEric Blake QObject *obj;
39b70ce101SEric Blake Visitor *v;
40d2f95f4dSMarkus Armbruster QNull *null;
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
48006ca09fSMarkus Armbruster g_assert(qnull_.base.refcnt == 1);
49006ca09fSMarkus Armbruster obj = QOBJECT(qnull());
50048abb7bSMarkus Armbruster v = qobject_input_visitor_new(obj);
51cb3e7f08SMarc-André Lureau qobject_unref(obj);
52d2f95f4dSMarkus Armbruster visit_type_null(v, NULL, &null, &error_abort);
53d2f95f4dSMarkus Armbruster g_assert(obj == QOBJECT(&qnull_));
54cb3e7f08SMarc-André Lureau qobject_unref(null);
55b70ce101SEric Blake visit_free(v);
563df016f1SEric Blake
57d2f95f4dSMarkus Armbruster null = NULL;
587d5e199aSDaniel P. Berrange v = qobject_output_visitor_new(&obj);
59d2f95f4dSMarkus Armbruster visit_type_null(v, NULL, &null, &error_abort);
603b098d56SEric Blake visit_complete(v, &obj);
61006ca09fSMarkus Armbruster g_assert(obj == QOBJECT(&qnull_));
62cb3e7f08SMarc-André Lureau qobject_unref(null);
63cb3e7f08SMarc-André Lureau qobject_unref(obj);
643b098d56SEric Blake visit_free(v);
653df016f1SEric Blake
66006ca09fSMarkus Armbruster g_assert(qnull_.base.refcnt == 1);
677d7a337eSEric Blake }
687d7a337eSEric Blake
main(int argc,char ** argv)697d7a337eSEric Blake int main(int argc, char **argv)
707d7a337eSEric Blake {
717d7a337eSEric Blake g_test_init(&argc, &argv, NULL);
727d7a337eSEric Blake
737d7a337eSEric Blake g_test_add_func("/public/qnull_ref", qnull_ref_test);
747d7a337eSEric Blake g_test_add_func("/public/qnull_visit", qnull_visit_test);
757d7a337eSEric Blake
767d7a337eSEric Blake return g_test_run();
777d7a337eSEric Blake }
78