vhost-user: fix use after free
[qemu.git] / tests / test-qmp-output-visitor.c
bloba7f8b45c77a1bf5e7d45e3f45a5e1edb46a8db55
1 /*
2 * QMP Output Visitor unit-tests.
4 * Copyright (C) 2011-2016 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 "qemu/osdep.h"
14 #include <glib.h>
16 #include "qemu-common.h"
17 #include "qapi/qmp-output-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestOutputVisitorData {
23 QmpOutputVisitor *qov;
24 Visitor *ov;
25 } TestOutputVisitorData;
27 static void visitor_output_setup(TestOutputVisitorData *data,
28 const void *unused)
30 data->qov = qmp_output_visitor_new();
31 g_assert(data->qov != NULL);
33 data->ov = qmp_output_get_visitor(data->qov);
34 g_assert(data->ov != NULL);
37 static void visitor_output_teardown(TestOutputVisitorData *data,
38 const void *unused)
40 qmp_output_visitor_cleanup(data->qov);
41 data->qov = NULL;
42 data->ov = NULL;
45 static void test_visitor_out_int(TestOutputVisitorData *data,
46 const void *unused)
48 int64_t value = -42;
49 QObject *obj;
51 visit_type_int(data->ov, NULL, &value, &error_abort);
53 obj = qmp_output_get_qobject(data->qov);
54 g_assert(obj != NULL);
55 g_assert(qobject_type(obj) == QTYPE_QINT);
56 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
58 qobject_decref(obj);
61 static void test_visitor_out_bool(TestOutputVisitorData *data,
62 const void *unused)
64 bool value = true;
65 QObject *obj;
67 visit_type_bool(data->ov, NULL, &value, &error_abort);
69 obj = qmp_output_get_qobject(data->qov);
70 g_assert(obj != NULL);
71 g_assert(qobject_type(obj) == QTYPE_QBOOL);
72 g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
74 qobject_decref(obj);
77 static void test_visitor_out_number(TestOutputVisitorData *data,
78 const void *unused)
80 double value = 3.14;
81 QObject *obj;
83 visit_type_number(data->ov, NULL, &value, &error_abort);
85 obj = qmp_output_get_qobject(data->qov);
86 g_assert(obj != NULL);
87 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
88 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
90 qobject_decref(obj);
93 static void test_visitor_out_string(TestOutputVisitorData *data,
94 const void *unused)
96 char *string = (char *) "Q E M U";
97 QObject *obj;
99 visit_type_str(data->ov, NULL, &string, &error_abort);
101 obj = qmp_output_get_qobject(data->qov);
102 g_assert(obj != NULL);
103 g_assert(qobject_type(obj) == QTYPE_QSTRING);
104 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
106 qobject_decref(obj);
109 static void test_visitor_out_no_string(TestOutputVisitorData *data,
110 const void *unused)
112 char *string = NULL;
113 QObject *obj;
115 /* A null string should return "" */
116 visit_type_str(data->ov, NULL, &string, &error_abort);
118 obj = qmp_output_get_qobject(data->qov);
119 g_assert(obj != NULL);
120 g_assert(qobject_type(obj) == QTYPE_QSTRING);
121 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
123 qobject_decref(obj);
126 static void test_visitor_out_enum(TestOutputVisitorData *data,
127 const void *unused)
129 QObject *obj;
130 EnumOne i;
132 for (i = 0; i < ENUM_ONE__MAX; i++) {
133 visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
135 obj = qmp_output_get_qobject(data->qov);
136 g_assert(obj != NULL);
137 g_assert(qobject_type(obj) == QTYPE_QSTRING);
138 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
139 EnumOne_lookup[i]);
140 qobject_decref(obj);
144 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
145 const void *unused)
147 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
148 Error *err;
150 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
151 err = NULL;
152 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
153 g_assert(err);
154 error_free(err);
159 static void test_visitor_out_struct(TestOutputVisitorData *data,
160 const void *unused)
162 TestStruct test_struct = { .integer = 42,
163 .boolean = false,
164 .string = (char *) "foo"};
165 TestStruct *p = &test_struct;
166 QObject *obj;
167 QDict *qdict;
169 visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
171 obj = qmp_output_get_qobject(data->qov);
172 g_assert(obj != NULL);
173 g_assert(qobject_type(obj) == QTYPE_QDICT);
175 qdict = qobject_to_qdict(obj);
176 g_assert_cmpint(qdict_size(qdict), ==, 3);
177 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
178 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
179 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
181 QDECREF(qdict);
184 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
185 const void *unused)
187 int64_t value = 42;
188 UserDefTwo *ud2;
189 QObject *obj;
190 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
191 const char *string = "user def string";
192 const char *strings[] = { "forty two", "forty three", "forty four",
193 "forty five" };
195 ud2 = g_malloc0(sizeof(*ud2));
196 ud2->string0 = g_strdup(strings[0]);
198 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
199 ud2->dict1->string1 = g_strdup(strings[1]);
201 ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
202 ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
203 ud2->dict1->dict2->userdef->string = g_strdup(string);
204 ud2->dict1->dict2->userdef->integer = value;
205 ud2->dict1->dict2->string = g_strdup(strings[2]);
207 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
208 ud2->dict1->has_dict3 = true;
209 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
210 ud2->dict1->dict3->userdef->string = g_strdup(string);
211 ud2->dict1->dict3->userdef->integer = value;
212 ud2->dict1->dict3->string = g_strdup(strings[3]);
214 visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
216 obj = qmp_output_get_qobject(data->qov);
217 g_assert(obj != NULL);
218 g_assert(qobject_type(obj) == QTYPE_QDICT);
220 qdict = qobject_to_qdict(obj);
221 g_assert_cmpint(qdict_size(qdict), ==, 2);
222 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
224 dict1 = qdict_get_qdict(qdict, "dict1");
225 g_assert_cmpint(qdict_size(dict1), ==, 3);
226 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
228 dict2 = qdict_get_qdict(dict1, "dict2");
229 g_assert_cmpint(qdict_size(dict2), ==, 2);
230 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
231 userdef = qdict_get_qdict(dict2, "userdef");
232 g_assert_cmpint(qdict_size(userdef), ==, 2);
233 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
234 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
236 dict3 = qdict_get_qdict(dict1, "dict3");
237 g_assert_cmpint(qdict_size(dict3), ==, 2);
238 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
239 userdef = qdict_get_qdict(dict3, "userdef");
240 g_assert_cmpint(qdict_size(userdef), ==, 2);
241 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
242 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
244 QDECREF(qdict);
245 qapi_free_UserDefTwo(ud2);
248 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
249 const void *unused)
251 EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
252 UserDefOne u = {0};
253 UserDefOne *pu = &u;
254 Error *err;
255 int i;
257 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
258 err = NULL;
259 u.has_enum1 = true;
260 u.enum1 = bad_values[i];
261 visit_type_UserDefOne(data->ov, "unused", &pu, &err);
262 g_assert(err);
263 error_free(err);
268 static void test_visitor_out_list(TestOutputVisitorData *data,
269 const void *unused)
271 const char *value_str = "list value";
272 TestStructList *p, *head = NULL;
273 const int max_items = 10;
274 bool value_bool = true;
275 int value_int = 10;
276 QListEntry *entry;
277 QObject *obj;
278 QList *qlist;
279 int i;
281 /* Build the list in reverse order... */
282 for (i = 0; i < max_items; i++) {
283 p = g_malloc0(sizeof(*p));
284 p->value = g_malloc0(sizeof(*p->value));
285 p->value->integer = value_int + (max_items - i - 1);
286 p->value->boolean = value_bool;
287 p->value->string = g_strdup(value_str);
289 p->next = head;
290 head = p;
293 visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
295 obj = qmp_output_get_qobject(data->qov);
296 g_assert(obj != NULL);
297 g_assert(qobject_type(obj) == QTYPE_QLIST);
299 qlist = qobject_to_qlist(obj);
300 g_assert(!qlist_empty(qlist));
302 /* ...and ensure that the visitor sees it in order */
303 i = 0;
304 QLIST_FOREACH_ENTRY(qlist, entry) {
305 QDict *qdict;
307 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
308 qdict = qobject_to_qdict(entry->value);
309 g_assert_cmpint(qdict_size(qdict), ==, 3);
310 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
311 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
312 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
313 i++;
315 g_assert_cmpint(i, ==, max_items);
317 QDECREF(qlist);
318 qapi_free_TestStructList(head);
321 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
322 const void *unused)
324 UserDefTwoList *p, *head = NULL;
325 const char string[] = "foo bar";
326 int i, max_count = 1024;
328 for (i = 0; i < max_count; i++) {
329 p = g_malloc0(sizeof(*p));
330 p->value = g_malloc0(sizeof(*p->value));
332 p->value->string0 = g_strdup(string);
333 p->value->dict1 = g_new0(UserDefTwoDict, 1);
334 p->value->dict1->string1 = g_strdup(string);
335 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
336 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
337 p->value->dict1->dict2->userdef->string = g_strdup(string);
338 p->value->dict1->dict2->userdef->integer = 42;
339 p->value->dict1->dict2->string = g_strdup(string);
340 p->value->dict1->has_dict3 = false;
342 p->next = head;
343 head = p;
346 qapi_free_UserDefTwoList(head);
349 static void test_visitor_out_any(TestOutputVisitorData *data,
350 const void *unused)
352 QObject *qobj;
353 QInt *qint;
354 QBool *qbool;
355 QString *qstring;
356 QDict *qdict;
357 QObject *obj;
359 qobj = QOBJECT(qint_from_int(-42));
360 visit_type_any(data->ov, NULL, &qobj, &error_abort);
361 obj = qmp_output_get_qobject(data->qov);
362 g_assert(obj != NULL);
363 g_assert(qobject_type(obj) == QTYPE_QINT);
364 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
365 qobject_decref(obj);
366 qobject_decref(qobj);
368 qdict = qdict_new();
369 qdict_put(qdict, "integer", qint_from_int(-42));
370 qdict_put(qdict, "boolean", qbool_from_bool(true));
371 qdict_put(qdict, "string", qstring_from_str("foo"));
372 qobj = QOBJECT(qdict);
373 visit_type_any(data->ov, NULL, &qobj, &error_abort);
374 qobject_decref(qobj);
375 obj = qmp_output_get_qobject(data->qov);
376 g_assert(obj != NULL);
377 qdict = qobject_to_qdict(obj);
378 g_assert(qdict);
379 qobj = qdict_get(qdict, "integer");
380 g_assert(qobj);
381 qint = qobject_to_qint(qobj);
382 g_assert(qint);
383 g_assert_cmpint(qint_get_int(qint), ==, -42);
384 qobj = qdict_get(qdict, "boolean");
385 g_assert(qobj);
386 qbool = qobject_to_qbool(qobj);
387 g_assert(qbool);
388 g_assert(qbool_get_bool(qbool) == true);
389 qobj = qdict_get(qdict, "string");
390 g_assert(qobj);
391 qstring = qobject_to_qstring(qobj);
392 g_assert(qstring);
393 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
394 qobject_decref(obj);
397 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
398 const void *unused)
400 QObject *arg;
401 QDict *qdict;
403 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
404 tmp->enum1 = ENUM_ONE_VALUE1;
405 tmp->string = g_strdup("str");
406 tmp->integer = 41;
407 tmp->u.value1.boolean = true;
409 visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
410 arg = qmp_output_get_qobject(data->qov);
412 g_assert(qobject_type(arg) == QTYPE_QDICT);
413 qdict = qobject_to_qdict(arg);
415 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
416 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
417 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
418 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
420 qapi_free_UserDefFlatUnion(tmp);
421 QDECREF(qdict);
424 static void test_visitor_out_alternate(TestOutputVisitorData *data,
425 const void *unused)
427 QObject *arg;
428 UserDefAlternate *tmp;
429 QDict *qdict;
431 tmp = g_new0(UserDefAlternate, 1);
432 tmp->type = QTYPE_QINT;
433 tmp->u.i = 42;
435 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
436 arg = qmp_output_get_qobject(data->qov);
438 g_assert(qobject_type(arg) == QTYPE_QINT);
439 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
441 qapi_free_UserDefAlternate(tmp);
442 qobject_decref(arg);
444 tmp = g_new0(UserDefAlternate, 1);
445 tmp->type = QTYPE_QSTRING;
446 tmp->u.s = g_strdup("hello");
448 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
449 arg = qmp_output_get_qobject(data->qov);
451 g_assert(qobject_type(arg) == QTYPE_QSTRING);
452 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");
454 qapi_free_UserDefAlternate(tmp);
455 qobject_decref(arg);
457 tmp = g_new0(UserDefAlternate, 1);
458 tmp->type = QTYPE_QDICT;
459 tmp->u.udfu.integer = 1;
460 tmp->u.udfu.string = g_strdup("str");
461 tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
462 tmp->u.udfu.u.value1.boolean = true;
464 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
465 arg = qmp_output_get_qobject(data->qov);
467 g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT);
468 qdict = qobject_to_qdict(arg);
469 g_assert_cmpint(qdict_size(qdict), ==, 4);
470 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1);
471 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
472 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
473 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
475 qapi_free_UserDefAlternate(tmp);
476 qobject_decref(arg);
479 static void test_visitor_out_empty(TestOutputVisitorData *data,
480 const void *unused)
482 QObject *arg;
484 arg = qmp_output_get_qobject(data->qov);
485 g_assert(qobject_type(arg) == QTYPE_QNULL);
486 /* Check that qnull reference counting is sane */
487 g_assert(arg->refcnt == 2);
488 qobject_decref(arg);
491 static void init_native_list(UserDefNativeListUnion *cvalue)
493 int i;
494 switch (cvalue->type) {
495 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
496 intList **list = &cvalue->u.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;
503 break;
505 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
506 int8List **list = &cvalue->u.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;
513 break;
515 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
516 int16List **list = &cvalue->u.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;
523 break;
525 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
526 int32List **list = &cvalue->u.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;
533 break;
535 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
536 int64List **list = &cvalue->u.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;
543 break;
545 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
546 uint8List **list = &cvalue->u.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;
553 break;
555 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
556 uint16List **list = &cvalue->u.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;
563 break;
565 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
566 uint32List **list = &cvalue->u.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;
573 break;
575 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
576 uint64List **list = &cvalue->u.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;
583 break;
585 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
586 boolList **list = &cvalue->u.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;
593 break;
595 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
596 strList **list = &cvalue->u.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;
603 break;
605 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
606 numberList **list = &cvalue->u.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;
613 break;
615 default:
616 g_assert_not_reached();
620 static void check_native_list(QObject *qobj,
621 UserDefNativeListUnionKind kind)
623 QDict *qdict;
624 QList *qlist;
625 int i;
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")));
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
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));
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));
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));
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("");
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);
696 qobject_decref(qlist_pop(qlist));
697 g_string_free(double_expected, true);
698 g_string_free(double_actual, true);
700 break;
701 default:
702 g_assert_not_reached();
704 QDECREF(qlist);
707 static void test_native_list(TestOutputVisitorData *data,
708 const void *unused,
709 UserDefNativeListUnionKind kind)
711 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
712 QObject *obj;
714 cvalue->type = kind;
715 init_native_list(cvalue);
717 visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
719 obj = qmp_output_get_qobject(data->qov);
720 check_native_list(obj, cvalue->type);
721 qapi_free_UserDefNativeListUnion(cvalue);
722 qobject_decref(obj);
725 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
726 const void *unused)
728 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
731 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
732 const void *unused)
734 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
737 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
738 const void *unused)
740 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
743 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
744 const void *unused)
746 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
749 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
750 const void *unused)
752 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
755 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
756 const void *unused)
758 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
761 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
762 const void *unused)
764 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
767 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
768 const void *unused)
770 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
773 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
774 const void *unused)
776 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
779 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
780 const void *unused)
782 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
785 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
786 const void *unused)
788 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
791 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
792 const void *unused)
794 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
797 static void output_visitor_test_add(const char *testpath,
798 TestOutputVisitorData *data,
799 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
801 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
802 test_func, visitor_output_teardown);
805 int main(int argc, char **argv)
807 TestOutputVisitorData out_visitor_data;
809 g_test_init(&argc, &argv, NULL);
811 output_visitor_test_add("/visitor/output/int",
812 &out_visitor_data, test_visitor_out_int);
813 output_visitor_test_add("/visitor/output/bool",
814 &out_visitor_data, test_visitor_out_bool);
815 output_visitor_test_add("/visitor/output/number",
816 &out_visitor_data, test_visitor_out_number);
817 output_visitor_test_add("/visitor/output/string",
818 &out_visitor_data, test_visitor_out_string);
819 output_visitor_test_add("/visitor/output/no-string",
820 &out_visitor_data, test_visitor_out_no_string);
821 output_visitor_test_add("/visitor/output/enum",
822 &out_visitor_data, test_visitor_out_enum);
823 output_visitor_test_add("/visitor/output/enum-errors",
824 &out_visitor_data, test_visitor_out_enum_errors);
825 output_visitor_test_add("/visitor/output/struct",
826 &out_visitor_data, test_visitor_out_struct);
827 output_visitor_test_add("/visitor/output/struct-nested",
828 &out_visitor_data, test_visitor_out_struct_nested);
829 output_visitor_test_add("/visitor/output/struct-errors",
830 &out_visitor_data, test_visitor_out_struct_errors);
831 output_visitor_test_add("/visitor/output/list",
832 &out_visitor_data, test_visitor_out_list);
833 output_visitor_test_add("/visitor/output/any",
834 &out_visitor_data, test_visitor_out_any);
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);
880 g_test_run();
882 return 0;