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.
15 #include "qemu-common.h"
16 #include "qapi/string-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/qmp/types.h"
21 typedef struct TestOutputVisitorData
{
22 StringOutputVisitor
*sov
;
25 } TestOutputVisitorData
;
27 static void visitor_output_setup(TestOutputVisitorData
*data
,
31 data
->sov
= string_output_visitor_new(data
->human
);
32 g_assert(data
->sov
!= NULL
);
34 data
->ov
= string_output_get_visitor(data
->sov
);
35 g_assert(data
->ov
!= NULL
);
38 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
42 data
->sov
= string_output_visitor_new(data
->human
);
43 g_assert(data
->sov
!= NULL
);
45 data
->ov
= string_output_get_visitor(data
->sov
);
46 g_assert(data
->ov
!= NULL
);
49 static void visitor_output_teardown(TestOutputVisitorData
*data
,
52 string_output_visitor_cleanup(data
->sov
);
57 static void test_visitor_out_int(TestOutputVisitorData
*data
,
64 visit_type_int(data
->ov
, &value
, NULL
, &err
);
67 str
= string_output_get_string(data
->sov
);
68 g_assert(str
!= NULL
);
70 g_assert_cmpstr(str
, ==, "42 (0x2a)");
72 g_assert_cmpstr(str
, ==, "42");
77 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
80 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
81 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
82 intList
*list
= NULL
, **tmp
= &list
;
87 for (i
= 0; i
< sizeof(value
) / sizeof(value
[0]); i
++) {
88 *tmp
= g_malloc0(sizeof(**tmp
));
89 (*tmp
)->value
= value
[i
];
93 visit_type_intList(data
->ov
, &list
, NULL
, &err
);
94 g_assert(err
== NULL
);
96 str
= string_output_get_string(data
->sov
);
97 g_assert(str
!= NULL
);
99 g_assert_cmpstr(str
, ==,
100 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
101 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
102 "0x7ffffffffffffffe-0x7fffffffffffffff)");
104 g_assert_cmpstr(str
, ==,
105 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
116 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
123 visit_type_bool(data
->ov
, &value
, NULL
, &err
);
126 str
= string_output_get_string(data
->sov
);
127 g_assert(str
!= NULL
);
128 g_assert_cmpstr(str
, ==, "true");
132 static void test_visitor_out_number(TestOutputVisitorData
*data
,
139 visit_type_number(data
->ov
, &value
, NULL
, &err
);
142 str
= string_output_get_string(data
->sov
);
143 g_assert(str
!= NULL
);
144 g_assert_cmpstr(str
, ==, "3.140000");
148 static void test_visitor_out_string(TestOutputVisitorData
*data
,
151 char *string
= (char *) "Q E M U";
152 const char *string_human
= "\"Q E M U\"";
156 visit_type_str(data
->ov
, &string
, NULL
, &err
);
159 str
= string_output_get_string(data
->sov
);
160 g_assert(str
!= NULL
);
162 g_assert_cmpstr(str
, ==, string_human
);
164 g_assert_cmpstr(str
, ==, string
);
169 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
176 /* A null string should return "" */
177 visit_type_str(data
->ov
, &string
, NULL
, &err
);
180 str
= string_output_get_string(data
->sov
);
181 g_assert(str
!= NULL
);
183 g_assert_cmpstr(str
, ==, "<null>");
185 g_assert_cmpstr(str
, ==, "");
190 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
197 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
200 visit_type_EnumOne(data
->ov
, &i
, "unused", &err
);
203 str_human
= g_strdup_printf("\"%s\"", EnumOne_lookup
[i
]);
205 str
= string_output_get_string(data
->sov
);
206 g_assert(str
!= NULL
);
208 g_assert_cmpstr(str
, ==, str_human
);
210 g_assert_cmpstr(str
, ==, EnumOne_lookup
[i
]);
217 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
220 EnumOne i
, bad_values
[] = { ENUM_ONE__MAX
, -1 };
223 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
225 visit_type_EnumOne(data
->ov
, &bad_values
[i
], "unused", &err
);
232 output_visitor_test_add(const char *testpath
,
233 TestOutputVisitorData
*data
,
234 void (*test_func
)(TestOutputVisitorData
*data
,
235 const void *user_data
),
238 g_test_add(testpath
, TestOutputVisitorData
, data
,
239 human
? visitor_output_setup_human
: visitor_output_setup
,
240 test_func
, visitor_output_teardown
);
243 int main(int argc
, char **argv
)
245 TestOutputVisitorData out_visitor_data
;
247 g_test_init(&argc
, &argv
, NULL
);
249 output_visitor_test_add("/string-visitor/output/int",
250 &out_visitor_data
, test_visitor_out_int
, false);
251 output_visitor_test_add("/string-visitor/output/int-human",
252 &out_visitor_data
, test_visitor_out_int
, true);
253 output_visitor_test_add("/string-visitor/output/bool",
254 &out_visitor_data
, test_visitor_out_bool
, false);
255 output_visitor_test_add("/string-visitor/output/bool-human",
256 &out_visitor_data
, test_visitor_out_bool
, true);
257 output_visitor_test_add("/string-visitor/output/number",
258 &out_visitor_data
, test_visitor_out_number
, false);
259 output_visitor_test_add("/string-visitor/output/number-human",
260 &out_visitor_data
, test_visitor_out_number
, true);
261 output_visitor_test_add("/string-visitor/output/string",
262 &out_visitor_data
, test_visitor_out_string
, false);
263 output_visitor_test_add("/string-visitor/output/string-human",
264 &out_visitor_data
, test_visitor_out_string
, true);
265 output_visitor_test_add("/string-visitor/output/no-string",
266 &out_visitor_data
, test_visitor_out_no_string
,
268 output_visitor_test_add("/string-visitor/output/no-string-human",
269 &out_visitor_data
, test_visitor_out_no_string
,
271 output_visitor_test_add("/string-visitor/output/enum",
272 &out_visitor_data
, test_visitor_out_enum
, false);
273 output_visitor_test_add("/string-visitor/output/enum-human",
274 &out_visitor_data
, test_visitor_out_enum
, true);
275 output_visitor_test_add("/string-visitor/output/enum-errors",
276 &out_visitor_data
, test_visitor_out_enum_errors
,
278 output_visitor_test_add("/string-visitor/output/enum-errors-human",
279 &out_visitor_data
, test_visitor_out_enum_errors
,
281 output_visitor_test_add("/string-visitor/output/intList",
282 &out_visitor_data
, test_visitor_out_intList
, false);
283 output_visitor_test_add("/string-visitor/output/intList-human",
284 &out_visitor_data
, test_visitor_out_intList
, true);