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