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