target-i386: Don't declare variables in the middle of blocks
[qemu/kevin.git] / tests / test-qmp-output-visitor.c
blobda279713f2bf7495cf521e0e758a45c7e370453b
1 /*
2 * QMP Output Visitor unit-tests.
4 * Copyright (C) 2011 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 *errp = NULL;
49 QObject *obj;
51 visit_type_int(data->ov, &value, NULL, &errp);
52 g_assert(!errp);
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 *errp = NULL;
66 bool value = true;
67 QObject *obj;
69 visit_type_bool(data->ov, &value, NULL, &errp);
70 g_assert(!errp);
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_int(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 *errp = NULL;
85 QObject *obj;
87 visit_type_number(data->ov, &value, NULL, &errp);
88 g_assert(!errp);
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 *errp = NULL;
103 QObject *obj;
105 visit_type_str(data->ov, &string, NULL, &errp);
106 g_assert(!errp);
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 *errp = NULL;
121 QObject *obj;
123 /* A null string should return "" */
124 visit_type_str(data->ov, &string, NULL, &errp);
125 g_assert(!errp);
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 *errp = NULL;
139 QObject *obj;
140 EnumOne i;
142 for (i = 0; i < ENUM_ONE_MAX; i++) {
143 visit_type_EnumOne(data->ov, &i, "unused", &errp);
144 g_assert(!errp);
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 *errp;
161 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
162 errp = NULL;
163 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
164 g_assert(errp);
165 error_free(errp);
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 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
180 errp);
182 visit_type_int(v, &(*obj)->integer, "integer", errp);
183 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
184 visit_type_str(v, &(*obj)->string, "string", errp);
186 visit_end_struct(v, errp);
189 static void test_visitor_out_struct(TestOutputVisitorData *data,
190 const void *unused)
192 TestStruct test_struct = { .integer = 42,
193 .boolean = false,
194 .string = (char *) "foo"};
195 TestStruct *p = &test_struct;
196 Error *errp = NULL;
197 QObject *obj;
198 QDict *qdict;
200 visit_type_TestStruct(data->ov, &p, NULL, &errp);
201 g_assert(!errp);
203 obj = qmp_output_get_qobject(data->qov);
204 g_assert(obj != NULL);
205 g_assert(qobject_type(obj) == QTYPE_QDICT);
207 qdict = qobject_to_qdict(obj);
208 g_assert_cmpint(qdict_size(qdict), ==, 3);
209 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
210 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0);
211 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
213 QDECREF(qdict);
216 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
217 const void *unused)
219 int64_t value = 42;
220 Error *errp = NULL;
221 UserDefNested *ud2;
222 QObject *obj;
223 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
224 const char *string = "user def string";
225 const char *strings[] = { "forty two", "forty three", "forty four",
226 "forty five" };
228 ud2 = g_malloc0(sizeof(*ud2));
229 ud2->string0 = g_strdup(strings[0]);
231 ud2->dict1.string1 = g_strdup(strings[1]);
232 ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
233 ud2->dict1.dict2.userdef1->string = g_strdup(string);
234 ud2->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
235 ud2->dict1.dict2.userdef1->base->integer = value;
236 ud2->dict1.dict2.string2 = g_strdup(strings[2]);
238 ud2->dict1.has_dict3 = true;
239 ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
240 ud2->dict1.dict3.userdef2->string = g_strdup(string);
241 ud2->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);
242 ud2->dict1.dict3.userdef2->base->integer = value;
243 ud2->dict1.dict3.string3 = g_strdup(strings[3]);
245 visit_type_UserDefNested(data->ov, &ud2, "unused", &errp);
246 g_assert(!errp);
248 obj = qmp_output_get_qobject(data->qov);
249 g_assert(obj != NULL);
250 g_assert(qobject_type(obj) == QTYPE_QDICT);
252 qdict = qobject_to_qdict(obj);
253 g_assert_cmpint(qdict_size(qdict), ==, 2);
254 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
256 dict1 = qdict_get_qdict(qdict, "dict1");
257 g_assert_cmpint(qdict_size(dict1), ==, 3);
258 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
260 dict2 = qdict_get_qdict(dict1, "dict2");
261 g_assert_cmpint(qdict_size(dict2), ==, 2);
262 g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
263 userdef = qdict_get_qdict(dict2, "userdef1");
264 g_assert_cmpint(qdict_size(userdef), ==, 2);
265 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
266 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
268 dict3 = qdict_get_qdict(dict1, "dict3");
269 g_assert_cmpint(qdict_size(dict3), ==, 2);
270 g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
271 userdef = qdict_get_qdict(dict3, "userdef2");
272 g_assert_cmpint(qdict_size(userdef), ==, 2);
273 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
274 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
276 QDECREF(qdict);
277 qapi_free_UserDefNested(ud2);
280 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
281 const void *unused)
283 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
284 UserDefZero b;
285 UserDefOne u = { .base = &b }, *pu = &u;
286 Error *errp;
287 int i;
289 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
290 errp = NULL;
291 u.has_enum1 = true;
292 u.enum1 = bad_values[i];
293 visit_type_UserDefOne(data->ov, &pu, "unused", &errp);
294 g_assert(errp);
295 error_free(errp);
299 typedef struct TestStructList
301 union {
302 TestStruct *value;
303 uint64_t padding;
305 struct TestStructList *next;
306 } TestStructList;
308 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
309 const char *name, Error **errp)
311 GenericList *i, **head = (GenericList **)obj;
313 visit_start_list(v, name, errp);
315 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
316 TestStructList *native_i = (TestStructList *)i;
317 visit_type_TestStruct(v, &native_i->value, NULL, errp);
320 visit_end_list(v, errp);
323 static void test_visitor_out_list(TestOutputVisitorData *data,
324 const void *unused)
326 char *value_str = (char *) "list value";
327 TestStructList *p, *head = NULL;
328 const int max_items = 10;
329 bool value_bool = true;
330 int value_int = 10;
331 Error *errp = NULL;
332 QListEntry *entry;
333 QObject *obj;
334 QList *qlist;
335 int i;
337 for (i = 0; i < max_items; i++) {
338 p = g_malloc0(sizeof(*p));
339 p->value = g_malloc0(sizeof(*p->value));
340 p->value->integer = value_int;
341 p->value->boolean = value_bool;
342 p->value->string = value_str;
344 p->next = head;
345 head = p;
348 visit_type_TestStructList(data->ov, &head, NULL, &errp);
349 g_assert(!errp);
351 obj = qmp_output_get_qobject(data->qov);
352 g_assert(obj != NULL);
353 g_assert(qobject_type(obj) == QTYPE_QLIST);
355 qlist = qobject_to_qlist(obj);
356 g_assert(!qlist_empty(qlist));
358 i = 0;
359 QLIST_FOREACH_ENTRY(qlist, entry) {
360 QDict *qdict;
362 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
363 qdict = qobject_to_qdict(entry->value);
364 g_assert_cmpint(qdict_size(qdict), ==, 3);
365 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
366 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
367 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
368 i++;
370 g_assert_cmpint(i, ==, max_items);
372 QDECREF(qlist);
374 for (p = head; p;) {
375 TestStructList *tmp = p->next;
376 g_free(p->value);
377 g_free(p);
378 p = tmp;
382 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
383 const void *unused)
385 UserDefNestedList *p, *head = NULL;
386 const char string[] = "foo bar";
387 int i, max_count = 1024;
389 for (i = 0; i < max_count; i++) {
390 p = g_malloc0(sizeof(*p));
391 p->value = g_malloc0(sizeof(*p->value));
393 p->value->string0 = g_strdup(string);
394 p->value->dict1.string1 = g_strdup(string);
395 p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
396 p->value->dict1.dict2.userdef1->string = g_strdup(string);
397 p->value->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
398 p->value->dict1.dict2.userdef1->base->integer = 42;
399 p->value->dict1.dict2.string2 = g_strdup(string);
400 p->value->dict1.has_dict3 = false;
402 p->next = head;
403 head = p;
406 qapi_free_UserDefNestedList(head);
409 static void test_visitor_out_union(TestOutputVisitorData *data,
410 const void *unused)
412 QObject *arg, *qvalue;
413 QDict *qdict, *value;
415 Error *err = NULL;
417 UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
418 tmp->kind = USER_DEF_UNION_KIND_A;
419 tmp->integer = 41;
420 tmp->a = g_malloc0(sizeof(UserDefA));
421 tmp->a->boolean = true;
423 visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
424 g_assert(err == NULL);
425 arg = qmp_output_get_qobject(data->qov);
427 g_assert(qobject_type(arg) == QTYPE_QDICT);
428 qdict = qobject_to_qdict(arg);
430 g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
431 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
433 qvalue = qdict_get(qdict, "data");
434 g_assert(data != NULL);
435 g_assert(qobject_type(qvalue) == QTYPE_QDICT);
436 value = qobject_to_qdict(qvalue);
437 g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
439 qapi_free_UserDefUnion(tmp);
440 QDECREF(qdict);
443 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
444 const void *unused)
446 QObject *arg;
447 QDict *qdict;
449 Error *err = NULL;
451 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
452 tmp->kind = ENUM_ONE_VALUE1;
453 tmp->string = g_strdup("str");
454 tmp->value1 = g_malloc0(sizeof(UserDefA));
455 /* TODO when generator bug is fixed: tmp->integer = 41; */
456 tmp->value1->boolean = true;
458 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
459 g_assert(err == NULL);
460 arg = qmp_output_get_qobject(data->qov);
462 g_assert(qobject_type(arg) == QTYPE_QDICT);
463 qdict = qobject_to_qdict(arg);
465 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
466 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
467 /* TODO g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); */
468 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
470 qapi_free_UserDefFlatUnion(tmp);
471 QDECREF(qdict);
474 static void test_visitor_out_union_anon(TestOutputVisitorData *data,
475 const void *unused)
477 QObject *arg;
478 Error *err = NULL;
480 UserDefAnonUnion *tmp = g_malloc0(sizeof(UserDefAnonUnion));
481 tmp->kind = USER_DEF_ANON_UNION_KIND_I;
482 tmp->i = 42;
484 visit_type_UserDefAnonUnion(data->ov, &tmp, NULL, &err);
485 g_assert(err == NULL);
486 arg = qmp_output_get_qobject(data->qov);
488 g_assert(qobject_type(arg) == QTYPE_QINT);
489 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
491 qapi_free_UserDefAnonUnion(tmp);
494 static void init_native_list(UserDefNativeListUnion *cvalue)
496 int i;
497 switch (cvalue->kind) {
498 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
499 intList **list = &cvalue->integer;
500 for (i = 0; i < 32; i++) {
501 *list = g_new0(intList, 1);
502 (*list)->value = i;
503 (*list)->next = NULL;
504 list = &(*list)->next;
506 break;
508 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
509 int8List **list = &cvalue->s8;
510 for (i = 0; i < 32; i++) {
511 *list = g_new0(int8List, 1);
512 (*list)->value = i;
513 (*list)->next = NULL;
514 list = &(*list)->next;
516 break;
518 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
519 int16List **list = &cvalue->s16;
520 for (i = 0; i < 32; i++) {
521 *list = g_new0(int16List, 1);
522 (*list)->value = i;
523 (*list)->next = NULL;
524 list = &(*list)->next;
526 break;
528 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
529 int32List **list = &cvalue->s32;
530 for (i = 0; i < 32; i++) {
531 *list = g_new0(int32List, 1);
532 (*list)->value = i;
533 (*list)->next = NULL;
534 list = &(*list)->next;
536 break;
538 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
539 int64List **list = &cvalue->s64;
540 for (i = 0; i < 32; i++) {
541 *list = g_new0(int64List, 1);
542 (*list)->value = i;
543 (*list)->next = NULL;
544 list = &(*list)->next;
546 break;
548 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
549 uint8List **list = &cvalue->u8;
550 for (i = 0; i < 32; i++) {
551 *list = g_new0(uint8List, 1);
552 (*list)->value = i;
553 (*list)->next = NULL;
554 list = &(*list)->next;
556 break;
558 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
559 uint16List **list = &cvalue->u16;
560 for (i = 0; i < 32; i++) {
561 *list = g_new0(uint16List, 1);
562 (*list)->value = i;
563 (*list)->next = NULL;
564 list = &(*list)->next;
566 break;
568 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
569 uint32List **list = &cvalue->u32;
570 for (i = 0; i < 32; i++) {
571 *list = g_new0(uint32List, 1);
572 (*list)->value = i;
573 (*list)->next = NULL;
574 list = &(*list)->next;
576 break;
578 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
579 uint64List **list = &cvalue->u64;
580 for (i = 0; i < 32; i++) {
581 *list = g_new0(uint64List, 1);
582 (*list)->value = i;
583 (*list)->next = NULL;
584 list = &(*list)->next;
586 break;
588 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
589 boolList **list = &cvalue->boolean;
590 for (i = 0; i < 32; i++) {
591 *list = g_new0(boolList, 1);
592 (*list)->value = (i % 3 == 0);
593 (*list)->next = NULL;
594 list = &(*list)->next;
596 break;
598 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
599 strList **list = &cvalue->string;
600 for (i = 0; i < 32; i++) {
601 *list = g_new0(strList, 1);
602 (*list)->value = g_strdup_printf("%d", i);
603 (*list)->next = NULL;
604 list = &(*list)->next;
606 break;
608 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
609 numberList **list = &cvalue->number;
610 for (i = 0; i < 32; i++) {
611 *list = g_new0(numberList, 1);
612 (*list)->value = (double)i / 3;
613 (*list)->next = NULL;
614 list = &(*list)->next;
616 break;
618 default:
619 g_assert_not_reached();
623 static void check_native_list(QObject *qobj,
624 UserDefNativeListUnionKind kind)
626 QDict *qdict;
627 QList *qlist;
628 int i;
630 g_assert(qobj);
631 g_assert(qobject_type(qobj) == QTYPE_QDICT);
632 qdict = qobject_to_qdict(qobj);
633 g_assert(qdict);
634 g_assert(qdict_haskey(qdict, "data"));
635 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
637 switch (kind) {
638 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
639 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
640 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
641 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
642 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
643 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
644 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
645 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
646 /* all integer elements in JSON arrays get stored into QInts when
647 * we convert to QObjects, so we can check them all in the same
648 * fashion, so simply fall through here
650 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
651 for (i = 0; i < 32; i++) {
652 QObject *tmp;
653 QInt *qvalue;
654 tmp = qlist_peek(qlist);
655 g_assert(tmp);
656 qvalue = qobject_to_qint(tmp);
657 g_assert_cmpint(qint_get_int(qvalue), ==, i);
658 qobject_decref(qlist_pop(qlist));
660 break;
661 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
662 for (i = 0; i < 32; i++) {
663 QObject *tmp;
664 QBool *qvalue;
665 tmp = qlist_peek(qlist);
666 g_assert(tmp);
667 qvalue = qobject_to_qbool(tmp);
668 g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0);
669 qobject_decref(qlist_pop(qlist));
671 break;
672 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
673 for (i = 0; i < 32; i++) {
674 QObject *tmp;
675 QString *qvalue;
676 gchar str[8];
677 tmp = qlist_peek(qlist);
678 g_assert(tmp);
679 qvalue = qobject_to_qstring(tmp);
680 sprintf(str, "%d", i);
681 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
682 qobject_decref(qlist_pop(qlist));
684 break;
685 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
686 for (i = 0; i < 32; i++) {
687 QObject *tmp;
688 QFloat *qvalue;
689 GString *double_expected = g_string_new("");
690 GString *double_actual = g_string_new("");
692 tmp = qlist_peek(qlist);
693 g_assert(tmp);
694 qvalue = qobject_to_qfloat(tmp);
695 g_string_printf(double_expected, "%.6f", (double)i / 3);
696 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
697 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
699 qobject_decref(qlist_pop(qlist));
700 g_string_free(double_expected, true);
701 g_string_free(double_actual, true);
703 break;
704 default:
705 g_assert_not_reached();
707 QDECREF(qlist);
710 static void test_native_list(TestOutputVisitorData *data,
711 const void *unused,
712 UserDefNativeListUnionKind kind)
714 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
715 Error *err = NULL;
716 QObject *obj;
718 cvalue->kind = kind;
719 init_native_list(cvalue);
721 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
722 g_assert(err == NULL);
724 obj = qmp_output_get_qobject(data->qov);
725 check_native_list(obj, cvalue->kind);
726 qapi_free_UserDefNativeListUnion(cvalue);
727 qobject_decref(obj);
730 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
731 const void *unused)
733 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
736 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
737 const void *unused)
739 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
742 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
743 const void *unused)
745 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
748 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
749 const void *unused)
751 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
754 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
755 const void *unused)
757 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
760 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
761 const void *unused)
763 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
766 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
767 const void *unused)
769 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
772 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
773 const void *unused)
775 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
778 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
779 const void *unused)
781 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
784 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
785 const void *unused)
787 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
790 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
791 const void *unused)
793 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
796 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
797 const void *unused)
799 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
802 static void output_visitor_test_add(const char *testpath,
803 TestOutputVisitorData *data,
804 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
806 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
807 test_func, visitor_output_teardown);
810 int main(int argc, char **argv)
812 TestOutputVisitorData out_visitor_data;
814 g_test_init(&argc, &argv, NULL);
816 output_visitor_test_add("/visitor/output/int",
817 &out_visitor_data, test_visitor_out_int);
818 output_visitor_test_add("/visitor/output/bool",
819 &out_visitor_data, test_visitor_out_bool);
820 output_visitor_test_add("/visitor/output/number",
821 &out_visitor_data, test_visitor_out_number);
822 output_visitor_test_add("/visitor/output/string",
823 &out_visitor_data, test_visitor_out_string);
824 output_visitor_test_add("/visitor/output/no-string",
825 &out_visitor_data, test_visitor_out_no_string);
826 output_visitor_test_add("/visitor/output/enum",
827 &out_visitor_data, test_visitor_out_enum);
828 output_visitor_test_add("/visitor/output/enum-errors",
829 &out_visitor_data, test_visitor_out_enum_errors);
830 output_visitor_test_add("/visitor/output/struct",
831 &out_visitor_data, test_visitor_out_struct);
832 output_visitor_test_add("/visitor/output/struct-nested",
833 &out_visitor_data, test_visitor_out_struct_nested);
834 output_visitor_test_add("/visitor/output/struct-errors",
835 &out_visitor_data, test_visitor_out_struct_errors);
836 output_visitor_test_add("/visitor/output/list",
837 &out_visitor_data, test_visitor_out_list);
838 output_visitor_test_add("/visitor/output/list-qapi-free",
839 &out_visitor_data, test_visitor_out_list_qapi_free);
840 output_visitor_test_add("/visitor/output/union",
841 &out_visitor_data, test_visitor_out_union);
842 output_visitor_test_add("/visitor/output/union-flat",
843 &out_visitor_data, test_visitor_out_union_flat);
844 output_visitor_test_add("/visitor/output/union-anon",
845 &out_visitor_data, test_visitor_out_union_anon);
846 output_visitor_test_add("/visitor/output/native_list/int",
847 &out_visitor_data, test_visitor_out_native_list_int);
848 output_visitor_test_add("/visitor/output/native_list/int8",
849 &out_visitor_data, test_visitor_out_native_list_int8);
850 output_visitor_test_add("/visitor/output/native_list/int16",
851 &out_visitor_data, test_visitor_out_native_list_int16);
852 output_visitor_test_add("/visitor/output/native_list/int32",
853 &out_visitor_data, test_visitor_out_native_list_int32);
854 output_visitor_test_add("/visitor/output/native_list/int64",
855 &out_visitor_data, test_visitor_out_native_list_int64);
856 output_visitor_test_add("/visitor/output/native_list/uint8",
857 &out_visitor_data, test_visitor_out_native_list_uint8);
858 output_visitor_test_add("/visitor/output/native_list/uint16",
859 &out_visitor_data, test_visitor_out_native_list_uint16);
860 output_visitor_test_add("/visitor/output/native_list/uint32",
861 &out_visitor_data, test_visitor_out_native_list_uint32);
862 output_visitor_test_add("/visitor/output/native_list/uint64",
863 &out_visitor_data, test_visitor_out_native_list_uint64);
864 output_visitor_test_add("/visitor/output/native_list/bool",
865 &out_visitor_data, test_visitor_out_native_list_bool);
866 output_visitor_test_add("/visitor/output/native_list/string",
867 &out_visitor_data, test_visitor_out_native_list_str);
868 output_visitor_test_add("/visitor/output/native_list/number",
869 &out_visitor_data, test_visitor_out_native_list_number);
871 g_test_run();
873 return 0;