migration: split migration hooks out of QEMUFileOps
[qemu/ar7.git] / tests / test-qmp-output-visitor.c
blob1f80e696ea3aadeea1577c72030d4219b20ccdef
1 /*
2 * QMP 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"
14 #include <glib.h>
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp-output-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
23 typedef struct TestOutputVisitorData {
24 QmpOutputVisitor *qov;
25 Visitor *ov;
26 } TestOutputVisitorData;
28 static void visitor_output_setup(TestOutputVisitorData *data,
29 const void *unused)
31 data->qov = qmp_output_visitor_new();
32 g_assert(data->qov != NULL);
34 data->ov = qmp_output_get_visitor(data->qov);
35 g_assert(data->ov != NULL);
38 static void visitor_output_teardown(TestOutputVisitorData *data,
39 const void *unused)
41 qmp_output_visitor_cleanup(data->qov);
42 data->qov = NULL;
43 data->ov = NULL;
46 static void visitor_reset(TestOutputVisitorData *data)
48 visitor_output_teardown(data, NULL);
49 visitor_output_setup(data, NULL);
52 static void test_visitor_out_int(TestOutputVisitorData *data,
53 const void *unused)
55 int64_t value = -42;
56 QObject *obj;
58 visit_type_int(data->ov, NULL, &value, &error_abort);
60 obj = qmp_output_get_qobject(data->qov);
61 g_assert(obj != NULL);
62 g_assert(qobject_type(obj) == QTYPE_QINT);
63 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
65 qobject_decref(obj);
68 static void test_visitor_out_bool(TestOutputVisitorData *data,
69 const void *unused)
71 bool value = true;
72 QObject *obj;
74 visit_type_bool(data->ov, NULL, &value, &error_abort);
76 obj = qmp_output_get_qobject(data->qov);
77 g_assert(obj != NULL);
78 g_assert(qobject_type(obj) == QTYPE_QBOOL);
79 g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
81 qobject_decref(obj);
84 static void test_visitor_out_number(TestOutputVisitorData *data,
85 const void *unused)
87 double value = 3.14;
88 QObject *obj;
90 visit_type_number(data->ov, NULL, &value, &error_abort);
92 obj = qmp_output_get_qobject(data->qov);
93 g_assert(obj != NULL);
94 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
95 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
97 qobject_decref(obj);
100 static void test_visitor_out_string(TestOutputVisitorData *data,
101 const void *unused)
103 char *string = (char *) "Q E M U";
104 QObject *obj;
106 visit_type_str(data->ov, NULL, &string, &error_abort);
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 QObject *obj;
122 /* A null string should return "" */
123 visit_type_str(data->ov, NULL, &string, &error_abort);
125 obj = qmp_output_get_qobject(data->qov);
126 g_assert(obj != NULL);
127 g_assert(qobject_type(obj) == QTYPE_QSTRING);
128 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
130 qobject_decref(obj);
133 static void test_visitor_out_enum(TestOutputVisitorData *data,
134 const void *unused)
136 QObject *obj;
137 EnumOne i;
139 for (i = 0; i < ENUM_ONE__MAX; i++) {
140 visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
142 obj = qmp_output_get_qobject(data->qov);
143 g_assert(obj != NULL);
144 g_assert(qobject_type(obj) == QTYPE_QSTRING);
145 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
146 EnumOne_lookup[i]);
147 qobject_decref(obj);
148 visitor_reset(data);
152 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
153 const void *unused)
155 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
156 Error *err;
158 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
159 err = NULL;
160 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
161 g_assert(err);
162 error_free(err);
163 visitor_reset(data);
168 static void test_visitor_out_struct(TestOutputVisitorData *data,
169 const void *unused)
171 TestStruct test_struct = { .integer = 42,
172 .boolean = false,
173 .string = (char *) "foo"};
174 TestStruct *p = &test_struct;
175 QObject *obj;
176 QDict *qdict;
178 visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
180 obj = qmp_output_get_qobject(data->qov);
181 g_assert(obj != NULL);
182 g_assert(qobject_type(obj) == QTYPE_QDICT);
184 qdict = qobject_to_qdict(obj);
185 g_assert_cmpint(qdict_size(qdict), ==, 3);
186 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
187 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
188 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
190 QDECREF(qdict);
193 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
194 const void *unused)
196 int64_t value = 42;
197 UserDefTwo *ud2;
198 QObject *obj;
199 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
200 const char *string = "user def string";
201 const char *strings[] = { "forty two", "forty three", "forty four",
202 "forty five" };
204 ud2 = g_malloc0(sizeof(*ud2));
205 ud2->string0 = g_strdup(strings[0]);
207 ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
208 ud2->dict1->string1 = g_strdup(strings[1]);
210 ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
211 ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
212 ud2->dict1->dict2->userdef->string = g_strdup(string);
213 ud2->dict1->dict2->userdef->integer = value;
214 ud2->dict1->dict2->string = g_strdup(strings[2]);
216 ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
217 ud2->dict1->has_dict3 = true;
218 ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
219 ud2->dict1->dict3->userdef->string = g_strdup(string);
220 ud2->dict1->dict3->userdef->integer = value;
221 ud2->dict1->dict3->string = g_strdup(strings[3]);
223 visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
225 obj = qmp_output_get_qobject(data->qov);
226 g_assert(obj != NULL);
227 g_assert(qobject_type(obj) == QTYPE_QDICT);
229 qdict = qobject_to_qdict(obj);
230 g_assert_cmpint(qdict_size(qdict), ==, 2);
231 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
233 dict1 = qdict_get_qdict(qdict, "dict1");
234 g_assert_cmpint(qdict_size(dict1), ==, 3);
235 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
237 dict2 = qdict_get_qdict(dict1, "dict2");
238 g_assert_cmpint(qdict_size(dict2), ==, 2);
239 g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
240 userdef = qdict_get_qdict(dict2, "userdef");
241 g_assert_cmpint(qdict_size(userdef), ==, 2);
242 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
243 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
245 dict3 = qdict_get_qdict(dict1, "dict3");
246 g_assert_cmpint(qdict_size(dict3), ==, 2);
247 g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
248 userdef = qdict_get_qdict(dict3, "userdef");
249 g_assert_cmpint(qdict_size(userdef), ==, 2);
250 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
251 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
253 QDECREF(qdict);
254 qapi_free_UserDefTwo(ud2);
257 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
258 const void *unused)
260 EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
261 UserDefOne u = {0};
262 UserDefOne *pu = &u;
263 Error *err;
264 int i;
266 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
267 err = NULL;
268 u.has_enum1 = true;
269 u.enum1 = bad_values[i];
270 visit_type_UserDefOne(data->ov, "unused", &pu, &err);
271 g_assert(err);
272 error_free(err);
273 visitor_reset(data);
278 static void test_visitor_out_list(TestOutputVisitorData *data,
279 const void *unused)
281 const char *value_str = "list value";
282 TestStructList *p, *head = NULL;
283 const int max_items = 10;
284 bool value_bool = true;
285 int value_int = 10;
286 QListEntry *entry;
287 QObject *obj;
288 QList *qlist;
289 int i;
291 /* Build the list in reverse order... */
292 for (i = 0; i < max_items; i++) {
293 p = g_malloc0(sizeof(*p));
294 p->value = g_malloc0(sizeof(*p->value));
295 p->value->integer = value_int + (max_items - i - 1);
296 p->value->boolean = value_bool;
297 p->value->string = g_strdup(value_str);
299 p->next = head;
300 head = p;
303 visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
305 obj = qmp_output_get_qobject(data->qov);
306 g_assert(obj != NULL);
307 g_assert(qobject_type(obj) == QTYPE_QLIST);
309 qlist = qobject_to_qlist(obj);
310 g_assert(!qlist_empty(qlist));
312 /* ...and ensure that the visitor sees it in order */
313 i = 0;
314 QLIST_FOREACH_ENTRY(qlist, entry) {
315 QDict *qdict;
317 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
318 qdict = qobject_to_qdict(entry->value);
319 g_assert_cmpint(qdict_size(qdict), ==, 3);
320 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
321 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
322 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
323 i++;
325 g_assert_cmpint(i, ==, max_items);
327 QDECREF(qlist);
328 qapi_free_TestStructList(head);
331 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
332 const void *unused)
334 UserDefTwoList *p, *head = NULL;
335 const char string[] = "foo bar";
336 int i, max_count = 1024;
338 for (i = 0; i < max_count; i++) {
339 p = g_malloc0(sizeof(*p));
340 p->value = g_malloc0(sizeof(*p->value));
342 p->value->string0 = g_strdup(string);
343 p->value->dict1 = g_new0(UserDefTwoDict, 1);
344 p->value->dict1->string1 = g_strdup(string);
345 p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
346 p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
347 p->value->dict1->dict2->userdef->string = g_strdup(string);
348 p->value->dict1->dict2->userdef->integer = 42;
349 p->value->dict1->dict2->string = g_strdup(string);
350 p->value->dict1->has_dict3 = false;
352 p->next = head;
353 head = p;
356 qapi_free_UserDefTwoList(head);
359 static void test_visitor_out_any(TestOutputVisitorData *data,
360 const void *unused)
362 QObject *qobj;
363 QInt *qint;
364 QBool *qbool;
365 QString *qstring;
366 QDict *qdict;
367 QObject *obj;
369 qobj = QOBJECT(qint_from_int(-42));
370 visit_type_any(data->ov, NULL, &qobj, &error_abort);
371 obj = qmp_output_get_qobject(data->qov);
372 g_assert(obj != NULL);
373 g_assert(qobject_type(obj) == QTYPE_QINT);
374 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
375 qobject_decref(obj);
376 qobject_decref(qobj);
378 visitor_reset(data);
379 qdict = qdict_new();
380 qdict_put(qdict, "integer", qint_from_int(-42));
381 qdict_put(qdict, "boolean", qbool_from_bool(true));
382 qdict_put(qdict, "string", qstring_from_str("foo"));
383 qobj = QOBJECT(qdict);
384 visit_type_any(data->ov, NULL, &qobj, &error_abort);
385 qobject_decref(qobj);
386 obj = qmp_output_get_qobject(data->qov);
387 g_assert(obj != NULL);
388 qdict = qobject_to_qdict(obj);
389 g_assert(qdict);
390 qobj = qdict_get(qdict, "integer");
391 g_assert(qobj);
392 qint = qobject_to_qint(qobj);
393 g_assert(qint);
394 g_assert_cmpint(qint_get_int(qint), ==, -42);
395 qobj = qdict_get(qdict, "boolean");
396 g_assert(qobj);
397 qbool = qobject_to_qbool(qobj);
398 g_assert(qbool);
399 g_assert(qbool_get_bool(qbool) == true);
400 qobj = qdict_get(qdict, "string");
401 g_assert(qobj);
402 qstring = qobject_to_qstring(qobj);
403 g_assert(qstring);
404 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
405 qobject_decref(obj);
408 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
409 const void *unused)
411 QObject *arg;
412 QDict *qdict;
414 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
415 tmp->enum1 = ENUM_ONE_VALUE1;
416 tmp->string = g_strdup("str");
417 tmp->integer = 41;
418 tmp->u.value1.boolean = true;
420 visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
421 arg = qmp_output_get_qobject(data->qov);
423 g_assert(qobject_type(arg) == QTYPE_QDICT);
424 qdict = qobject_to_qdict(arg);
426 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
427 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
428 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
429 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
431 qapi_free_UserDefFlatUnion(tmp);
432 QDECREF(qdict);
435 static void test_visitor_out_alternate(TestOutputVisitorData *data,
436 const void *unused)
438 QObject *arg;
439 UserDefAlternate *tmp;
440 QDict *qdict;
442 tmp = g_new0(UserDefAlternate, 1);
443 tmp->type = QTYPE_QINT;
444 tmp->u.i = 42;
446 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
447 arg = qmp_output_get_qobject(data->qov);
449 g_assert(qobject_type(arg) == QTYPE_QINT);
450 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
452 qapi_free_UserDefAlternate(tmp);
453 qobject_decref(arg);
455 visitor_reset(data);
456 tmp = g_new0(UserDefAlternate, 1);
457 tmp->type = QTYPE_QSTRING;
458 tmp->u.s = g_strdup("hello");
460 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
461 arg = qmp_output_get_qobject(data->qov);
463 g_assert(qobject_type(arg) == QTYPE_QSTRING);
464 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");
466 qapi_free_UserDefAlternate(tmp);
467 qobject_decref(arg);
469 visitor_reset(data);
470 tmp = g_new0(UserDefAlternate, 1);
471 tmp->type = QTYPE_QDICT;
472 tmp->u.udfu.integer = 1;
473 tmp->u.udfu.string = g_strdup("str");
474 tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
475 tmp->u.udfu.u.value1.boolean = true;
477 visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
478 arg = qmp_output_get_qobject(data->qov);
480 g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT);
481 qdict = qobject_to_qdict(arg);
482 g_assert_cmpint(qdict_size(qdict), ==, 4);
483 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1);
484 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
485 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
486 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
488 qapi_free_UserDefAlternate(tmp);
489 qobject_decref(arg);
492 static void test_visitor_out_null(TestOutputVisitorData *data,
493 const void *unused)
495 QObject *arg;
496 QDict *qdict;
497 QObject *nil;
499 visit_start_struct(data->ov, NULL, NULL, 0, &error_abort);
500 visit_type_null(data->ov, "a", &error_abort);
501 visit_check_struct(data->ov, &error_abort);
502 visit_end_struct(data->ov);
503 arg = qmp_output_get_qobject(data->qov);
504 g_assert(qobject_type(arg) == QTYPE_QDICT);
505 qdict = qobject_to_qdict(arg);
506 g_assert_cmpint(qdict_size(qdict), ==, 1);
507 nil = qdict_get(qdict, "a");
508 g_assert(nil);
509 g_assert(qobject_type(nil) == QTYPE_QNULL);
510 qobject_decref(arg);
513 static void init_native_list(UserDefNativeListUnion *cvalue)
515 int i;
516 switch (cvalue->type) {
517 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
518 intList **list = &cvalue->u.integer.data;
519 for (i = 0; i < 32; i++) {
520 *list = g_new0(intList, 1);
521 (*list)->value = i;
522 (*list)->next = NULL;
523 list = &(*list)->next;
525 break;
527 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
528 int8List **list = &cvalue->u.s8.data;
529 for (i = 0; i < 32; i++) {
530 *list = g_new0(int8List, 1);
531 (*list)->value = i;
532 (*list)->next = NULL;
533 list = &(*list)->next;
535 break;
537 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
538 int16List **list = &cvalue->u.s16.data;
539 for (i = 0; i < 32; i++) {
540 *list = g_new0(int16List, 1);
541 (*list)->value = i;
542 (*list)->next = NULL;
543 list = &(*list)->next;
545 break;
547 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
548 int32List **list = &cvalue->u.s32.data;
549 for (i = 0; i < 32; i++) {
550 *list = g_new0(int32List, 1);
551 (*list)->value = i;
552 (*list)->next = NULL;
553 list = &(*list)->next;
555 break;
557 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
558 int64List **list = &cvalue->u.s64.data;
559 for (i = 0; i < 32; i++) {
560 *list = g_new0(int64List, 1);
561 (*list)->value = i;
562 (*list)->next = NULL;
563 list = &(*list)->next;
565 break;
567 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
568 uint8List **list = &cvalue->u.u8.data;
569 for (i = 0; i < 32; i++) {
570 *list = g_new0(uint8List, 1);
571 (*list)->value = i;
572 (*list)->next = NULL;
573 list = &(*list)->next;
575 break;
577 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
578 uint16List **list = &cvalue->u.u16.data;
579 for (i = 0; i < 32; i++) {
580 *list = g_new0(uint16List, 1);
581 (*list)->value = i;
582 (*list)->next = NULL;
583 list = &(*list)->next;
585 break;
587 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
588 uint32List **list = &cvalue->u.u32.data;
589 for (i = 0; i < 32; i++) {
590 *list = g_new0(uint32List, 1);
591 (*list)->value = i;
592 (*list)->next = NULL;
593 list = &(*list)->next;
595 break;
597 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
598 uint64List **list = &cvalue->u.u64.data;
599 for (i = 0; i < 32; i++) {
600 *list = g_new0(uint64List, 1);
601 (*list)->value = i;
602 (*list)->next = NULL;
603 list = &(*list)->next;
605 break;
607 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
608 boolList **list = &cvalue->u.boolean.data;
609 for (i = 0; i < 32; i++) {
610 *list = g_new0(boolList, 1);
611 (*list)->value = (i % 3 == 0);
612 (*list)->next = NULL;
613 list = &(*list)->next;
615 break;
617 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
618 strList **list = &cvalue->u.string.data;
619 for (i = 0; i < 32; i++) {
620 *list = g_new0(strList, 1);
621 (*list)->value = g_strdup_printf("%d", i);
622 (*list)->next = NULL;
623 list = &(*list)->next;
625 break;
627 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
628 numberList **list = &cvalue->u.number.data;
629 for (i = 0; i < 32; i++) {
630 *list = g_new0(numberList, 1);
631 (*list)->value = (double)i / 3;
632 (*list)->next = NULL;
633 list = &(*list)->next;
635 break;
637 default:
638 g_assert_not_reached();
642 static void check_native_list(QObject *qobj,
643 UserDefNativeListUnionKind kind)
645 QDict *qdict;
646 QList *qlist;
647 int i;
649 g_assert(qobj);
650 g_assert(qobject_type(qobj) == QTYPE_QDICT);
651 qdict = qobject_to_qdict(qobj);
652 g_assert(qdict);
653 g_assert(qdict_haskey(qdict, "data"));
654 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
656 switch (kind) {
657 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
658 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
659 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
660 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
661 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
662 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
663 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
664 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
665 /* all integer elements in JSON arrays get stored into QInts when
666 * we convert to QObjects, so we can check them all in the same
667 * fashion, so simply fall through here
669 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
670 for (i = 0; i < 32; i++) {
671 QObject *tmp;
672 QInt *qvalue;
673 tmp = qlist_peek(qlist);
674 g_assert(tmp);
675 qvalue = qobject_to_qint(tmp);
676 g_assert_cmpint(qint_get_int(qvalue), ==, i);
677 qobject_decref(qlist_pop(qlist));
679 break;
680 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
681 for (i = 0; i < 32; i++) {
682 QObject *tmp;
683 QBool *qvalue;
684 tmp = qlist_peek(qlist);
685 g_assert(tmp);
686 qvalue = qobject_to_qbool(tmp);
687 g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
688 qobject_decref(qlist_pop(qlist));
690 break;
691 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
692 for (i = 0; i < 32; i++) {
693 QObject *tmp;
694 QString *qvalue;
695 gchar str[8];
696 tmp = qlist_peek(qlist);
697 g_assert(tmp);
698 qvalue = qobject_to_qstring(tmp);
699 sprintf(str, "%d", i);
700 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
701 qobject_decref(qlist_pop(qlist));
703 break;
704 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
705 for (i = 0; i < 32; i++) {
706 QObject *tmp;
707 QFloat *qvalue;
708 GString *double_expected = g_string_new("");
709 GString *double_actual = g_string_new("");
711 tmp = qlist_peek(qlist);
712 g_assert(tmp);
713 qvalue = qobject_to_qfloat(tmp);
714 g_string_printf(double_expected, "%.6f", (double)i / 3);
715 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
716 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
718 qobject_decref(qlist_pop(qlist));
719 g_string_free(double_expected, true);
720 g_string_free(double_actual, true);
722 break;
723 default:
724 g_assert_not_reached();
726 QDECREF(qlist);
729 static void test_native_list(TestOutputVisitorData *data,
730 const void *unused,
731 UserDefNativeListUnionKind kind)
733 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
734 QObject *obj;
736 cvalue->type = kind;
737 init_native_list(cvalue);
739 visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
741 obj = qmp_output_get_qobject(data->qov);
742 check_native_list(obj, cvalue->type);
743 qapi_free_UserDefNativeListUnion(cvalue);
744 qobject_decref(obj);
747 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
748 const void *unused)
750 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
753 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
754 const void *unused)
756 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
759 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
760 const void *unused)
762 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
765 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
766 const void *unused)
768 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
771 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
772 const void *unused)
774 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
777 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
778 const void *unused)
780 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
783 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
784 const void *unused)
786 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
789 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
790 const void *unused)
792 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
795 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
796 const void *unused)
798 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
801 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
802 const void *unused)
804 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
807 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
808 const void *unused)
810 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
813 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
814 const void *unused)
816 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
819 static void output_visitor_test_add(const char *testpath,
820 TestOutputVisitorData *data,
821 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
823 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
824 test_func, visitor_output_teardown);
827 int main(int argc, char **argv)
829 TestOutputVisitorData out_visitor_data;
831 g_test_init(&argc, &argv, NULL);
833 output_visitor_test_add("/visitor/output/int",
834 &out_visitor_data, test_visitor_out_int);
835 output_visitor_test_add("/visitor/output/bool",
836 &out_visitor_data, test_visitor_out_bool);
837 output_visitor_test_add("/visitor/output/number",
838 &out_visitor_data, test_visitor_out_number);
839 output_visitor_test_add("/visitor/output/string",
840 &out_visitor_data, test_visitor_out_string);
841 output_visitor_test_add("/visitor/output/no-string",
842 &out_visitor_data, test_visitor_out_no_string);
843 output_visitor_test_add("/visitor/output/enum",
844 &out_visitor_data, test_visitor_out_enum);
845 output_visitor_test_add("/visitor/output/enum-errors",
846 &out_visitor_data, test_visitor_out_enum_errors);
847 output_visitor_test_add("/visitor/output/struct",
848 &out_visitor_data, test_visitor_out_struct);
849 output_visitor_test_add("/visitor/output/struct-nested",
850 &out_visitor_data, test_visitor_out_struct_nested);
851 output_visitor_test_add("/visitor/output/struct-errors",
852 &out_visitor_data, test_visitor_out_struct_errors);
853 output_visitor_test_add("/visitor/output/list",
854 &out_visitor_data, test_visitor_out_list);
855 output_visitor_test_add("/visitor/output/any",
856 &out_visitor_data, test_visitor_out_any);
857 output_visitor_test_add("/visitor/output/list-qapi-free",
858 &out_visitor_data, test_visitor_out_list_qapi_free);
859 output_visitor_test_add("/visitor/output/union-flat",
860 &out_visitor_data, test_visitor_out_union_flat);
861 output_visitor_test_add("/visitor/output/alternate",
862 &out_visitor_data, test_visitor_out_alternate);
863 output_visitor_test_add("/visitor/output/null",
864 &out_visitor_data, test_visitor_out_null);
865 output_visitor_test_add("/visitor/output/native_list/int",
866 &out_visitor_data,
867 test_visitor_out_native_list_int);
868 output_visitor_test_add("/visitor/output/native_list/int8",
869 &out_visitor_data,
870 test_visitor_out_native_list_int8);
871 output_visitor_test_add("/visitor/output/native_list/int16",
872 &out_visitor_data,
873 test_visitor_out_native_list_int16);
874 output_visitor_test_add("/visitor/output/native_list/int32",
875 &out_visitor_data,
876 test_visitor_out_native_list_int32);
877 output_visitor_test_add("/visitor/output/native_list/int64",
878 &out_visitor_data,
879 test_visitor_out_native_list_int64);
880 output_visitor_test_add("/visitor/output/native_list/uint8",
881 &out_visitor_data,
882 test_visitor_out_native_list_uint8);
883 output_visitor_test_add("/visitor/output/native_list/uint16",
884 &out_visitor_data,
885 test_visitor_out_native_list_uint16);
886 output_visitor_test_add("/visitor/output/native_list/uint32",
887 &out_visitor_data,
888 test_visitor_out_native_list_uint32);
889 output_visitor_test_add("/visitor/output/native_list/uint64",
890 &out_visitor_data,
891 test_visitor_out_native_list_uint64);
892 output_visitor_test_add("/visitor/output/native_list/bool",
893 &out_visitor_data,
894 test_visitor_out_native_list_bool);
895 output_visitor_test_add("/visitor/output/native_list/string",
896 &out_visitor_data,
897 test_visitor_out_native_list_str);
898 output_visitor_test_add("/visitor/output/native_list/number",
899 &out_visitor_data,
900 test_visitor_out_native_list_number);
902 g_test_run();
904 return 0;