migration: rename cancel to cleanup in SaveVMHandles
[qemu/ar7.git] / tests / test-qmp-output-visitor.c
blob09d0dd81f09fe2b4a8d7c86b9e182e1c546fe5db
1 /*
2 * QMP Output Visitor unit-tests.
4 * Copyright (C) 2011, 2015 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include <glib.h>
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"
21 typedef struct TestOutputVisitorData {
22 QmpOutputVisitor *qov;
23 Visitor *ov;
24 } TestOutputVisitorData;
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
29 data->qov = qmp_output_visitor_new();
30 g_assert(data->qov != NULL);
32 data->ov = qmp_output_get_visitor(data->qov);
33 g_assert(data->ov != NULL);
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
39 qmp_output_visitor_cleanup(data->qov);
40 data->qov = NULL;
41 data->ov = NULL;
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
47 int64_t value = -42;
48 Error *err = NULL;
49 QObject *obj;
51 visit_type_int(data->ov, &value, NULL, &err);
52 g_assert(!err);
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);
59 qobject_decref(obj);
62 static void test_visitor_out_bool(TestOutputVisitorData *data,
63 const void *unused)
65 Error *err = NULL;
66 bool value = true;
67 QObject *obj;
69 visit_type_bool(data->ov, &value, NULL, &err);
70 g_assert(!err);
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);
77 qobject_decref(obj);
80 static void test_visitor_out_number(TestOutputVisitorData *data,
81 const void *unused)
83 double value = 3.14;
84 Error *err = NULL;
85 QObject *obj;
87 visit_type_number(data->ov, &value, NULL, &err);
88 g_assert(!err);
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);
95 qobject_decref(obj);
98 static void test_visitor_out_string(TestOutputVisitorData *data,
99 const void *unused)
101 char *string = (char *) "Q E M U";
102 Error *err = NULL;
103 QObject *obj;
105 visit_type_str(data->ov, &string, NULL, &err);
106 g_assert(!err);
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);
113 qobject_decref(obj);
116 static void test_visitor_out_no_string(TestOutputVisitorData *data,
117 const void *unused)
119 char *string = NULL;
120 Error *err = NULL;
121 QObject *obj;
123 /* A null string should return "" */
124 visit_type_str(data->ov, &string, NULL, &err);
125 g_assert(!err);
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)), ==, "");
132 qobject_decref(obj);
135 static void test_visitor_out_enum(TestOutputVisitorData *data,
136 const void *unused)
138 Error *err = NULL;
139 QObject *obj;
140 EnumOne i;
142 for (i = 0; i < ENUM_ONE_MAX; i++) {
143 visit_type_EnumOne(data->ov, &i, "unused", &err);
144 g_assert(!err);
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);
155 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
156 const void *unused)
158 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
159 Error *err;
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);
169 typedef struct TestStruct
171 int64_t integer;
172 bool boolean;
173 char *string;
174 } TestStruct;
176 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
177 const char *name, Error **errp)
179 Error *err = NULL;
181 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
182 &err);
183 if (err) {
184 goto out;
187 visit_type_int(v, &(*obj)->integer, "integer", &err);
188 if (err) {
189 goto out_end;
191 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
192 if (err) {
193 goto out_end;
195 visit_type_str(v, &(*obj)->string, "string", &err);
197 out_end:
198 error_propagate(errp, err);
199 err = NULL;
200 visit_end_struct(v, &err);
201 out:
202 error_propagate(errp, err);
205 static void test_visitor_out_struct(TestOutputVisitorData *data,
206 const void *unused)
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;
216 visit_type_TestStruct(data->ov, &p, NULL, &err);
217 g_assert(!err);
219 obj = qmp_output_get_qobject(data->qov);
220 g_assert(obj != NULL);
221 g_assert(qobject_type(obj) == QTYPE_QDICT);
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");
229 QDECREF(qdict);
232 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
233 const void *unused)
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" };
244 ud2 = g_malloc0(sizeof(*ud2));
245 ud2->string0 = g_strdup(strings[0]);
247 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
248 ud2->dict1->string1 = g_strdup(strings[1]);
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->integer = value;
254 ud2->dict1->dict2->string = g_strdup(strings[2]);
256 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
257 ud2->dict1->has_dict3 = true;
258 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
259 ud2->dict1->dict3->userdef->string = g_strdup(string);
260 ud2->dict1->dict3->userdef->integer = value;
261 ud2->dict1->dict3->string = g_strdup(strings[3]);
263 visit_type_UserDefTwo(data->ov, &ud2, "unused", &err);
264 g_assert(!err);
266 obj = qmp_output_get_qobject(data->qov);
267 g_assert(obj != NULL);
268 g_assert(qobject_type(obj) == QTYPE_QDICT);
270 qdict = qobject_to_qdict(obj);
271 g_assert_cmpint(qdict_size(qdict), ==, 2);
272 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
274 dict1 = qdict_get_qdict(qdict, "dict1");
275 g_assert_cmpint(qdict_size(dict1), ==, 3);
276 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
278 dict2 = qdict_get_qdict(dict1, "dict2");
279 g_assert_cmpint(qdict_size(dict2), ==, 2);
280 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
281 userdef = qdict_get_qdict(dict2, "userdef");
282 g_assert_cmpint(qdict_size(userdef), ==, 2);
283 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
284 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
286 dict3 = qdict_get_qdict(dict1, "dict3");
287 g_assert_cmpint(qdict_size(dict3), ==, 2);
288 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
289 userdef = qdict_get_qdict(dict3, "userdef");
290 g_assert_cmpint(qdict_size(userdef), ==, 2);
291 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
292 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
294 QDECREF(qdict);
295 qapi_free_UserDefTwo(ud2);
298 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
299 const void *unused)
301 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
302 UserDefOne u = {0};
303 UserDefOne *pu = &u;
304 Error *err;
305 int i;
307 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
308 err = NULL;
309 u.has_enum1 = true;
310 u.enum1 = bad_values[i];
311 visit_type_UserDefOne(data->ov, &pu, "unused", &err);
312 g_assert(err);
313 error_free(err);
317 typedef struct TestStructList
319 union {
320 TestStruct *value;
321 uint64_t padding;
323 struct TestStructList *next;
324 } TestStructList;
326 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
327 const char *name, Error **errp)
329 GenericList *i, **head = (GenericList **)obj;
331 visit_start_list(v, name, errp);
333 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
334 TestStructList *native_i = (TestStructList *)i;
335 visit_type_TestStruct(v, &native_i->value, NULL, errp);
338 visit_end_list(v, errp);
341 static void test_visitor_out_list(TestOutputVisitorData *data,
342 const void *unused)
344 char *value_str = (char *) "list value";
345 TestStructList *p, *head = NULL;
346 const int max_items = 10;
347 bool value_bool = true;
348 int value_int = 10;
349 Error *err = NULL;
350 QListEntry *entry;
351 QObject *obj;
352 QList *qlist;
353 int i;
355 for (i = 0; i < max_items; i++) {
356 p = g_malloc0(sizeof(*p));
357 p->value = g_malloc0(sizeof(*p->value));
358 p->value->integer = value_int;
359 p->value->boolean = value_bool;
360 p->value->string = value_str;
362 p->next = head;
363 head = p;
366 visit_type_TestStructList(data->ov, &head, NULL, &err);
367 g_assert(!err);
369 obj = qmp_output_get_qobject(data->qov);
370 g_assert(obj != NULL);
371 g_assert(qobject_type(obj) == QTYPE_QLIST);
373 qlist = qobject_to_qlist(obj);
374 g_assert(!qlist_empty(qlist));
376 i = 0;
377 QLIST_FOREACH_ENTRY(qlist, entry) {
378 QDict *qdict;
380 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
381 qdict = qobject_to_qdict(entry->value);
382 g_assert_cmpint(qdict_size(qdict), ==, 3);
383 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
384 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
385 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
386 i++;
388 g_assert_cmpint(i, ==, max_items);
390 QDECREF(qlist);
392 for (p = head; p;) {
393 TestStructList *tmp = p->next;
394 g_free(p->value);
395 g_free(p);
396 p = tmp;
400 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
401 const void *unused)
403 UserDefTwoList *p, *head = NULL;
404 const char string[] = "foo bar";
405 int i, max_count = 1024;
407 for (i = 0; i < max_count; i++) {
408 p = g_malloc0(sizeof(*p));
409 p->value = g_malloc0(sizeof(*p->value));
411 p->value->string0 = g_strdup(string);
412 p->value->dict1 = g_new0(UserDefTwoDict, 1);
413 p->value->dict1->string1 = g_strdup(string);
414 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
415 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
416 p->value->dict1->dict2->userdef->string = g_strdup(string);
417 p->value->dict1->dict2->userdef->integer = 42;
418 p->value->dict1->dict2->string = g_strdup(string);
419 p->value->dict1->has_dict3 = false;
421 p->next = head;
422 head = p;
425 qapi_free_UserDefTwoList(head);
428 static void test_visitor_out_any(TestOutputVisitorData *data,
429 const void *unused)
431 QObject *qobj;
432 Error *err = NULL;
433 QInt *qint;
434 QBool *qbool;
435 QString *qstring;
436 QDict *qdict;
437 QObject *obj;
439 qobj = QOBJECT(qint_from_int(-42));
440 visit_type_any(data->ov, &qobj, NULL, &err);
441 g_assert(!err);
442 obj = qmp_output_get_qobject(data->qov);
443 g_assert(obj != NULL);
444 g_assert(qobject_type(obj) == QTYPE_QINT);
445 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
446 qobject_decref(obj);
447 qobject_decref(qobj);
449 qdict = qdict_new();
450 qdict_put(qdict, "integer", qint_from_int(-42));
451 qdict_put(qdict, "boolean", qbool_from_bool(true));
452 qdict_put(qdict, "string", qstring_from_str("foo"));
453 qobj = QOBJECT(qdict);
454 visit_type_any(data->ov, &qobj, NULL, &err);
455 g_assert(!err);
456 obj = qmp_output_get_qobject(data->qov);
457 g_assert(obj != NULL);
458 qdict = qobject_to_qdict(obj);
459 g_assert(qdict);
460 qobj = qdict_get(qdict, "integer");
461 g_assert(qobj);
462 qint = qobject_to_qint(qobj);
463 g_assert(qint);
464 g_assert_cmpint(qint_get_int(qint), ==, -42);
465 qobj = qdict_get(qdict, "boolean");
466 g_assert(qobj);
467 qbool = qobject_to_qbool(qobj);
468 g_assert(qbool);
469 g_assert(qbool_get_bool(qbool) == true);
470 qobj = qdict_get(qdict, "string");
471 g_assert(qobj);
472 qstring = qobject_to_qstring(qobj);
473 g_assert(qstring);
474 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
475 qobject_decref(obj);
476 qobject_decref(qobj);
479 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
480 const void *unused)
482 QObject *arg;
483 QDict *qdict;
485 Error *err = NULL;
487 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
488 tmp->enum1 = ENUM_ONE_VALUE1;
489 tmp->string = g_strdup("str");
490 tmp->u.value1 = g_malloc0(sizeof(UserDefA));
491 tmp->integer = 41;
492 tmp->u.value1->boolean = true;
494 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
495 g_assert(err == NULL);
496 arg = qmp_output_get_qobject(data->qov);
498 g_assert(qobject_type(arg) == QTYPE_QDICT);
499 qdict = qobject_to_qdict(arg);
501 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
502 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
503 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
504 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
506 qapi_free_UserDefFlatUnion(tmp);
507 QDECREF(qdict);
510 static void test_visitor_out_alternate(TestOutputVisitorData *data,
511 const void *unused)
513 QObject *arg;
514 Error *err = NULL;
516 UserDefAlternate *tmp = g_malloc0(sizeof(UserDefAlternate));
517 tmp->type = USER_DEF_ALTERNATE_KIND_I;
518 tmp->u.i = 42;
520 visit_type_UserDefAlternate(data->ov, &tmp, NULL, &err);
521 g_assert(err == NULL);
522 arg = qmp_output_get_qobject(data->qov);
524 g_assert(qobject_type(arg) == QTYPE_QINT);
525 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
527 qapi_free_UserDefAlternate(tmp);
530 static void test_visitor_out_empty(TestOutputVisitorData *data,
531 const void *unused)
533 QObject *arg;
535 arg = qmp_output_get_qobject(data->qov);
536 g_assert(qobject_type(arg) == QTYPE_QNULL);
537 qobject_decref(arg);
540 static void init_native_list(UserDefNativeListUnion *cvalue)
542 int i;
543 switch (cvalue->type) {
544 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
545 intList **list = &cvalue->u.integer;
546 for (i = 0; i < 32; i++) {
547 *list = g_new0(intList, 1);
548 (*list)->value = i;
549 (*list)->next = NULL;
550 list = &(*list)->next;
552 break;
554 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
555 int8List **list = &cvalue->u.s8;
556 for (i = 0; i < 32; i++) {
557 *list = g_new0(int8List, 1);
558 (*list)->value = i;
559 (*list)->next = NULL;
560 list = &(*list)->next;
562 break;
564 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
565 int16List **list = &cvalue->u.s16;
566 for (i = 0; i < 32; i++) {
567 *list = g_new0(int16List, 1);
568 (*list)->value = i;
569 (*list)->next = NULL;
570 list = &(*list)->next;
572 break;
574 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
575 int32List **list = &cvalue->u.s32;
576 for (i = 0; i < 32; i++) {
577 *list = g_new0(int32List, 1);
578 (*list)->value = i;
579 (*list)->next = NULL;
580 list = &(*list)->next;
582 break;
584 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
585 int64List **list = &cvalue->u.s64;
586 for (i = 0; i < 32; i++) {
587 *list = g_new0(int64List, 1);
588 (*list)->value = i;
589 (*list)->next = NULL;
590 list = &(*list)->next;
592 break;
594 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
595 uint8List **list = &cvalue->u.u8;
596 for (i = 0; i < 32; i++) {
597 *list = g_new0(uint8List, 1);
598 (*list)->value = i;
599 (*list)->next = NULL;
600 list = &(*list)->next;
602 break;
604 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
605 uint16List **list = &cvalue->u.u16;
606 for (i = 0; i < 32; i++) {
607 *list = g_new0(uint16List, 1);
608 (*list)->value = i;
609 (*list)->next = NULL;
610 list = &(*list)->next;
612 break;
614 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
615 uint32List **list = &cvalue->u.u32;
616 for (i = 0; i < 32; i++) {
617 *list = g_new0(uint32List, 1);
618 (*list)->value = i;
619 (*list)->next = NULL;
620 list = &(*list)->next;
622 break;
624 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
625 uint64List **list = &cvalue->u.u64;
626 for (i = 0; i < 32; i++) {
627 *list = g_new0(uint64List, 1);
628 (*list)->value = i;
629 (*list)->next = NULL;
630 list = &(*list)->next;
632 break;
634 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
635 boolList **list = &cvalue->u.boolean;
636 for (i = 0; i < 32; i++) {
637 *list = g_new0(boolList, 1);
638 (*list)->value = (i % 3 == 0);
639 (*list)->next = NULL;
640 list = &(*list)->next;
642 break;
644 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
645 strList **list = &cvalue->u.string;
646 for (i = 0; i < 32; i++) {
647 *list = g_new0(strList, 1);
648 (*list)->value = g_strdup_printf("%d", i);
649 (*list)->next = NULL;
650 list = &(*list)->next;
652 break;
654 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
655 numberList **list = &cvalue->u.number;
656 for (i = 0; i < 32; i++) {
657 *list = g_new0(numberList, 1);
658 (*list)->value = (double)i / 3;
659 (*list)->next = NULL;
660 list = &(*list)->next;
662 break;
664 default:
665 g_assert_not_reached();
669 static void check_native_list(QObject *qobj,
670 UserDefNativeListUnionKind kind)
672 QDict *qdict;
673 QList *qlist;
674 int i;
676 g_assert(qobj);
677 g_assert(qobject_type(qobj) == QTYPE_QDICT);
678 qdict = qobject_to_qdict(qobj);
679 g_assert(qdict);
680 g_assert(qdict_haskey(qdict, "data"));
681 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
683 switch (kind) {
684 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
685 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
686 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
687 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
688 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
689 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
690 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
691 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
692 /* all integer elements in JSON arrays get stored into QInts when
693 * we convert to QObjects, so we can check them all in the same
694 * fashion, so simply fall through here
696 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
697 for (i = 0; i < 32; i++) {
698 QObject *tmp;
699 QInt *qvalue;
700 tmp = qlist_peek(qlist);
701 g_assert(tmp);
702 qvalue = qobject_to_qint(tmp);
703 g_assert_cmpint(qint_get_int(qvalue), ==, i);
704 qobject_decref(qlist_pop(qlist));
706 break;
707 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
708 for (i = 0; i < 32; i++) {
709 QObject *tmp;
710 QBool *qvalue;
711 tmp = qlist_peek(qlist);
712 g_assert(tmp);
713 qvalue = qobject_to_qbool(tmp);
714 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
715 qobject_decref(qlist_pop(qlist));
717 break;
718 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
719 for (i = 0; i < 32; i++) {
720 QObject *tmp;
721 QString *qvalue;
722 gchar str[8];
723 tmp = qlist_peek(qlist);
724 g_assert(tmp);
725 qvalue = qobject_to_qstring(tmp);
726 sprintf(str, "%d", i);
727 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
728 qobject_decref(qlist_pop(qlist));
730 break;
731 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
732 for (i = 0; i < 32; i++) {
733 QObject *tmp;
734 QFloat *qvalue;
735 GString *double_expected = g_string_new("");
736 GString *double_actual = g_string_new("");
738 tmp = qlist_peek(qlist);
739 g_assert(tmp);
740 qvalue = qobject_to_qfloat(tmp);
741 g_string_printf(double_expected, "%.6f", (double)i / 3);
742 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
743 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
745 qobject_decref(qlist_pop(qlist));
746 g_string_free(double_expected, true);
747 g_string_free(double_actual, true);
749 break;
750 default:
751 g_assert_not_reached();
753 QDECREF(qlist);
756 static void test_native_list(TestOutputVisitorData *data,
757 const void *unused,
758 UserDefNativeListUnionKind kind)
760 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
761 Error *err = NULL;
762 QObject *obj;
764 cvalue->type = kind;
765 init_native_list(cvalue);
767 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
768 g_assert(err == NULL);
770 obj = qmp_output_get_qobject(data->qov);
771 check_native_list(obj, cvalue->type);
772 qapi_free_UserDefNativeListUnion(cvalue);
773 qobject_decref(obj);
776 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
777 const void *unused)
779 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
782 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
783 const void *unused)
785 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
788 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
789 const void *unused)
791 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
794 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
795 const void *unused)
797 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
800 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
801 const void *unused)
803 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
806 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
807 const void *unused)
809 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
812 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
813 const void *unused)
815 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
818 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
819 const void *unused)
821 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
824 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
825 const void *unused)
827 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
830 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
831 const void *unused)
833 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
836 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
837 const void *unused)
839 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
842 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
843 const void *unused)
845 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
848 static void output_visitor_test_add(const char *testpath,
849 TestOutputVisitorData *data,
850 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
852 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
853 test_func, visitor_output_teardown);
856 int main(int argc, char **argv)
858 TestOutputVisitorData out_visitor_data;
860 g_test_init(&argc, &argv, NULL);
862 output_visitor_test_add("/visitor/output/int",
863 &out_visitor_data, test_visitor_out_int);
864 output_visitor_test_add("/visitor/output/bool",
865 &out_visitor_data, test_visitor_out_bool);
866 output_visitor_test_add("/visitor/output/number",
867 &out_visitor_data, test_visitor_out_number);
868 output_visitor_test_add("/visitor/output/string",
869 &out_visitor_data, test_visitor_out_string);
870 output_visitor_test_add("/visitor/output/no-string",
871 &out_visitor_data, test_visitor_out_no_string);
872 output_visitor_test_add("/visitor/output/enum",
873 &out_visitor_data, test_visitor_out_enum);
874 output_visitor_test_add("/visitor/output/enum-errors",
875 &out_visitor_data, test_visitor_out_enum_errors);
876 output_visitor_test_add("/visitor/output/struct",
877 &out_visitor_data, test_visitor_out_struct);
878 output_visitor_test_add("/visitor/output/struct-nested",
879 &out_visitor_data, test_visitor_out_struct_nested);
880 output_visitor_test_add("/visitor/output/struct-errors",
881 &out_visitor_data, test_visitor_out_struct_errors);
882 output_visitor_test_add("/visitor/output/list",
883 &out_visitor_data, test_visitor_out_list);
884 output_visitor_test_add("/visitor/output/any",
885 &out_visitor_data, test_visitor_out_any);
886 output_visitor_test_add("/visitor/output/list-qapi-free",
887 &out_visitor_data, test_visitor_out_list_qapi_free);
888 output_visitor_test_add("/visitor/output/union-flat",
889 &out_visitor_data, test_visitor_out_union_flat);
890 output_visitor_test_add("/visitor/output/alternate",
891 &out_visitor_data, test_visitor_out_alternate);
892 output_visitor_test_add("/visitor/output/empty",
893 &out_visitor_data, test_visitor_out_empty);
894 output_visitor_test_add("/visitor/output/native_list/int",
895 &out_visitor_data,
896 test_visitor_out_native_list_int);
897 output_visitor_test_add("/visitor/output/native_list/int8",
898 &out_visitor_data,
899 test_visitor_out_native_list_int8);
900 output_visitor_test_add("/visitor/output/native_list/int16",
901 &out_visitor_data,
902 test_visitor_out_native_list_int16);
903 output_visitor_test_add("/visitor/output/native_list/int32",
904 &out_visitor_data,
905 test_visitor_out_native_list_int32);
906 output_visitor_test_add("/visitor/output/native_list/int64",
907 &out_visitor_data,
908 test_visitor_out_native_list_int64);
909 output_visitor_test_add("/visitor/output/native_list/uint8",
910 &out_visitor_data,
911 test_visitor_out_native_list_uint8);
912 output_visitor_test_add("/visitor/output/native_list/uint16",
913 &out_visitor_data,
914 test_visitor_out_native_list_uint16);
915 output_visitor_test_add("/visitor/output/native_list/uint32",
916 &out_visitor_data,
917 test_visitor_out_native_list_uint32);
918 output_visitor_test_add("/visitor/output/native_list/uint64",
919 &out_visitor_data,
920 test_visitor_out_native_list_uint64);
921 output_visitor_test_add("/visitor/output/native_list/bool",
922 &out_visitor_data,
923 test_visitor_out_native_list_bool);
924 output_visitor_test_add("/visitor/output/native_list/string",
925 &out_visitor_data,
926 test_visitor_out_native_list_str);
927 output_visitor_test_add("/visitor/output/native_list/number",
928 &out_visitor_data,
929 test_visitor_out_native_list_number);
931 g_test_run();
933 return 0;