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.
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);
22 src
->string
= g_strdup("Hello");
23 src
->has_enum1
= false;
24 src
->enum1
= ENUM_ONE_VALUE2
;
26 dst
= QAPI_CLONE(UserDefOne
, src
);
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
;
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
);
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
);
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(void)
68 uint8List
*src
= NULL
, *dst
;
69 uint8List
*tmp
= NULL
;
72 /* Build list in reverse */
73 for (i
= 10; i
; i
--) {
74 QAPI_LIST_PREPEND(src
, i
);
77 dst
= QAPI_CLONE(uint8List
, src
);
78 for (tmp
= dst
, i
= 1; i
<= 10; i
++) {
80 g_assert_cmpint(tmp
->value
, ==, i
);
85 qapi_free_uint8List(src
);
86 qapi_free_uint8List(dst
);
89 static void test_clone_empty(void)
93 src
= g_new0(Empty2
, 1);
94 dst
= QAPI_CLONE(Empty2
, src
);
96 qapi_free_Empty2(src
);
97 qapi_free_Empty2(dst
);
100 static void test_clone_complex1(void)
102 UserDefFlatUnion
*src
, *dst
;
104 src
= g_new0(UserDefFlatUnion
, 1);
106 src
->string
= g_strdup("abc");
107 src
->enum1
= ENUM_ONE_VALUE1
;
108 src
->u
.value1
.boolean
= true;
110 dst
= QAPI_CLONE(UserDefFlatUnion
, src
);
113 g_assert_cmpint(dst
->integer
, ==, 123);
114 g_assert_cmpstr(dst
->string
, ==, "abc");
115 g_assert_cmpint(dst
->enum1
, ==, ENUM_ONE_VALUE1
);
116 g_assert(dst
->u
.value1
.boolean
);
117 g_assert(!dst
->u
.value1
.has_a_b
);
118 g_assert_cmpint(dst
->u
.value1
.a_b
, ==, 0);
120 qapi_free_UserDefFlatUnion(src
);
121 qapi_free_UserDefFlatUnion(dst
);
124 static void test_clone_complex2(void)
126 WrapAlternate
*src
, *dst
;
128 src
= g_new0(WrapAlternate
, 1);
129 src
->alt
= g_new(UserDefAlternate
, 1);
130 src
->alt
->type
= QTYPE_QDICT
;
131 src
->alt
->u
.udfu
.integer
= 42;
132 /* Clone intentionally converts NULL into "" for strings */
133 src
->alt
->u
.udfu
.string
= NULL
;
134 src
->alt
->u
.udfu
.enum1
= ENUM_ONE_VALUE3
;
135 src
->alt
->u
.udfu
.u
.value3
.intb
= 99;
136 src
->alt
->u
.udfu
.u
.value3
.has_a_b
= true;
137 src
->alt
->u
.udfu
.u
.value3
.a_b
= true;
139 dst
= QAPI_CLONE(WrapAlternate
, src
);
142 g_assert_cmpint(dst
->alt
->type
, ==, QTYPE_QDICT
);
143 g_assert_cmpint(dst
->alt
->u
.udfu
.integer
, ==, 42);
144 g_assert_cmpstr(dst
->alt
->u
.udfu
.string
, ==, "");
145 g_assert_cmpint(dst
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE3
);
146 g_assert_cmpint(dst
->alt
->u
.udfu
.u
.value3
.intb
, ==, 99);
147 g_assert_cmpint(dst
->alt
->u
.udfu
.u
.value3
.has_a_b
, ==, true);
148 g_assert_cmpint(dst
->alt
->u
.udfu
.u
.value3
.a_b
, ==, true);
150 qapi_free_WrapAlternate(src
);
151 qapi_free_WrapAlternate(dst
);
154 static void test_clone_complex3(void)
156 UserDefOneList
*src
, *dst
, *tail
;
160 elt
= g_new0(UserDefOne
, 1);
162 elt
->string
= g_strdup("three");
163 elt
->has_enum1
= true;
164 elt
->enum1
= ENUM_ONE_VALUE3
;
165 QAPI_LIST_PREPEND(src
, elt
);
166 elt
= g_new0(UserDefOne
, 1);
168 elt
->string
= g_strdup("two");
169 QAPI_LIST_PREPEND(src
, elt
);
170 elt
= g_new0(UserDefOne
, 1);
172 elt
->string
= g_strdup("one");
173 QAPI_LIST_PREPEND(src
, elt
);
175 dst
= QAPI_CLONE(UserDefOneList
, src
);
180 g_assert_cmpint(elt
->integer
, ==, 1);
181 g_assert_cmpstr(elt
->string
, ==, "one");
182 g_assert(!elt
->has_enum1
);
185 g_assert_cmpint(elt
->integer
, ==, 2);
186 g_assert_cmpstr(elt
->string
, ==, "two");
187 g_assert(!elt
->has_enum1
);
190 g_assert_cmpint(elt
->integer
, ==, 3);
191 g_assert_cmpstr(elt
->string
, ==, "three");
192 g_assert(elt
->has_enum1
);
193 g_assert_cmpint(elt
->enum1
, ==, ENUM_ONE_VALUE3
);
194 g_assert(!tail
->next
);
196 qapi_free_UserDefOneList(src
);
197 qapi_free_UserDefOneList(dst
);
200 int main(int argc
, char **argv
)
202 g_test_init(&argc
, &argv
, NULL
);
204 g_test_add_func("/visitor/clone/struct", test_clone_struct
);
205 g_test_add_func("/visitor/clone/alternate", test_clone_alternate
);
206 g_test_add_func("/visitor/clone/list", test_clone_list
);
207 g_test_add_func("/visitor/clone/empty", test_clone_empty
);
208 g_test_add_func("/visitor/clone/complex1", test_clone_complex1
);
209 g_test_add_func("/visitor/clone/complex2", test_clone_complex2
);
210 g_test_add_func("/visitor/clone/complex3", test_clone_complex3
);