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/error.h"
18 #include "qapi/string-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 StringOutputVisitor
*sov
;
27 } TestOutputVisitorData
;
29 static void visitor_output_setup(TestOutputVisitorData
*data
,
33 data
->sov
= string_output_visitor_new(data
->human
);
34 g_assert(data
->sov
!= NULL
);
36 data
->ov
= string_output_get_visitor(data
->sov
);
37 g_assert(data
->ov
!= NULL
);
40 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
44 data
->sov
= string_output_visitor_new(data
->human
);
45 g_assert(data
->sov
!= NULL
);
47 data
->ov
= string_output_get_visitor(data
->sov
);
48 g_assert(data
->ov
!= NULL
);
51 static void visitor_output_teardown(TestOutputVisitorData
*data
,
54 string_output_visitor_cleanup(data
->sov
);
59 static void test_visitor_out_int(TestOutputVisitorData
*data
,
66 visit_type_int(data
->ov
, NULL
, &value
, &err
);
69 str
= string_output_get_string(data
->sov
);
70 g_assert(str
!= NULL
);
72 g_assert_cmpstr(str
, ==, "42 (0x2a)");
74 g_assert_cmpstr(str
, ==, "42");
79 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
82 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
83 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
84 intList
*list
= NULL
, **tmp
= &list
;
89 for (i
= 0; i
< sizeof(value
) / sizeof(value
[0]); i
++) {
90 *tmp
= g_malloc0(sizeof(**tmp
));
91 (*tmp
)->value
= value
[i
];
95 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
96 g_assert(err
== NULL
);
98 str
= string_output_get_string(data
->sov
);
99 g_assert(str
!= NULL
);
101 g_assert_cmpstr(str
, ==,
102 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
103 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
104 "0x7ffffffffffffffe-0x7fffffffffffffff)");
106 g_assert_cmpstr(str
, ==,
107 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
118 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
125 visit_type_bool(data
->ov
, NULL
, &value
, &err
);
128 str
= string_output_get_string(data
->sov
);
129 g_assert(str
!= NULL
);
130 g_assert_cmpstr(str
, ==, "true");
134 static void test_visitor_out_number(TestOutputVisitorData
*data
,
141 visit_type_number(data
->ov
, NULL
, &value
, &err
);
144 str
= string_output_get_string(data
->sov
);
145 g_assert(str
!= NULL
);
146 g_assert_cmpstr(str
, ==, "3.140000");
150 static void test_visitor_out_string(TestOutputVisitorData
*data
,
153 char *string
= (char *) "Q E M U";
154 const char *string_human
= "\"Q E M U\"";
158 visit_type_str(data
->ov
, NULL
, &string
, &err
);
161 str
= string_output_get_string(data
->sov
);
162 g_assert(str
!= NULL
);
164 g_assert_cmpstr(str
, ==, string_human
);
166 g_assert_cmpstr(str
, ==, string
);
171 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
178 /* A null string should return "" */
179 visit_type_str(data
->ov
, NULL
, &string
, &err
);
182 str
= string_output_get_string(data
->sov
);
183 g_assert(str
!= NULL
);
185 g_assert_cmpstr(str
, ==, "<null>");
187 g_assert_cmpstr(str
, ==, "");
192 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
199 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
202 visit_type_EnumOne(data
->ov
, "unused", &i
, &err
);
205 str_human
= g_strdup_printf("\"%s\"", EnumOne_lookup
[i
]);
207 str
= string_output_get_string(data
->sov
);
208 g_assert(str
!= NULL
);
210 g_assert_cmpstr(str
, ==, str_human
);
212 g_assert_cmpstr(str
, ==, EnumOne_lookup
[i
]);
219 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
222 EnumOne i
, bad_values
[] = { ENUM_ONE__MAX
, -1 };
225 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
227 visit_type_EnumOne(data
->ov
, "unused", &bad_values
[i
], &err
);
234 output_visitor_test_add(const char *testpath
,
235 TestOutputVisitorData
*data
,
236 void (*test_func
)(TestOutputVisitorData
*data
,
237 const void *user_data
),
240 g_test_add(testpath
, TestOutputVisitorData
, data
,
241 human
? visitor_output_setup_human
: visitor_output_setup
,
242 test_func
, visitor_output_teardown
);
245 int main(int argc
, char **argv
)
247 TestOutputVisitorData out_visitor_data
;
249 g_test_init(&argc
, &argv
, NULL
);
251 output_visitor_test_add("/string-visitor/output/int",
252 &out_visitor_data
, test_visitor_out_int
, false);
253 output_visitor_test_add("/string-visitor/output/int-human",
254 &out_visitor_data
, test_visitor_out_int
, true);
255 output_visitor_test_add("/string-visitor/output/bool",
256 &out_visitor_data
, test_visitor_out_bool
, false);
257 output_visitor_test_add("/string-visitor/output/bool-human",
258 &out_visitor_data
, test_visitor_out_bool
, true);
259 output_visitor_test_add("/string-visitor/output/number",
260 &out_visitor_data
, test_visitor_out_number
, false);
261 output_visitor_test_add("/string-visitor/output/number-human",
262 &out_visitor_data
, test_visitor_out_number
, true);
263 output_visitor_test_add("/string-visitor/output/string",
264 &out_visitor_data
, test_visitor_out_string
, false);
265 output_visitor_test_add("/string-visitor/output/string-human",
266 &out_visitor_data
, test_visitor_out_string
, true);
267 output_visitor_test_add("/string-visitor/output/no-string",
268 &out_visitor_data
, test_visitor_out_no_string
,
270 output_visitor_test_add("/string-visitor/output/no-string-human",
271 &out_visitor_data
, test_visitor_out_no_string
,
273 output_visitor_test_add("/string-visitor/output/enum",
274 &out_visitor_data
, test_visitor_out_enum
, false);
275 output_visitor_test_add("/string-visitor/output/enum-human",
276 &out_visitor_data
, test_visitor_out_enum
, true);
277 output_visitor_test_add("/string-visitor/output/enum-errors",
278 &out_visitor_data
, test_visitor_out_enum_errors
,
280 output_visitor_test_add("/string-visitor/output/enum-errors-human",
281 &out_visitor_data
, test_visitor_out_enum_errors
,
283 output_visitor_test_add("/string-visitor/output/intList",
284 &out_visitor_data
, test_visitor_out_intList
, false);
285 output_visitor_test_add("/string-visitor/output/intList-human",
286 &out_visitor_data
, test_visitor_out_intList
, true);