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