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