Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / check-qom-interface.c
1 /*
2  * QOM interface test.
3  *
4  * Copyright (C) 2013 Red Hat Inc.
5  *
6  * Authors:
7  *  Igor Mammedov <imammedo@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 #include <glib.h>
13
14 #include "qom/object.h"
15 #include "qemu/module.h"
16
17
18 #define TYPE_TEST_IF "test-interface"
19 #define TEST_IF_CLASS(klass) \
20      OBJECT_CLASS_CHECK(TestIfClass, (klass), TYPE_TEST_IF)
21 #define TEST_IF_GET_CLASS(obj) \
22      OBJECT_GET_CLASS(TestIfClass, (obj), TYPE_TEST_IF)
23 #define TEST_IF(obj) \
24      INTERFACE_CHECK(TestIf, (obj), TYPE_TEST_IF)
25
26 typedef struct TestIf {
27     Object parent_obj;
28 } TestIf;
29
30 typedef struct TestIfClass {
31     InterfaceClass parent_class;
32
33     uint32_t test;
34 } TestIfClass;
35
36 static const TypeInfo test_if_info = {
37     .name          = TYPE_TEST_IF,
38     .parent        = TYPE_INTERFACE,
39     .class_size = sizeof(TestIfClass),
40 };
41
42 #define PATTERN 0xFAFBFCFD
43
44 static void test_class_init(ObjectClass *oc, void *data)
45 {
46     TestIfClass *tc = TEST_IF_CLASS(oc);
47
48     g_assert(tc);
49     tc->test = PATTERN;
50 }
51
52 #define TYPE_DIRECT_IMPL "direct-impl"
53
54 static const TypeInfo direct_impl_info = {
55     .name = TYPE_DIRECT_IMPL,
56     .parent = TYPE_OBJECT,
57     .class_init = test_class_init,
58     .interfaces = (InterfaceInfo[]) {
59         { TYPE_TEST_IF },
60         { }
61     }
62 };
63
64 #define TYPE_INTERMEDIATE_IMPL "intermediate-impl"
65
66 static const TypeInfo intermediate_impl_info = {
67     .name = TYPE_INTERMEDIATE_IMPL,
68     .parent = TYPE_DIRECT_IMPL,
69 };
70
71 static void test_interface_impl(const char *type)
72 {
73     Object *obj = object_new(type);
74     TestIf *iobj = TEST_IF(obj);
75     TestIfClass *ioc = TEST_IF_GET_CLASS(iobj);
76
77     g_assert(iobj);
78     g_assert(ioc->test == PATTERN);
79 }
80
81 static void interface_direct_test(void)
82 {
83     test_interface_impl(TYPE_DIRECT_IMPL);
84 }
85
86 static void interface_intermediate_test(void)
87 {
88     test_interface_impl(TYPE_INTERMEDIATE_IMPL);
89 }
90
91 int main(int argc, char **argv)
92 {
93     g_test_init(&argc, &argv, NULL);
94
95     module_call_init(MODULE_INIT_QOM);
96     type_register_static(&test_if_info);
97     type_register_static(&direct_impl_info);
98     type_register_static(&intermediate_impl_info);
99
100     g_test_add_func("/qom/interface/direct_impl", interface_direct_test);
101     g_test_add_func("/qom/interface/intermediate_impl",
102                     interface_intermediate_test);
103
104     return g_test_run();
105 }