qemu/tests/check-qom-interface.c
<<
>>
Prefs
   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 "qemu/osdep.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
  26typedef struct TestIf TestIf;
  27
  28typedef struct TestIfClass {
  29    InterfaceClass parent_class;
  30
  31    uint32_t test;
  32} TestIfClass;
  33
  34static const TypeInfo test_if_info = {
  35    .name          = TYPE_TEST_IF,
  36    .parent        = TYPE_INTERFACE,
  37    .class_size = sizeof(TestIfClass),
  38};
  39
  40#define PATTERN 0xFAFBFCFD
  41
  42static void test_class_init(ObjectClass *oc, void *data)
  43{
  44    TestIfClass *tc = TEST_IF_CLASS(oc);
  45
  46    g_assert(tc);
  47    tc->test = PATTERN;
  48}
  49
  50#define TYPE_DIRECT_IMPL "direct-impl"
  51
  52static const TypeInfo direct_impl_info = {
  53    .name = TYPE_DIRECT_IMPL,
  54    .parent = TYPE_OBJECT,
  55    .class_init = test_class_init,
  56    .interfaces = (InterfaceInfo[]) {
  57        { TYPE_TEST_IF },
  58        { }
  59    }
  60};
  61
  62#define TYPE_INTERMEDIATE_IMPL "intermediate-impl"
  63
  64static const TypeInfo intermediate_impl_info = {
  65    .name = TYPE_INTERMEDIATE_IMPL,
  66    .parent = TYPE_DIRECT_IMPL,
  67};
  68
  69static void test_interface_impl(const char *type)
  70{
  71    Object *obj = object_new(type);
  72    TestIf *iobj = TEST_IF(obj);
  73    TestIfClass *ioc = TEST_IF_GET_CLASS(iobj);
  74
  75    g_assert(iobj);
  76    g_assert(ioc->test == PATTERN);
  77    object_unref(obj);
  78}
  79
  80static void interface_direct_test(void)
  81{
  82    test_interface_impl(TYPE_DIRECT_IMPL);
  83}
  84
  85static void interface_intermediate_test(void)
  86{
  87    test_interface_impl(TYPE_INTERMEDIATE_IMPL);
  88}
  89
  90int main(int argc, char **argv)
  91{
  92    g_test_init(&argc, &argv, NULL);
  93
  94    module_call_init(MODULE_INIT_QOM);
  95    type_register_static(&test_if_info);
  96    type_register_static(&direct_impl_info);
  97    type_register_static(&intermediate_impl_info);
  98
  99    g_test_add_func("/qom/interface/direct_impl", interface_direct_test);
 100    g_test_add_func("/qom/interface/intermediate_impl",
 101                    interface_intermediate_test);
 102
 103    return g_test_run();
 104}
 105