2
0

check-qom-interface.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "qom/object.h"
  14. #include "qemu/module.h"
  15. #define TYPE_TEST_IF "test-interface"
  16. #define TEST_IF_CLASS(klass) \
  17. OBJECT_CLASS_CHECK(TestIfClass, (klass), TYPE_TEST_IF)
  18. #define TEST_IF_GET_CLASS(obj) \
  19. OBJECT_GET_CLASS(TestIfClass, (obj), TYPE_TEST_IF)
  20. #define TEST_IF(obj) \
  21. INTERFACE_CHECK(TestIf, (obj), TYPE_TEST_IF)
  22. typedef struct TestIf TestIf;
  23. typedef struct TestIfClass {
  24. InterfaceClass parent_class;
  25. uint32_t test;
  26. } TestIfClass;
  27. static const TypeInfo test_if_info = {
  28. .name = TYPE_TEST_IF,
  29. .parent = TYPE_INTERFACE,
  30. .class_size = sizeof(TestIfClass),
  31. };
  32. #define PATTERN 0xFAFBFCFD
  33. static void test_class_init(ObjectClass *oc, void *data)
  34. {
  35. TestIfClass *tc = TEST_IF_CLASS(oc);
  36. g_assert(tc);
  37. tc->test = PATTERN;
  38. }
  39. #define TYPE_DIRECT_IMPL "direct-impl"
  40. static const TypeInfo direct_impl_info = {
  41. .name = TYPE_DIRECT_IMPL,
  42. .parent = TYPE_OBJECT,
  43. .class_init = test_class_init,
  44. .interfaces = (InterfaceInfo[]) {
  45. { TYPE_TEST_IF },
  46. { }
  47. }
  48. };
  49. #define TYPE_INTERMEDIATE_IMPL "intermediate-impl"
  50. static const TypeInfo intermediate_impl_info = {
  51. .name = TYPE_INTERMEDIATE_IMPL,
  52. .parent = TYPE_DIRECT_IMPL,
  53. };
  54. static void test_interface_impl(const char *type)
  55. {
  56. Object *obj = object_new(type);
  57. TestIf *iobj = TEST_IF(obj);
  58. TestIfClass *ioc = TEST_IF_GET_CLASS(iobj);
  59. g_assert(iobj);
  60. g_assert(ioc->test == PATTERN);
  61. object_unref(obj);
  62. }
  63. static void interface_direct_test(void)
  64. {
  65. test_interface_impl(TYPE_DIRECT_IMPL);
  66. }
  67. static void interface_intermediate_test(void)
  68. {
  69. test_interface_impl(TYPE_INTERMEDIATE_IMPL);
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. g_test_init(&argc, &argv, NULL);
  74. module_call_init(MODULE_INIT_QOM);
  75. type_register_static(&test_if_info);
  76. type_register_static(&direct_impl_info);
  77. type_register_static(&intermediate_impl_info);
  78. g_test_add_func("/qom/interface/direct_impl", interface_direct_test);
  79. g_test_add_func("/qom/interface/intermediate_impl",
  80. interface_intermediate_test);
  81. return g_test_run();
  82. }