qcow2: Inform block layer about discard boundaries
[qemu/ar7.git] / tests / test-qobject-output-visitor.c
blob4e2d79c5d1b8fc6b3de3c03ab346cb831f500fab
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 QObject *obj;
63 visit_type_int(data->ov, NULL, &value, &error_abort);
65 obj = visitor_get(data);
66 g_assert(qobject_type(obj) == QTYPE_QINT);
67 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
70 static void test_visitor_out_bool(TestOutputVisitorData *data,
71 const void *unused)
73 bool value = true;
74 QObject *obj;
76 visit_type_bool(data->ov, NULL, &value, &error_abort);
78 obj = visitor_get(data);
79 g_assert(qobject_type(obj) == QTYPE_QBOOL);
80 g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
83 static void test_visitor_out_number(TestOutputVisitorData *data,
84 const void *unused)
86 double value = 3.14;
87 QObject *obj;
89 visit_type_number(data->ov, NULL, &value, &error_abort);
91 obj = visitor_get(data);
92 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
93 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
96 static void test_visitor_out_string(TestOutputVisitorData *data,
97 const void *unused)
99 char *string = (char *) "Q E M U";
100 QObject *obj;
102 visit_type_str(data->ov, NULL, &string, &error_abort);
104 obj = visitor_get(data);
105 g_assert(qobject_type(obj) == QTYPE_QSTRING);
106 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
109 static void test_visitor_out_no_string(TestOutputVisitorData *data,
110 const void *unused)
112 char *string = NULL;
113 QObject *obj;
115 /* A null string should return "" */
116 visit_type_str(data->ov, NULL, &string, &error_abort);
118 obj = visitor_get(data);
119 g_assert(qobject_type(obj) == QTYPE_QSTRING);
120 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
123 static void test_visitor_out_enum(TestOutputVisitorData *data,
124 const void *unused)
126 QObject *obj;
127 EnumOne i;
129 for (i = 0; i < ENUM_ONE__MAX; i++) {
130 visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
132 obj = visitor_get(data);
133 g_assert(qobject_type(obj) == QTYPE_QSTRING);
134 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
135 EnumOne_lookup[i]);
136 visitor_reset(data);
140 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
141 const void *unused)
143 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
144 Error *err;
146 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
147 err = NULL;
148 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
149 g_assert(err);
150 error_free(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 QObject *obj;
164 QDict *qdict;
166 visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
168 obj = visitor_get(data);
169 g_assert(qobject_type(obj) == QTYPE_QDICT);
171 qdict = qobject_to_qdict(obj);
172 g_assert_cmpint(qdict_size(qdict), ==, 3);
173 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
174 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
175 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
178 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
179 const void *unused)
181 int64_t value = 42;
182 UserDefTwo *ud2;
183 QObject *obj;
184 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
185 const char *string = "user def string";
186 const char *strings[] = { "forty two", "forty three", "forty four",
187 "forty five" };
189 ud2 = g_malloc0(sizeof(*ud2));
190 ud2->string0 = g_strdup(strings[0]);
192 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
193 ud2->dict1->string1 = g_strdup(strings[1]);
195 ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
196 ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
197 ud2->dict1->dict2->userdef->string = g_strdup(string);
198 ud2->dict1->dict2->userdef->integer = value;
199 ud2->dict1->dict2->string = g_strdup(strings[2]);
201 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
202 ud2->dict1->has_dict3 = true;
203 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
204 ud2->dict1->dict3->userdef->string = g_strdup(string);
205 ud2->dict1->dict3->userdef->integer = value;
206 ud2->dict1->dict3->string = g_strdup(strings[3]);
208 visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
210 obj = visitor_get(data);
211 g_assert(qobject_type(obj) == QTYPE_QDICT);
213 qdict = qobject_to_qdict(obj);
214 g_assert_cmpint(qdict_size(qdict), ==, 2);
215 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
217 dict1 = qdict_get_qdict(qdict, "dict1");
218 g_assert_cmpint(qdict_size(dict1), ==, 3);
219 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
221 dict2 = qdict_get_qdict(dict1, "dict2");
222 g_assert_cmpint(qdict_size(dict2), ==, 2);
223 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
224 userdef = qdict_get_qdict(dict2, "userdef");
225 g_assert_cmpint(qdict_size(userdef), ==, 2);
226 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
227 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
229 dict3 = qdict_get_qdict(dict1, "dict3");
230 g_assert_cmpint(qdict_size(dict3), ==, 2);
231 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
232 userdef = qdict_get_qdict(dict3, "userdef");
233 g_assert_cmpint(qdict_size(userdef), ==, 2);
234 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
235 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
237 qapi_free_UserDefTwo(ud2);
240 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
241 const void *unused)
243 EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
244 UserDefOne u = {0};
245 UserDefOne *pu = &u;
246 Error *err;
247 int i;
249 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
250 err = NULL;
251 u.has_enum1 = true;
252 u.enum1 = bad_values[i];
253 visit_type_UserDefOne(data->ov, "unused", &pu, &err);
254 g_assert(err);
255 error_free(err);
256 visitor_reset(data);
261 static void test_visitor_out_list(TestOutputVisitorData *data,
262 const void *unused)
264 const char *value_str = "list value";
265 TestStructList *p, *head = NULL;
266 const int max_items = 10;
267 bool value_bool = true;
268 int value_int = 10;
269 QListEntry *entry;
270 QObject *obj;
271 QList *qlist;
272 int i;
274 /* Build the list in reverse order... */
275 for (i = 0; i < max_items; i++) {
276 p = g_malloc0(sizeof(*p));
277 p->value = g_malloc0(sizeof(*p->value));
278 p->value->integer = value_int + (max_items - i - 1);
279 p->value->boolean = value_bool;
280 p->value->string = g_strdup(value_str);
282 p->next = head;
283 head = p;
286 visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
288 obj = visitor_get(data);
289 g_assert(qobject_type(obj) == QTYPE_QLIST);
291 qlist = qobject_to_qlist(obj);
292 g_assert(!qlist_empty(qlist));
294 /* ...and ensure that the visitor sees it in order */
295 i = 0;
296 QLIST_FOREACH_ENTRY(qlist, entry) {
297 QDict *qdict;
299 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
300 qdict = qobject_to_qdict(entry->value);
301 g_assert_cmpint(qdict_size(qdict), ==, 3);
302 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
303 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
304 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
305 i++;
307 g_assert_cmpint(i, ==, max_items);
309 qapi_free_TestStructList(head);
312 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
313 const void *unused)
315 UserDefTwoList *p, *head = NULL;
316 const char string[] = "foo bar";
317 int i, max_count = 1024;
319 for (i = 0; i < max_count; i++) {
320 p = g_malloc0(sizeof(*p));
321 p->value = g_malloc0(sizeof(*p->value));
323 p->value->string0 = g_strdup(string);
324 p->value->dict1 = g_new0(UserDefTwoDict, 1);
325 p->value->dict1->string1 = g_strdup(string);
326 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
327 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
328 p->value->dict1->dict2->userdef->string = g_strdup(string);
329 p->value->dict1->dict2->userdef->integer = 42;
330 p->value->dict1->dict2->string = g_strdup(string);
331 p->value->dict1->has_dict3 = false;
333 p->next = head;
334 head = p;
337 qapi_free_UserDefTwoList(head);
340 static void test_visitor_out_any(TestOutputVisitorData *data,
341 const void *unused)
343 QObject *qobj;
344 QInt *qint;
345 QBool *qbool;
346 QString *qstring;
347 QDict *qdict;
348 QObject *obj;
350 qobj = QOBJECT(qint_from_int(-42));
351 visit_type_any(data->ov, NULL, &qobj, &error_abort);
352 obj = visitor_get(data);
353 g_assert(qobject_type(obj) == QTYPE_QINT);
354 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
355 qobject_decref(qobj);
357 visitor_reset(data);
358 qdict = qdict_new();
359 qdict_put(qdict, "integer", qint_from_int(-42));
360 qdict_put(qdict, "boolean", qbool_from_bool(true));
361 qdict_put(qdict, "string", qstring_from_str("foo"));
362 qobj = QOBJECT(qdict);
363 visit_type_any(data->ov, NULL, &qobj, &error_abort);
364 qobject_decref(qobj);
365 obj = visitor_get(data);
366 qdict = qobject_to_qdict(obj);
367 g_assert(qdict);
368 qobj = qdict_get(qdict, "integer");
369 g_assert(qobj);
370 qint = qobject_to_qint(qobj);
371 g_assert(qint);
372 g_assert_cmpint(qint_get_int(qint), ==, -42);
373 qobj = qdict_get(qdict, "boolean");
374 g_assert(qobj);
375 qbool = qobject_to_qbool(qobj);
376 g_assert(qbool);
377 g_assert(qbool_get_bool(qbool) == true);
378 qobj = qdict_get(qdict, "string");
379 g_assert(qobj);
380 qstring = qobject_to_qstring(qobj);
381 g_assert(qstring);
382 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
385 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
386 const void *unused)
388 QObject *arg;
389 QDict *qdict;
391 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
392 tmp->enum1 = ENUM_ONE_VALUE1;
393 tmp->string = g_strdup("str");
394 tmp->integer = 41;
395 tmp->u.value1.boolean = true;
397 visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
398 arg = visitor_get(data);
400 g_assert(qobject_type(arg) == QTYPE_QDICT);
401 qdict = qobject_to_qdict(arg);
403 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
404 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
405 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
406 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
408 qapi_free_UserDefFlatUnion(tmp);
411 static void test_visitor_out_alternate(TestOutputVisitorData *data,
412 const void *unused)
414 QObject *arg;
415 UserDefAlternate *tmp;
416 QDict *qdict;
418 tmp = g_new0(UserDefAlternate, 1);
419 tmp->type = QTYPE_QINT;
420 tmp->u.i = 42;
422 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
423 arg = visitor_get(data);
425 g_assert(qobject_type(arg) == QTYPE_QINT);
426 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
428 qapi_free_UserDefAlternate(tmp);
430 visitor_reset(data);
431 tmp = g_new0(UserDefAlternate, 1);
432 tmp->type = QTYPE_QSTRING;
433 tmp->u.s = g_strdup("hello");
435 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
436 arg = visitor_get(data);
438 g_assert(qobject_type(arg) == QTYPE_QSTRING);
439 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");
441 qapi_free_UserDefAlternate(tmp);
443 visitor_reset(data);
444 tmp = g_new0(UserDefAlternate, 1);
445 tmp->type = QTYPE_QDICT;
446 tmp->u.udfu.integer = 1;
447 tmp->u.udfu.string = g_strdup("str");
448 tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
449 tmp->u.udfu.u.value1.boolean = true;
451 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
452 arg = visitor_get(data);
454 g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT);
455 qdict = qobject_to_qdict(arg);
456 g_assert_cmpint(qdict_size(qdict), ==, 4);
457 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1);
458 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
459 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
460 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
462 qapi_free_UserDefAlternate(tmp);
465 static void test_visitor_out_null(TestOutputVisitorData *data,
466 const void *unused)
468 QObject *arg;
469 QDict *qdict;
470 QObject *nil;
472 visit_start_struct(data->ov, NULL, NULL, 0, &error_abort);
473 visit_type_null(data->ov, "a", &error_abort);
474 visit_check_struct(data->ov, &error_abort);
475 visit_end_struct(data->ov, NULL);
476 arg = visitor_get(data);
477 g_assert(qobject_type(arg) == QTYPE_QDICT);
478 qdict = qobject_to_qdict(arg);
479 g_assert_cmpint(qdict_size(qdict), ==, 1);
480 nil = qdict_get(qdict, "a");
481 g_assert(nil);
482 g_assert(qobject_type(nil) == QTYPE_QNULL);
485 static void init_native_list(UserDefNativeListUnion *cvalue)
487 int i;
488 switch (cvalue->type) {
489 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
490 intList **list = &cvalue->u.integer.data;
491 for (i = 0; i < 32; i++) {
492 *list = g_new0(intList, 1);
493 (*list)->value = i;
494 (*list)->next = NULL;
495 list = &(*list)->next;
497 break;
499 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
500 int8List **list = &cvalue->u.s8.data;
501 for (i = 0; i < 32; i++) {
502 *list = g_new0(int8List, 1);
503 (*list)->value = i;
504 (*list)->next = NULL;
505 list = &(*list)->next;
507 break;
509 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
510 int16List **list = &cvalue->u.s16.data;
511 for (i = 0; i < 32; i++) {
512 *list = g_new0(int16List, 1);
513 (*list)->value = i;
514 (*list)->next = NULL;
515 list = &(*list)->next;
517 break;
519 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
520 int32List **list = &cvalue->u.s32.data;
521 for (i = 0; i < 32; i++) {
522 *list = g_new0(int32List, 1);
523 (*list)->value = i;
524 (*list)->next = NULL;
525 list = &(*list)->next;
527 break;
529 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
530 int64List **list = &cvalue->u.s64.data;
531 for (i = 0; i < 32; i++) {
532 *list = g_new0(int64List, 1);
533 (*list)->value = i;
534 (*list)->next = NULL;
535 list = &(*list)->next;
537 break;
539 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
540 uint8List **list = &cvalue->u.u8.data;
541 for (i = 0; i < 32; i++) {
542 *list = g_new0(uint8List, 1);
543 (*list)->value = i;
544 (*list)->next = NULL;
545 list = &(*list)->next;
547 break;
549 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
550 uint16List **list = &cvalue->u.u16.data;
551 for (i = 0; i < 32; i++) {
552 *list = g_new0(uint16List, 1);
553 (*list)->value = i;
554 (*list)->next = NULL;
555 list = &(*list)->next;
557 break;
559 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
560 uint32List **list = &cvalue->u.u32.data;
561 for (i = 0; i < 32; i++) {
562 *list = g_new0(uint32List, 1);
563 (*list)->value = i;
564 (*list)->next = NULL;
565 list = &(*list)->next;
567 break;
569 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
570 uint64List **list = &cvalue->u.u64.data;
571 for (i = 0; i < 32; i++) {
572 *list = g_new0(uint64List, 1);
573 (*list)->value = i;
574 (*list)->next = NULL;
575 list = &(*list)->next;
577 break;
579 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
580 boolList **list = &cvalue->u.boolean.data;
581 for (i = 0; i < 32; i++) {
582 *list = g_new0(boolList, 1);
583 (*list)->value = (i % 3 == 0);
584 (*list)->next = NULL;
585 list = &(*list)->next;
587 break;
589 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
590 strList **list = &cvalue->u.string.data;
591 for (i = 0; i < 32; i++) {
592 *list = g_new0(strList, 1);
593 (*list)->value = g_strdup_printf("%d", i);
594 (*list)->next = NULL;
595 list = &(*list)->next;
597 break;
599 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
600 numberList **list = &cvalue->u.number.data;
601 for (i = 0; i < 32; i++) {
602 *list = g_new0(numberList, 1);
603 (*list)->value = (double)i / 3;
604 (*list)->next = NULL;
605 list = &(*list)->next;
607 break;
609 default:
610 g_assert_not_reached();
614 static void check_native_list(QObject *qobj,
615 UserDefNativeListUnionKind kind)
617 QDict *qdict;
618 QList *qlist;
619 int i;
621 g_assert(qobj);
622 g_assert(qobject_type(qobj) == QTYPE_QDICT);
623 qdict = qobject_to_qdict(qobj);
624 g_assert(qdict);
625 g_assert(qdict_haskey(qdict, "data"));
626 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
628 switch (kind) {
629 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
630 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
631 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
632 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
633 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
634 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
635 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
636 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
637 /* all integer elements in JSON arrays get stored into QInts when
638 * we convert to QObjects, so we can check them all in the same
639 * fashion, so simply fall through here
641 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
642 for (i = 0; i < 32; i++) {
643 QObject *tmp;
644 QInt *qvalue;
645 tmp = qlist_peek(qlist);
646 g_assert(tmp);
647 qvalue = qobject_to_qint(tmp);
648 g_assert_cmpint(qint_get_int(qvalue), ==, i);
649 qobject_decref(qlist_pop(qlist));
651 break;
652 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
653 for (i = 0; i < 32; i++) {
654 QObject *tmp;
655 QBool *qvalue;
656 tmp = qlist_peek(qlist);
657 g_assert(tmp);
658 qvalue = qobject_to_qbool(tmp);
659 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
660 qobject_decref(qlist_pop(qlist));
662 break;
663 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
664 for (i = 0; i < 32; i++) {
665 QObject *tmp;
666 QString *qvalue;
667 gchar str[8];
668 tmp = qlist_peek(qlist);
669 g_assert(tmp);
670 qvalue = qobject_to_qstring(tmp);
671 sprintf(str, "%d", i);
672 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
673 qobject_decref(qlist_pop(qlist));
675 break;
676 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
677 for (i = 0; i < 32; i++) {
678 QObject *tmp;
679 QFloat *qvalue;
680 GString *double_expected = g_string_new("");
681 GString *double_actual = g_string_new("");
683 tmp = qlist_peek(qlist);
684 g_assert(tmp);
685 qvalue = qobject_to_qfloat(tmp);
686 g_string_printf(double_expected, "%.6f", (double)i / 3);
687 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
688 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
690 qobject_decref(qlist_pop(qlist));
691 g_string_free(double_expected, true);
692 g_string_free(double_actual, true);
694 break;
695 default:
696 g_assert_not_reached();
698 QDECREF(qlist);
701 static void test_native_list(TestOutputVisitorData *data,
702 const void *unused,
703 UserDefNativeListUnionKind kind)
705 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
706 QObject *obj;
708 cvalue->type = kind;
709 init_native_list(cvalue);
711 visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
713 obj = visitor_get(data);
714 check_native_list(obj, cvalue->type);
715 qapi_free_UserDefNativeListUnion(cvalue);
718 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
719 const void *unused)
721 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
724 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
725 const void *unused)
727 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
730 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
731 const void *unused)
733 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
736 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
737 const void *unused)
739 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
742 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
743 const void *unused)
745 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
748 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
749 const void *unused)
751 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
754 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
755 const void *unused)
757 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
760 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
761 const void *unused)
763 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
766 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
767 const void *unused)
769 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
772 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
773 const void *unused)
775 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
778 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
779 const void *unused)
781 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
784 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
785 const void *unused)
787 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
790 static void output_visitor_test_add(const char *testpath,
791 TestOutputVisitorData *data,
792 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
794 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
795 test_func, visitor_output_teardown);
798 int main(int argc, char **argv)
800 TestOutputVisitorData out_visitor_data;
802 g_test_init(&argc, &argv, NULL);
804 output_visitor_test_add("/visitor/output/int",
805 &out_visitor_data, test_visitor_out_int);
806 output_visitor_test_add("/visitor/output/bool",
807 &out_visitor_data, test_visitor_out_bool);
808 output_visitor_test_add("/visitor/output/number",
809 &out_visitor_data, test_visitor_out_number);
810 output_visitor_test_add("/visitor/output/string",
811 &out_visitor_data, test_visitor_out_string);
812 output_visitor_test_add("/visitor/output/no-string",
813 &out_visitor_data, test_visitor_out_no_string);
814 output_visitor_test_add("/visitor/output/enum",
815 &out_visitor_data, test_visitor_out_enum);
816 output_visitor_test_add("/visitor/output/enum-errors",
817 &out_visitor_data, test_visitor_out_enum_errors);
818 output_visitor_test_add("/visitor/output/struct",
819 &out_visitor_data, test_visitor_out_struct);
820 output_visitor_test_add("/visitor/output/struct-nested",
821 &out_visitor_data, test_visitor_out_struct_nested);
822 output_visitor_test_add("/visitor/output/struct-errors",
823 &out_visitor_data, test_visitor_out_struct_errors);
824 output_visitor_test_add("/visitor/output/list",
825 &out_visitor_data, test_visitor_out_list);
826 output_visitor_test_add("/visitor/output/any",
827 &out_visitor_data, test_visitor_out_any);
828 output_visitor_test_add("/visitor/output/list-qapi-free",
829 &out_visitor_data, test_visitor_out_list_qapi_free);
830 output_visitor_test_add("/visitor/output/union-flat",
831 &out_visitor_data, test_visitor_out_union_flat);
832 output_visitor_test_add("/visitor/output/alternate",
833 &out_visitor_data, test_visitor_out_alternate);
834 output_visitor_test_add("/visitor/output/null",
835 &out_visitor_data, test_visitor_out_null);
836 output_visitor_test_add("/visitor/output/native_list/int",
837 &out_visitor_data,
838 test_visitor_out_native_list_int);
839 output_visitor_test_add("/visitor/output/native_list/int8",
840 &out_visitor_data,
841 test_visitor_out_native_list_int8);
842 output_visitor_test_add("/visitor/output/native_list/int16",
843 &out_visitor_data,
844 test_visitor_out_native_list_int16);
845 output_visitor_test_add("/visitor/output/native_list/int32",
846 &out_visitor_data,
847 test_visitor_out_native_list_int32);
848 output_visitor_test_add("/visitor/output/native_list/int64",
849 &out_visitor_data,
850 test_visitor_out_native_list_int64);
851 output_visitor_test_add("/visitor/output/native_list/uint8",
852 &out_visitor_data,
853 test_visitor_out_native_list_uint8);
854 output_visitor_test_add("/visitor/output/native_list/uint16",
855 &out_visitor_data,
856 test_visitor_out_native_list_uint16);
857 output_visitor_test_add("/visitor/output/native_list/uint32",
858 &out_visitor_data,
859 test_visitor_out_native_list_uint32);
860 output_visitor_test_add("/visitor/output/native_list/uint64",
861 &out_visitor_data,
862 test_visitor_out_native_list_uint64);
863 output_visitor_test_add("/visitor/output/native_list/bool",
864 &out_visitor_data,
865 test_visitor_out_native_list_bool);
866 output_visitor_test_add("/visitor/output/native_list/string",
867 &out_visitor_data,
868 test_visitor_out_native_list_str);
869 output_visitor_test_add("/visitor/output/native_list/number",
870 &out_visitor_data,
871 test_visitor_out_native_list_number);
873 g_test_run();
875 return 0;