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