root / tests / test-qmp-output-visitor.c @ b7fcff01
History | View | Annotate | Download (26.5 kB)
1 | f294f82a | Luiz Capitulino | /*
|
---|---|---|---|
2 | f294f82a | Luiz Capitulino | * QMP Output Visitor unit-tests.
|
3 | f294f82a | Luiz Capitulino | *
|
4 | f294f82a | Luiz Capitulino | * Copyright (C) 2011 Red Hat Inc.
|
5 | f294f82a | Luiz Capitulino | *
|
6 | f294f82a | Luiz Capitulino | * Authors:
|
7 | f294f82a | Luiz Capitulino | * Luiz Capitulino <lcapitulino@redhat.com>
|
8 | f294f82a | Luiz Capitulino | *
|
9 | f294f82a | Luiz Capitulino | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
10 | f294f82a | Luiz Capitulino | * See the COPYING file in the top-level directory.
|
11 | f294f82a | Luiz Capitulino | */
|
12 | f294f82a | Luiz Capitulino | |
13 | f294f82a | Luiz Capitulino | #include <glib.h> |
14 | f294f82a | Luiz Capitulino | |
15 | 79ee7df8 | Paolo Bonzini | #include "qemu-common.h" |
16 | f294f82a | Luiz Capitulino | #include "qapi/qmp-output-visitor.h" |
17 | f294f82a | Luiz Capitulino | #include "test-qapi-types.h" |
18 | f294f82a | Luiz Capitulino | #include "test-qapi-visit.h" |
19 | 7b1b5d19 | Paolo Bonzini | #include "qapi/qmp/types.h" |
20 | f294f82a | Luiz Capitulino | |
21 | f294f82a | Luiz Capitulino | typedef struct TestOutputVisitorData { |
22 | f294f82a | Luiz Capitulino | QmpOutputVisitor *qov; |
23 | f294f82a | Luiz Capitulino | Visitor *ov; |
24 | f294f82a | Luiz Capitulino | } TestOutputVisitorData; |
25 | f294f82a | Luiz Capitulino | |
26 | f294f82a | Luiz Capitulino | static void visitor_output_setup(TestOutputVisitorData *data, |
27 | f294f82a | Luiz Capitulino | const void *unused) |
28 | f294f82a | Luiz Capitulino | { |
29 | f294f82a | Luiz Capitulino | data->qov = qmp_output_visitor_new(); |
30 | f294f82a | Luiz Capitulino | g_assert(data->qov != NULL);
|
31 | f294f82a | Luiz Capitulino | |
32 | f294f82a | Luiz Capitulino | data->ov = qmp_output_get_visitor(data->qov); |
33 | f294f82a | Luiz Capitulino | g_assert(data->ov != NULL);
|
34 | f294f82a | Luiz Capitulino | } |
35 | f294f82a | Luiz Capitulino | |
36 | f294f82a | Luiz Capitulino | static void visitor_output_teardown(TestOutputVisitorData *data, |
37 | f294f82a | Luiz Capitulino | const void *unused) |
38 | f294f82a | Luiz Capitulino | { |
39 | f294f82a | Luiz Capitulino | qmp_output_visitor_cleanup(data->qov); |
40 | f294f82a | Luiz Capitulino | data->qov = NULL;
|
41 | f294f82a | Luiz Capitulino | data->ov = NULL;
|
42 | f294f82a | Luiz Capitulino | } |
43 | f294f82a | Luiz Capitulino | |
44 | f294f82a | Luiz Capitulino | static void test_visitor_out_int(TestOutputVisitorData *data, |
45 | f294f82a | Luiz Capitulino | const void *unused) |
46 | f294f82a | Luiz Capitulino | { |
47 | f294f82a | Luiz Capitulino | int64_t value = -42;
|
48 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
49 | f294f82a | Luiz Capitulino | QObject *obj; |
50 | f294f82a | Luiz Capitulino | |
51 | f294f82a | Luiz Capitulino | visit_type_int(data->ov, &value, NULL, &errp);
|
52 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == 0);
|
53 | f294f82a | Luiz Capitulino | |
54 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
55 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
56 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QINT); |
57 | f294f82a | Luiz Capitulino | g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value); |
58 | f294f82a | Luiz Capitulino | |
59 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
60 | f294f82a | Luiz Capitulino | } |
61 | f294f82a | Luiz Capitulino | |
62 | f294f82a | Luiz Capitulino | static void test_visitor_out_bool(TestOutputVisitorData *data, |
63 | f294f82a | Luiz Capitulino | const void *unused) |
64 | f294f82a | Luiz Capitulino | { |
65 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
66 | f294f82a | Luiz Capitulino | bool value = true; |
67 | f294f82a | Luiz Capitulino | QObject *obj; |
68 | f294f82a | Luiz Capitulino | |
69 | f294f82a | Luiz Capitulino | visit_type_bool(data->ov, &value, NULL, &errp);
|
70 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == 0);
|
71 | f294f82a | Luiz Capitulino | |
72 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
73 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
74 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QBOOL); |
75 | f294f82a | Luiz Capitulino | g_assert(qbool_get_int(qobject_to_qbool(obj)) == value); |
76 | f294f82a | Luiz Capitulino | |
77 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
78 | f294f82a | Luiz Capitulino | } |
79 | f294f82a | Luiz Capitulino | |
80 | f294f82a | Luiz Capitulino | static void test_visitor_out_number(TestOutputVisitorData *data, |
81 | f294f82a | Luiz Capitulino | const void *unused) |
82 | f294f82a | Luiz Capitulino | { |
83 | f294f82a | Luiz Capitulino | double value = 3.14; |
84 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
85 | f294f82a | Luiz Capitulino | QObject *obj; |
86 | f294f82a | Luiz Capitulino | |
87 | f294f82a | Luiz Capitulino | visit_type_number(data->ov, &value, NULL, &errp);
|
88 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == 0);
|
89 | f294f82a | Luiz Capitulino | |
90 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
91 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
92 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QFLOAT); |
93 | f294f82a | Luiz Capitulino | g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value); |
94 | f294f82a | Luiz Capitulino | |
95 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
96 | f294f82a | Luiz Capitulino | } |
97 | f294f82a | Luiz Capitulino | |
98 | f294f82a | Luiz Capitulino | static void test_visitor_out_string(TestOutputVisitorData *data, |
99 | f294f82a | Luiz Capitulino | const void *unused) |
100 | f294f82a | Luiz Capitulino | { |
101 | f294f82a | Luiz Capitulino | char *string = (char *) "Q E M U"; |
102 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
103 | f294f82a | Luiz Capitulino | QObject *obj; |
104 | f294f82a | Luiz Capitulino | |
105 | f294f82a | Luiz Capitulino | visit_type_str(data->ov, &string, NULL, &errp);
|
106 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == 0);
|
107 | f294f82a | Luiz Capitulino | |
108 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
109 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
110 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QSTRING); |
111 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string); |
112 | f294f82a | Luiz Capitulino | |
113 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
114 | f294f82a | Luiz Capitulino | } |
115 | f294f82a | Luiz Capitulino | |
116 | f294f82a | Luiz Capitulino | static void test_visitor_out_no_string(TestOutputVisitorData *data, |
117 | f294f82a | Luiz Capitulino | const void *unused) |
118 | f294f82a | Luiz Capitulino | { |
119 | f294f82a | Luiz Capitulino | char *string = NULL; |
120 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
121 | f294f82a | Luiz Capitulino | QObject *obj; |
122 | f294f82a | Luiz Capitulino | |
123 | f294f82a | Luiz Capitulino | /* A null string should return "" */
|
124 | f294f82a | Luiz Capitulino | visit_type_str(data->ov, &string, NULL, &errp);
|
125 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == 0);
|
126 | f294f82a | Luiz Capitulino | |
127 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
128 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
129 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QSTRING); |
130 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
|
131 | f294f82a | Luiz Capitulino | |
132 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
133 | f294f82a | Luiz Capitulino | } |
134 | f294f82a | Luiz Capitulino | |
135 | f294f82a | Luiz Capitulino | static void test_visitor_out_enum(TestOutputVisitorData *data, |
136 | f294f82a | Luiz Capitulino | const void *unused) |
137 | f294f82a | Luiz Capitulino | { |
138 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
139 | f294f82a | Luiz Capitulino | QObject *obj; |
140 | f294f82a | Luiz Capitulino | EnumOne i; |
141 | f294f82a | Luiz Capitulino | |
142 | f294f82a | Luiz Capitulino | for (i = 0; i < ENUM_ONE_MAX; i++) { |
143 | f294f82a | Luiz Capitulino | visit_type_EnumOne(data->ov, &i, "unused", &errp);
|
144 | f294f82a | Luiz Capitulino | g_assert(!error_is_set(&errp)); |
145 | f294f82a | Luiz Capitulino | |
146 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
147 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
148 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QSTRING); |
149 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, |
150 | f294f82a | Luiz Capitulino | EnumOne_lookup[i]); |
151 | f294f82a | Luiz Capitulino | qobject_decref(obj); |
152 | f294f82a | Luiz Capitulino | } |
153 | f294f82a | Luiz Capitulino | } |
154 | f294f82a | Luiz Capitulino | |
155 | f294f82a | Luiz Capitulino | static void test_visitor_out_enum_errors(TestOutputVisitorData *data, |
156 | f294f82a | Luiz Capitulino | const void *unused) |
157 | f294f82a | Luiz Capitulino | { |
158 | f294f82a | Luiz Capitulino | EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
|
159 | f294f82a | Luiz Capitulino | Error *errp; |
160 | f294f82a | Luiz Capitulino | |
161 | f294f82a | Luiz Capitulino | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { |
162 | f294f82a | Luiz Capitulino | errp = NULL;
|
163 | f294f82a | Luiz Capitulino | visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
|
164 | f294f82a | Luiz Capitulino | g_assert(error_is_set(&errp) == true);
|
165 | f294f82a | Luiz Capitulino | error_free(errp); |
166 | f294f82a | Luiz Capitulino | } |
167 | f294f82a | Luiz Capitulino | } |
168 | f294f82a | Luiz Capitulino | |
169 | f294f82a | Luiz Capitulino | typedef struct TestStruct |
170 | f294f82a | Luiz Capitulino | { |
171 | f294f82a | Luiz Capitulino | int64_t integer; |
172 | f294f82a | Luiz Capitulino | bool boolean;
|
173 | f294f82a | Luiz Capitulino | char *string;
|
174 | f294f82a | Luiz Capitulino | } TestStruct; |
175 | f294f82a | Luiz Capitulino | |
176 | f294f82a | Luiz Capitulino | static void visit_type_TestStruct(Visitor *v, TestStruct **obj, |
177 | f294f82a | Luiz Capitulino | const char *name, Error **errp) |
178 | f294f82a | Luiz Capitulino | { |
179 | f294f82a | Luiz Capitulino | visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), |
180 | f294f82a | Luiz Capitulino | errp); |
181 | f294f82a | Luiz Capitulino | |
182 | f294f82a | Luiz Capitulino | visit_type_int(v, &(*obj)->integer, "integer", errp);
|
183 | f294f82a | Luiz Capitulino | visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
|
184 | f294f82a | Luiz Capitulino | visit_type_str(v, &(*obj)->string, "string", errp);
|
185 | f294f82a | Luiz Capitulino | |
186 | f294f82a | Luiz Capitulino | visit_end_struct(v, errp); |
187 | f294f82a | Luiz Capitulino | } |
188 | f294f82a | Luiz Capitulino | |
189 | f294f82a | Luiz Capitulino | static void test_visitor_out_struct(TestOutputVisitorData *data, |
190 | f294f82a | Luiz Capitulino | const void *unused) |
191 | f294f82a | Luiz Capitulino | { |
192 | f294f82a | Luiz Capitulino | TestStruct test_struct = { .integer = 42,
|
193 | f294f82a | Luiz Capitulino | .boolean = false,
|
194 | f294f82a | Luiz Capitulino | .string = (char *) "foo"}; |
195 | f294f82a | Luiz Capitulino | TestStruct *p = &test_struct; |
196 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
197 | f294f82a | Luiz Capitulino | QObject *obj; |
198 | f294f82a | Luiz Capitulino | QDict *qdict; |
199 | f294f82a | Luiz Capitulino | |
200 | f294f82a | Luiz Capitulino | visit_type_TestStruct(data->ov, &p, NULL, &errp);
|
201 | f294f82a | Luiz Capitulino | g_assert(!error_is_set(&errp)); |
202 | f294f82a | Luiz Capitulino | |
203 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
204 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
205 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QDICT); |
206 | f294f82a | Luiz Capitulino | |
207 | f294f82a | Luiz Capitulino | qdict = qobject_to_qdict(obj); |
208 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(qdict), ==, 3);
|
209 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42); |
210 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0); |
211 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo"); |
212 | f294f82a | Luiz Capitulino | |
213 | f294f82a | Luiz Capitulino | QDECREF(qdict); |
214 | f294f82a | Luiz Capitulino | } |
215 | f294f82a | Luiz Capitulino | |
216 | f294f82a | Luiz Capitulino | static void test_visitor_out_struct_nested(TestOutputVisitorData *data, |
217 | f294f82a | Luiz Capitulino | const void *unused) |
218 | f294f82a | Luiz Capitulino | { |
219 | f294f82a | Luiz Capitulino | int64_t value = 42;
|
220 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
221 | f294f82a | Luiz Capitulino | UserDefNested *ud2; |
222 | f294f82a | Luiz Capitulino | QObject *obj; |
223 | f294f82a | Luiz Capitulino | QDict *qdict, *dict1, *dict2, *dict3, *userdef; |
224 | f294f82a | Luiz Capitulino | const char *string = "user def string"; |
225 | beae9d79 | Stefan Weil | const char *strings[] = { "forty two", "forty three", "forty four", |
226 | beae9d79 | Stefan Weil | "forty five" };
|
227 | f294f82a | Luiz Capitulino | |
228 | f294f82a | Luiz Capitulino | ud2 = g_malloc0(sizeof(*ud2));
|
229 | f294f82a | Luiz Capitulino | ud2->string0 = g_strdup(strings[0]);
|
230 | f294f82a | Luiz Capitulino | |
231 | f294f82a | Luiz Capitulino | ud2->dict1.string1 = g_strdup(strings[1]);
|
232 | f294f82a | Luiz Capitulino | ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
|
233 | f294f82a | Luiz Capitulino | ud2->dict1.dict2.userdef1->string = g_strdup(string); |
234 | f294f82a | Luiz Capitulino | ud2->dict1.dict2.userdef1->integer = value; |
235 | f294f82a | Luiz Capitulino | ud2->dict1.dict2.string2 = g_strdup(strings[2]);
|
236 | f294f82a | Luiz Capitulino | |
237 | f294f82a | Luiz Capitulino | ud2->dict1.has_dict3 = true;
|
238 | f294f82a | Luiz Capitulino | ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
|
239 | f294f82a | Luiz Capitulino | ud2->dict1.dict3.userdef2->string = g_strdup(string); |
240 | f294f82a | Luiz Capitulino | ud2->dict1.dict3.userdef2->integer = value; |
241 | f294f82a | Luiz Capitulino | ud2->dict1.dict3.string3 = g_strdup(strings[3]);
|
242 | f294f82a | Luiz Capitulino | |
243 | f294f82a | Luiz Capitulino | visit_type_UserDefNested(data->ov, &ud2, "unused", &errp);
|
244 | f294f82a | Luiz Capitulino | g_assert(!error_is_set(&errp)); |
245 | f294f82a | Luiz Capitulino | |
246 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
247 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
248 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QDICT); |
249 | f294f82a | Luiz Capitulino | |
250 | f294f82a | Luiz Capitulino | qdict = qobject_to_qdict(obj); |
251 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(qdict), ==, 2);
|
252 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]); |
253 | f294f82a | Luiz Capitulino | |
254 | f294f82a | Luiz Capitulino | dict1 = qdict_get_qdict(qdict, "dict1");
|
255 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(dict1), ==, 3);
|
256 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]); |
257 | f294f82a | Luiz Capitulino | |
258 | f294f82a | Luiz Capitulino | dict2 = qdict_get_qdict(dict1, "dict2");
|
259 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(dict2), ==, 2);
|
260 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]); |
261 | f294f82a | Luiz Capitulino | userdef = qdict_get_qdict(dict2, "userdef1");
|
262 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(userdef), ==, 2);
|
263 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
|
264 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
|
265 | f294f82a | Luiz Capitulino | |
266 | f294f82a | Luiz Capitulino | dict3 = qdict_get_qdict(dict1, "dict3");
|
267 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(dict3), ==, 2);
|
268 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]); |
269 | f294f82a | Luiz Capitulino | userdef = qdict_get_qdict(dict3, "userdef2");
|
270 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(userdef), ==, 2);
|
271 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
|
272 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
|
273 | f294f82a | Luiz Capitulino | |
274 | f294f82a | Luiz Capitulino | QDECREF(qdict); |
275 | f294f82a | Luiz Capitulino | qapi_free_UserDefNested(ud2); |
276 | f294f82a | Luiz Capitulino | } |
277 | f294f82a | Luiz Capitulino | |
278 | 9e9eace8 | Paolo Bonzini | static void test_visitor_out_struct_errors(TestOutputVisitorData *data, |
279 | 9e9eace8 | Paolo Bonzini | const void *unused) |
280 | 9e9eace8 | Paolo Bonzini | { |
281 | 9e9eace8 | Paolo Bonzini | EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
|
282 | 9e9eace8 | Paolo Bonzini | UserDefOne u = { 0 }, *pu = &u;
|
283 | 9e9eace8 | Paolo Bonzini | Error *errp; |
284 | 9e9eace8 | Paolo Bonzini | int i;
|
285 | 9e9eace8 | Paolo Bonzini | |
286 | 9e9eace8 | Paolo Bonzini | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { |
287 | 9e9eace8 | Paolo Bonzini | errp = NULL;
|
288 | 9e9eace8 | Paolo Bonzini | u.has_enum1 = true;
|
289 | 9e9eace8 | Paolo Bonzini | u.enum1 = bad_values[i]; |
290 | 9e9eace8 | Paolo Bonzini | visit_type_UserDefOne(data->ov, &pu, "unused", &errp);
|
291 | 9e9eace8 | Paolo Bonzini | g_assert(error_is_set(&errp) == true);
|
292 | 9e9eace8 | Paolo Bonzini | error_free(errp); |
293 | 9e9eace8 | Paolo Bonzini | } |
294 | 9e9eace8 | Paolo Bonzini | } |
295 | 9e9eace8 | Paolo Bonzini | |
296 | f294f82a | Luiz Capitulino | typedef struct TestStructList |
297 | f294f82a | Luiz Capitulino | { |
298 | a678e26c | Michael Roth | union {
|
299 | a678e26c | Michael Roth | TestStruct *value; |
300 | a678e26c | Michael Roth | uint64_t padding; |
301 | a678e26c | Michael Roth | }; |
302 | f294f82a | Luiz Capitulino | struct TestStructList *next;
|
303 | f294f82a | Luiz Capitulino | } TestStructList; |
304 | f294f82a | Luiz Capitulino | |
305 | f294f82a | Luiz Capitulino | static void visit_type_TestStructList(Visitor *v, TestStructList **obj, |
306 | f294f82a | Luiz Capitulino | const char *name, Error **errp) |
307 | f294f82a | Luiz Capitulino | { |
308 | f294f82a | Luiz Capitulino | GenericList *i, **head = (GenericList **)obj; |
309 | f294f82a | Luiz Capitulino | |
310 | f294f82a | Luiz Capitulino | visit_start_list(v, name, errp); |
311 | f294f82a | Luiz Capitulino | |
312 | f294f82a | Luiz Capitulino | for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
|
313 | f294f82a | Luiz Capitulino | TestStructList *native_i = (TestStructList *)i; |
314 | f294f82a | Luiz Capitulino | visit_type_TestStruct(v, &native_i->value, NULL, errp);
|
315 | f294f82a | Luiz Capitulino | } |
316 | f294f82a | Luiz Capitulino | |
317 | f294f82a | Luiz Capitulino | visit_end_list(v, errp); |
318 | f294f82a | Luiz Capitulino | } |
319 | f294f82a | Luiz Capitulino | |
320 | f294f82a | Luiz Capitulino | static void test_visitor_out_list(TestOutputVisitorData *data, |
321 | f294f82a | Luiz Capitulino | const void *unused) |
322 | f294f82a | Luiz Capitulino | { |
323 | f294f82a | Luiz Capitulino | char *value_str = (char *) "list value"; |
324 | f294f82a | Luiz Capitulino | TestStructList *p, *head = NULL;
|
325 | f294f82a | Luiz Capitulino | const int max_items = 10; |
326 | f294f82a | Luiz Capitulino | bool value_bool = true; |
327 | f294f82a | Luiz Capitulino | int value_int = 10; |
328 | f294f82a | Luiz Capitulino | Error *errp = NULL;
|
329 | f294f82a | Luiz Capitulino | QListEntry *entry; |
330 | f294f82a | Luiz Capitulino | QObject *obj; |
331 | f294f82a | Luiz Capitulino | QList *qlist; |
332 | f294f82a | Luiz Capitulino | int i;
|
333 | f294f82a | Luiz Capitulino | |
334 | f294f82a | Luiz Capitulino | for (i = 0; i < max_items; i++) { |
335 | f294f82a | Luiz Capitulino | p = g_malloc0(sizeof(*p));
|
336 | f294f82a | Luiz Capitulino | p->value = g_malloc0(sizeof(*p->value));
|
337 | f294f82a | Luiz Capitulino | p->value->integer = value_int; |
338 | f294f82a | Luiz Capitulino | p->value->boolean = value_bool; |
339 | f294f82a | Luiz Capitulino | p->value->string = value_str; |
340 | f294f82a | Luiz Capitulino | |
341 | f294f82a | Luiz Capitulino | p->next = head; |
342 | f294f82a | Luiz Capitulino | head = p; |
343 | f294f82a | Luiz Capitulino | } |
344 | f294f82a | Luiz Capitulino | |
345 | f294f82a | Luiz Capitulino | visit_type_TestStructList(data->ov, &head, NULL, &errp);
|
346 | f294f82a | Luiz Capitulino | g_assert(!error_is_set(&errp)); |
347 | f294f82a | Luiz Capitulino | |
348 | f294f82a | Luiz Capitulino | obj = qmp_output_get_qobject(data->qov); |
349 | f294f82a | Luiz Capitulino | g_assert(obj != NULL);
|
350 | f294f82a | Luiz Capitulino | g_assert(qobject_type(obj) == QTYPE_QLIST); |
351 | f294f82a | Luiz Capitulino | |
352 | f294f82a | Luiz Capitulino | qlist = qobject_to_qlist(obj); |
353 | f294f82a | Luiz Capitulino | g_assert(!qlist_empty(qlist)); |
354 | f294f82a | Luiz Capitulino | |
355 | f294f82a | Luiz Capitulino | i = 0;
|
356 | f294f82a | Luiz Capitulino | QLIST_FOREACH_ENTRY(qlist, entry) { |
357 | f294f82a | Luiz Capitulino | QDict *qdict; |
358 | f294f82a | Luiz Capitulino | |
359 | f294f82a | Luiz Capitulino | g_assert(qobject_type(entry->value) == QTYPE_QDICT); |
360 | f294f82a | Luiz Capitulino | qdict = qobject_to_qdict(entry->value); |
361 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_size(qdict), ==, 3);
|
362 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
|
363 | f294f82a | Luiz Capitulino | g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
|
364 | f294f82a | Luiz Capitulino | g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
|
365 | f294f82a | Luiz Capitulino | i++; |
366 | f294f82a | Luiz Capitulino | } |
367 | f294f82a | Luiz Capitulino | g_assert_cmpint(i, ==, max_items); |
368 | f294f82a | Luiz Capitulino | |
369 | f294f82a | Luiz Capitulino | QDECREF(qlist); |
370 | f294f82a | Luiz Capitulino | |
371 | f294f82a | Luiz Capitulino | for (p = head; p;) {
|
372 | f294f82a | Luiz Capitulino | TestStructList *tmp = p->next; |
373 | f294f82a | Luiz Capitulino | g_free(p->value); |
374 | f294f82a | Luiz Capitulino | g_free(p); |
375 | f294f82a | Luiz Capitulino | p = tmp; |
376 | f294f82a | Luiz Capitulino | } |
377 | f294f82a | Luiz Capitulino | } |
378 | f294f82a | Luiz Capitulino | |
379 | f294f82a | Luiz Capitulino | static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, |
380 | f294f82a | Luiz Capitulino | const void *unused) |
381 | f294f82a | Luiz Capitulino | { |
382 | f294f82a | Luiz Capitulino | UserDefNestedList *p, *head = NULL;
|
383 | f294f82a | Luiz Capitulino | const char string[] = "foo bar"; |
384 | f294f82a | Luiz Capitulino | int i, max_count = 1024; |
385 | f294f82a | Luiz Capitulino | |
386 | f294f82a | Luiz Capitulino | for (i = 0; i < max_count; i++) { |
387 | f294f82a | Luiz Capitulino | p = g_malloc0(sizeof(*p));
|
388 | f294f82a | Luiz Capitulino | p->value = g_malloc0(sizeof(*p->value));
|
389 | f294f82a | Luiz Capitulino | |
390 | f294f82a | Luiz Capitulino | p->value->string0 = g_strdup(string); |
391 | f294f82a | Luiz Capitulino | p->value->dict1.string1 = g_strdup(string); |
392 | f294f82a | Luiz Capitulino | p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
|
393 | f294f82a | Luiz Capitulino | p->value->dict1.dict2.userdef1->string = g_strdup(string); |
394 | f294f82a | Luiz Capitulino | p->value->dict1.dict2.userdef1->integer = 42;
|
395 | f294f82a | Luiz Capitulino | p->value->dict1.dict2.string2 = g_strdup(string); |
396 | f294f82a | Luiz Capitulino | p->value->dict1.has_dict3 = false;
|
397 | f294f82a | Luiz Capitulino | |
398 | f294f82a | Luiz Capitulino | p->next = head; |
399 | f294f82a | Luiz Capitulino | head = p; |
400 | f294f82a | Luiz Capitulino | } |
401 | f294f82a | Luiz Capitulino | |
402 | f294f82a | Luiz Capitulino | qapi_free_UserDefNestedList(head); |
403 | f294f82a | Luiz Capitulino | } |
404 | f294f82a | Luiz Capitulino | |
405 | dc8fb6df | Paolo Bonzini | static void test_visitor_out_union(TestOutputVisitorData *data, |
406 | dc8fb6df | Paolo Bonzini | const void *unused) |
407 | dc8fb6df | Paolo Bonzini | { |
408 | dc8fb6df | Paolo Bonzini | QObject *arg, *qvalue; |
409 | dc8fb6df | Paolo Bonzini | QDict *qdict, *value; |
410 | dc8fb6df | Paolo Bonzini | |
411 | dc8fb6df | Paolo Bonzini | Error *err = NULL;
|
412 | dc8fb6df | Paolo Bonzini | |
413 | dc8fb6df | Paolo Bonzini | UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
|
414 | dc8fb6df | Paolo Bonzini | tmp->kind = USER_DEF_UNION_KIND_A; |
415 | dc8fb6df | Paolo Bonzini | tmp->a = g_malloc0(sizeof(UserDefA));
|
416 | dc8fb6df | Paolo Bonzini | tmp->a->boolean = true;
|
417 | dc8fb6df | Paolo Bonzini | |
418 | dc8fb6df | Paolo Bonzini | visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
|
419 | dc8fb6df | Paolo Bonzini | g_assert(err == NULL);
|
420 | dc8fb6df | Paolo Bonzini | arg = qmp_output_get_qobject(data->qov); |
421 | dc8fb6df | Paolo Bonzini | |
422 | dc8fb6df | Paolo Bonzini | g_assert(qobject_type(arg) == QTYPE_QDICT); |
423 | dc8fb6df | Paolo Bonzini | qdict = qobject_to_qdict(arg); |
424 | dc8fb6df | Paolo Bonzini | |
425 | dc8fb6df | Paolo Bonzini | g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a"); |
426 | dc8fb6df | Paolo Bonzini | |
427 | dc8fb6df | Paolo Bonzini | qvalue = qdict_get(qdict, "data");
|
428 | dc8fb6df | Paolo Bonzini | g_assert(data != NULL);
|
429 | dc8fb6df | Paolo Bonzini | g_assert(qobject_type(qvalue) == QTYPE_QDICT); |
430 | dc8fb6df | Paolo Bonzini | value = qobject_to_qdict(qvalue); |
431 | dc8fb6df | Paolo Bonzini | g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true); |
432 | dc8fb6df | Paolo Bonzini | |
433 | dc8fb6df | Paolo Bonzini | qapi_free_UserDefUnion(tmp); |
434 | dc8fb6df | Paolo Bonzini | QDECREF(qdict); |
435 | dc8fb6df | Paolo Bonzini | } |
436 | dc8fb6df | Paolo Bonzini | |
437 | 83c84667 | Michael Roth | static void init_native_list(UserDefNativeListUnion *cvalue) |
438 | 83c84667 | Michael Roth | { |
439 | 83c84667 | Michael Roth | int i;
|
440 | 83c84667 | Michael Roth | switch (cvalue->kind) {
|
441 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
|
442 | 83c84667 | Michael Roth | intList **list = &cvalue->integer; |
443 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
444 | 83c84667 | Michael Roth | *list = g_new0(intList, 1);
|
445 | 83c84667 | Michael Roth | (*list)->value = i; |
446 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
447 | 83c84667 | Michael Roth | list = &(*list)->next; |
448 | 83c84667 | Michael Roth | } |
449 | 83c84667 | Michael Roth | break;
|
450 | 83c84667 | Michael Roth | } |
451 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
|
452 | 83c84667 | Michael Roth | int8List **list = &cvalue->s8; |
453 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
454 | 83c84667 | Michael Roth | *list = g_new0(int8List, 1);
|
455 | 83c84667 | Michael Roth | (*list)->value = i; |
456 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
457 | 83c84667 | Michael Roth | list = &(*list)->next; |
458 | 83c84667 | Michael Roth | } |
459 | 83c84667 | Michael Roth | break;
|
460 | 83c84667 | Michael Roth | } |
461 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
|
462 | 83c84667 | Michael Roth | int16List **list = &cvalue->s16; |
463 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
464 | 83c84667 | Michael Roth | *list = g_new0(int16List, 1);
|
465 | 83c84667 | Michael Roth | (*list)->value = i; |
466 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
467 | 83c84667 | Michael Roth | list = &(*list)->next; |
468 | 83c84667 | Michael Roth | } |
469 | 83c84667 | Michael Roth | break;
|
470 | 83c84667 | Michael Roth | } |
471 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
|
472 | 83c84667 | Michael Roth | int32List **list = &cvalue->s32; |
473 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
474 | 83c84667 | Michael Roth | *list = g_new0(int32List, 1);
|
475 | 83c84667 | Michael Roth | (*list)->value = i; |
476 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
477 | 83c84667 | Michael Roth | list = &(*list)->next; |
478 | 83c84667 | Michael Roth | } |
479 | 83c84667 | Michael Roth | break;
|
480 | 83c84667 | Michael Roth | } |
481 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
|
482 | 83c84667 | Michael Roth | int64List **list = &cvalue->s64; |
483 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
484 | 83c84667 | Michael Roth | *list = g_new0(int64List, 1);
|
485 | 83c84667 | Michael Roth | (*list)->value = i; |
486 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
487 | 83c84667 | Michael Roth | list = &(*list)->next; |
488 | 83c84667 | Michael Roth | } |
489 | 83c84667 | Michael Roth | break;
|
490 | 83c84667 | Michael Roth | } |
491 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
|
492 | 83c84667 | Michael Roth | uint8List **list = &cvalue->u8; |
493 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
494 | 83c84667 | Michael Roth | *list = g_new0(uint8List, 1);
|
495 | 83c84667 | Michael Roth | (*list)->value = i; |
496 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
497 | 83c84667 | Michael Roth | list = &(*list)->next; |
498 | 83c84667 | Michael Roth | } |
499 | 83c84667 | Michael Roth | break;
|
500 | 83c84667 | Michael Roth | } |
501 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
|
502 | 83c84667 | Michael Roth | uint16List **list = &cvalue->u16; |
503 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
504 | 83c84667 | Michael Roth | *list = g_new0(uint16List, 1);
|
505 | 83c84667 | Michael Roth | (*list)->value = i; |
506 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
507 | 83c84667 | Michael Roth | list = &(*list)->next; |
508 | 83c84667 | Michael Roth | } |
509 | 83c84667 | Michael Roth | break;
|
510 | 83c84667 | Michael Roth | } |
511 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
|
512 | 83c84667 | Michael Roth | uint32List **list = &cvalue->u32; |
513 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
514 | 83c84667 | Michael Roth | *list = g_new0(uint32List, 1);
|
515 | 83c84667 | Michael Roth | (*list)->value = i; |
516 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
517 | 83c84667 | Michael Roth | list = &(*list)->next; |
518 | 83c84667 | Michael Roth | } |
519 | 83c84667 | Michael Roth | break;
|
520 | 83c84667 | Michael Roth | } |
521 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
|
522 | 83c84667 | Michael Roth | uint64List **list = &cvalue->u64; |
523 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
524 | 83c84667 | Michael Roth | *list = g_new0(uint64List, 1);
|
525 | 83c84667 | Michael Roth | (*list)->value = i; |
526 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
527 | 83c84667 | Michael Roth | list = &(*list)->next; |
528 | 83c84667 | Michael Roth | } |
529 | 83c84667 | Michael Roth | break;
|
530 | 83c84667 | Michael Roth | } |
531 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
|
532 | 83c84667 | Michael Roth | boolList **list = &cvalue->boolean; |
533 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
534 | 83c84667 | Michael Roth | *list = g_new0(boolList, 1);
|
535 | 83c84667 | Michael Roth | (*list)->value = (i % 3 == 0); |
536 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
537 | 83c84667 | Michael Roth | list = &(*list)->next; |
538 | 83c84667 | Michael Roth | } |
539 | 83c84667 | Michael Roth | break;
|
540 | 83c84667 | Michael Roth | } |
541 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
|
542 | 83c84667 | Michael Roth | strList **list = &cvalue->string; |
543 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
544 | 83c84667 | Michael Roth | *list = g_new0(strList, 1);
|
545 | 83c84667 | Michael Roth | (*list)->value = g_strdup_printf("%d", i);
|
546 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
547 | 83c84667 | Michael Roth | list = &(*list)->next; |
548 | 83c84667 | Michael Roth | } |
549 | 83c84667 | Michael Roth | break;
|
550 | 83c84667 | Michael Roth | } |
551 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
|
552 | 83c84667 | Michael Roth | numberList **list = &cvalue->number; |
553 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
554 | 83c84667 | Michael Roth | *list = g_new0(numberList, 1);
|
555 | 83c84667 | Michael Roth | (*list)->value = (double)i / 3; |
556 | 83c84667 | Michael Roth | (*list)->next = NULL;
|
557 | 83c84667 | Michael Roth | list = &(*list)->next; |
558 | 83c84667 | Michael Roth | } |
559 | 83c84667 | Michael Roth | break;
|
560 | 83c84667 | Michael Roth | } |
561 | 83c84667 | Michael Roth | default:
|
562 | dfc6f865 | Stefan Weil | g_assert_not_reached(); |
563 | 83c84667 | Michael Roth | } |
564 | 83c84667 | Michael Roth | } |
565 | 83c84667 | Michael Roth | |
566 | 83c84667 | Michael Roth | static void check_native_list(QObject *qobj, |
567 | 83c84667 | Michael Roth | UserDefNativeListUnionKind kind) |
568 | 83c84667 | Michael Roth | { |
569 | 83c84667 | Michael Roth | QDict *qdict; |
570 | 83c84667 | Michael Roth | QList *qlist; |
571 | 83c84667 | Michael Roth | int i;
|
572 | 83c84667 | Michael Roth | |
573 | 83c84667 | Michael Roth | g_assert(qobj); |
574 | 83c84667 | Michael Roth | g_assert(qobject_type(qobj) == QTYPE_QDICT); |
575 | 83c84667 | Michael Roth | qdict = qobject_to_qdict(qobj); |
576 | 83c84667 | Michael Roth | g_assert(qdict); |
577 | 83c84667 | Michael Roth | g_assert(qdict_haskey(qdict, "data"));
|
578 | 83c84667 | Michael Roth | qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
|
579 | 83c84667 | Michael Roth | |
580 | 83c84667 | Michael Roth | switch (kind) {
|
581 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
|
582 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
|
583 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
|
584 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
|
585 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
|
586 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
|
587 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
|
588 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
|
589 | 83c84667 | Michael Roth | /* all integer elements in JSON arrays get stored into QInts when
|
590 | 83c84667 | Michael Roth | * we convert to QObjects, so we can check them all in the same
|
591 | 83c84667 | Michael Roth | * fashion, so simply fall through here
|
592 | 83c84667 | Michael Roth | */
|
593 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
|
594 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
595 | 83c84667 | Michael Roth | QObject *tmp; |
596 | 83c84667 | Michael Roth | QInt *qvalue; |
597 | 83c84667 | Michael Roth | tmp = qlist_peek(qlist); |
598 | 83c84667 | Michael Roth | g_assert(tmp); |
599 | 83c84667 | Michael Roth | qvalue = qobject_to_qint(tmp); |
600 | 83c84667 | Michael Roth | g_assert_cmpint(qint_get_int(qvalue), ==, i); |
601 | 83c84667 | Michael Roth | qobject_decref(qlist_pop(qlist)); |
602 | 83c84667 | Michael Roth | } |
603 | 83c84667 | Michael Roth | break;
|
604 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
|
605 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
606 | 83c84667 | Michael Roth | QObject *tmp; |
607 | 83c84667 | Michael Roth | QBool *qvalue; |
608 | 83c84667 | Michael Roth | tmp = qlist_peek(qlist); |
609 | 83c84667 | Michael Roth | g_assert(tmp); |
610 | 83c84667 | Michael Roth | qvalue = qobject_to_qbool(tmp); |
611 | 83c84667 | Michael Roth | g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0); |
612 | 83c84667 | Michael Roth | qobject_decref(qlist_pop(qlist)); |
613 | 83c84667 | Michael Roth | } |
614 | 83c84667 | Michael Roth | break;
|
615 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
|
616 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
617 | 83c84667 | Michael Roth | QObject *tmp; |
618 | 83c84667 | Michael Roth | QString *qvalue; |
619 | 83c84667 | Michael Roth | gchar str[8];
|
620 | 83c84667 | Michael Roth | tmp = qlist_peek(qlist); |
621 | 83c84667 | Michael Roth | g_assert(tmp); |
622 | 83c84667 | Michael Roth | qvalue = qobject_to_qstring(tmp); |
623 | 83c84667 | Michael Roth | sprintf(str, "%d", i);
|
624 | 83c84667 | Michael Roth | g_assert_cmpstr(qstring_get_str(qvalue), ==, str); |
625 | 83c84667 | Michael Roth | qobject_decref(qlist_pop(qlist)); |
626 | 83c84667 | Michael Roth | } |
627 | 83c84667 | Michael Roth | break;
|
628 | 83c84667 | Michael Roth | case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
|
629 | 83c84667 | Michael Roth | for (i = 0; i < 32; i++) { |
630 | 83c84667 | Michael Roth | QObject *tmp; |
631 | 83c84667 | Michael Roth | QFloat *qvalue; |
632 | 83c84667 | Michael Roth | GString *double_expected = g_string_new("");
|
633 | 83c84667 | Michael Roth | GString *double_actual = g_string_new("");
|
634 | 83c84667 | Michael Roth | |
635 | 83c84667 | Michael Roth | tmp = qlist_peek(qlist); |
636 | 83c84667 | Michael Roth | g_assert(tmp); |
637 | 83c84667 | Michael Roth | qvalue = qobject_to_qfloat(tmp); |
638 | 83c84667 | Michael Roth | g_string_printf(double_expected, "%.6f", (double)i / 3); |
639 | 83c84667 | Michael Roth | g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
|
640 | 83c84667 | Michael Roth | g_assert_cmpstr(double_actual->str, ==, double_expected->str); |
641 | 83c84667 | Michael Roth | |
642 | 83c84667 | Michael Roth | qobject_decref(qlist_pop(qlist)); |
643 | 83c84667 | Michael Roth | g_string_free(double_expected, true);
|
644 | 83c84667 | Michael Roth | g_string_free(double_actual, true);
|
645 | 83c84667 | Michael Roth | } |
646 | 83c84667 | Michael Roth | break;
|
647 | 83c84667 | Michael Roth | default:
|
648 | dfc6f865 | Stefan Weil | g_assert_not_reached(); |
649 | 83c84667 | Michael Roth | } |
650 | 83c84667 | Michael Roth | QDECREF(qlist); |
651 | 83c84667 | Michael Roth | } |
652 | 83c84667 | Michael Roth | |
653 | 83c84667 | Michael Roth | static void test_native_list(TestOutputVisitorData *data, |
654 | 83c84667 | Michael Roth | const void *unused, |
655 | 83c84667 | Michael Roth | UserDefNativeListUnionKind kind) |
656 | 83c84667 | Michael Roth | { |
657 | 83c84667 | Michael Roth | UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
|
658 | 83c84667 | Michael Roth | Error *err = NULL;
|
659 | 83c84667 | Michael Roth | QObject *obj; |
660 | 83c84667 | Michael Roth | |
661 | 83c84667 | Michael Roth | cvalue->kind = kind; |
662 | 83c84667 | Michael Roth | init_native_list(cvalue); |
663 | 83c84667 | Michael Roth | |
664 | 83c84667 | Michael Roth | visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
|
665 | 83c84667 | Michael Roth | g_assert(err == NULL);
|
666 | 83c84667 | Michael Roth | |
667 | 83c84667 | Michael Roth | obj = qmp_output_get_qobject(data->qov); |
668 | 83c84667 | Michael Roth | check_native_list(obj, cvalue->kind); |
669 | 83c84667 | Michael Roth | qapi_free_UserDefNativeListUnion(cvalue); |
670 | 83c84667 | Michael Roth | qobject_decref(obj); |
671 | 83c84667 | Michael Roth | } |
672 | 83c84667 | Michael Roth | |
673 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_int(TestOutputVisitorData *data, |
674 | 83c84667 | Michael Roth | const void *unused) |
675 | 83c84667 | Michael Roth | { |
676 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); |
677 | 83c84667 | Michael Roth | } |
678 | 83c84667 | Michael Roth | |
679 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_int8(TestOutputVisitorData *data, |
680 | 83c84667 | Michael Roth | const void *unused) |
681 | 83c84667 | Michael Roth | { |
682 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8); |
683 | 83c84667 | Michael Roth | } |
684 | 83c84667 | Michael Roth | |
685 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_int16(TestOutputVisitorData *data, |
686 | 83c84667 | Michael Roth | const void *unused) |
687 | 83c84667 | Michael Roth | { |
688 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16); |
689 | 83c84667 | Michael Roth | } |
690 | 83c84667 | Michael Roth | |
691 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_int32(TestOutputVisitorData *data, |
692 | 83c84667 | Michael Roth | const void *unused) |
693 | 83c84667 | Michael Roth | { |
694 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32); |
695 | 83c84667 | Michael Roth | } |
696 | 83c84667 | Michael Roth | |
697 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_int64(TestOutputVisitorData *data, |
698 | 83c84667 | Michael Roth | const void *unused) |
699 | 83c84667 | Michael Roth | { |
700 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64); |
701 | 83c84667 | Michael Roth | } |
702 | 83c84667 | Michael Roth | |
703 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data, |
704 | 83c84667 | Michael Roth | const void *unused) |
705 | 83c84667 | Michael Roth | { |
706 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8); |
707 | 83c84667 | Michael Roth | } |
708 | 83c84667 | Michael Roth | |
709 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data, |
710 | 83c84667 | Michael Roth | const void *unused) |
711 | 83c84667 | Michael Roth | { |
712 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16); |
713 | 83c84667 | Michael Roth | } |
714 | 83c84667 | Michael Roth | |
715 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data, |
716 | 83c84667 | Michael Roth | const void *unused) |
717 | 83c84667 | Michael Roth | { |
718 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32); |
719 | 83c84667 | Michael Roth | } |
720 | 83c84667 | Michael Roth | |
721 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data, |
722 | 83c84667 | Michael Roth | const void *unused) |
723 | 83c84667 | Michael Roth | { |
724 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64); |
725 | 83c84667 | Michael Roth | } |
726 | 83c84667 | Michael Roth | |
727 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_bool(TestOutputVisitorData *data, |
728 | 83c84667 | Michael Roth | const void *unused) |
729 | 83c84667 | Michael Roth | { |
730 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); |
731 | 83c84667 | Michael Roth | } |
732 | 83c84667 | Michael Roth | |
733 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_str(TestOutputVisitorData *data, |
734 | 83c84667 | Michael Roth | const void *unused) |
735 | 83c84667 | Michael Roth | { |
736 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); |
737 | 83c84667 | Michael Roth | } |
738 | 83c84667 | Michael Roth | |
739 | 83c84667 | Michael Roth | static void test_visitor_out_native_list_number(TestOutputVisitorData *data, |
740 | 83c84667 | Michael Roth | const void *unused) |
741 | 83c84667 | Michael Roth | { |
742 | 83c84667 | Michael Roth | test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); |
743 | 83c84667 | Michael Roth | } |
744 | 83c84667 | Michael Roth | |
745 | f294f82a | Luiz Capitulino | static void output_visitor_test_add(const char *testpath, |
746 | f294f82a | Luiz Capitulino | TestOutputVisitorData *data, |
747 | f294f82a | Luiz Capitulino | void (*test_func)(TestOutputVisitorData *data, const void *user_data)) |
748 | f294f82a | Luiz Capitulino | { |
749 | f294f82a | Luiz Capitulino | g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup, |
750 | f294f82a | Luiz Capitulino | test_func, visitor_output_teardown); |
751 | f294f82a | Luiz Capitulino | } |
752 | f294f82a | Luiz Capitulino | |
753 | f294f82a | Luiz Capitulino | int main(int argc, char **argv) |
754 | f294f82a | Luiz Capitulino | { |
755 | f294f82a | Luiz Capitulino | TestOutputVisitorData out_visitor_data; |
756 | f294f82a | Luiz Capitulino | |
757 | f294f82a | Luiz Capitulino | g_test_init(&argc, &argv, NULL);
|
758 | f294f82a | Luiz Capitulino | |
759 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/int",
|
760 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_int); |
761 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/bool",
|
762 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_bool); |
763 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/number",
|
764 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_number); |
765 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/string",
|
766 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_string); |
767 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/no-string",
|
768 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_no_string); |
769 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/enum",
|
770 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_enum); |
771 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/enum-errors",
|
772 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_enum_errors); |
773 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/struct",
|
774 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_struct); |
775 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/struct-nested",
|
776 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_struct_nested); |
777 | 9e9eace8 | Paolo Bonzini | output_visitor_test_add("/visitor/output/struct-errors",
|
778 | 9e9eace8 | Paolo Bonzini | &out_visitor_data, test_visitor_out_struct_errors); |
779 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/list",
|
780 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_list); |
781 | f294f82a | Luiz Capitulino | output_visitor_test_add("/visitor/output/list-qapi-free",
|
782 | f294f82a | Luiz Capitulino | &out_visitor_data, test_visitor_out_list_qapi_free); |
783 | dc8fb6df | Paolo Bonzini | output_visitor_test_add("/visitor/output/union",
|
784 | dc8fb6df | Paolo Bonzini | &out_visitor_data, test_visitor_out_union); |
785 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/int",
|
786 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_int); |
787 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/int8",
|
788 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_int8); |
789 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/int16",
|
790 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_int16); |
791 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/int32",
|
792 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_int32); |
793 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/int64",
|
794 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_int64); |
795 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/uint8",
|
796 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_uint8); |
797 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/uint16",
|
798 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_uint16); |
799 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/uint32",
|
800 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_uint32); |
801 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/uint64",
|
802 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_uint64); |
803 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/bool",
|
804 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_bool); |
805 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/string",
|
806 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_str); |
807 | 83c84667 | Michael Roth | output_visitor_test_add("/visitor/output/native_list/number",
|
808 | 83c84667 | Michael Roth | &out_visitor_data, test_visitor_out_native_list_number); |
809 | f294f82a | Luiz Capitulino | |
810 | f294f82a | Luiz Capitulino | g_test_run(); |
811 | f294f82a | Luiz Capitulino | |
812 | f294f82a | Luiz Capitulino | return 0; |
813 | f294f82a | Luiz Capitulino | } |