]>
Commit | Line | Data |
---|---|---|
a15fcc3c EB |
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 | ||
10 | #include "qemu/osdep.h" | |
11 | #include <glib.h> | |
12 | ||
13 | #include "qemu-common.h" | |
14 | #include "qapi/clone-visitor.h" | |
15 | #include "test-qapi-types.h" | |
16 | #include "test-qapi-visit.h" | |
17 | #include "qapi/qmp/types.h" | |
18 | ||
19 | static void test_clone_struct(void) | |
20 | { | |
21 | UserDefOne *src, *dst; | |
22 | ||
23 | src = g_new0(UserDefOne, 1); | |
24 | src->integer = 42; | |
25 | src->string = g_strdup("Hello"); | |
26 | src->has_enum1 = false; | |
27 | src->enum1 = ENUM_ONE_VALUE2; | |
28 | ||
29 | dst = QAPI_CLONE(UserDefOne, src); | |
30 | g_assert(dst); | |
31 | g_assert_cmpint(dst->integer, ==, 42); | |
32 | g_assert(dst->string != src->string); | |
33 | g_assert_cmpstr(dst->string, ==, "Hello"); | |
34 | g_assert_cmpint(dst->has_enum1, ==, false); | |
35 | /* Our implementation does this, but it is not required: | |
36 | g_assert_cmpint(dst->enum1, ==, ENUM_ONE_VALUE2); | |
37 | */ | |
38 | ||
39 | qapi_free_UserDefOne(src); | |
40 | qapi_free_UserDefOne(dst); | |
41 | } | |
42 | ||
43 | static void test_clone_alternate(void) | |
44 | { | |
8168ca8e | 45 | AltEnumBool *b_src, *s_src, *b_dst, *s_dst; |
a15fcc3c | 46 | |
8168ca8e | 47 | b_src = g_new0(AltEnumBool, 1); |
a15fcc3c EB |
48 | b_src->type = QTYPE_QBOOL; |
49 | b_src->u.b = true; | |
8168ca8e | 50 | s_src = g_new0(AltEnumBool, 1); |
a15fcc3c | 51 | s_src->type = QTYPE_QSTRING; |
8168ca8e | 52 | s_src->u.e = ENUM_ONE_VALUE1; |
a15fcc3c | 53 | |
8168ca8e | 54 | b_dst = QAPI_CLONE(AltEnumBool, b_src); |
a15fcc3c EB |
55 | g_assert(b_dst); |
56 | g_assert_cmpint(b_dst->type, ==, b_src->type); | |
57 | g_assert_cmpint(b_dst->u.b, ==, b_src->u.b); | |
8168ca8e | 58 | s_dst = QAPI_CLONE(AltEnumBool, s_src); |
a15fcc3c EB |
59 | g_assert(s_dst); |
60 | g_assert_cmpint(s_dst->type, ==, s_src->type); | |
8168ca8e | 61 | g_assert_cmpint(s_dst->u.e, ==, s_src->u.e); |
a15fcc3c | 62 | |
8168ca8e MA |
63 | qapi_free_AltEnumBool(b_src); |
64 | qapi_free_AltEnumBool(s_src); | |
65 | qapi_free_AltEnumBool(b_dst); | |
66 | qapi_free_AltEnumBool(s_dst); | |
a15fcc3c EB |
67 | } |
68 | ||
69 | static void test_clone_native_list(void) | |
70 | { | |
71 | uint8List *src, *dst; | |
72 | uint8List *tmp = NULL; | |
73 | int i; | |
74 | ||
75 | /* Build list in reverse */ | |
76 | for (i = 10; i; i--) { | |
77 | src = g_new0(uint8List, 1); | |
78 | src->next = tmp; | |
79 | src->value = i; | |
80 | tmp = src; | |
81 | } | |
82 | ||
83 | dst = QAPI_CLONE(uint8List, src); | |
84 | for (tmp = dst, i = 1; i <= 10; i++) { | |
85 | g_assert(tmp); | |
86 | g_assert_cmpint(tmp->value, ==, i); | |
87 | tmp = tmp->next; | |
88 | } | |
89 | g_assert(!tmp); | |
90 | ||
91 | qapi_free_uint8List(src); | |
92 | qapi_free_uint8List(dst); | |
93 | } | |
94 | ||
95 | static void test_clone_empty(void) | |
96 | { | |
97 | Empty2 *src, *dst; | |
98 | ||
99 | src = g_new0(Empty2, 1); | |
100 | dst = QAPI_CLONE(Empty2, src); | |
101 | g_assert(dst); | |
102 | qapi_free_Empty2(src); | |
103 | qapi_free_Empty2(dst); | |
104 | } | |
105 | ||
106 | static void test_clone_complex1(void) | |
107 | { | |
108 | UserDefNativeListUnion *src, *dst; | |
109 | ||
110 | src = g_new0(UserDefNativeListUnion, 1); | |
111 | src->type = USER_DEF_NATIVE_LIST_UNION_KIND_STRING; | |
112 | ||
113 | dst = QAPI_CLONE(UserDefNativeListUnion, src); | |
114 | g_assert(dst); | |
115 | g_assert_cmpint(dst->type, ==, src->type); | |
116 | g_assert(!dst->u.string.data); | |
117 | ||
118 | qapi_free_UserDefNativeListUnion(src); | |
119 | qapi_free_UserDefNativeListUnion(dst); | |
120 | } | |
121 | ||
122 | static void test_clone_complex2(void) | |
123 | { | |
124 | WrapAlternate *src, *dst; | |
125 | ||
126 | src = g_new0(WrapAlternate, 1); | |
127 | src->alt = g_new(UserDefAlternate, 1); | |
128 | src->alt->type = QTYPE_QDICT; | |
129 | src->alt->u.udfu.integer = 42; | |
130 | /* Clone intentionally converts NULL into "" for strings */ | |
131 | src->alt->u.udfu.string = NULL; | |
132 | src->alt->u.udfu.enum1 = ENUM_ONE_VALUE3; | |
133 | src->alt->u.udfu.u.value3.intb = 99; | |
134 | src->alt->u.udfu.u.value3.has_a_b = true; | |
135 | src->alt->u.udfu.u.value3.a_b = true; | |
136 | ||
137 | dst = QAPI_CLONE(WrapAlternate, src); | |
138 | g_assert(dst); | |
139 | g_assert(dst->alt); | |
140 | g_assert_cmpint(dst->alt->type, ==, QTYPE_QDICT); | |
141 | g_assert_cmpint(dst->alt->u.udfu.integer, ==, 42); | |
142 | g_assert_cmpstr(dst->alt->u.udfu.string, ==, ""); | |
143 | g_assert_cmpint(dst->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE3); | |
144 | g_assert_cmpint(dst->alt->u.udfu.u.value3.intb, ==, 99); | |
145 | g_assert_cmpint(dst->alt->u.udfu.u.value3.has_a_b, ==, true); | |
146 | g_assert_cmpint(dst->alt->u.udfu.u.value3.a_b, ==, true); | |
147 | ||
148 | qapi_free_WrapAlternate(src); | |
149 | qapi_free_WrapAlternate(dst); | |
150 | } | |
151 | ||
152 | static void test_clone_complex3(void) | |
153 | { | |
154 | __org_qemu_x_Struct2 *src, *dst; | |
155 | __org_qemu_x_Union1List *tmp; | |
156 | ||
157 | src = g_new0(__org_qemu_x_Struct2, 1); | |
158 | tmp = src->array = g_new0(__org_qemu_x_Union1List, 1); | |
159 | tmp->value = g_new0(__org_qemu_x_Union1, 1); | |
160 | tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH; | |
161 | tmp->value->u.__org_qemu_x_branch.data = g_strdup("one"); | |
162 | tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1); | |
163 | tmp->value = g_new0(__org_qemu_x_Union1, 1); | |
164 | tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH; | |
165 | tmp->value->u.__org_qemu_x_branch.data = g_strdup("two"); | |
166 | tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1); | |
167 | tmp->value = g_new0(__org_qemu_x_Union1, 1); | |
168 | tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH; | |
169 | tmp->value->u.__org_qemu_x_branch.data = g_strdup("three"); | |
170 | ||
171 | dst = QAPI_CLONE(__org_qemu_x_Struct2, src); | |
172 | g_assert(dst); | |
173 | tmp = dst->array; | |
174 | g_assert(tmp); | |
175 | g_assert(tmp->value); | |
176 | g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "one"); | |
177 | tmp = tmp->next; | |
178 | g_assert(tmp); | |
179 | g_assert(tmp->value); | |
180 | g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "two"); | |
181 | tmp = tmp->next; | |
182 | g_assert(tmp); | |
183 | g_assert(tmp->value); | |
184 | g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "three"); | |
185 | tmp = tmp->next; | |
186 | g_assert(!tmp); | |
187 | ||
188 | qapi_free___org_qemu_x_Struct2(src); | |
189 | qapi_free___org_qemu_x_Struct2(dst); | |
190 | } | |
191 | ||
192 | int main(int argc, char **argv) | |
193 | { | |
194 | g_test_init(&argc, &argv, NULL); | |
195 | ||
196 | g_test_add_func("/visitor/clone/struct", test_clone_struct); | |
197 | g_test_add_func("/visitor/clone/alternate", test_clone_alternate); | |
198 | g_test_add_func("/visitor/clone/native_list", test_clone_native_list); | |
199 | g_test_add_func("/visitor/clone/empty", test_clone_empty); | |
200 | g_test_add_func("/visitor/clone/complex1", test_clone_complex1); | |
201 | g_test_add_func("/visitor/clone/complex2", test_clone_complex2); | |
202 | g_test_add_func("/visitor/clone/complex3", test_clone_complex3); | |
203 | ||
204 | return g_test_run(); | |
205 | } |