xref: /qemu/tests/unit/test-clone-visitor.c (revision 16821fc85b078b08f4e6f1cc3d57fb4c00568a25)
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 {
6854aa3de7SEric 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--) {
7454aa3de7SEric 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 {
102*16821fc8SMarkus Armbruster     UserDefFlatUnion *src, *dst;
103a15fcc3cSEric Blake 
104*16821fc8SMarkus Armbruster     src = g_new0(UserDefFlatUnion, 1);
105*16821fc8SMarkus Armbruster     src->integer = 123;
106*16821fc8SMarkus Armbruster     src->string = g_strdup("abc");
107*16821fc8SMarkus Armbruster     src->enum1 = ENUM_ONE_VALUE1;
108*16821fc8SMarkus Armbruster     src->u.value1.boolean = true;
109a15fcc3cSEric Blake 
110*16821fc8SMarkus Armbruster     dst = QAPI_CLONE(UserDefFlatUnion, src);
111a15fcc3cSEric Blake     g_assert(dst);
112a15fcc3cSEric Blake 
113*16821fc8SMarkus Armbruster     g_assert_cmpint(dst->integer, ==, 123);
114*16821fc8SMarkus Armbruster     g_assert_cmpstr(dst->string, ==, "abc");
115*16821fc8SMarkus Armbruster     g_assert_cmpint(dst->enum1, ==, ENUM_ONE_VALUE1);
116*16821fc8SMarkus Armbruster     g_assert(dst->u.value1.boolean);
117*16821fc8SMarkus Armbruster     g_assert(!dst->u.value1.has_a_b);
118*16821fc8SMarkus Armbruster     g_assert_cmpint(dst->u.value1.a_b, ==, 0);
119*16821fc8SMarkus Armbruster 
120*16821fc8SMarkus Armbruster     qapi_free_UserDefFlatUnion(src);
121*16821fc8SMarkus Armbruster     qapi_free_UserDefFlatUnion(dst);
122a15fcc3cSEric Blake }
123a15fcc3cSEric Blake 
124a15fcc3cSEric Blake static void test_clone_complex2(void)
125a15fcc3cSEric Blake {
126a15fcc3cSEric Blake     WrapAlternate *src, *dst;
127a15fcc3cSEric Blake 
128a15fcc3cSEric Blake     src = g_new0(WrapAlternate, 1);
129a15fcc3cSEric Blake     src->alt = g_new(UserDefAlternate, 1);
130a15fcc3cSEric Blake     src->alt->type = QTYPE_QDICT;
131a15fcc3cSEric Blake     src->alt->u.udfu.integer = 42;
132a15fcc3cSEric Blake     /* Clone intentionally converts NULL into "" for strings */
133a15fcc3cSEric Blake     src->alt->u.udfu.string = NULL;
134a15fcc3cSEric Blake     src->alt->u.udfu.enum1 = ENUM_ONE_VALUE3;
135a15fcc3cSEric Blake     src->alt->u.udfu.u.value3.intb = 99;
136a15fcc3cSEric Blake     src->alt->u.udfu.u.value3.has_a_b = true;
137a15fcc3cSEric Blake     src->alt->u.udfu.u.value3.a_b = true;
138a15fcc3cSEric Blake 
139a15fcc3cSEric Blake     dst = QAPI_CLONE(WrapAlternate, src);
140a15fcc3cSEric Blake     g_assert(dst);
141a15fcc3cSEric Blake     g_assert(dst->alt);
142a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->type, ==, QTYPE_QDICT);
143a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->u.udfu.integer, ==, 42);
144a15fcc3cSEric Blake     g_assert_cmpstr(dst->alt->u.udfu.string, ==, "");
145a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE3);
146a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->u.udfu.u.value3.intb, ==, 99);
147a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->u.udfu.u.value3.has_a_b, ==, true);
148a15fcc3cSEric Blake     g_assert_cmpint(dst->alt->u.udfu.u.value3.a_b, ==, true);
149a15fcc3cSEric Blake 
150a15fcc3cSEric Blake     qapi_free_WrapAlternate(src);
151a15fcc3cSEric Blake     qapi_free_WrapAlternate(dst);
152a15fcc3cSEric Blake }
153a15fcc3cSEric Blake 
154a15fcc3cSEric Blake static void test_clone_complex3(void)
155a15fcc3cSEric Blake {
156a15fcc3cSEric Blake     __org_qemu_x_Struct2 *src, *dst;
157a15fcc3cSEric Blake     __org_qemu_x_Union1List *tmp;
158a15fcc3cSEric Blake 
159a15fcc3cSEric Blake     src = g_new0(__org_qemu_x_Struct2, 1);
160a15fcc3cSEric Blake     tmp = src->array = 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("one");
164a15fcc3cSEric Blake     tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
165a15fcc3cSEric Blake     tmp->value = g_new0(__org_qemu_x_Union1, 1);
166a15fcc3cSEric Blake     tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
167a15fcc3cSEric Blake     tmp->value->u.__org_qemu_x_branch.data = g_strdup("two");
168a15fcc3cSEric Blake     tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
169a15fcc3cSEric Blake     tmp->value = g_new0(__org_qemu_x_Union1, 1);
170a15fcc3cSEric Blake     tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
171a15fcc3cSEric Blake     tmp->value->u.__org_qemu_x_branch.data = g_strdup("three");
172a15fcc3cSEric Blake 
173a15fcc3cSEric Blake     dst = QAPI_CLONE(__org_qemu_x_Struct2, src);
174a15fcc3cSEric Blake     g_assert(dst);
175a15fcc3cSEric Blake     tmp = dst->array;
176a15fcc3cSEric Blake     g_assert(tmp);
177a15fcc3cSEric Blake     g_assert(tmp->value);
178a15fcc3cSEric Blake     g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "one");
179a15fcc3cSEric Blake     tmp = tmp->next;
180a15fcc3cSEric Blake     g_assert(tmp);
181a15fcc3cSEric Blake     g_assert(tmp->value);
182a15fcc3cSEric Blake     g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "two");
183a15fcc3cSEric Blake     tmp = tmp->next;
184a15fcc3cSEric Blake     g_assert(tmp);
185a15fcc3cSEric Blake     g_assert(tmp->value);
186a15fcc3cSEric Blake     g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "three");
187a15fcc3cSEric Blake     tmp = tmp->next;
188a15fcc3cSEric Blake     g_assert(!tmp);
189a15fcc3cSEric Blake 
190a15fcc3cSEric Blake     qapi_free___org_qemu_x_Struct2(src);
191a15fcc3cSEric Blake     qapi_free___org_qemu_x_Struct2(dst);
192a15fcc3cSEric Blake }
193a15fcc3cSEric Blake 
194a15fcc3cSEric Blake int main(int argc, char **argv)
195a15fcc3cSEric Blake {
196a15fcc3cSEric Blake     g_test_init(&argc, &argv, NULL);
197a15fcc3cSEric Blake 
198a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/struct", test_clone_struct);
199a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/alternate", test_clone_alternate);
200b359f4b2SMarkus Armbruster     g_test_add_func("/visitor/clone/list_union", test_clone_list_union);
201a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/empty", test_clone_empty);
202a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/complex1", test_clone_complex1);
203a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/complex2", test_clone_complex2);
204a15fcc3cSEric Blake     g_test_add_func("/visitor/clone/complex3", test_clone_complex3);
205a15fcc3cSEric Blake 
206a15fcc3cSEric Blake     return g_test_run();
207a15fcc3cSEric Blake }
208