summaryrefslogtreecommitdiffstats
path: root/src/tests/check-qom-interface.c
blob: f06380ef14d810c3743fc7ca8c1b7bb9acc97ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * QOM interface test.
 *
 * Copyright (C) 2013 Red Hat Inc.
 *
 * Authors:
 *  Igor Mammedov <imammedo@redhat.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 */
#include <glib.h>

#include "qom/object.h"
#include "qemu/module.h"


#define TYPE_TEST_IF "test-interface"
#define TEST_IF_CLASS(klass) \
     OBJECT_CLASS_CHECK(TestIfClass, (klass), TYPE_TEST_IF)
#define TEST_IF_GET_CLASS(obj) \
     OBJECT_GET_CLASS(TestIfClass, (obj), TYPE_TEST_IF)
#define TEST_IF(obj) \
     INTERFACE_CHECK(TestIf, (obj), TYPE_TEST_IF)

typedef struct TestIf {
    Object parent_obj;
} TestIf;

typedef struct TestIfClass {
    InterfaceClass parent_class;

    uint32_t test;
} TestIfClass;

static const TypeInfo test_if_info = {
    .name          = TYPE_TEST_IF,
    .parent        = TYPE_INTERFACE,
    .class_size = sizeof(TestIfClass),
};

#define PATTERN 0xFAFBFCFD

static void test_class_init(ObjectClass *oc, void *data)
{
    TestIfClass *tc = TEST_IF_CLASS(oc);

    g_assert(tc);
    tc->test = PATTERN;
}

#define TYPE_DIRECT_IMPL "direct-impl"

static const TypeInfo direct_impl_info = {
    .name = TYPE_DIRECT_IMPL,
    .parent = TYPE_OBJECT,
    .class_init = test_class_init,
    .interfaces = (InterfaceInfo[]) {
        { TYPE_TEST_IF },
        { }
    }
};

#define TYPE_INTERMEDIATE_IMPL "intermediate-impl"

static const TypeInfo intermediate_impl_info = {
    .name = TYPE_INTERMEDIATE_IMPL,
    .parent = TYPE_DIRECT_IMPL,
};

static void test_interface_impl(const char *type)
{
    Object *obj = object_new(type);
    TestIf *iobj = TEST_IF(obj);
    TestIfClass *ioc = TEST_IF_GET_CLASS(iobj);

    g_assert(iobj);
    g_assert(ioc->test == PATTERN);
}

static void interface_direct_test(void)
{
    test_interface_impl(TYPE_DIRECT_IMPL);
}

static void interface_intermediate_test(void)
{
    test_interface_impl(TYPE_INTERMEDIATE_IMPL);
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    module_call_init(MODULE_INIT_QOM);
    type_register_static(&test_if_info);
    type_register_static(&direct_impl_info);
    type_register_static(&intermediate_impl_info);

    g_test_add_func("/qom/interface/direct_impl", interface_direct_test);
    g_test_add_func("/qom/interface/intermediate_impl",
                    interface_intermediate_test);

    return g_test_run();
}
OpenPOWER on IntegriCloud