qemu-nbd: Replace BSDism <err.h> by error_report()
[qemu/ar7.git] / tests / test-qmp-output-visitor.c
blob30784424e4298f202ee3b502b7de24821c3885e0
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 QObject *obj;
50 visit_type_int(data->ov, &value, NULL, &error_abort);
52 obj = qmp_output_get_qobject(data->qov);
53 g_assert(obj != NULL);
54 g_assert(qobject_type(obj) == QTYPE_QINT);
55 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
57 qobject_decref(obj);
60 static void test_visitor_out_bool(TestOutputVisitorData *data,
61 const void *unused)
63 bool value = true;
64 QObject *obj;
66 visit_type_bool(data->ov, &value, NULL, &error_abort);
68 obj = qmp_output_get_qobject(data->qov);
69 g_assert(obj != NULL);
70 g_assert(qobject_type(obj) == QTYPE_QBOOL);
71 g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
73 qobject_decref(obj);
76 static void test_visitor_out_number(TestOutputVisitorData *data,
77 const void *unused)
79 double value = 3.14;
80 QObject *obj;
82 visit_type_number(data->ov, &value, NULL, &error_abort);
84 obj = qmp_output_get_qobject(data->qov);
85 g_assert(obj != NULL);
86 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
87 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
89 qobject_decref(obj);
92 static void test_visitor_out_string(TestOutputVisitorData *data,
93 const void *unused)
95 char *string = (char *) "Q E M U";
96 QObject *obj;
98 visit_type_str(data->ov, &string, NULL, &error_abort);
100 obj = qmp_output_get_qobject(data->qov);
101 g_assert(obj != NULL);
102 g_assert(qobject_type(obj) == QTYPE_QSTRING);
103 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
105 qobject_decref(obj);
108 static void test_visitor_out_no_string(TestOutputVisitorData *data,
109 const void *unused)
111 char *string = NULL;
112 QObject *obj;
114 /* A null string should return "" */
115 visit_type_str(data->ov, &string, NULL, &error_abort);
117 obj = qmp_output_get_qobject(data->qov);
118 g_assert(obj != NULL);
119 g_assert(qobject_type(obj) == QTYPE_QSTRING);
120 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
122 qobject_decref(obj);
125 static void test_visitor_out_enum(TestOutputVisitorData *data,
126 const void *unused)
128 QObject *obj;
129 EnumOne i;
131 for (i = 0; i < ENUM_ONE__MAX; i++) {
132 visit_type_EnumOne(data->ov, &i, "unused", &error_abort);
134 obj = qmp_output_get_qobject(data->qov);
135 g_assert(obj != NULL);
136 g_assert(qobject_type(obj) == QTYPE_QSTRING);
137 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
138 EnumOne_lookup[i]);
139 qobject_decref(obj);
143 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
144 const void *unused)
146 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
147 Error *err;
149 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
150 err = NULL;
151 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
152 g_assert(err);
153 error_free(err);
158 static void test_visitor_out_struct(TestOutputVisitorData *data,
159 const void *unused)
161 TestStruct test_struct = { .integer = 42,
162 .boolean = false,
163 .string = (char *) "foo"};
164 TestStruct *p = &test_struct;
165 QObject *obj;
166 QDict *qdict;
168 visit_type_TestStruct(data->ov, &p, NULL, &error_abort);
170 obj = qmp_output_get_qobject(data->qov);
171 g_assert(obj != NULL);
172 g_assert(qobject_type(obj) == QTYPE_QDICT);
174 qdict = qobject_to_qdict(obj);
175 g_assert_cmpint(qdict_size(qdict), ==, 3);
176 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
177 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
178 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
180 QDECREF(qdict);
183 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
184 const void *unused)
186 int64_t value = 42;
187 UserDefTwo *ud2;
188 QObject *obj;
189 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
190 const char *string = "user def string";
191 const char *strings[] = { "forty two", "forty three", "forty four",
192 "forty five" };
194 ud2 = g_malloc0(sizeof(*ud2));
195 ud2->string0 = g_strdup(strings[0]);
197 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
198 ud2->dict1->string1 = g_strdup(strings[1]);
200 ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
201 ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
202 ud2->dict1->dict2->userdef->string = g_strdup(string);
203 ud2->dict1->dict2->userdef->integer = value;
204 ud2->dict1->dict2->string = g_strdup(strings[2]);
206 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
207 ud2->dict1->has_dict3 = true;
208 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
209 ud2->dict1->dict3->userdef->string = g_strdup(string);
210 ud2->dict1->dict3->userdef->integer = value;
211 ud2->dict1->dict3->string = g_strdup(strings[3]);
213 visit_type_UserDefTwo(data->ov, &ud2, "unused", &error_abort);
215 obj = qmp_output_get_qobject(data->qov);
216 g_assert(obj != NULL);
217 g_assert(qobject_type(obj) == QTYPE_QDICT);
219 qdict = qobject_to_qdict(obj);
220 g_assert_cmpint(qdict_size(qdict), ==, 2);
221 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
223 dict1 = qdict_get_qdict(qdict, "dict1");
224 g_assert_cmpint(qdict_size(dict1), ==, 3);
225 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
227 dict2 = qdict_get_qdict(dict1, "dict2");
228 g_assert_cmpint(qdict_size(dict2), ==, 2);
229 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
230 userdef = qdict_get_qdict(dict2, "userdef");
231 g_assert_cmpint(qdict_size(userdef), ==, 2);
232 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
233 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
235 dict3 = qdict_get_qdict(dict1, "dict3");
236 g_assert_cmpint(qdict_size(dict3), ==, 2);
237 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
238 userdef = qdict_get_qdict(dict3, "userdef");
239 g_assert_cmpint(qdict_size(userdef), ==, 2);
240 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
241 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
243 QDECREF(qdict);
244 qapi_free_UserDefTwo(ud2);
247 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
248 const void *unused)
250 EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
251 UserDefOne u = {0};
252 UserDefOne *pu = &u;
253 Error *err;
254 int i;
256 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
257 err = NULL;
258 u.has_enum1 = true;
259 u.enum1 = bad_values[i];
260 visit_type_UserDefOne(data->ov, &pu, "unused", &err);
261 g_assert(err);
262 error_free(err);
267 static void test_visitor_out_list(TestOutputVisitorData *data,
268 const void *unused)
270 const char *value_str = "list value";
271 TestStructList *p, *head = NULL;
272 const int max_items = 10;
273 bool value_bool = true;
274 int value_int = 10;
275 QListEntry *entry;
276 QObject *obj;
277 QList *qlist;
278 int i;
280 /* Build the list in reverse order... */
281 for (i = 0; i < max_items; i++) {
282 p = g_malloc0(sizeof(*p));
283 p->value = g_malloc0(sizeof(*p->value));
284 p->value->integer = value_int + (max_items - i - 1);
285 p->value->boolean = value_bool;
286 p->value->string = g_strdup(value_str);
288 p->next = head;
289 head = p;
292 visit_type_TestStructList(data->ov, &head, NULL, &error_abort);
294 obj = qmp_output_get_qobject(data->qov);
295 g_assert(obj != NULL);
296 g_assert(qobject_type(obj) == QTYPE_QLIST);
298 qlist = qobject_to_qlist(obj);
299 g_assert(!qlist_empty(qlist));
301 /* ...and ensure that the visitor sees it in order */
302 i = 0;
303 QLIST_FOREACH_ENTRY(qlist, entry) {
304 QDict *qdict;
306 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
307 qdict = qobject_to_qdict(entry->value);
308 g_assert_cmpint(qdict_size(qdict), ==, 3);
309 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
310 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
311 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
312 i++;
314 g_assert_cmpint(i, ==, max_items);
316 QDECREF(qlist);
317 qapi_free_TestStructList(head);
320 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
321 const void *unused)
323 UserDefTwoList *p, *head = NULL;
324 const char string[] = "foo bar";
325 int i, max_count = 1024;
327 for (i = 0; i < max_count; i++) {
328 p = g_malloc0(sizeof(*p));
329 p->value = g_malloc0(sizeof(*p->value));
331 p->value->string0 = g_strdup(string);
332 p->value->dict1 = g_new0(UserDefTwoDict, 1);
333 p->value->dict1->string1 = g_strdup(string);
334 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
335 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
336 p->value->dict1->dict2->userdef->string = g_strdup(string);
337 p->value->dict1->dict2->userdef->integer = 42;
338 p->value->dict1->dict2->string = g_strdup(string);
339 p->value->dict1->has_dict3 = false;
341 p->next = head;
342 head = p;
345 qapi_free_UserDefTwoList(head);
348 static void test_visitor_out_any(TestOutputVisitorData *data,
349 const void *unused)
351 QObject *qobj;
352 QInt *qint;
353 QBool *qbool;
354 QString *qstring;
355 QDict *qdict;
356 QObject *obj;
358 qobj = QOBJECT(qint_from_int(-42));
359 visit_type_any(data->ov, &qobj, NULL, &error_abort);
360 obj = qmp_output_get_qobject(data->qov);
361 g_assert(obj != NULL);
362 g_assert(qobject_type(obj) == QTYPE_QINT);
363 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
364 qobject_decref(obj);
365 qobject_decref(qobj);
367 qdict = qdict_new();
368 qdict_put(qdict, "integer", qint_from_int(-42));
369 qdict_put(qdict, "boolean", qbool_from_bool(true));
370 qdict_put(qdict, "string", qstring_from_str("foo"));
371 qobj = QOBJECT(qdict);
372 visit_type_any(data->ov, &qobj, NULL, &error_abort);
373 qobject_decref(qobj);
374 obj = qmp_output_get_qobject(data->qov);
375 g_assert(obj != NULL);
376 qdict = qobject_to_qdict(obj);
377 g_assert(qdict);
378 qobj = qdict_get(qdict, "integer");
379 g_assert(qobj);
380 qint = qobject_to_qint(qobj);
381 g_assert(qint);
382 g_assert_cmpint(qint_get_int(qint), ==, -42);
383 qobj = qdict_get(qdict, "boolean");
384 g_assert(qobj);
385 qbool = qobject_to_qbool(qobj);
386 g_assert(qbool);
387 g_assert(qbool_get_bool(qbool) == true);
388 qobj = qdict_get(qdict, "string");
389 g_assert(qobj);
390 qstring = qobject_to_qstring(qobj);
391 g_assert(qstring);
392 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
393 qobject_decref(obj);
396 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
397 const void *unused)
399 QObject *arg;
400 QDict *qdict;
402 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
403 tmp->enum1 = ENUM_ONE_VALUE1;
404 tmp->string = g_strdup("str");
405 tmp->u.value1 = g_malloc0(sizeof(UserDefA));
406 tmp->integer = 41;
407 tmp->u.value1->boolean = true;
409 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &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;
430 tmp = g_new0(UserDefAlternate, 1);
431 tmp->type = QTYPE_QINT;
432 tmp->u.i = 42;
434 visit_type_UserDefAlternate(data->ov, &tmp, NULL, &error_abort);
435 arg = qmp_output_get_qobject(data->qov);
437 g_assert(qobject_type(arg) == QTYPE_QINT);
438 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
440 qapi_free_UserDefAlternate(tmp);
441 qobject_decref(arg);
443 tmp = g_new0(UserDefAlternate, 1);
444 tmp->type = QTYPE_QSTRING;
445 tmp->u.s = g_strdup("hello");
447 visit_type_UserDefAlternate(data->ov, &tmp, NULL, &error_abort);
448 arg = qmp_output_get_qobject(data->qov);
450 g_assert(qobject_type(arg) == QTYPE_QSTRING);
451 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");
453 qapi_free_UserDefAlternate(tmp);
454 qobject_decref(arg);
457 static void test_visitor_out_empty(TestOutputVisitorData *data,
458 const void *unused)
460 QObject *arg;
462 arg = qmp_output_get_qobject(data->qov);
463 g_assert(qobject_type(arg) == QTYPE_QNULL);
464 qobject_decref(arg);
467 static void init_native_list(UserDefNativeListUnion *cvalue)
469 int i;
470 switch (cvalue->type) {
471 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
472 intList **list = &cvalue->u.integer;
473 for (i = 0; i < 32; i++) {
474 *list = g_new0(intList, 1);
475 (*list)->value = i;
476 (*list)->next = NULL;
477 list = &(*list)->next;
479 break;
481 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
482 int8List **list = &cvalue->u.s8;
483 for (i = 0; i < 32; i++) {
484 *list = g_new0(int8List, 1);
485 (*list)->value = i;
486 (*list)->next = NULL;
487 list = &(*list)->next;
489 break;
491 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
492 int16List **list = &cvalue->u.s16;
493 for (i = 0; i < 32; i++) {
494 *list = g_new0(int16List, 1);
495 (*list)->value = i;
496 (*list)->next = NULL;
497 list = &(*list)->next;
499 break;
501 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
502 int32List **list = &cvalue->u.s32;
503 for (i = 0; i < 32; i++) {
504 *list = g_new0(int32List, 1);
505 (*list)->value = i;
506 (*list)->next = NULL;
507 list = &(*list)->next;
509 break;
511 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
512 int64List **list = &cvalue->u.s64;
513 for (i = 0; i < 32; i++) {
514 *list = g_new0(int64List, 1);
515 (*list)->value = i;
516 (*list)->next = NULL;
517 list = &(*list)->next;
519 break;
521 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
522 uint8List **list = &cvalue->u.u8;
523 for (i = 0; i < 32; i++) {
524 *list = g_new0(uint8List, 1);
525 (*list)->value = i;
526 (*list)->next = NULL;
527 list = &(*list)->next;
529 break;
531 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
532 uint16List **list = &cvalue->u.u16;
533 for (i = 0; i < 32; i++) {
534 *list = g_new0(uint16List, 1);
535 (*list)->value = i;
536 (*list)->next = NULL;
537 list = &(*list)->next;
539 break;
541 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
542 uint32List **list = &cvalue->u.u32;
543 for (i = 0; i < 32; i++) {
544 *list = g_new0(uint32List, 1);
545 (*list)->value = i;
546 (*list)->next = NULL;
547 list = &(*list)->next;
549 break;
551 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
552 uint64List **list = &cvalue->u.u64;
553 for (i = 0; i < 32; i++) {
554 *list = g_new0(uint64List, 1);
555 (*list)->value = i;
556 (*list)->next = NULL;
557 list = &(*list)->next;
559 break;
561 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
562 boolList **list = &cvalue->u.boolean;
563 for (i = 0; i < 32; i++) {
564 *list = g_new0(boolList, 1);
565 (*list)->value = (i % 3 == 0);
566 (*list)->next = NULL;
567 list = &(*list)->next;
569 break;
571 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
572 strList **list = &cvalue->u.string;
573 for (i = 0; i < 32; i++) {
574 *list = g_new0(strList, 1);
575 (*list)->value = g_strdup_printf("%d", i);
576 (*list)->next = NULL;
577 list = &(*list)->next;
579 break;
581 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
582 numberList **list = &cvalue->u.number;
583 for (i = 0; i < 32; i++) {
584 *list = g_new0(numberList, 1);
585 (*list)->value = (double)i / 3;
586 (*list)->next = NULL;
587 list = &(*list)->next;
589 break;
591 default:
592 g_assert_not_reached();
596 static void check_native_list(QObject *qobj,
597 UserDefNativeListUnionKind kind)
599 QDict *qdict;
600 QList *qlist;
601 int i;
603 g_assert(qobj);
604 g_assert(qobject_type(qobj) == QTYPE_QDICT);
605 qdict = qobject_to_qdict(qobj);
606 g_assert(qdict);
607 g_assert(qdict_haskey(qdict, "data"));
608 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
610 switch (kind) {
611 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
612 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
613 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
614 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
615 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
616 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
617 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
618 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
619 /* all integer elements in JSON arrays get stored into QInts when
620 * we convert to QObjects, so we can check them all in the same
621 * fashion, so simply fall through here
623 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
624 for (i = 0; i < 32; i++) {
625 QObject *tmp;
626 QInt *qvalue;
627 tmp = qlist_peek(qlist);
628 g_assert(tmp);
629 qvalue = qobject_to_qint(tmp);
630 g_assert_cmpint(qint_get_int(qvalue), ==, i);
631 qobject_decref(qlist_pop(qlist));
633 break;
634 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
635 for (i = 0; i < 32; i++) {
636 QObject *tmp;
637 QBool *qvalue;
638 tmp = qlist_peek(qlist);
639 g_assert(tmp);
640 qvalue = qobject_to_qbool(tmp);
641 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
642 qobject_decref(qlist_pop(qlist));
644 break;
645 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
646 for (i = 0; i < 32; i++) {
647 QObject *tmp;
648 QString *qvalue;
649 gchar str[8];
650 tmp = qlist_peek(qlist);
651 g_assert(tmp);
652 qvalue = qobject_to_qstring(tmp);
653 sprintf(str, "%d", i);
654 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
655 qobject_decref(qlist_pop(qlist));
657 break;
658 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
659 for (i = 0; i < 32; i++) {
660 QObject *tmp;
661 QFloat *qvalue;
662 GString *double_expected = g_string_new("");
663 GString *double_actual = g_string_new("");
665 tmp = qlist_peek(qlist);
666 g_assert(tmp);
667 qvalue = qobject_to_qfloat(tmp);
668 g_string_printf(double_expected, "%.6f", (double)i / 3);
669 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
670 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
672 qobject_decref(qlist_pop(qlist));
673 g_string_free(double_expected, true);
674 g_string_free(double_actual, true);
676 break;
677 default:
678 g_assert_not_reached();
680 QDECREF(qlist);
683 static void test_native_list(TestOutputVisitorData *data,
684 const void *unused,
685 UserDefNativeListUnionKind kind)
687 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
688 QObject *obj;
690 cvalue->type = kind;
691 init_native_list(cvalue);
693 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &error_abort);
695 obj = qmp_output_get_qobject(data->qov);
696 check_native_list(obj, cvalue->type);
697 qapi_free_UserDefNativeListUnion(cvalue);
698 qobject_decref(obj);
701 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
702 const void *unused)
704 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
707 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
708 const void *unused)
710 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
713 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
714 const void *unused)
716 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
719 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
720 const void *unused)
722 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
725 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
726 const void *unused)
728 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
731 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
732 const void *unused)
734 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
737 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
738 const void *unused)
740 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
743 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
744 const void *unused)
746 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
749 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
750 const void *unused)
752 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
755 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
756 const void *unused)
758 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
761 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
762 const void *unused)
764 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
767 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
768 const void *unused)
770 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
773 static void output_visitor_test_add(const char *testpath,
774 TestOutputVisitorData *data,
775 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
777 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
778 test_func, visitor_output_teardown);
781 int main(int argc, char **argv)
783 TestOutputVisitorData out_visitor_data;
785 g_test_init(&argc, &argv, NULL);
787 output_visitor_test_add("/visitor/output/int",
788 &out_visitor_data, test_visitor_out_int);
789 output_visitor_test_add("/visitor/output/bool",
790 &out_visitor_data, test_visitor_out_bool);
791 output_visitor_test_add("/visitor/output/number",
792 &out_visitor_data, test_visitor_out_number);
793 output_visitor_test_add("/visitor/output/string",
794 &out_visitor_data, test_visitor_out_string);
795 output_visitor_test_add("/visitor/output/no-string",
796 &out_visitor_data, test_visitor_out_no_string);
797 output_visitor_test_add("/visitor/output/enum",
798 &out_visitor_data, test_visitor_out_enum);
799 output_visitor_test_add("/visitor/output/enum-errors",
800 &out_visitor_data, test_visitor_out_enum_errors);
801 output_visitor_test_add("/visitor/output/struct",
802 &out_visitor_data, test_visitor_out_struct);
803 output_visitor_test_add("/visitor/output/struct-nested",
804 &out_visitor_data, test_visitor_out_struct_nested);
805 output_visitor_test_add("/visitor/output/struct-errors",
806 &out_visitor_data, test_visitor_out_struct_errors);
807 output_visitor_test_add("/visitor/output/list",
808 &out_visitor_data, test_visitor_out_list);
809 output_visitor_test_add("/visitor/output/any",
810 &out_visitor_data, test_visitor_out_any);
811 output_visitor_test_add("/visitor/output/list-qapi-free",
812 &out_visitor_data, test_visitor_out_list_qapi_free);
813 output_visitor_test_add("/visitor/output/union-flat",
814 &out_visitor_data, test_visitor_out_union_flat);
815 output_visitor_test_add("/visitor/output/alternate",
816 &out_visitor_data, test_visitor_out_alternate);
817 output_visitor_test_add("/visitor/output/empty",
818 &out_visitor_data, test_visitor_out_empty);
819 output_visitor_test_add("/visitor/output/native_list/int",
820 &out_visitor_data,
821 test_visitor_out_native_list_int);
822 output_visitor_test_add("/visitor/output/native_list/int8",
823 &out_visitor_data,
824 test_visitor_out_native_list_int8);
825 output_visitor_test_add("/visitor/output/native_list/int16",
826 &out_visitor_data,
827 test_visitor_out_native_list_int16);
828 output_visitor_test_add("/visitor/output/native_list/int32",
829 &out_visitor_data,
830 test_visitor_out_native_list_int32);
831 output_visitor_test_add("/visitor/output/native_list/int64",
832 &out_visitor_data,
833 test_visitor_out_native_list_int64);
834 output_visitor_test_add("/visitor/output/native_list/uint8",
835 &out_visitor_data,
836 test_visitor_out_native_list_uint8);
837 output_visitor_test_add("/visitor/output/native_list/uint16",
838 &out_visitor_data,
839 test_visitor_out_native_list_uint16);
840 output_visitor_test_add("/visitor/output/native_list/uint32",
841 &out_visitor_data,
842 test_visitor_out_native_list_uint32);
843 output_visitor_test_add("/visitor/output/native_list/uint64",
844 &out_visitor_data,
845 test_visitor_out_native_list_uint64);
846 output_visitor_test_add("/visitor/output/native_list/bool",
847 &out_visitor_data,
848 test_visitor_out_native_list_bool);
849 output_visitor_test_add("/visitor/output/native_list/string",
850 &out_visitor_data,
851 test_visitor_out_native_list_str);
852 output_visitor_test_add("/visitor/output/native_list/number",
853 &out_visitor_data,
854 test_visitor_out_native_list_number);
856 g_test_run();
858 return 0;