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"
15 #include "qemu-common.h"
16 #include "qapi/error.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
{
26 } TestOutputVisitorData
;
28 static void visitor_output_setup_internal(TestOutputVisitorData
*data
,
32 data
->ov
= string_output_visitor_new(human
, &data
->str
);
36 static void visitor_output_setup(TestOutputVisitorData
*data
,
39 return visitor_output_setup_internal(data
, false);
42 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
45 return visitor_output_setup_internal(data
, true);
48 static void visitor_output_teardown(TestOutputVisitorData
*data
,
57 static char *visitor_get(TestOutputVisitorData
*data
)
59 visit_complete(data
->ov
, &data
->str
);
64 static void visitor_reset(TestOutputVisitorData
*data
)
66 bool human
= data
->human
;
68 visitor_output_teardown(data
, NULL
);
69 visitor_output_setup_internal(data
, human
);
72 static void test_visitor_out_int(TestOutputVisitorData
*data
,
79 visit_type_int(data
->ov
, NULL
, &value
, &err
);
82 str
= visitor_get(data
);
84 g_assert_cmpstr(str
, ==, "42 (0x2a)");
86 g_assert_cmpstr(str
, ==, "42");
90 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
93 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
94 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
95 intList
*list
= NULL
, **tmp
= &list
;
100 for (i
= 0; i
< sizeof(value
) / sizeof(value
[0]); i
++) {
101 *tmp
= g_malloc0(sizeof(**tmp
));
102 (*tmp
)->value
= value
[i
];
106 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
107 g_assert(err
== NULL
);
109 str
= visitor_get(data
);
111 g_assert_cmpstr(str
, ==,
112 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
113 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
114 "0x7ffffffffffffffe-0x7fffffffffffffff)");
116 g_assert_cmpstr(str
, ==,
117 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
119 qapi_free_intList(list
);
122 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
129 visit_type_bool(data
->ov
, NULL
, &value
, &err
);
132 str
= visitor_get(data
);
133 g_assert_cmpstr(str
, ==, "true");
136 static void test_visitor_out_number(TestOutputVisitorData
*data
,
143 visit_type_number(data
->ov
, NULL
, &value
, &err
);
146 str
= visitor_get(data
);
147 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
= visitor_get(data
);
163 g_assert_cmpstr(str
, ==, string_human
);
165 g_assert_cmpstr(str
, ==, string
);
169 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
175 /* A null string should return "" */
176 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
178 str
= visitor_get(data
);
180 g_assert_cmpstr(str
, ==, "<null>");
182 g_assert_cmpstr(str
, ==, "");
186 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
192 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
193 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
195 str
= visitor_get(data
);
197 char *str_human
= g_strdup_printf("\"%s\"", EnumOne_lookup
[i
]);
199 g_assert_cmpstr(str
, ==, str_human
);
202 g_assert_cmpstr(str
, ==, EnumOne_lookup
[i
]);
208 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
211 EnumOne i
, bad_values
[] = { ENUM_ONE__MAX
, -1 };
214 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
216 visit_type_EnumOne(data
->ov
, "unused", &bad_values
[i
], &err
);
217 error_free_or_abort(&err
);
222 output_visitor_test_add(const char *testpath
,
223 TestOutputVisitorData
*data
,
224 void (*test_func
)(TestOutputVisitorData
*data
,
225 const void *user_data
),
228 g_test_add(testpath
, TestOutputVisitorData
, data
,
229 human
? visitor_output_setup_human
: visitor_output_setup
,
230 test_func
, visitor_output_teardown
);
233 int main(int argc
, char **argv
)
235 TestOutputVisitorData out_visitor_data
;
237 g_test_init(&argc
, &argv
, NULL
);
239 output_visitor_test_add("/string-visitor/output/int",
240 &out_visitor_data
, test_visitor_out_int
, false);
241 output_visitor_test_add("/string-visitor/output/int-human",
242 &out_visitor_data
, test_visitor_out_int
, true);
243 output_visitor_test_add("/string-visitor/output/bool",
244 &out_visitor_data
, test_visitor_out_bool
, false);
245 output_visitor_test_add("/string-visitor/output/bool-human",
246 &out_visitor_data
, test_visitor_out_bool
, true);
247 output_visitor_test_add("/string-visitor/output/number",
248 &out_visitor_data
, test_visitor_out_number
, false);
249 output_visitor_test_add("/string-visitor/output/number-human",
250 &out_visitor_data
, test_visitor_out_number
, true);
251 output_visitor_test_add("/string-visitor/output/string",
252 &out_visitor_data
, test_visitor_out_string
, false);
253 output_visitor_test_add("/string-visitor/output/string-human",
254 &out_visitor_data
, test_visitor_out_string
, true);
255 output_visitor_test_add("/string-visitor/output/no-string",
256 &out_visitor_data
, test_visitor_out_no_string
,
258 output_visitor_test_add("/string-visitor/output/no-string-human",
259 &out_visitor_data
, test_visitor_out_no_string
,
261 output_visitor_test_add("/string-visitor/output/enum",
262 &out_visitor_data
, test_visitor_out_enum
, false);
263 output_visitor_test_add("/string-visitor/output/enum-human",
264 &out_visitor_data
, test_visitor_out_enum
, true);
265 output_visitor_test_add("/string-visitor/output/enum-errors",
266 &out_visitor_data
, test_visitor_out_enum_errors
,
268 output_visitor_test_add("/string-visitor/output/enum-errors-human",
269 &out_visitor_data
, test_visitor_out_enum_errors
,
271 output_visitor_test_add("/string-visitor/output/intList",
272 &out_visitor_data
, test_visitor_out_intList
, false);
273 output_visitor_test_add("/string-visitor/output/intList-human",
274 &out_visitor_data
, test_visitor_out_intList
, true);