test-clone-visitor.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * QAPI Clone Visitor unit-tests.
  3. *
  4. * Copyright (C) 2016 Red Hat Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu-common.h"
  11. #include "qapi/clone-visitor.h"
  12. #include "test-qapi-visit.h"
  13. static void test_clone_struct(void)
  14. {
  15. UserDefOne *src, *dst;
  16. src = g_new0(UserDefOne, 1);
  17. src->integer = 42;
  18. src->string = g_strdup("Hello");
  19. src->has_enum1 = false;
  20. src->enum1 = ENUM_ONE_VALUE2;
  21. dst = QAPI_CLONE(UserDefOne, src);
  22. g_assert(dst);
  23. g_assert_cmpint(dst->integer, ==, 42);
  24. g_assert(dst->string != src->string);
  25. g_assert_cmpstr(dst->string, ==, "Hello");
  26. g_assert_cmpint(dst->has_enum1, ==, false);
  27. /* Our implementation does this, but it is not required:
  28. g_assert_cmpint(dst->enum1, ==, ENUM_ONE_VALUE2);
  29. */
  30. qapi_free_UserDefOne(src);
  31. qapi_free_UserDefOne(dst);
  32. }
  33. static void test_clone_alternate(void)
  34. {
  35. AltEnumBool *b_src, *s_src, *b_dst, *s_dst;
  36. b_src = g_new0(AltEnumBool, 1);
  37. b_src->type = QTYPE_QBOOL;
  38. b_src->u.b = true;
  39. s_src = g_new0(AltEnumBool, 1);
  40. s_src->type = QTYPE_QSTRING;
  41. s_src->u.e = ENUM_ONE_VALUE1;
  42. b_dst = QAPI_CLONE(AltEnumBool, b_src);
  43. g_assert(b_dst);
  44. g_assert_cmpint(b_dst->type, ==, b_src->type);
  45. g_assert_cmpint(b_dst->u.b, ==, b_src->u.b);
  46. s_dst = QAPI_CLONE(AltEnumBool, s_src);
  47. g_assert(s_dst);
  48. g_assert_cmpint(s_dst->type, ==, s_src->type);
  49. g_assert_cmpint(s_dst->u.e, ==, s_src->u.e);
  50. qapi_free_AltEnumBool(b_src);
  51. qapi_free_AltEnumBool(s_src);
  52. qapi_free_AltEnumBool(b_dst);
  53. qapi_free_AltEnumBool(s_dst);
  54. }
  55. static void test_clone_list_union(void)
  56. {
  57. uint8List *src, *dst;
  58. uint8List *tmp = NULL;
  59. int i;
  60. /* Build list in reverse */
  61. for (i = 10; i; i--) {
  62. src = g_new0(uint8List, 1);
  63. src->next = tmp;
  64. src->value = i;
  65. tmp = src;
  66. }
  67. dst = QAPI_CLONE(uint8List, src);
  68. for (tmp = dst, i = 1; i <= 10; i++) {
  69. g_assert(tmp);
  70. g_assert_cmpint(tmp->value, ==, i);
  71. tmp = tmp->next;
  72. }
  73. g_assert(!tmp);
  74. qapi_free_uint8List(src);
  75. qapi_free_uint8List(dst);
  76. }
  77. static void test_clone_empty(void)
  78. {
  79. Empty2 *src, *dst;
  80. src = g_new0(Empty2, 1);
  81. dst = QAPI_CLONE(Empty2, src);
  82. g_assert(dst);
  83. qapi_free_Empty2(src);
  84. qapi_free_Empty2(dst);
  85. }
  86. static void test_clone_complex1(void)
  87. {
  88. UserDefListUnion *src, *dst;
  89. src = g_new0(UserDefListUnion, 1);
  90. src->type = USER_DEF_LIST_UNION_KIND_STRING;
  91. dst = QAPI_CLONE(UserDefListUnion, src);
  92. g_assert(dst);
  93. g_assert_cmpint(dst->type, ==, src->type);
  94. g_assert(!dst->u.string.data);
  95. qapi_free_UserDefListUnion(src);
  96. qapi_free_UserDefListUnion(dst);
  97. }
  98. static void test_clone_complex2(void)
  99. {
  100. WrapAlternate *src, *dst;
  101. src = g_new0(WrapAlternate, 1);
  102. src->alt = g_new(UserDefAlternate, 1);
  103. src->alt->type = QTYPE_QDICT;
  104. src->alt->u.udfu.integer = 42;
  105. /* Clone intentionally converts NULL into "" for strings */
  106. src->alt->u.udfu.string = NULL;
  107. src->alt->u.udfu.enum1 = ENUM_ONE_VALUE3;
  108. src->alt->u.udfu.u.value3.intb = 99;
  109. src->alt->u.udfu.u.value3.has_a_b = true;
  110. src->alt->u.udfu.u.value3.a_b = true;
  111. dst = QAPI_CLONE(WrapAlternate, src);
  112. g_assert(dst);
  113. g_assert(dst->alt);
  114. g_assert_cmpint(dst->alt->type, ==, QTYPE_QDICT);
  115. g_assert_cmpint(dst->alt->u.udfu.integer, ==, 42);
  116. g_assert_cmpstr(dst->alt->u.udfu.string, ==, "");
  117. g_assert_cmpint(dst->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE3);
  118. g_assert_cmpint(dst->alt->u.udfu.u.value3.intb, ==, 99);
  119. g_assert_cmpint(dst->alt->u.udfu.u.value3.has_a_b, ==, true);
  120. g_assert_cmpint(dst->alt->u.udfu.u.value3.a_b, ==, true);
  121. qapi_free_WrapAlternate(src);
  122. qapi_free_WrapAlternate(dst);
  123. }
  124. static void test_clone_complex3(void)
  125. {
  126. __org_qemu_x_Struct2 *src, *dst;
  127. __org_qemu_x_Union1List *tmp;
  128. src = g_new0(__org_qemu_x_Struct2, 1);
  129. tmp = src->array = g_new0(__org_qemu_x_Union1List, 1);
  130. tmp->value = g_new0(__org_qemu_x_Union1, 1);
  131. tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
  132. tmp->value->u.__org_qemu_x_branch.data = g_strdup("one");
  133. tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
  134. tmp->value = g_new0(__org_qemu_x_Union1, 1);
  135. tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
  136. tmp->value->u.__org_qemu_x_branch.data = g_strdup("two");
  137. tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
  138. tmp->value = g_new0(__org_qemu_x_Union1, 1);
  139. tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
  140. tmp->value->u.__org_qemu_x_branch.data = g_strdup("three");
  141. dst = QAPI_CLONE(__org_qemu_x_Struct2, src);
  142. g_assert(dst);
  143. tmp = dst->array;
  144. g_assert(tmp);
  145. g_assert(tmp->value);
  146. g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "one");
  147. tmp = tmp->next;
  148. g_assert(tmp);
  149. g_assert(tmp->value);
  150. g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "two");
  151. tmp = tmp->next;
  152. g_assert(tmp);
  153. g_assert(tmp->value);
  154. g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "three");
  155. tmp = tmp->next;
  156. g_assert(!tmp);
  157. qapi_free___org_qemu_x_Struct2(src);
  158. qapi_free___org_qemu_x_Struct2(dst);
  159. }
  160. int main(int argc, char **argv)
  161. {
  162. g_test_init(&argc, &argv, NULL);
  163. g_test_add_func("/visitor/clone/struct", test_clone_struct);
  164. g_test_add_func("/visitor/clone/alternate", test_clone_alternate);
  165. g_test_add_func("/visitor/clone/list_union", test_clone_list_union);
  166. g_test_add_func("/visitor/clone/empty", test_clone_empty);
  167. g_test_add_func("/visitor/clone/complex1", test_clone_complex1);
  168. g_test_add_func("/visitor/clone/complex2", test_clone_complex2);
  169. g_test_add_func("/visitor/clone/complex3", test_clone_complex3);
  170. return g_test_run();
  171. }