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