iscsi: Use bs->sg for everything else than disks
[qemu.git] / tests / test-qmp-output-visitor.c
blob105f4cf94b25d00feff2e9e8c1f34b2a90d45640
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 = USER_DEF_UNION_KIND_A;
453 tmp->a = g_malloc0(sizeof(UserDefA));
454 /* TODO when generator bug is fixed: tmp->integer = 41; */
455 tmp->a->boolean = true;
457 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
458 g_assert(err == NULL);
459 arg = qmp_output_get_qobject(data->qov);
461 g_assert(qobject_type(arg) == QTYPE_QDICT);
462 qdict = qobject_to_qdict(arg);
464 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "a");
465 /* TODO g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); */
466 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
468 qapi_free_UserDefFlatUnion(tmp);
469 QDECREF(qdict);
472 static void test_visitor_out_union_anon(TestOutputVisitorData *data,
473 const void *unused)
475 QObject *arg;
476 Error *err = NULL;
478 UserDefAnonUnion *tmp = g_malloc0(sizeof(UserDefAnonUnion));
479 tmp->kind = USER_DEF_ANON_UNION_KIND_I;
480 tmp->i = 42;
482 visit_type_UserDefAnonUnion(data->ov, &tmp, NULL, &err);
483 g_assert(err == NULL);
484 arg = qmp_output_get_qobject(data->qov);
486 g_assert(qobject_type(arg) == QTYPE_QINT);
487 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
489 qapi_free_UserDefAnonUnion(tmp);
492 static void init_native_list(UserDefNativeListUnion *cvalue)
494 int i;
495 switch (cvalue->kind) {
496 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
497 intList **list = &cvalue->integer;
498 for (i = 0; i < 32; i++) {
499 *list = g_new0(intList, 1);
500 (*list)->value = i;
501 (*list)->next = NULL;
502 list = &(*list)->next;
504 break;
506 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
507 int8List **list = &cvalue->s8;
508 for (i = 0; i < 32; i++) {
509 *list = g_new0(int8List, 1);
510 (*list)->value = i;
511 (*list)->next = NULL;
512 list = &(*list)->next;
514 break;
516 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
517 int16List **list = &cvalue->s16;
518 for (i = 0; i < 32; i++) {
519 *list = g_new0(int16List, 1);
520 (*list)->value = i;
521 (*list)->next = NULL;
522 list = &(*list)->next;
524 break;
526 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
527 int32List **list = &cvalue->s32;
528 for (i = 0; i < 32; i++) {
529 *list = g_new0(int32List, 1);
530 (*list)->value = i;
531 (*list)->next = NULL;
532 list = &(*list)->next;
534 break;
536 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
537 int64List **list = &cvalue->s64;
538 for (i = 0; i < 32; i++) {
539 *list = g_new0(int64List, 1);
540 (*list)->value = i;
541 (*list)->next = NULL;
542 list = &(*list)->next;
544 break;
546 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
547 uint8List **list = &cvalue->u8;
548 for (i = 0; i < 32; i++) {
549 *list = g_new0(uint8List, 1);
550 (*list)->value = i;
551 (*list)->next = NULL;
552 list = &(*list)->next;
554 break;
556 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
557 uint16List **list = &cvalue->u16;
558 for (i = 0; i < 32; i++) {
559 *list = g_new0(uint16List, 1);
560 (*list)->value = i;
561 (*list)->next = NULL;
562 list = &(*list)->next;
564 break;
566 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
567 uint32List **list = &cvalue->u32;
568 for (i = 0; i < 32; i++) {
569 *list = g_new0(uint32List, 1);
570 (*list)->value = i;
571 (*list)->next = NULL;
572 list = &(*list)->next;
574 break;
576 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
577 uint64List **list = &cvalue->u64;
578 for (i = 0; i < 32; i++) {
579 *list = g_new0(uint64List, 1);
580 (*list)->value = i;
581 (*list)->next = NULL;
582 list = &(*list)->next;
584 break;
586 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
587 boolList **list = &cvalue->boolean;
588 for (i = 0; i < 32; i++) {
589 *list = g_new0(boolList, 1);
590 (*list)->value = (i % 3 == 0);
591 (*list)->next = NULL;
592 list = &(*list)->next;
594 break;
596 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
597 strList **list = &cvalue->string;
598 for (i = 0; i < 32; i++) {
599 *list = g_new0(strList, 1);
600 (*list)->value = g_strdup_printf("%d", i);
601 (*list)->next = NULL;
602 list = &(*list)->next;
604 break;
606 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
607 numberList **list = &cvalue->number;
608 for (i = 0; i < 32; i++) {
609 *list = g_new0(numberList, 1);
610 (*list)->value = (double)i / 3;
611 (*list)->next = NULL;
612 list = &(*list)->next;
614 break;
616 default:
617 g_assert_not_reached();
621 static void check_native_list(QObject *qobj,
622 UserDefNativeListUnionKind kind)
624 QDict *qdict;
625 QList *qlist;
626 int i;
628 g_assert(qobj);
629 g_assert(qobject_type(qobj) == QTYPE_QDICT);
630 qdict = qobject_to_qdict(qobj);
631 g_assert(qdict);
632 g_assert(qdict_haskey(qdict, "data"));
633 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
635 switch (kind) {
636 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
637 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
638 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
639 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
640 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
641 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
642 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
643 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
644 /* all integer elements in JSON arrays get stored into QInts when
645 * we convert to QObjects, so we can check them all in the same
646 * fashion, so simply fall through here
648 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
649 for (i = 0; i < 32; i++) {
650 QObject *tmp;
651 QInt *qvalue;
652 tmp = qlist_peek(qlist);
653 g_assert(tmp);
654 qvalue = qobject_to_qint(tmp);
655 g_assert_cmpint(qint_get_int(qvalue), ==, i);
656 qobject_decref(qlist_pop(qlist));
658 break;
659 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
660 for (i = 0; i < 32; i++) {
661 QObject *tmp;
662 QBool *qvalue;
663 tmp = qlist_peek(qlist);
664 g_assert(tmp);
665 qvalue = qobject_to_qbool(tmp);
666 g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0);
667 qobject_decref(qlist_pop(qlist));
669 break;
670 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
671 for (i = 0; i < 32; i++) {
672 QObject *tmp;
673 QString *qvalue;
674 gchar str[8];
675 tmp = qlist_peek(qlist);
676 g_assert(tmp);
677 qvalue = qobject_to_qstring(tmp);
678 sprintf(str, "%d", i);
679 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
680 qobject_decref(qlist_pop(qlist));
682 break;
683 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
684 for (i = 0; i < 32; i++) {
685 QObject *tmp;
686 QFloat *qvalue;
687 GString *double_expected = g_string_new("");
688 GString *double_actual = g_string_new("");
690 tmp = qlist_peek(qlist);
691 g_assert(tmp);
692 qvalue = qobject_to_qfloat(tmp);
693 g_string_printf(double_expected, "%.6f", (double)i / 3);
694 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
695 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
697 qobject_decref(qlist_pop(qlist));
698 g_string_free(double_expected, true);
699 g_string_free(double_actual, true);
701 break;
702 default:
703 g_assert_not_reached();
705 QDECREF(qlist);
708 static void test_native_list(TestOutputVisitorData *data,
709 const void *unused,
710 UserDefNativeListUnionKind kind)
712 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
713 Error *err = NULL;
714 QObject *obj;
716 cvalue->kind = kind;
717 init_native_list(cvalue);
719 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
720 g_assert(err == NULL);
722 obj = qmp_output_get_qobject(data->qov);
723 check_native_list(obj, cvalue->kind);
724 qapi_free_UserDefNativeListUnion(cvalue);
725 qobject_decref(obj);
728 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
729 const void *unused)
731 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
734 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
735 const void *unused)
737 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
740 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
741 const void *unused)
743 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
746 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
747 const void *unused)
749 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
752 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
753 const void *unused)
755 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
758 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
759 const void *unused)
761 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
764 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
765 const void *unused)
767 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
770 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
771 const void *unused)
773 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
776 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
777 const void *unused)
779 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
782 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
783 const void *unused)
785 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
788 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
789 const void *unused)
791 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
794 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
795 const void *unused)
797 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
800 static void output_visitor_test_add(const char *testpath,
801 TestOutputVisitorData *data,
802 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
804 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
805 test_func, visitor_output_teardown);
808 int main(int argc, char **argv)
810 TestOutputVisitorData out_visitor_data;
812 g_test_init(&argc, &argv, NULL);
814 output_visitor_test_add("/visitor/output/int",
815 &out_visitor_data, test_visitor_out_int);
816 output_visitor_test_add("/visitor/output/bool",
817 &out_visitor_data, test_visitor_out_bool);
818 output_visitor_test_add("/visitor/output/number",
819 &out_visitor_data, test_visitor_out_number);
820 output_visitor_test_add("/visitor/output/string",
821 &out_visitor_data, test_visitor_out_string);
822 output_visitor_test_add("/visitor/output/no-string",
823 &out_visitor_data, test_visitor_out_no_string);
824 output_visitor_test_add("/visitor/output/enum",
825 &out_visitor_data, test_visitor_out_enum);
826 output_visitor_test_add("/visitor/output/enum-errors",
827 &out_visitor_data, test_visitor_out_enum_errors);
828 output_visitor_test_add("/visitor/output/struct",
829 &out_visitor_data, test_visitor_out_struct);
830 output_visitor_test_add("/visitor/output/struct-nested",
831 &out_visitor_data, test_visitor_out_struct_nested);
832 output_visitor_test_add("/visitor/output/struct-errors",
833 &out_visitor_data, test_visitor_out_struct_errors);
834 output_visitor_test_add("/visitor/output/list",
835 &out_visitor_data, test_visitor_out_list);
836 output_visitor_test_add("/visitor/output/list-qapi-free",
837 &out_visitor_data, test_visitor_out_list_qapi_free);
838 output_visitor_test_add("/visitor/output/union",
839 &out_visitor_data, test_visitor_out_union);
840 output_visitor_test_add("/visitor/output/union-flat",
841 &out_visitor_data, test_visitor_out_union_flat);
842 output_visitor_test_add("/visitor/output/union-anon",
843 &out_visitor_data, test_visitor_out_union_anon);
844 output_visitor_test_add("/visitor/output/native_list/int",
845 &out_visitor_data, test_visitor_out_native_list_int);
846 output_visitor_test_add("/visitor/output/native_list/int8",
847 &out_visitor_data, test_visitor_out_native_list_int8);
848 output_visitor_test_add("/visitor/output/native_list/int16",
849 &out_visitor_data, test_visitor_out_native_list_int16);
850 output_visitor_test_add("/visitor/output/native_list/int32",
851 &out_visitor_data, test_visitor_out_native_list_int32);
852 output_visitor_test_add("/visitor/output/native_list/int64",
853 &out_visitor_data, test_visitor_out_native_list_int64);
854 output_visitor_test_add("/visitor/output/native_list/uint8",
855 &out_visitor_data, test_visitor_out_native_list_uint8);
856 output_visitor_test_add("/visitor/output/native_list/uint16",
857 &out_visitor_data, test_visitor_out_native_list_uint16);
858 output_visitor_test_add("/visitor/output/native_list/uint32",
859 &out_visitor_data, test_visitor_out_native_list_uint32);
860 output_visitor_test_add("/visitor/output/native_list/uint64",
861 &out_visitor_data, test_visitor_out_native_list_uint64);
862 output_visitor_test_add("/visitor/output/native_list/bool",
863 &out_visitor_data, test_visitor_out_native_list_bool);
864 output_visitor_test_add("/visitor/output/native_list/string",
865 &out_visitor_data, test_visitor_out_native_list_str);
866 output_visitor_test_add("/visitor/output/native_list/number",
867 &out_visitor_data, test_visitor_out_native_list_number);
869 g_test_run();
871 return 0;