qapi: Improve reporting of invalid flags
[qemu/kevin.git] / tests / test-clone-visitor.c
blob5e1e8b2f5e8a9aa1eccfc3428ef899ffa32452a1
1 /*
2 * QAPI Clone Visitor unit-tests.
4 * Copyright (C) 2016 Red Hat Inc.
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 */
10 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "qapi/clone-visitor.h"
14 #include "test-qapi-visit.h"
16 static void test_clone_struct(void)
18 UserDefOne *src, *dst;
20 src = g_new0(UserDefOne, 1);
21 src->integer = 42;
22 src->string = g_strdup("Hello");
23 src->has_enum1 = false;
24 src->enum1 = ENUM_ONE_VALUE2;
26 dst = QAPI_CLONE(UserDefOne, src);
27 g_assert(dst);
28 g_assert_cmpint(dst->integer, ==, 42);
29 g_assert(dst->string != src->string);
30 g_assert_cmpstr(dst->string, ==, "Hello");
31 g_assert_cmpint(dst->has_enum1, ==, false);
32 /* Our implementation does this, but it is not required:
33 g_assert_cmpint(dst->enum1, ==, ENUM_ONE_VALUE2);
36 qapi_free_UserDefOne(src);
37 qapi_free_UserDefOne(dst);
40 static void test_clone_alternate(void)
42 AltEnumBool *b_src, *s_src, *b_dst, *s_dst;
44 b_src = g_new0(AltEnumBool, 1);
45 b_src->type = QTYPE_QBOOL;
46 b_src->u.b = true;
47 s_src = g_new0(AltEnumBool, 1);
48 s_src->type = QTYPE_QSTRING;
49 s_src->u.e = ENUM_ONE_VALUE1;
51 b_dst = QAPI_CLONE(AltEnumBool, b_src);
52 g_assert(b_dst);
53 g_assert_cmpint(b_dst->type, ==, b_src->type);
54 g_assert_cmpint(b_dst->u.b, ==, b_src->u.b);
55 s_dst = QAPI_CLONE(AltEnumBool, s_src);
56 g_assert(s_dst);
57 g_assert_cmpint(s_dst->type, ==, s_src->type);
58 g_assert_cmpint(s_dst->u.e, ==, s_src->u.e);
60 qapi_free_AltEnumBool(b_src);
61 qapi_free_AltEnumBool(s_src);
62 qapi_free_AltEnumBool(b_dst);
63 qapi_free_AltEnumBool(s_dst);
66 static void test_clone_list_union(void)
68 uint8List *src, *dst;
69 uint8List *tmp = NULL;
70 int i;
72 /* Build list in reverse */
73 for (i = 10; i; i--) {
74 src = g_new0(uint8List, 1);
75 src->next = tmp;
76 src->value = i;
77 tmp = src;
80 dst = QAPI_CLONE(uint8List, src);
81 for (tmp = dst, i = 1; i <= 10; i++) {
82 g_assert(tmp);
83 g_assert_cmpint(tmp->value, ==, i);
84 tmp = tmp->next;
86 g_assert(!tmp);
88 qapi_free_uint8List(src);
89 qapi_free_uint8List(dst);
92 static void test_clone_empty(void)
94 Empty2 *src, *dst;
96 src = g_new0(Empty2, 1);
97 dst = QAPI_CLONE(Empty2, src);
98 g_assert(dst);
99 qapi_free_Empty2(src);
100 qapi_free_Empty2(dst);
103 static void test_clone_complex1(void)
105 UserDefListUnion *src, *dst;
107 src = g_new0(UserDefListUnion, 1);
108 src->type = USER_DEF_LIST_UNION_KIND_STRING;
110 dst = QAPI_CLONE(UserDefListUnion, src);
111 g_assert(dst);
112 g_assert_cmpint(dst->type, ==, src->type);
113 g_assert(!dst->u.string.data);
115 qapi_free_UserDefListUnion(src);
116 qapi_free_UserDefListUnion(dst);
119 static void test_clone_complex2(void)
121 WrapAlternate *src, *dst;
123 src = g_new0(WrapAlternate, 1);
124 src->alt = g_new(UserDefAlternate, 1);
125 src->alt->type = QTYPE_QDICT;
126 src->alt->u.udfu.integer = 42;
127 /* Clone intentionally converts NULL into "" for strings */
128 src->alt->u.udfu.string = NULL;
129 src->alt->u.udfu.enum1 = ENUM_ONE_VALUE3;
130 src->alt->u.udfu.u.value3.intb = 99;
131 src->alt->u.udfu.u.value3.has_a_b = true;
132 src->alt->u.udfu.u.value3.a_b = true;
134 dst = QAPI_CLONE(WrapAlternate, src);
135 g_assert(dst);
136 g_assert(dst->alt);
137 g_assert_cmpint(dst->alt->type, ==, QTYPE_QDICT);
138 g_assert_cmpint(dst->alt->u.udfu.integer, ==, 42);
139 g_assert_cmpstr(dst->alt->u.udfu.string, ==, "");
140 g_assert_cmpint(dst->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE3);
141 g_assert_cmpint(dst->alt->u.udfu.u.value3.intb, ==, 99);
142 g_assert_cmpint(dst->alt->u.udfu.u.value3.has_a_b, ==, true);
143 g_assert_cmpint(dst->alt->u.udfu.u.value3.a_b, ==, true);
145 qapi_free_WrapAlternate(src);
146 qapi_free_WrapAlternate(dst);
149 static void test_clone_complex3(void)
151 __org_qemu_x_Struct2 *src, *dst;
152 __org_qemu_x_Union1List *tmp;
154 src = g_new0(__org_qemu_x_Struct2, 1);
155 tmp = src->array = g_new0(__org_qemu_x_Union1List, 1);
156 tmp->value = g_new0(__org_qemu_x_Union1, 1);
157 tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
158 tmp->value->u.__org_qemu_x_branch.data = g_strdup("one");
159 tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
160 tmp->value = g_new0(__org_qemu_x_Union1, 1);
161 tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
162 tmp->value->u.__org_qemu_x_branch.data = g_strdup("two");
163 tmp = tmp->next = g_new0(__org_qemu_x_Union1List, 1);
164 tmp->value = g_new0(__org_qemu_x_Union1, 1);
165 tmp->value->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
166 tmp->value->u.__org_qemu_x_branch.data = g_strdup("three");
168 dst = QAPI_CLONE(__org_qemu_x_Struct2, src);
169 g_assert(dst);
170 tmp = dst->array;
171 g_assert(tmp);
172 g_assert(tmp->value);
173 g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "one");
174 tmp = tmp->next;
175 g_assert(tmp);
176 g_assert(tmp->value);
177 g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "two");
178 tmp = tmp->next;
179 g_assert(tmp);
180 g_assert(tmp->value);
181 g_assert_cmpstr(tmp->value->u.__org_qemu_x_branch.data, ==, "three");
182 tmp = tmp->next;
183 g_assert(!tmp);
185 qapi_free___org_qemu_x_Struct2(src);
186 qapi_free___org_qemu_x_Struct2(dst);
189 int main(int argc, char **argv)
191 g_test_init(&argc, &argv, NULL);
193 g_test_add_func("/visitor/clone/struct", test_clone_struct);
194 g_test_add_func("/visitor/clone/alternate", test_clone_alternate);
195 g_test_add_func("/visitor/clone/list_union", test_clone_list_union);
196 g_test_add_func("/visitor/clone/empty", test_clone_empty);
197 g_test_add_func("/visitor/clone/complex1", test_clone_complex1);
198 g_test_add_func("/visitor/clone/complex2", test_clone_complex2);
199 g_test_add_func("/visitor/clone/complex3", test_clone_complex3);
201 return g_test_run();