2 * String Output Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
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"
16 #include "qemu-common.h"
17 #include "qapi/string-output-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestOutputVisitorData
{
23 StringOutputVisitor
*sov
;
26 } TestOutputVisitorData
;
28 static void visitor_output_setup(TestOutputVisitorData
*data
,
32 data
->sov
= string_output_visitor_new(data
->human
);
33 g_assert(data
->sov
!= NULL
);
35 data
->ov
= string_output_get_visitor(data
->sov
);
36 g_assert(data
->ov
!= NULL
);
39 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
43 data
->sov
= string_output_visitor_new(data
->human
);
44 g_assert(data
->sov
!= NULL
);
46 data
->ov
= string_output_get_visitor(data
->sov
);
47 g_assert(data
->ov
!= NULL
);
50 static void visitor_output_teardown(TestOutputVisitorData
*data
,
53 string_output_visitor_cleanup(data
->sov
);
58 static void test_visitor_out_int(TestOutputVisitorData
*data
,
65 visit_type_int(data
->ov
, NULL
, &value
, &err
);
68 str
= string_output_get_string(data
->sov
);
69 g_assert(str
!= NULL
);
71 g_assert_cmpstr(str
, ==, "42 (0x2a)");
73 g_assert_cmpstr(str
, ==, "42");
78 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
81 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
82 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
83 intList
*list
= NULL
, **tmp
= &list
;
88 for (i
= 0; i
< sizeof(value
) / sizeof(value
[0]); i
++) {
89 *tmp
= g_malloc0(sizeof(**tmp
));
90 (*tmp
)->value
= value
[i
];
94 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
95 g_assert(err
== NULL
);
97 str
= string_output_get_string(data
->sov
);
98 g_assert(str
!= NULL
);
100 g_assert_cmpstr(str
, ==,
101 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
102 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
103 "0x7ffffffffffffffe-0x7fffffffffffffff)");
105 g_assert_cmpstr(str
, ==,
106 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
117 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
124 visit_type_bool(data
->ov
, NULL
, &value
, &err
);
127 str
= string_output_get_string(data
->sov
);
128 g_assert(str
!= NULL
);
129 g_assert_cmpstr(str
, ==, "true");
133 static void test_visitor_out_number(TestOutputVisitorData
*data
,
140 visit_type_number(data
->ov
, NULL
, &value
, &err
);
143 str
= string_output_get_string(data
->sov
);
144 g_assert(str
!= NULL
);
145 g_assert_cmpstr(str
, ==, "3.140000");
149 static void test_visitor_out_string(TestOutputVisitorData
*data
,
152 char *string
= (char *) "Q E M U";
153 const char *string_human
= "\"Q E M U\"";
157 visit_type_str(data
->ov
, NULL
, &string
, &err
);
160 str
= string_output_get_string(data
->sov
);
161 g_assert(str
!= NULL
);
163 g_assert_cmpstr(str
, ==, string_human
);
165 g_assert_cmpstr(str
, ==, string
);
170 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
177 /* A null string should return "" */
178 visit_type_str(data
->ov
, NULL
, &string
, &err
);
181 str
= string_output_get_string(data
->sov
);
182 g_assert(str
!= NULL
);
184 g_assert_cmpstr(str
, ==, "<null>");
186 g_assert_cmpstr(str
, ==, "");
191 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
198 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
201 visit_type_EnumOne(data
->ov
, "unused", &i
, &err
);
204 str_human
= g_strdup_printf("\"%s\"", EnumOne_lookup
[i
]);
206 str
= string_output_get_string(data
->sov
);
207 g_assert(str
!= NULL
);
209 g_assert_cmpstr(str
, ==, str_human
);
211 g_assert_cmpstr(str
, ==, EnumOne_lookup
[i
]);
218 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
221 EnumOne i
, bad_values
[] = { ENUM_ONE__MAX
, -1 };
224 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
226 visit_type_EnumOne(data
->ov
, "unused", &bad_values
[i
], &err
);
233 output_visitor_test_add(const char *testpath
,
234 TestOutputVisitorData
*data
,
235 void (*test_func
)(TestOutputVisitorData
*data
,
236 const void *user_data
),
239 g_test_add(testpath
, TestOutputVisitorData
, data
,
240 human
? visitor_output_setup_human
: visitor_output_setup
,
241 test_func
, visitor_output_teardown
);
244 int main(int argc
, char **argv
)
246 TestOutputVisitorData out_visitor_data
;
248 g_test_init(&argc
, &argv
, NULL
);
250 output_visitor_test_add("/string-visitor/output/int",
251 &out_visitor_data
, test_visitor_out_int
, false);
252 output_visitor_test_add("/string-visitor/output/int-human",
253 &out_visitor_data
, test_visitor_out_int
, true);
254 output_visitor_test_add("/string-visitor/output/bool",
255 &out_visitor_data
, test_visitor_out_bool
, false);
256 output_visitor_test_add("/string-visitor/output/bool-human",
257 &out_visitor_data
, test_visitor_out_bool
, true);
258 output_visitor_test_add("/string-visitor/output/number",
259 &out_visitor_data
, test_visitor_out_number
, false);
260 output_visitor_test_add("/string-visitor/output/number-human",
261 &out_visitor_data
, test_visitor_out_number
, true);
262 output_visitor_test_add("/string-visitor/output/string",
263 &out_visitor_data
, test_visitor_out_string
, false);
264 output_visitor_test_add("/string-visitor/output/string-human",
265 &out_visitor_data
, test_visitor_out_string
, true);
266 output_visitor_test_add("/string-visitor/output/no-string",
267 &out_visitor_data
, test_visitor_out_no_string
,
269 output_visitor_test_add("/string-visitor/output/no-string-human",
270 &out_visitor_data
, test_visitor_out_no_string
,
272 output_visitor_test_add("/string-visitor/output/enum",
273 &out_visitor_data
, test_visitor_out_enum
, false);
274 output_visitor_test_add("/string-visitor/output/enum-human",
275 &out_visitor_data
, test_visitor_out_enum
, true);
276 output_visitor_test_add("/string-visitor/output/enum-errors",
277 &out_visitor_data
, test_visitor_out_enum_errors
,
279 output_visitor_test_add("/string-visitor/output/enum-errors-human",
280 &out_visitor_data
, test_visitor_out_enum_errors
,
282 output_visitor_test_add("/string-visitor/output/intList",
283 &out_visitor_data
, test_visitor_out_intList
, false);
284 output_visitor_test_add("/string-visitor/output/intList-human",
285 &out_visitor_data
, test_visitor_out_intList
, true);