block/qapi: Use blk_all_next() for query-block
[qemu/kevin.git] / tests / test-qobject-output-visitor.c
blob749c54065f862aa0d1a26fb75d242ca97a2b80ed
1 /*
2 * QObject Output Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/qobject-output-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
21 #include "qapi/qmp/qjson.h"
23 typedef struct TestOutputVisitorData {
24 Visitor *ov;
25 QObject *obj;
26 } TestOutputVisitorData;
28 static void visitor_output_setup(TestOutputVisitorData *data,
29 const void *unused)
31 data->ov = qobject_output_visitor_new(&data->obj);
32 g_assert(data->ov);
35 static void visitor_output_teardown(TestOutputVisitorData *data,
36 const void *unused)
38 visit_free(data->ov);
39 data->ov = NULL;
40 qobject_decref(data->obj);
41 data->obj = NULL;
44 static QObject *visitor_get(TestOutputVisitorData *data)
46 visit_complete(data->ov, &data->obj);
47 g_assert(data->obj);
48 return data->obj;
51 static void visitor_reset(TestOutputVisitorData *data)
53 visitor_output_teardown(data, NULL);
54 visitor_output_setup(data, NULL);
57 static void test_visitor_out_int(TestOutputVisitorData *data,
58 const void *unused)
60 int64_t value = -42;
61 int64_t val;
62 QNum *qnum;
64 visit_type_int(data->ov, NULL, &value, &error_abort);
66 qnum = qobject_to_qnum(visitor_get(data));
67 g_assert(qnum);
68 g_assert(qnum_get_try_int(qnum, &val));
69 g_assert_cmpint(val, ==, value);
72 static void test_visitor_out_bool(TestOutputVisitorData *data,
73 const void *unused)
75 bool value = true;
76 QBool *qbool;
78 visit_type_bool(data->ov, NULL, &value, &error_abort);
80 qbool = qobject_to_qbool(visitor_get(data));
81 g_assert(qbool);
82 g_assert(qbool_get_bool(qbool) == value);
85 static void test_visitor_out_number(TestOutputVisitorData *data,
86 const void *unused)
88 double value = 3.14;
89 QNum *qnum;
91 visit_type_number(data->ov, NULL, &value, &error_abort);
93 qnum = qobject_to_qnum(visitor_get(data));
94 g_assert(qnum);
95 g_assert(qnum_get_double(qnum) == value);
98 static void test_visitor_out_string(TestOutputVisitorData *data,
99 const void *unused)
101 char *string = (char *) "Q E M U";
102 QString *qstr;
104 visit_type_str(data->ov, NULL, &string, &error_abort);
106 qstr = qobject_to_qstring(visitor_get(data));
107 g_assert(qstr);
108 g_assert_cmpstr(qstring_get_str(qstr), ==, string);
111 static void test_visitor_out_no_string(TestOutputVisitorData *data,
112 const void *unused)
114 char *string = NULL;
115 QString *qstr;
117 /* A null string should return "" */
118 visit_type_str(data->ov, NULL, &string, &error_abort);
120 qstr = qobject_to_qstring(visitor_get(data));
121 g_assert(qstr);
122 g_assert_cmpstr(qstring_get_str(qstr), ==, "");
125 static void test_visitor_out_enum(TestOutputVisitorData *data,
126 const void *unused)
128 EnumOne i;
129 QString *qstr;
131 for (i = 0; i < ENUM_ONE__MAX; i++) {
132 visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
134 qstr = qobject_to_qstring(visitor_get(data));
135 g_assert(qstr);
136 g_assert_cmpstr(qstring_get_str(qstr), ==, EnumOne_lookup[i]);
137 visitor_reset(data);
141 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
142 const void *unused)
144 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
145 Error *err;
147 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
148 err = NULL;
149 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
150 error_free_or_abort(&err);
151 visitor_reset(data);
156 static void test_visitor_out_struct(TestOutputVisitorData *data,
157 const void *unused)
159 TestStruct test_struct = { .integer = 42,
160 .boolean = false,
161 .string = (char *) "foo"};
162 TestStruct *p = &test_struct;
163 QDict *qdict;
165 visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
167 qdict = qobject_to_qdict(visitor_get(data));
168 g_assert(qdict);
169 g_assert_cmpint(qdict_size(qdict), ==, 3);
170 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
171 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
172 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
175 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
176 const void *unused)
178 int64_t value = 42;
179 UserDefTwo *ud2;
180 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
181 const char *string = "user def string";
182 const char *strings[] = { "forty two", "forty three", "forty four",
183 "forty five" };
185 ud2 = g_malloc0(sizeof(*ud2));
186 ud2->string0 = g_strdup(strings[0]);
188 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
189 ud2->dict1->string1 = g_strdup(strings[1]);
191 ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
192 ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
193 ud2->dict1->dict2->userdef->string = g_strdup(string);
194 ud2->dict1->dict2->userdef->integer = value;
195 ud2->dict1->dict2->string = g_strdup(strings[2]);
197 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
198 ud2->dict1->has_dict3 = true;
199 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
200 ud2->dict1->dict3->userdef->string = g_strdup(string);
201 ud2->dict1->dict3->userdef->integer = value;
202 ud2->dict1->dict3->string = g_strdup(strings[3]);
204 visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
206 qdict = qobject_to_qdict(visitor_get(data));
207 g_assert(qdict);
208 g_assert_cmpint(qdict_size(qdict), ==, 2);
209 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
211 dict1 = qdict_get_qdict(qdict, "dict1");
212 g_assert_cmpint(qdict_size(dict1), ==, 3);
213 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
215 dict2 = qdict_get_qdict(dict1, "dict2");
216 g_assert_cmpint(qdict_size(dict2), ==, 2);
217 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
218 userdef = qdict_get_qdict(dict2, "userdef");
219 g_assert_cmpint(qdict_size(userdef), ==, 2);
220 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
221 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
223 dict3 = qdict_get_qdict(dict1, "dict3");
224 g_assert_cmpint(qdict_size(dict3), ==, 2);
225 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
226 userdef = qdict_get_qdict(dict3, "userdef");
227 g_assert_cmpint(qdict_size(userdef), ==, 2);
228 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
229 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
231 qapi_free_UserDefTwo(ud2);
234 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
235 const void *unused)
237 EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
238 UserDefOne u = {0};
239 UserDefOne *pu = &u;
240 Error *err;
241 int i;
243 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
244 err = NULL;
245 u.has_enum1 = true;
246 u.enum1 = bad_values[i];
247 visit_type_UserDefOne(data->ov, "unused", &pu, &err);
248 error_free_or_abort(&err);
249 visitor_reset(data);
254 static void test_visitor_out_list(TestOutputVisitorData *data,
255 const void *unused)
257 const char *value_str = "list value";
258 TestStructList *p, *head = NULL;
259 const int max_items = 10;
260 bool value_bool = true;
261 int value_int = 10;
262 QListEntry *entry;
263 QList *qlist;
264 int i;
266 /* Build the list in reverse order... */
267 for (i = 0; i < max_items; i++) {
268 p = g_malloc0(sizeof(*p));
269 p->value = g_malloc0(sizeof(*p->value));
270 p->value->integer = value_int + (max_items - i - 1);
271 p->value->boolean = value_bool;
272 p->value->string = g_strdup(value_str);
274 p->next = head;
275 head = p;
278 visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
280 qlist = qobject_to_qlist(visitor_get(data));
281 g_assert(qlist);
282 g_assert(!qlist_empty(qlist));
284 /* ...and ensure that the visitor sees it in order */
285 i = 0;
286 QLIST_FOREACH_ENTRY(qlist, entry) {
287 QDict *qdict;
289 qdict = qobject_to_qdict(entry->value);
290 g_assert(qdict);
291 g_assert_cmpint(qdict_size(qdict), ==, 3);
292 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
293 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
294 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
295 i++;
297 g_assert_cmpint(i, ==, max_items);
299 qapi_free_TestStructList(head);
302 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
303 const void *unused)
305 UserDefTwoList *p, *head = NULL;
306 const char string[] = "foo bar";
307 int i, max_count = 1024;
309 for (i = 0; i < max_count; i++) {
310 p = g_malloc0(sizeof(*p));
311 p->value = g_malloc0(sizeof(*p->value));
313 p->value->string0 = g_strdup(string);
314 p->value->dict1 = g_new0(UserDefTwoDict, 1);
315 p->value->dict1->string1 = g_strdup(string);
316 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
317 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
318 p->value->dict1->dict2->userdef->string = g_strdup(string);
319 p->value->dict1->dict2->userdef->integer = 42;
320 p->value->dict1->dict2->string = g_strdup(string);
321 p->value->dict1->has_dict3 = false;
323 p->next = head;
324 head = p;
327 qapi_free_UserDefTwoList(head);
330 static void test_visitor_out_any(TestOutputVisitorData *data,
331 const void *unused)
333 QObject *qobj;
334 QNum *qnum;
335 QBool *qbool;
336 QString *qstring;
337 QDict *qdict;
338 int64_t val;
340 qobj = QOBJECT(qnum_from_int(-42));
341 visit_type_any(data->ov, NULL, &qobj, &error_abort);
342 qnum = qobject_to_qnum(visitor_get(data));
343 g_assert(qnum);
344 g_assert(qnum_get_try_int(qnum, &val));
345 g_assert_cmpint(val, ==, -42);
346 qobject_decref(qobj);
348 visitor_reset(data);
349 qdict = qdict_new();
350 qdict_put_int(qdict, "integer", -42);
351 qdict_put_bool(qdict, "boolean", true);
352 qdict_put_str(qdict, "string", "foo");
353 qobj = QOBJECT(qdict);
354 visit_type_any(data->ov, NULL, &qobj, &error_abort);
355 qobject_decref(qobj);
356 qdict = qobject_to_qdict(visitor_get(data));
357 g_assert(qdict);
358 qnum = qobject_to_qnum(qdict_get(qdict, "integer"));
359 g_assert(qnum);
360 g_assert(qnum_get_try_int(qnum, &val));
361 g_assert_cmpint(val, ==, -42);
362 qbool = qobject_to_qbool(qdict_get(qdict, "boolean"));
363 g_assert(qbool);
364 g_assert(qbool_get_bool(qbool) == true);
365 qstring = qobject_to_qstring(qdict_get(qdict, "string"));
366 g_assert(qstring);
367 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
370 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
371 const void *unused)
373 QDict *qdict;
375 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
376 tmp->enum1 = ENUM_ONE_VALUE1;
377 tmp->string = g_strdup("str");
378 tmp->integer = 41;
379 tmp->u.value1.boolean = true;
381 visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
382 qdict = qobject_to_qdict(visitor_get(data));
383 g_assert(qdict);
384 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
385 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
386 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
387 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
389 qapi_free_UserDefFlatUnion(tmp);
392 static void test_visitor_out_alternate(TestOutputVisitorData *data,
393 const void *unused)
395 UserDefAlternate *tmp;
396 QNum *qnum;
397 QString *qstr;
398 QDict *qdict;
399 int64_t val;
401 tmp = g_new0(UserDefAlternate, 1);
402 tmp->type = QTYPE_QNUM;
403 tmp->u.i = 42;
405 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
406 qnum = qobject_to_qnum(visitor_get(data));
407 g_assert(qnum);
408 g_assert(qnum_get_try_int(qnum, &val));
409 g_assert_cmpint(val, ==, 42);
411 qapi_free_UserDefAlternate(tmp);
413 visitor_reset(data);
414 tmp = g_new0(UserDefAlternate, 1);
415 tmp->type = QTYPE_QSTRING;
416 tmp->u.e = ENUM_ONE_VALUE1;
418 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
419 qstr = qobject_to_qstring(visitor_get(data));
420 g_assert(qstr);
421 g_assert_cmpstr(qstring_get_str(qstr), ==, "value1");
423 qapi_free_UserDefAlternate(tmp);
425 visitor_reset(data);
426 tmp = g_new0(UserDefAlternate, 1);
427 tmp->type = QTYPE_QDICT;
428 tmp->u.udfu.integer = 1;
429 tmp->u.udfu.string = g_strdup("str");
430 tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
431 tmp->u.udfu.u.value1.boolean = true;
433 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
434 qdict = qobject_to_qdict(visitor_get(data));
435 g_assert(qdict);
436 g_assert_cmpint(qdict_size(qdict), ==, 4);
437 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1);
438 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
439 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
440 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
442 qapi_free_UserDefAlternate(tmp);
445 static void test_visitor_out_null(TestOutputVisitorData *data,
446 const void *unused)
448 QDict *qdict;
449 QObject *nil;
451 visit_start_struct(data->ov, NULL, NULL, 0, &error_abort);
452 visit_type_null(data->ov, "a", &error_abort);
453 visit_check_struct(data->ov, &error_abort);
454 visit_end_struct(data->ov, NULL);
455 qdict = qobject_to_qdict(visitor_get(data));
456 g_assert(qdict);
457 g_assert_cmpint(qdict_size(qdict), ==, 1);
458 nil = qdict_get(qdict, "a");
459 g_assert(nil);
460 g_assert(qobject_type(nil) == QTYPE_QNULL);
463 static void init_native_list(UserDefNativeListUnion *cvalue)
465 int i;
466 switch (cvalue->type) {
467 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
468 intList **list = &cvalue->u.integer.data;
469 for (i = 0; i < 32; i++) {
470 *list = g_new0(intList, 1);
471 (*list)->value = i;
472 (*list)->next = NULL;
473 list = &(*list)->next;
475 break;
477 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
478 int8List **list = &cvalue->u.s8.data;
479 for (i = 0; i < 32; i++) {
480 *list = g_new0(int8List, 1);
481 (*list)->value = i;
482 (*list)->next = NULL;
483 list = &(*list)->next;
485 break;
487 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
488 int16List **list = &cvalue->u.s16.data;
489 for (i = 0; i < 32; i++) {
490 *list = g_new0(int16List, 1);
491 (*list)->value = i;
492 (*list)->next = NULL;
493 list = &(*list)->next;
495 break;
497 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
498 int32List **list = &cvalue->u.s32.data;
499 for (i = 0; i < 32; i++) {
500 *list = g_new0(int32List, 1);
501 (*list)->value = i;
502 (*list)->next = NULL;
503 list = &(*list)->next;
505 break;
507 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
508 int64List **list = &cvalue->u.s64.data;
509 for (i = 0; i < 32; i++) {
510 *list = g_new0(int64List, 1);
511 (*list)->value = i;
512 (*list)->next = NULL;
513 list = &(*list)->next;
515 break;
517 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
518 uint8List **list = &cvalue->u.u8.data;
519 for (i = 0; i < 32; i++) {
520 *list = g_new0(uint8List, 1);
521 (*list)->value = i;
522 (*list)->next = NULL;
523 list = &(*list)->next;
525 break;
527 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
528 uint16List **list = &cvalue->u.u16.data;
529 for (i = 0; i < 32; i++) {
530 *list = g_new0(uint16List, 1);
531 (*list)->value = i;
532 (*list)->next = NULL;
533 list = &(*list)->next;
535 break;
537 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
538 uint32List **list = &cvalue->u.u32.data;
539 for (i = 0; i < 32; i++) {
540 *list = g_new0(uint32List, 1);
541 (*list)->value = i;
542 (*list)->next = NULL;
543 list = &(*list)->next;
545 break;
547 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
548 uint64List **list = &cvalue->u.u64.data;
549 for (i = 0; i < 32; i++) {
550 *list = g_new0(uint64List, 1);
551 (*list)->value = i;
552 (*list)->next = NULL;
553 list = &(*list)->next;
555 break;
557 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
558 boolList **list = &cvalue->u.boolean.data;
559 for (i = 0; i < 32; i++) {
560 *list = g_new0(boolList, 1);
561 (*list)->value = (i % 3 == 0);
562 (*list)->next = NULL;
563 list = &(*list)->next;
565 break;
567 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
568 strList **list = &cvalue->u.string.data;
569 for (i = 0; i < 32; i++) {
570 *list = g_new0(strList, 1);
571 (*list)->value = g_strdup_printf("%d", i);
572 (*list)->next = NULL;
573 list = &(*list)->next;
575 break;
577 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
578 numberList **list = &cvalue->u.number.data;
579 for (i = 0; i < 32; i++) {
580 *list = g_new0(numberList, 1);
581 (*list)->value = (double)i / 3;
582 (*list)->next = NULL;
583 list = &(*list)->next;
585 break;
587 default:
588 g_assert_not_reached();
592 static void check_native_list(QObject *qobj,
593 UserDefNativeListUnionKind kind)
595 QDict *qdict;
596 QList *qlist;
597 int i;
599 qdict = qobject_to_qdict(qobj);
600 g_assert(qdict);
601 g_assert(qdict_haskey(qdict, "data"));
602 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
604 switch (kind) {
605 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
606 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
607 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
608 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
609 for (i = 0; i < 32; i++) {
610 QObject *tmp;
611 QNum *qvalue;
612 uint64_t val;
614 tmp = qlist_peek(qlist);
615 g_assert(tmp);
616 qvalue = qobject_to_qnum(tmp);
617 g_assert(qnum_get_try_uint(qvalue, &val));
618 g_assert_cmpint(val, ==, i);
619 qobject_decref(qlist_pop(qlist));
621 break;
623 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
624 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
625 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
626 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
628 * All integer elements in JSON arrays get stored into QNums
629 * when we convert to QObjects, so we can check them all in
630 * the same fashion, so simply fall through here.
632 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
633 for (i = 0; i < 32; i++) {
634 QObject *tmp;
635 QNum *qvalue;
636 int64_t val;
638 tmp = qlist_peek(qlist);
639 g_assert(tmp);
640 qvalue = qobject_to_qnum(tmp);
641 g_assert(qnum_get_try_int(qvalue, &val));
642 g_assert_cmpint(val, ==, i);
643 qobject_decref(qlist_pop(qlist));
645 break;
646 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
647 for (i = 0; i < 32; i++) {
648 QObject *tmp;
649 QBool *qvalue;
650 tmp = qlist_peek(qlist);
651 g_assert(tmp);
652 qvalue = qobject_to_qbool(tmp);
653 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
654 qobject_decref(qlist_pop(qlist));
656 break;
657 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
658 for (i = 0; i < 32; i++) {
659 QObject *tmp;
660 QString *qvalue;
661 gchar str[8];
662 tmp = qlist_peek(qlist);
663 g_assert(tmp);
664 qvalue = qobject_to_qstring(tmp);
665 sprintf(str, "%d", i);
666 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
667 qobject_decref(qlist_pop(qlist));
669 break;
670 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
671 for (i = 0; i < 32; i++) {
672 QObject *tmp;
673 QNum *qvalue;
674 GString *double_expected = g_string_new("");
675 GString *double_actual = g_string_new("");
677 tmp = qlist_peek(qlist);
678 g_assert(tmp);
679 qvalue = qobject_to_qnum(tmp);
680 g_string_printf(double_expected, "%.6f", (double)i / 3);
681 g_string_printf(double_actual, "%.6f", qnum_get_double(qvalue));
682 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
684 qobject_decref(qlist_pop(qlist));
685 g_string_free(double_expected, true);
686 g_string_free(double_actual, true);
688 break;
689 default:
690 g_assert_not_reached();
692 QDECREF(qlist);
695 static void test_native_list(TestOutputVisitorData *data,
696 const void *unused,
697 UserDefNativeListUnionKind kind)
699 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
700 QObject *obj;
702 cvalue->type = kind;
703 init_native_list(cvalue);
705 visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
707 obj = visitor_get(data);
708 check_native_list(obj, cvalue->type);
709 qapi_free_UserDefNativeListUnion(cvalue);
712 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
713 const void *unused)
715 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
718 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
719 const void *unused)
721 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
724 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
725 const void *unused)
727 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
730 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
731 const void *unused)
733 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
736 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
737 const void *unused)
739 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
742 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
743 const void *unused)
745 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
748 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
749 const void *unused)
751 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
754 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
755 const void *unused)
757 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
760 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
761 const void *unused)
763 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
766 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
767 const void *unused)
769 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
772 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
773 const void *unused)
775 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
778 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
779 const void *unused)
781 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
784 static void output_visitor_test_add(const char *testpath,
785 TestOutputVisitorData *data,
786 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
788 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
789 test_func, visitor_output_teardown);
792 int main(int argc, char **argv)
794 TestOutputVisitorData out_visitor_data;
796 g_test_init(&argc, &argv, NULL);
798 output_visitor_test_add("/visitor/output/int",
799 &out_visitor_data, test_visitor_out_int);
800 output_visitor_test_add("/visitor/output/bool",
801 &out_visitor_data, test_visitor_out_bool);
802 output_visitor_test_add("/visitor/output/number",
803 &out_visitor_data, test_visitor_out_number);
804 output_visitor_test_add("/visitor/output/string",
805 &out_visitor_data, test_visitor_out_string);
806 output_visitor_test_add("/visitor/output/no-string",
807 &out_visitor_data, test_visitor_out_no_string);
808 output_visitor_test_add("/visitor/output/enum",
809 &out_visitor_data, test_visitor_out_enum);
810 output_visitor_test_add("/visitor/output/enum-errors",
811 &out_visitor_data, test_visitor_out_enum_errors);
812 output_visitor_test_add("/visitor/output/struct",
813 &out_visitor_data, test_visitor_out_struct);
814 output_visitor_test_add("/visitor/output/struct-nested",
815 &out_visitor_data, test_visitor_out_struct_nested);
816 output_visitor_test_add("/visitor/output/struct-errors",
817 &out_visitor_data, test_visitor_out_struct_errors);
818 output_visitor_test_add("/visitor/output/list",
819 &out_visitor_data, test_visitor_out_list);
820 output_visitor_test_add("/visitor/output/any",
821 &out_visitor_data, test_visitor_out_any);
822 output_visitor_test_add("/visitor/output/list-qapi-free",
823 &out_visitor_data, test_visitor_out_list_qapi_free);
824 output_visitor_test_add("/visitor/output/union-flat",
825 &out_visitor_data, test_visitor_out_union_flat);
826 output_visitor_test_add("/visitor/output/alternate",
827 &out_visitor_data, test_visitor_out_alternate);
828 output_visitor_test_add("/visitor/output/null",
829 &out_visitor_data, test_visitor_out_null);
830 output_visitor_test_add("/visitor/output/native_list/int",
831 &out_visitor_data,
832 test_visitor_out_native_list_int);
833 output_visitor_test_add("/visitor/output/native_list/int8",
834 &out_visitor_data,
835 test_visitor_out_native_list_int8);
836 output_visitor_test_add("/visitor/output/native_list/int16",
837 &out_visitor_data,
838 test_visitor_out_native_list_int16);
839 output_visitor_test_add("/visitor/output/native_list/int32",
840 &out_visitor_data,
841 test_visitor_out_native_list_int32);
842 output_visitor_test_add("/visitor/output/native_list/int64",
843 &out_visitor_data,
844 test_visitor_out_native_list_int64);
845 output_visitor_test_add("/visitor/output/native_list/uint8",
846 &out_visitor_data,
847 test_visitor_out_native_list_uint8);
848 output_visitor_test_add("/visitor/output/native_list/uint16",
849 &out_visitor_data,
850 test_visitor_out_native_list_uint16);
851 output_visitor_test_add("/visitor/output/native_list/uint32",
852 &out_visitor_data,
853 test_visitor_out_native_list_uint32);
854 output_visitor_test_add("/visitor/output/native_list/uint64",
855 &out_visitor_data,
856 test_visitor_out_native_list_uint64);
857 output_visitor_test_add("/visitor/output/native_list/bool",
858 &out_visitor_data,
859 test_visitor_out_native_list_bool);
860 output_visitor_test_add("/visitor/output/native_list/string",
861 &out_visitor_data,
862 test_visitor_out_native_list_str);
863 output_visitor_test_add("/visitor/output/native_list/number",
864 &out_visitor_data,
865 test_visitor_out_native_list_number);
867 g_test_run();
869 return 0;