2 * QObject Output Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
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 "qapi/error.h"
16 #include "qapi/qobject-output-visitor.h"
17 #include "test-qapi-visit.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qnull.h"
22 #include "qapi/qmp/qnum.h"
23 #include "qapi/qmp/qstring.h"
25 typedef struct TestOutputVisitorData
{
28 } TestOutputVisitorData
;
30 static void visitor_output_setup(TestOutputVisitorData
*data
,
33 data
->ov
= qobject_output_visitor_new(&data
->obj
);
37 static void visitor_output_teardown(TestOutputVisitorData
*data
,
42 qobject_unref(data
->obj
);
46 static QObject
*visitor_get(TestOutputVisitorData
*data
)
48 visit_complete(data
->ov
, &data
->obj
);
53 static void visitor_reset(TestOutputVisitorData
*data
)
55 visitor_output_teardown(data
, NULL
);
56 visitor_output_setup(data
, NULL
);
59 static void test_visitor_out_int(TestOutputVisitorData
*data
,
66 visit_type_int(data
->ov
, NULL
, &value
, &error_abort
);
68 qnum
= qobject_to(QNum
, visitor_get(data
));
70 g_assert(qnum_get_try_int(qnum
, &val
));
71 g_assert_cmpint(val
, ==, value
);
74 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
80 visit_type_bool(data
->ov
, NULL
, &value
, &error_abort
);
82 qbool
= qobject_to(QBool
, visitor_get(data
));
84 g_assert(qbool_get_bool(qbool
) == value
);
87 static void test_visitor_out_number(TestOutputVisitorData
*data
,
93 visit_type_number(data
->ov
, NULL
, &value
, &error_abort
);
95 qnum
= qobject_to(QNum
, visitor_get(data
));
97 g_assert(qnum_get_double(qnum
) == value
);
100 static void test_visitor_out_string(TestOutputVisitorData
*data
,
103 char *string
= (char *) "Q E M U";
106 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
108 qstr
= qobject_to(QString
, visitor_get(data
));
110 g_assert_cmpstr(qstring_get_str(qstr
), ==, string
);
113 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
119 /* A null string should return "" */
120 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
122 qstr
= qobject_to(QString
, visitor_get(data
));
124 g_assert_cmpstr(qstring_get_str(qstr
), ==, "");
127 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
133 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
134 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
136 qstr
= qobject_to(QString
, visitor_get(data
));
138 g_assert_cmpstr(qstring_get_str(qstr
), ==, EnumOne_str(i
));
143 static void test_visitor_out_struct(TestOutputVisitorData
*data
,
146 TestStruct test_struct
= { .integer
= 42,
148 .string
= (char *) "foo"};
149 TestStruct
*p
= &test_struct
;
152 visit_type_TestStruct(data
->ov
, NULL
, &p
, &error_abort
);
154 qdict
= qobject_to(QDict
, visitor_get(data
));
156 g_assert_cmpint(qdict_size(qdict
), ==, 3);
157 g_assert_cmpint(qdict_get_int(qdict
, "integer"), ==, 42);
158 g_assert_cmpint(qdict_get_bool(qdict
, "boolean"), ==, false);
159 g_assert_cmpstr(qdict_get_str(qdict
, "string"), ==, "foo");
162 static void test_visitor_out_struct_nested(TestOutputVisitorData
*data
,
167 QDict
*qdict
, *dict1
, *dict2
, *dict3
, *userdef
;
168 const char *string
= "user def string";
169 const char *strings
[] = { "forty two", "forty three", "forty four",
172 ud2
= g_malloc0(sizeof(*ud2
));
173 ud2
->string0
= g_strdup(strings
[0]);
175 ud2
->dict1
= g_malloc0(sizeof(*ud2
->dict1
));
176 ud2
->dict1
->string1
= g_strdup(strings
[1]);
178 ud2
->dict1
->dict2
= g_malloc0(sizeof(*ud2
->dict1
->dict2
));
179 ud2
->dict1
->dict2
->userdef
= g_new0(UserDefOne
, 1);
180 ud2
->dict1
->dict2
->userdef
->string
= g_strdup(string
);
181 ud2
->dict1
->dict2
->userdef
->integer
= value
;
182 ud2
->dict1
->dict2
->string
= g_strdup(strings
[2]);
184 ud2
->dict1
->dict3
= g_malloc0(sizeof(*ud2
->dict1
->dict3
));
185 ud2
->dict1
->dict3
->userdef
= g_new0(UserDefOne
, 1);
186 ud2
->dict1
->dict3
->userdef
->string
= g_strdup(string
);
187 ud2
->dict1
->dict3
->userdef
->integer
= value
;
188 ud2
->dict1
->dict3
->string
= g_strdup(strings
[3]);
190 visit_type_UserDefTwo(data
->ov
, "unused", &ud2
, &error_abort
);
192 qdict
= qobject_to(QDict
, visitor_get(data
));
194 g_assert_cmpint(qdict_size(qdict
), ==, 2);
195 g_assert_cmpstr(qdict_get_str(qdict
, "string0"), ==, strings
[0]);
197 dict1
= qdict_get_qdict(qdict
, "dict1");
198 g_assert_cmpint(qdict_size(dict1
), ==, 3);
199 g_assert_cmpstr(qdict_get_str(dict1
, "string1"), ==, strings
[1]);
201 dict2
= qdict_get_qdict(dict1
, "dict2");
202 g_assert_cmpint(qdict_size(dict2
), ==, 2);
203 g_assert_cmpstr(qdict_get_str(dict2
, "string"), ==, strings
[2]);
204 userdef
= qdict_get_qdict(dict2
, "userdef");
205 g_assert_cmpint(qdict_size(userdef
), ==, 2);
206 g_assert_cmpint(qdict_get_int(userdef
, "integer"), ==, value
);
207 g_assert_cmpstr(qdict_get_str(userdef
, "string"), ==, string
);
209 dict3
= qdict_get_qdict(dict1
, "dict3");
210 g_assert_cmpint(qdict_size(dict3
), ==, 2);
211 g_assert_cmpstr(qdict_get_str(dict3
, "string"), ==, strings
[3]);
212 userdef
= qdict_get_qdict(dict3
, "userdef");
213 g_assert_cmpint(qdict_size(userdef
), ==, 2);
214 g_assert_cmpint(qdict_get_int(userdef
, "integer"), ==, value
);
215 g_assert_cmpstr(qdict_get_str(userdef
, "string"), ==, string
);
217 qapi_free_UserDefTwo(ud2
);
220 static void test_visitor_out_list(TestOutputVisitorData
*data
,
223 const char *value_str
= "list value";
225 TestStructList
*head
= NULL
;
226 const int max_items
= 10;
227 bool value_bool
= true;
233 /* Build the list in reverse order... */
234 for (i
= 0; i
< max_items
; i
++) {
235 value
= g_malloc0(sizeof(*value
));
236 value
->integer
= value_int
+ (max_items
- i
- 1);
237 value
->boolean
= value_bool
;
238 value
->string
= g_strdup(value_str
);
240 QAPI_LIST_PREPEND(head
, value
);
243 visit_type_TestStructList(data
->ov
, NULL
, &head
, &error_abort
);
245 qlist
= qobject_to(QList
, visitor_get(data
));
247 g_assert(!qlist_empty(qlist
));
249 /* ...and ensure that the visitor sees it in order */
251 QLIST_FOREACH_ENTRY(qlist
, entry
) {
254 qdict
= qobject_to(QDict
, entry
->value
);
256 g_assert_cmpint(qdict_size(qdict
), ==, 3);
257 g_assert_cmpint(qdict_get_int(qdict
, "integer"), ==, value_int
+ i
);
258 g_assert_cmpint(qdict_get_bool(qdict
, "boolean"), ==, value_bool
);
259 g_assert_cmpstr(qdict_get_str(qdict
, "string"), ==, value_str
);
262 g_assert_cmpint(i
, ==, max_items
);
264 qapi_free_TestStructList(head
);
267 static void test_visitor_out_list_qapi_free(TestOutputVisitorData
*data
,
271 UserDefTwoList
*head
= NULL
;
272 const char string
[] = "foo bar";
273 int i
, max_count
= 1024;
275 for (i
= 0; i
< max_count
; i
++) {
276 value
= g_malloc0(sizeof(*value
));
278 value
->string0
= g_strdup(string
);
279 value
->dict1
= g_new0(UserDefTwoDict
, 1);
280 value
->dict1
->string1
= g_strdup(string
);
281 value
->dict1
->dict2
= g_new0(UserDefTwoDictDict
, 1);
282 value
->dict1
->dict2
->userdef
= g_new0(UserDefOne
, 1);
283 value
->dict1
->dict2
->userdef
->string
= g_strdup(string
);
284 value
->dict1
->dict2
->userdef
->integer
= 42;
285 value
->dict1
->dict2
->string
= g_strdup(string
);
287 QAPI_LIST_PREPEND(head
, value
);
290 qapi_free_UserDefTwoList(head
);
293 static void test_visitor_out_any(TestOutputVisitorData
*data
,
303 qobj
= QOBJECT(qnum_from_int(-42));
304 visit_type_any(data
->ov
, NULL
, &qobj
, &error_abort
);
305 qnum
= qobject_to(QNum
, visitor_get(data
));
307 g_assert(qnum_get_try_int(qnum
, &val
));
308 g_assert_cmpint(val
, ==, -42);
313 qdict_put_int(qdict
, "integer", -42);
314 qdict_put_bool(qdict
, "boolean", true);
315 qdict_put_str(qdict
, "string", "foo");
316 qobj
= QOBJECT(qdict
);
317 visit_type_any(data
->ov
, NULL
, &qobj
, &error_abort
);
319 qdict
= qobject_to(QDict
, visitor_get(data
));
321 qnum
= qobject_to(QNum
, qdict_get(qdict
, "integer"));
323 g_assert(qnum_get_try_int(qnum
, &val
));
324 g_assert_cmpint(val
, ==, -42);
325 qbool
= qobject_to(QBool
, qdict_get(qdict
, "boolean"));
327 g_assert(qbool_get_bool(qbool
) == true);
328 qstring
= qobject_to(QString
, qdict_get(qdict
, "string"));
330 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
333 static void test_visitor_out_union_flat(TestOutputVisitorData
*data
,
338 UserDefFlatUnion
*tmp
= g_new0(UserDefFlatUnion
, 1);
339 tmp
->enum1
= ENUM_ONE_VALUE1
;
340 tmp
->string
= g_strdup("str");
342 tmp
->u
.value1
.boolean
= true;
344 visit_type_UserDefFlatUnion(data
->ov
, NULL
, &tmp
, &error_abort
);
345 qdict
= qobject_to(QDict
, visitor_get(data
));
347 g_assert_cmpstr(qdict_get_str(qdict
, "enum1"), ==, "value1");
348 g_assert_cmpstr(qdict_get_str(qdict
, "string"), ==, "str");
349 g_assert_cmpint(qdict_get_int(qdict
, "integer"), ==, 41);
350 g_assert_cmpint(qdict_get_bool(qdict
, "boolean"), ==, true);
352 qapi_free_UserDefFlatUnion(tmp
);
355 static void test_visitor_out_alternate(TestOutputVisitorData
*data
,
358 UserDefAlternate
*tmp
;
364 tmp
= g_new0(UserDefAlternate
, 1);
365 tmp
->type
= QTYPE_QNUM
;
368 visit_type_UserDefAlternate(data
->ov
, NULL
, &tmp
, &error_abort
);
369 qnum
= qobject_to(QNum
, visitor_get(data
));
371 g_assert(qnum_get_try_int(qnum
, &val
));
372 g_assert_cmpint(val
, ==, 42);
374 qapi_free_UserDefAlternate(tmp
);
377 tmp
= g_new0(UserDefAlternate
, 1);
378 tmp
->type
= QTYPE_QSTRING
;
379 tmp
->u
.e
= ENUM_ONE_VALUE1
;
381 visit_type_UserDefAlternate(data
->ov
, NULL
, &tmp
, &error_abort
);
382 qstr
= qobject_to(QString
, visitor_get(data
));
384 g_assert_cmpstr(qstring_get_str(qstr
), ==, "value1");
386 qapi_free_UserDefAlternate(tmp
);
389 tmp
= g_new0(UserDefAlternate
, 1);
390 tmp
->type
= QTYPE_QNULL
;
393 visit_type_UserDefAlternate(data
->ov
, NULL
, &tmp
, &error_abort
);
394 g_assert_cmpint(qobject_type(visitor_get(data
)), ==, QTYPE_QNULL
);
396 qapi_free_UserDefAlternate(tmp
);
399 tmp
= g_new0(UserDefAlternate
, 1);
400 tmp
->type
= QTYPE_QDICT
;
401 tmp
->u
.udfu
.integer
= 1;
402 tmp
->u
.udfu
.string
= g_strdup("str");
403 tmp
->u
.udfu
.enum1
= ENUM_ONE_VALUE1
;
404 tmp
->u
.udfu
.u
.value1
.boolean
= true;
406 visit_type_UserDefAlternate(data
->ov
, NULL
, &tmp
, &error_abort
);
407 qdict
= qobject_to(QDict
, visitor_get(data
));
409 g_assert_cmpint(qdict_size(qdict
), ==, 4);
410 g_assert_cmpint(qdict_get_int(qdict
, "integer"), ==, 1);
411 g_assert_cmpstr(qdict_get_str(qdict
, "string"), ==, "str");
412 g_assert_cmpstr(qdict_get_str(qdict
, "enum1"), ==, "value1");
413 g_assert_cmpint(qdict_get_bool(qdict
, "boolean"), ==, true);
415 qapi_free_UserDefAlternate(tmp
);
418 static void test_visitor_out_null(TestOutputVisitorData
*data
,
425 visit_start_struct(data
->ov
, NULL
, NULL
, 0, &error_abort
);
426 visit_type_null(data
->ov
, "a", &null
, &error_abort
);
427 visit_check_struct(data
->ov
, &error_abort
);
428 visit_end_struct(data
->ov
, NULL
);
429 qdict
= qobject_to(QDict
, visitor_get(data
));
431 g_assert_cmpint(qdict_size(qdict
), ==, 1);
432 nil
= qdict_get(qdict
, "a");
434 g_assert(qobject_type(nil
) == QTYPE_QNULL
);
437 static void test_visitor_out_list_struct(TestOutputVisitorData
*data
,
440 const char *int_member
[] = {
441 "integer", "s8", "s16", "s32", "s64", "u8", "u16", "u32", "u64" };
442 g_autoptr(ArrayStruct
) arrs
= g_new0(ArrayStruct
, 1);
448 for (i
= 31; i
>= 0; i
--) {
449 QAPI_LIST_PREPEND(arrs
->integer
, i
);
452 for (i
= 31; i
>= 0; i
--) {
453 QAPI_LIST_PREPEND(arrs
->s8
, i
);
456 for (i
= 31; i
>= 0; i
--) {
457 QAPI_LIST_PREPEND(arrs
->s16
, i
);
460 for (i
= 31; i
>= 0; i
--) {
461 QAPI_LIST_PREPEND(arrs
->s32
, i
);
464 for (i
= 31; i
>= 0; i
--) {
465 QAPI_LIST_PREPEND(arrs
->s64
, i
);
468 for (i
= 31; i
>= 0; i
--) {
469 QAPI_LIST_PREPEND(arrs
->u8
, i
);
472 for (i
= 31; i
>= 0; i
--) {
473 QAPI_LIST_PREPEND(arrs
->u16
, i
);
476 for (i
= 31; i
>= 0; i
--) {
477 QAPI_LIST_PREPEND(arrs
->u32
, i
);
480 for (i
= 31; i
>= 0; i
--) {
481 QAPI_LIST_PREPEND(arrs
->u64
, i
);
484 for (i
= 31; i
>= 0; i
--) {
485 QAPI_LIST_PREPEND(arrs
->number
, (double)i
/ 3);
488 for (i
= 31; i
>= 0; i
--) {
489 QAPI_LIST_PREPEND(arrs
->boolean
, QEMU_IS_ALIGNED(i
, 3));
492 for (i
= 31; i
>= 0; i
--) {
493 QAPI_LIST_PREPEND(arrs
->string
, g_strdup_printf("%d", i
));
496 visit_type_ArrayStruct(data
->ov
, NULL
, &arrs
, &error_abort
);
498 qdict
= qobject_to(QDict
, visitor_get(data
));
501 for (i
= 0; i
< G_N_ELEMENTS(int_member
); i
++) {
502 qlist
= qdict_get_qlist(qdict
, int_member
[i
]);
505 QLIST_FOREACH_ENTRY(qlist
, e
) {
506 QNum
*qvalue
= qobject_to(QNum
, qlist_entry_obj(e
));
508 g_assert_cmpint(qnum_get_int(qvalue
), ==, j
);
513 qlist
= qdict_get_qlist(qdict
, "number");
516 QLIST_FOREACH_ENTRY(qlist
, e
) {
517 QNum
*qvalue
= qobject_to(QNum
, qlist_entry_obj(e
));
518 char expected
[32], actual
[32];
521 sprintf(expected
, "%.6f", (double)i
/ 3);
522 sprintf(actual
, "%.6f", qnum_get_double(qvalue
));
523 g_assert_cmpstr(actual
, ==, expected
);
527 qlist
= qdict_get_qlist(qdict
, "boolean");
530 QLIST_FOREACH_ENTRY(qlist
, e
) {
531 QBool
*qvalue
= qobject_to(QBool
, qlist_entry_obj(e
));
533 g_assert_cmpint(qbool_get_bool(qvalue
), ==, i
% 3 == 0);
537 qlist
= qdict_get_qlist(qdict
, "string");
540 QLIST_FOREACH_ENTRY(qlist
, e
) {
541 QString
*qvalue
= qobject_to(QString
, qlist_entry_obj(e
));
545 sprintf(expected
, "%d", i
);
546 g_assert_cmpstr(qstring_get_str(qvalue
), ==, expected
);
551 static void output_visitor_test_add(const char *testpath
,
552 TestOutputVisitorData
*data
,
553 void (*test_func
)(TestOutputVisitorData
*data
, const void *user_data
))
555 g_test_add(testpath
, TestOutputVisitorData
, data
, visitor_output_setup
,
556 test_func
, visitor_output_teardown
);
559 int main(int argc
, char **argv
)
561 TestOutputVisitorData out_visitor_data
;
563 g_test_init(&argc
, &argv
, NULL
);
565 output_visitor_test_add("/visitor/output/int",
566 &out_visitor_data
, test_visitor_out_int
);
567 output_visitor_test_add("/visitor/output/bool",
568 &out_visitor_data
, test_visitor_out_bool
);
569 output_visitor_test_add("/visitor/output/number",
570 &out_visitor_data
, test_visitor_out_number
);
571 output_visitor_test_add("/visitor/output/string",
572 &out_visitor_data
, test_visitor_out_string
);
573 output_visitor_test_add("/visitor/output/no-string",
574 &out_visitor_data
, test_visitor_out_no_string
);
575 output_visitor_test_add("/visitor/output/enum",
576 &out_visitor_data
, test_visitor_out_enum
);
577 output_visitor_test_add("/visitor/output/struct",
578 &out_visitor_data
, test_visitor_out_struct
);
579 output_visitor_test_add("/visitor/output/struct-nested",
580 &out_visitor_data
, test_visitor_out_struct_nested
);
581 output_visitor_test_add("/visitor/output/list",
582 &out_visitor_data
, test_visitor_out_list
);
583 output_visitor_test_add("/visitor/output/any",
584 &out_visitor_data
, test_visitor_out_any
);
585 output_visitor_test_add("/visitor/output/list-qapi-free",
586 &out_visitor_data
, test_visitor_out_list_qapi_free
);
587 output_visitor_test_add("/visitor/output/union-flat",
588 &out_visitor_data
, test_visitor_out_union_flat
);
589 output_visitor_test_add("/visitor/output/alternate",
590 &out_visitor_data
, test_visitor_out_alternate
);
591 output_visitor_test_add("/visitor/output/null",
592 &out_visitor_data
, test_visitor_out_null
);
593 output_visitor_test_add("/visitor/output/list_struct",
594 &out_visitor_data
, test_visitor_out_list_struct
);