ahci: prohibit "restarting" the FIS or CLB engines
[qemu.git] / tests / test-qmp-output-visitor.c
blob26dc752b81df5858651825fe920e18a6c521fdce
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, NULL, &value, &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, NULL, &value, &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, NULL, &value, &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, NULL, &string, &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, NULL, &string, &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, "unused", &i, &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, "unused", &bad_values[i], &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, NULL, &p, &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, "unused", &ud2, &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, "unused", &pu, &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, NULL, &head, &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, NULL, &qobj, &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, NULL, &qobj, &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, 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;
430 tmp = g_new0(UserDefAlternate, 1);
431 tmp->type = QTYPE_QINT;
432 tmp->u.i = 42;
434 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &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, NULL, &tmp, &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 /* Check that qnull reference counting is sane */
465 g_assert(arg->refcnt == 2);
466 qobject_decref(arg);
469 static void init_native_list(UserDefNativeListUnion *cvalue)
471 int i;
472 switch (cvalue->type) {
473 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
474 intList **list = &cvalue->u.integer;
475 for (i = 0; i < 32; i++) {
476 *list = g_new0(intList, 1);
477 (*list)->value = i;
478 (*list)->next = NULL;
479 list = &(*list)->next;
481 break;
483 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
484 int8List **list = &cvalue->u.s8;
485 for (i = 0; i < 32; i++) {
486 *list = g_new0(int8List, 1);
487 (*list)->value = i;
488 (*list)->next = NULL;
489 list = &(*list)->next;
491 break;
493 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
494 int16List **list = &cvalue->u.s16;
495 for (i = 0; i < 32; i++) {
496 *list = g_new0(int16List, 1);
497 (*list)->value = i;
498 (*list)->next = NULL;
499 list = &(*list)->next;
501 break;
503 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
504 int32List **list = &cvalue->u.s32;
505 for (i = 0; i < 32; i++) {
506 *list = g_new0(int32List, 1);
507 (*list)->value = i;
508 (*list)->next = NULL;
509 list = &(*list)->next;
511 break;
513 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
514 int64List **list = &cvalue->u.s64;
515 for (i = 0; i < 32; i++) {
516 *list = g_new0(int64List, 1);
517 (*list)->value = i;
518 (*list)->next = NULL;
519 list = &(*list)->next;
521 break;
523 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
524 uint8List **list = &cvalue->u.u8;
525 for (i = 0; i < 32; i++) {
526 *list = g_new0(uint8List, 1);
527 (*list)->value = i;
528 (*list)->next = NULL;
529 list = &(*list)->next;
531 break;
533 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
534 uint16List **list = &cvalue->u.u16;
535 for (i = 0; i < 32; i++) {
536 *list = g_new0(uint16List, 1);
537 (*list)->value = i;
538 (*list)->next = NULL;
539 list = &(*list)->next;
541 break;
543 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
544 uint32List **list = &cvalue->u.u32;
545 for (i = 0; i < 32; i++) {
546 *list = g_new0(uint32List, 1);
547 (*list)->value = i;
548 (*list)->next = NULL;
549 list = &(*list)->next;
551 break;
553 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
554 uint64List **list = &cvalue->u.u64;
555 for (i = 0; i < 32; i++) {
556 *list = g_new0(uint64List, 1);
557 (*list)->value = i;
558 (*list)->next = NULL;
559 list = &(*list)->next;
561 break;
563 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
564 boolList **list = &cvalue->u.boolean;
565 for (i = 0; i < 32; i++) {
566 *list = g_new0(boolList, 1);
567 (*list)->value = (i % 3 == 0);
568 (*list)->next = NULL;
569 list = &(*list)->next;
571 break;
573 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
574 strList **list = &cvalue->u.string;
575 for (i = 0; i < 32; i++) {
576 *list = g_new0(strList, 1);
577 (*list)->value = g_strdup_printf("%d", i);
578 (*list)->next = NULL;
579 list = &(*list)->next;
581 break;
583 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
584 numberList **list = &cvalue->u.number;
585 for (i = 0; i < 32; i++) {
586 *list = g_new0(numberList, 1);
587 (*list)->value = (double)i / 3;
588 (*list)->next = NULL;
589 list = &(*list)->next;
591 break;
593 default:
594 g_assert_not_reached();
598 static void check_native_list(QObject *qobj,
599 UserDefNativeListUnionKind kind)
601 QDict *qdict;
602 QList *qlist;
603 int i;
605 g_assert(qobj);
606 g_assert(qobject_type(qobj) == QTYPE_QDICT);
607 qdict = qobject_to_qdict(qobj);
608 g_assert(qdict);
609 g_assert(qdict_haskey(qdict, "data"));
610 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
612 switch (kind) {
613 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
614 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
615 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
616 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
617 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
618 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
619 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
620 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
621 /* all integer elements in JSON arrays get stored into QInts when
622 * we convert to QObjects, so we can check them all in the same
623 * fashion, so simply fall through here
625 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
626 for (i = 0; i < 32; i++) {
627 QObject *tmp;
628 QInt *qvalue;
629 tmp = qlist_peek(qlist);
630 g_assert(tmp);
631 qvalue = qobject_to_qint(tmp);
632 g_assert_cmpint(qint_get_int(qvalue), ==, i);
633 qobject_decref(qlist_pop(qlist));
635 break;
636 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
637 for (i = 0; i < 32; i++) {
638 QObject *tmp;
639 QBool *qvalue;
640 tmp = qlist_peek(qlist);
641 g_assert(tmp);
642 qvalue = qobject_to_qbool(tmp);
643 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
644 qobject_decref(qlist_pop(qlist));
646 break;
647 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
648 for (i = 0; i < 32; i++) {
649 QObject *tmp;
650 QString *qvalue;
651 gchar str[8];
652 tmp = qlist_peek(qlist);
653 g_assert(tmp);
654 qvalue = qobject_to_qstring(tmp);
655 sprintf(str, "%d", i);
656 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
657 qobject_decref(qlist_pop(qlist));
659 break;
660 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
661 for (i = 0; i < 32; i++) {
662 QObject *tmp;
663 QFloat *qvalue;
664 GString *double_expected = g_string_new("");
665 GString *double_actual = g_string_new("");
667 tmp = qlist_peek(qlist);
668 g_assert(tmp);
669 qvalue = qobject_to_qfloat(tmp);
670 g_string_printf(double_expected, "%.6f", (double)i / 3);
671 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
672 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
674 qobject_decref(qlist_pop(qlist));
675 g_string_free(double_expected, true);
676 g_string_free(double_actual, true);
678 break;
679 default:
680 g_assert_not_reached();
682 QDECREF(qlist);
685 static void test_native_list(TestOutputVisitorData *data,
686 const void *unused,
687 UserDefNativeListUnionKind kind)
689 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
690 QObject *obj;
692 cvalue->type = kind;
693 init_native_list(cvalue);
695 visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
697 obj = qmp_output_get_qobject(data->qov);
698 check_native_list(obj, cvalue->type);
699 qapi_free_UserDefNativeListUnion(cvalue);
700 qobject_decref(obj);
703 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
704 const void *unused)
706 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
709 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
710 const void *unused)
712 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
715 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
716 const void *unused)
718 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
721 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
722 const void *unused)
724 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
727 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
728 const void *unused)
730 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
733 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
734 const void *unused)
736 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
739 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
740 const void *unused)
742 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
745 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
746 const void *unused)
748 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
751 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
752 const void *unused)
754 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
757 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
758 const void *unused)
760 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
763 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
764 const void *unused)
766 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
769 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
770 const void *unused)
772 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
775 static void output_visitor_test_add(const char *testpath,
776 TestOutputVisitorData *data,
777 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
779 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
780 test_func, visitor_output_teardown);
783 int main(int argc, char **argv)
785 TestOutputVisitorData out_visitor_data;
787 g_test_init(&argc, &argv, NULL);
789 output_visitor_test_add("/visitor/output/int",
790 &out_visitor_data, test_visitor_out_int);
791 output_visitor_test_add("/visitor/output/bool",
792 &out_visitor_data, test_visitor_out_bool);
793 output_visitor_test_add("/visitor/output/number",
794 &out_visitor_data, test_visitor_out_number);
795 output_visitor_test_add("/visitor/output/string",
796 &out_visitor_data, test_visitor_out_string);
797 output_visitor_test_add("/visitor/output/no-string",
798 &out_visitor_data, test_visitor_out_no_string);
799 output_visitor_test_add("/visitor/output/enum",
800 &out_visitor_data, test_visitor_out_enum);
801 output_visitor_test_add("/visitor/output/enum-errors",
802 &out_visitor_data, test_visitor_out_enum_errors);
803 output_visitor_test_add("/visitor/output/struct",
804 &out_visitor_data, test_visitor_out_struct);
805 output_visitor_test_add("/visitor/output/struct-nested",
806 &out_visitor_data, test_visitor_out_struct_nested);
807 output_visitor_test_add("/visitor/output/struct-errors",
808 &out_visitor_data, test_visitor_out_struct_errors);
809 output_visitor_test_add("/visitor/output/list",
810 &out_visitor_data, test_visitor_out_list);
811 output_visitor_test_add("/visitor/output/any",
812 &out_visitor_data, test_visitor_out_any);
813 output_visitor_test_add("/visitor/output/list-qapi-free",
814 &out_visitor_data, test_visitor_out_list_qapi_free);
815 output_visitor_test_add("/visitor/output/union-flat",
816 &out_visitor_data, test_visitor_out_union_flat);
817 output_visitor_test_add("/visitor/output/alternate",
818 &out_visitor_data, test_visitor_out_alternate);
819 output_visitor_test_add("/visitor/output/empty",
820 &out_visitor_data, test_visitor_out_empty);
821 output_visitor_test_add("/visitor/output/native_list/int",
822 &out_visitor_data,
823 test_visitor_out_native_list_int);
824 output_visitor_test_add("/visitor/output/native_list/int8",
825 &out_visitor_data,
826 test_visitor_out_native_list_int8);
827 output_visitor_test_add("/visitor/output/native_list/int16",
828 &out_visitor_data,
829 test_visitor_out_native_list_int16);
830 output_visitor_test_add("/visitor/output/native_list/int32",
831 &out_visitor_data,
832 test_visitor_out_native_list_int32);
833 output_visitor_test_add("/visitor/output/native_list/int64",
834 &out_visitor_data,
835 test_visitor_out_native_list_int64);
836 output_visitor_test_add("/visitor/output/native_list/uint8",
837 &out_visitor_data,
838 test_visitor_out_native_list_uint8);
839 output_visitor_test_add("/visitor/output/native_list/uint16",
840 &out_visitor_data,
841 test_visitor_out_native_list_uint16);
842 output_visitor_test_add("/visitor/output/native_list/uint32",
843 &out_visitor_data,
844 test_visitor_out_native_list_uint32);
845 output_visitor_test_add("/visitor/output/native_list/uint64",
846 &out_visitor_data,
847 test_visitor_out_native_list_uint64);
848 output_visitor_test_add("/visitor/output/native_list/bool",
849 &out_visitor_data,
850 test_visitor_out_native_list_bool);
851 output_visitor_test_add("/visitor/output/native_list/string",
852 &out_visitor_data,
853 test_visitor_out_native_list_str);
854 output_visitor_test_add("/visitor/output/native_list/number",
855 &out_visitor_data,
856 test_visitor_out_native_list_number);
858 g_test_run();
860 return 0;