]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-qmp-output-visitor.c
qtest/ahci: Add DMA test variants
[mirror_qemu.git] / tests / test-qmp-output-visitor.c
1 /*
2 * QMP Output Visitor unit-tests.
3 *
4 * Copyright (C) 2011 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
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.
11 */
12
13 #include <glib.h>
14
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"
20
21 typedef struct TestOutputVisitorData {
22 QmpOutputVisitor *qov;
23 Visitor *ov;
24 } TestOutputVisitorData;
25
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
28 {
29 data->qov = qmp_output_visitor_new();
30 g_assert(data->qov != NULL);
31
32 data->ov = qmp_output_get_visitor(data->qov);
33 g_assert(data->ov != NULL);
34 }
35
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
38 {
39 qmp_output_visitor_cleanup(data->qov);
40 data->qov = NULL;
41 data->ov = NULL;
42 }
43
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
46 {
47 int64_t value = -42;
48 Error *err = NULL;
49 QObject *obj;
50
51 visit_type_int(data->ov, &value, NULL, &err);
52 g_assert(!err);
53
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);
58
59 qobject_decref(obj);
60 }
61
62 static void test_visitor_out_bool(TestOutputVisitorData *data,
63 const void *unused)
64 {
65 Error *err = NULL;
66 bool value = true;
67 QObject *obj;
68
69 visit_type_bool(data->ov, &value, NULL, &err);
70 g_assert(!err);
71
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);
76
77 qobject_decref(obj);
78 }
79
80 static void test_visitor_out_number(TestOutputVisitorData *data,
81 const void *unused)
82 {
83 double value = 3.14;
84 Error *err = NULL;
85 QObject *obj;
86
87 visit_type_number(data->ov, &value, NULL, &err);
88 g_assert(!err);
89
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);
94
95 qobject_decref(obj);
96 }
97
98 static void test_visitor_out_string(TestOutputVisitorData *data,
99 const void *unused)
100 {
101 char *string = (char *) "Q E M U";
102 Error *err = NULL;
103 QObject *obj;
104
105 visit_type_str(data->ov, &string, NULL, &err);
106 g_assert(!err);
107
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);
112
113 qobject_decref(obj);
114 }
115
116 static void test_visitor_out_no_string(TestOutputVisitorData *data,
117 const void *unused)
118 {
119 char *string = NULL;
120 Error *err = NULL;
121 QObject *obj;
122
123 /* A null string should return "" */
124 visit_type_str(data->ov, &string, NULL, &err);
125 g_assert(!err);
126
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)), ==, "");
131
132 qobject_decref(obj);
133 }
134
135 static void test_visitor_out_enum(TestOutputVisitorData *data,
136 const void *unused)
137 {
138 Error *err = NULL;
139 QObject *obj;
140 EnumOne i;
141
142 for (i = 0; i < ENUM_ONE_MAX; i++) {
143 visit_type_EnumOne(data->ov, &i, "unused", &err);
144 g_assert(!err);
145
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);
152 }
153 }
154
155 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
156 const void *unused)
157 {
158 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
159 Error *err;
160
161 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
162 err = NULL;
163 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
164 g_assert(err);
165 error_free(err);
166 }
167 }
168
169 typedef struct TestStruct
170 {
171 int64_t integer;
172 bool boolean;
173 char *string;
174 } TestStruct;
175
176 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
177 const char *name, Error **errp)
178 {
179 Error *err = NULL;
180
181 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
182 &err);
183 if (err) {
184 goto out;
185 }
186
187 visit_type_int(v, &(*obj)->integer, "integer", &err);
188 if (err) {
189 goto out_end;
190 }
191 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
192 if (err) {
193 goto out_end;
194 }
195 visit_type_str(v, &(*obj)->string, "string", &err);
196
197 out_end:
198 error_propagate(errp, err);
199 err = NULL;
200 visit_end_struct(v, &err);
201 out:
202 error_propagate(errp, err);
203 }
204
205 static void test_visitor_out_struct(TestOutputVisitorData *data,
206 const void *unused)
207 {
208 TestStruct test_struct = { .integer = 42,
209 .boolean = false,
210 .string = (char *) "foo"};
211 TestStruct *p = &test_struct;
212 Error *err = NULL;
213 QObject *obj;
214 QDict *qdict;
215
216 visit_type_TestStruct(data->ov, &p, NULL, &err);
217 g_assert(!err);
218
219 obj = qmp_output_get_qobject(data->qov);
220 g_assert(obj != NULL);
221 g_assert(qobject_type(obj) == QTYPE_QDICT);
222
223 qdict = qobject_to_qdict(obj);
224 g_assert_cmpint(qdict_size(qdict), ==, 3);
225 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
226 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0);
227 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
228
229 QDECREF(qdict);
230 }
231
232 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
233 const void *unused)
234 {
235 int64_t value = 42;
236 Error *err = NULL;
237 UserDefNested *ud2;
238 QObject *obj;
239 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
240 const char *string = "user def string";
241 const char *strings[] = { "forty two", "forty three", "forty four",
242 "forty five" };
243
244 ud2 = g_malloc0(sizeof(*ud2));
245 ud2->string0 = g_strdup(strings[0]);
246
247 ud2->dict1.string1 = g_strdup(strings[1]);
248 ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
249 ud2->dict1.dict2.userdef1->string = g_strdup(string);
250 ud2->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
251 ud2->dict1.dict2.userdef1->base->integer = value;
252 ud2->dict1.dict2.string2 = g_strdup(strings[2]);
253
254 ud2->dict1.has_dict3 = true;
255 ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
256 ud2->dict1.dict3.userdef2->string = g_strdup(string);
257 ud2->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);
258 ud2->dict1.dict3.userdef2->base->integer = value;
259 ud2->dict1.dict3.string3 = g_strdup(strings[3]);
260
261 visit_type_UserDefNested(data->ov, &ud2, "unused", &err);
262 g_assert(!err);
263
264 obj = qmp_output_get_qobject(data->qov);
265 g_assert(obj != NULL);
266 g_assert(qobject_type(obj) == QTYPE_QDICT);
267
268 qdict = qobject_to_qdict(obj);
269 g_assert_cmpint(qdict_size(qdict), ==, 2);
270 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
271
272 dict1 = qdict_get_qdict(qdict, "dict1");
273 g_assert_cmpint(qdict_size(dict1), ==, 3);
274 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
275
276 dict2 = qdict_get_qdict(dict1, "dict2");
277 g_assert_cmpint(qdict_size(dict2), ==, 2);
278 g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
279 userdef = qdict_get_qdict(dict2, "userdef1");
280 g_assert_cmpint(qdict_size(userdef), ==, 2);
281 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
282 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
283
284 dict3 = qdict_get_qdict(dict1, "dict3");
285 g_assert_cmpint(qdict_size(dict3), ==, 2);
286 g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
287 userdef = qdict_get_qdict(dict3, "userdef2");
288 g_assert_cmpint(qdict_size(userdef), ==, 2);
289 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
290 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
291
292 QDECREF(qdict);
293 qapi_free_UserDefNested(ud2);
294 }
295
296 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
297 const void *unused)
298 {
299 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
300 UserDefZero b;
301 UserDefOne u = { .base = &b }, *pu = &u;
302 Error *err;
303 int i;
304
305 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
306 err = NULL;
307 u.has_enum1 = true;
308 u.enum1 = bad_values[i];
309 visit_type_UserDefOne(data->ov, &pu, "unused", &err);
310 g_assert(err);
311 error_free(err);
312 }
313 }
314
315 typedef struct TestStructList
316 {
317 union {
318 TestStruct *value;
319 uint64_t padding;
320 };
321 struct TestStructList *next;
322 } TestStructList;
323
324 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
325 const char *name, Error **errp)
326 {
327 GenericList *i, **head = (GenericList **)obj;
328
329 visit_start_list(v, name, errp);
330
331 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
332 TestStructList *native_i = (TestStructList *)i;
333 visit_type_TestStruct(v, &native_i->value, NULL, errp);
334 }
335
336 visit_end_list(v, errp);
337 }
338
339 static void test_visitor_out_list(TestOutputVisitorData *data,
340 const void *unused)
341 {
342 char *value_str = (char *) "list value";
343 TestStructList *p, *head = NULL;
344 const int max_items = 10;
345 bool value_bool = true;
346 int value_int = 10;
347 Error *err = NULL;
348 QListEntry *entry;
349 QObject *obj;
350 QList *qlist;
351 int i;
352
353 for (i = 0; i < max_items; i++) {
354 p = g_malloc0(sizeof(*p));
355 p->value = g_malloc0(sizeof(*p->value));
356 p->value->integer = value_int;
357 p->value->boolean = value_bool;
358 p->value->string = value_str;
359
360 p->next = head;
361 head = p;
362 }
363
364 visit_type_TestStructList(data->ov, &head, NULL, &err);
365 g_assert(!err);
366
367 obj = qmp_output_get_qobject(data->qov);
368 g_assert(obj != NULL);
369 g_assert(qobject_type(obj) == QTYPE_QLIST);
370
371 qlist = qobject_to_qlist(obj);
372 g_assert(!qlist_empty(qlist));
373
374 i = 0;
375 QLIST_FOREACH_ENTRY(qlist, entry) {
376 QDict *qdict;
377
378 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
379 qdict = qobject_to_qdict(entry->value);
380 g_assert_cmpint(qdict_size(qdict), ==, 3);
381 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
382 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
383 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
384 i++;
385 }
386 g_assert_cmpint(i, ==, max_items);
387
388 QDECREF(qlist);
389
390 for (p = head; p;) {
391 TestStructList *tmp = p->next;
392 g_free(p->value);
393 g_free(p);
394 p = tmp;
395 }
396 }
397
398 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
399 const void *unused)
400 {
401 UserDefNestedList *p, *head = NULL;
402 const char string[] = "foo bar";
403 int i, max_count = 1024;
404
405 for (i = 0; i < max_count; i++) {
406 p = g_malloc0(sizeof(*p));
407 p->value = g_malloc0(sizeof(*p->value));
408
409 p->value->string0 = g_strdup(string);
410 p->value->dict1.string1 = g_strdup(string);
411 p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
412 p->value->dict1.dict2.userdef1->string = g_strdup(string);
413 p->value->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
414 p->value->dict1.dict2.userdef1->base->integer = 42;
415 p->value->dict1.dict2.string2 = g_strdup(string);
416 p->value->dict1.has_dict3 = false;
417
418 p->next = head;
419 head = p;
420 }
421
422 qapi_free_UserDefNestedList(head);
423 }
424
425 static void test_visitor_out_union(TestOutputVisitorData *data,
426 const void *unused)
427 {
428 QObject *arg, *qvalue;
429 QDict *qdict, *value;
430
431 Error *err = NULL;
432
433 UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
434 tmp->kind = USER_DEF_UNION_KIND_A;
435 tmp->integer = 41;
436 tmp->a = g_malloc0(sizeof(UserDefA));
437 tmp->a->boolean = true;
438
439 visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
440 g_assert(err == NULL);
441 arg = qmp_output_get_qobject(data->qov);
442
443 g_assert(qobject_type(arg) == QTYPE_QDICT);
444 qdict = qobject_to_qdict(arg);
445
446 g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
447 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
448
449 qvalue = qdict_get(qdict, "data");
450 g_assert(data != NULL);
451 g_assert(qobject_type(qvalue) == QTYPE_QDICT);
452 value = qobject_to_qdict(qvalue);
453 g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
454
455 qapi_free_UserDefUnion(tmp);
456 QDECREF(qdict);
457 }
458
459 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
460 const void *unused)
461 {
462 QObject *arg;
463 QDict *qdict;
464
465 Error *err = NULL;
466
467 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
468 tmp->kind = ENUM_ONE_VALUE1;
469 tmp->string = g_strdup("str");
470 tmp->value1 = g_malloc0(sizeof(UserDefA));
471 /* TODO when generator bug is fixed: tmp->integer = 41; */
472 tmp->value1->boolean = true;
473
474 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
475 g_assert(err == NULL);
476 arg = qmp_output_get_qobject(data->qov);
477
478 g_assert(qobject_type(arg) == QTYPE_QDICT);
479 qdict = qobject_to_qdict(arg);
480
481 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
482 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
483 /* TODO g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); */
484 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
485
486 qapi_free_UserDefFlatUnion(tmp);
487 QDECREF(qdict);
488 }
489
490 static void test_visitor_out_union_anon(TestOutputVisitorData *data,
491 const void *unused)
492 {
493 QObject *arg;
494 Error *err = NULL;
495
496 UserDefAnonUnion *tmp = g_malloc0(sizeof(UserDefAnonUnion));
497 tmp->kind = USER_DEF_ANON_UNION_KIND_I;
498 tmp->i = 42;
499
500 visit_type_UserDefAnonUnion(data->ov, &tmp, NULL, &err);
501 g_assert(err == NULL);
502 arg = qmp_output_get_qobject(data->qov);
503
504 g_assert(qobject_type(arg) == QTYPE_QINT);
505 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
506
507 qapi_free_UserDefAnonUnion(tmp);
508 }
509
510 static void test_visitor_out_empty(TestOutputVisitorData *data,
511 const void *unused)
512 {
513 QObject *arg;
514
515 arg = qmp_output_get_qobject(data->qov);
516 g_assert(!arg);
517 }
518
519 static void init_native_list(UserDefNativeListUnion *cvalue)
520 {
521 int i;
522 switch (cvalue->kind) {
523 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
524 intList **list = &cvalue->integer;
525 for (i = 0; i < 32; i++) {
526 *list = g_new0(intList, 1);
527 (*list)->value = i;
528 (*list)->next = NULL;
529 list = &(*list)->next;
530 }
531 break;
532 }
533 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
534 int8List **list = &cvalue->s8;
535 for (i = 0; i < 32; i++) {
536 *list = g_new0(int8List, 1);
537 (*list)->value = i;
538 (*list)->next = NULL;
539 list = &(*list)->next;
540 }
541 break;
542 }
543 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
544 int16List **list = &cvalue->s16;
545 for (i = 0; i < 32; i++) {
546 *list = g_new0(int16List, 1);
547 (*list)->value = i;
548 (*list)->next = NULL;
549 list = &(*list)->next;
550 }
551 break;
552 }
553 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
554 int32List **list = &cvalue->s32;
555 for (i = 0; i < 32; i++) {
556 *list = g_new0(int32List, 1);
557 (*list)->value = i;
558 (*list)->next = NULL;
559 list = &(*list)->next;
560 }
561 break;
562 }
563 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
564 int64List **list = &cvalue->s64;
565 for (i = 0; i < 32; i++) {
566 *list = g_new0(int64List, 1);
567 (*list)->value = i;
568 (*list)->next = NULL;
569 list = &(*list)->next;
570 }
571 break;
572 }
573 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
574 uint8List **list = &cvalue->u8;
575 for (i = 0; i < 32; i++) {
576 *list = g_new0(uint8List, 1);
577 (*list)->value = i;
578 (*list)->next = NULL;
579 list = &(*list)->next;
580 }
581 break;
582 }
583 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
584 uint16List **list = &cvalue->u16;
585 for (i = 0; i < 32; i++) {
586 *list = g_new0(uint16List, 1);
587 (*list)->value = i;
588 (*list)->next = NULL;
589 list = &(*list)->next;
590 }
591 break;
592 }
593 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
594 uint32List **list = &cvalue->u32;
595 for (i = 0; i < 32; i++) {
596 *list = g_new0(uint32List, 1);
597 (*list)->value = i;
598 (*list)->next = NULL;
599 list = &(*list)->next;
600 }
601 break;
602 }
603 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
604 uint64List **list = &cvalue->u64;
605 for (i = 0; i < 32; i++) {
606 *list = g_new0(uint64List, 1);
607 (*list)->value = i;
608 (*list)->next = NULL;
609 list = &(*list)->next;
610 }
611 break;
612 }
613 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
614 boolList **list = &cvalue->boolean;
615 for (i = 0; i < 32; i++) {
616 *list = g_new0(boolList, 1);
617 (*list)->value = (i % 3 == 0);
618 (*list)->next = NULL;
619 list = &(*list)->next;
620 }
621 break;
622 }
623 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
624 strList **list = &cvalue->string;
625 for (i = 0; i < 32; i++) {
626 *list = g_new0(strList, 1);
627 (*list)->value = g_strdup_printf("%d", i);
628 (*list)->next = NULL;
629 list = &(*list)->next;
630 }
631 break;
632 }
633 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
634 numberList **list = &cvalue->number;
635 for (i = 0; i < 32; i++) {
636 *list = g_new0(numberList, 1);
637 (*list)->value = (double)i / 3;
638 (*list)->next = NULL;
639 list = &(*list)->next;
640 }
641 break;
642 }
643 default:
644 g_assert_not_reached();
645 }
646 }
647
648 static void check_native_list(QObject *qobj,
649 UserDefNativeListUnionKind kind)
650 {
651 QDict *qdict;
652 QList *qlist;
653 int i;
654
655 g_assert(qobj);
656 g_assert(qobject_type(qobj) == QTYPE_QDICT);
657 qdict = qobject_to_qdict(qobj);
658 g_assert(qdict);
659 g_assert(qdict_haskey(qdict, "data"));
660 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
661
662 switch (kind) {
663 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
664 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
665 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
666 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
667 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
668 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
669 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
670 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
671 /* all integer elements in JSON arrays get stored into QInts when
672 * we convert to QObjects, so we can check them all in the same
673 * fashion, so simply fall through here
674 */
675 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
676 for (i = 0; i < 32; i++) {
677 QObject *tmp;
678 QInt *qvalue;
679 tmp = qlist_peek(qlist);
680 g_assert(tmp);
681 qvalue = qobject_to_qint(tmp);
682 g_assert_cmpint(qint_get_int(qvalue), ==, i);
683 qobject_decref(qlist_pop(qlist));
684 }
685 break;
686 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
687 for (i = 0; i < 32; i++) {
688 QObject *tmp;
689 QBool *qvalue;
690 tmp = qlist_peek(qlist);
691 g_assert(tmp);
692 qvalue = qobject_to_qbool(tmp);
693 g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0);
694 qobject_decref(qlist_pop(qlist));
695 }
696 break;
697 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
698 for (i = 0; i < 32; i++) {
699 QObject *tmp;
700 QString *qvalue;
701 gchar str[8];
702 tmp = qlist_peek(qlist);
703 g_assert(tmp);
704 qvalue = qobject_to_qstring(tmp);
705 sprintf(str, "%d", i);
706 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
707 qobject_decref(qlist_pop(qlist));
708 }
709 break;
710 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
711 for (i = 0; i < 32; i++) {
712 QObject *tmp;
713 QFloat *qvalue;
714 GString *double_expected = g_string_new("");
715 GString *double_actual = g_string_new("");
716
717 tmp = qlist_peek(qlist);
718 g_assert(tmp);
719 qvalue = qobject_to_qfloat(tmp);
720 g_string_printf(double_expected, "%.6f", (double)i / 3);
721 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
722 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
723
724 qobject_decref(qlist_pop(qlist));
725 g_string_free(double_expected, true);
726 g_string_free(double_actual, true);
727 }
728 break;
729 default:
730 g_assert_not_reached();
731 }
732 QDECREF(qlist);
733 }
734
735 static void test_native_list(TestOutputVisitorData *data,
736 const void *unused,
737 UserDefNativeListUnionKind kind)
738 {
739 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
740 Error *err = NULL;
741 QObject *obj;
742
743 cvalue->kind = kind;
744 init_native_list(cvalue);
745
746 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
747 g_assert(err == NULL);
748
749 obj = qmp_output_get_qobject(data->qov);
750 check_native_list(obj, cvalue->kind);
751 qapi_free_UserDefNativeListUnion(cvalue);
752 qobject_decref(obj);
753 }
754
755 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
756 const void *unused)
757 {
758 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
759 }
760
761 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
762 const void *unused)
763 {
764 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
765 }
766
767 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
768 const void *unused)
769 {
770 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
771 }
772
773 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
774 const void *unused)
775 {
776 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
777 }
778
779 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
780 const void *unused)
781 {
782 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
783 }
784
785 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
786 const void *unused)
787 {
788 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
789 }
790
791 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
792 const void *unused)
793 {
794 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
795 }
796
797 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
798 const void *unused)
799 {
800 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
801 }
802
803 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
804 const void *unused)
805 {
806 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
807 }
808
809 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
810 const void *unused)
811 {
812 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
813 }
814
815 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
816 const void *unused)
817 {
818 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
819 }
820
821 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
822 const void *unused)
823 {
824 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
825 }
826
827 static void output_visitor_test_add(const char *testpath,
828 TestOutputVisitorData *data,
829 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
830 {
831 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
832 test_func, visitor_output_teardown);
833 }
834
835 int main(int argc, char **argv)
836 {
837 TestOutputVisitorData out_visitor_data;
838
839 g_test_init(&argc, &argv, NULL);
840
841 output_visitor_test_add("/visitor/output/int",
842 &out_visitor_data, test_visitor_out_int);
843 output_visitor_test_add("/visitor/output/bool",
844 &out_visitor_data, test_visitor_out_bool);
845 output_visitor_test_add("/visitor/output/number",
846 &out_visitor_data, test_visitor_out_number);
847 output_visitor_test_add("/visitor/output/string",
848 &out_visitor_data, test_visitor_out_string);
849 output_visitor_test_add("/visitor/output/no-string",
850 &out_visitor_data, test_visitor_out_no_string);
851 output_visitor_test_add("/visitor/output/enum",
852 &out_visitor_data, test_visitor_out_enum);
853 output_visitor_test_add("/visitor/output/enum-errors",
854 &out_visitor_data, test_visitor_out_enum_errors);
855 output_visitor_test_add("/visitor/output/struct",
856 &out_visitor_data, test_visitor_out_struct);
857 output_visitor_test_add("/visitor/output/struct-nested",
858 &out_visitor_data, test_visitor_out_struct_nested);
859 output_visitor_test_add("/visitor/output/struct-errors",
860 &out_visitor_data, test_visitor_out_struct_errors);
861 output_visitor_test_add("/visitor/output/list",
862 &out_visitor_data, test_visitor_out_list);
863 output_visitor_test_add("/visitor/output/list-qapi-free",
864 &out_visitor_data, test_visitor_out_list_qapi_free);
865 output_visitor_test_add("/visitor/output/union",
866 &out_visitor_data, test_visitor_out_union);
867 output_visitor_test_add("/visitor/output/union-flat",
868 &out_visitor_data, test_visitor_out_union_flat);
869 output_visitor_test_add("/visitor/output/union-anon",
870 &out_visitor_data, test_visitor_out_union_anon);
871 output_visitor_test_add("/visitor/output/empty",
872 &out_visitor_data, test_visitor_out_empty);
873 output_visitor_test_add("/visitor/output/native_list/int",
874 &out_visitor_data, test_visitor_out_native_list_int);
875 output_visitor_test_add("/visitor/output/native_list/int8",
876 &out_visitor_data, test_visitor_out_native_list_int8);
877 output_visitor_test_add("/visitor/output/native_list/int16",
878 &out_visitor_data, test_visitor_out_native_list_int16);
879 output_visitor_test_add("/visitor/output/native_list/int32",
880 &out_visitor_data, test_visitor_out_native_list_int32);
881 output_visitor_test_add("/visitor/output/native_list/int64",
882 &out_visitor_data, test_visitor_out_native_list_int64);
883 output_visitor_test_add("/visitor/output/native_list/uint8",
884 &out_visitor_data, test_visitor_out_native_list_uint8);
885 output_visitor_test_add("/visitor/output/native_list/uint16",
886 &out_visitor_data, test_visitor_out_native_list_uint16);
887 output_visitor_test_add("/visitor/output/native_list/uint32",
888 &out_visitor_data, test_visitor_out_native_list_uint32);
889 output_visitor_test_add("/visitor/output/native_list/uint64",
890 &out_visitor_data, test_visitor_out_native_list_uint64);
891 output_visitor_test_add("/visitor/output/native_list/bool",
892 &out_visitor_data, test_visitor_out_native_list_bool);
893 output_visitor_test_add("/visitor/output/native_list/string",
894 &out_visitor_data, test_visitor_out_native_list_str);
895 output_visitor_test_add("/visitor/output/native_list/number",
896 &out_visitor_data, test_visitor_out_native_list_number);
897
898 g_test_run();
899
900 return 0;
901 }