2 * String Output Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qobject-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-visit.h"
20 typedef struct TestOutputVisitorData
{
24 } TestOutputVisitorData
;
26 static void visitor_output_setup_internal(TestOutputVisitorData
*data
,
30 data
->ov
= string_output_visitor_new(human
, &data
->str
);
34 static void visitor_output_setup(TestOutputVisitorData
*data
,
37 return visitor_output_setup_internal(data
, false);
40 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
43 return visitor_output_setup_internal(data
, true);
46 static void visitor_output_teardown(TestOutputVisitorData
*data
,
55 static char *visitor_get(TestOutputVisitorData
*data
)
57 visit_complete(data
->ov
, &data
->str
);
62 static void visitor_reset(TestOutputVisitorData
*data
)
64 bool human
= data
->human
;
66 visitor_output_teardown(data
, NULL
);
67 visitor_output_setup_internal(data
, human
);
70 static void test_visitor_out_int(TestOutputVisitorData
*data
,
77 visit_type_int(data
->ov
, NULL
, &value
, &err
);
80 str
= visitor_get(data
);
82 g_assert_cmpstr(str
, ==, "42 (0x2a)");
84 g_assert_cmpstr(str
, ==, "42");
88 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
91 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
92 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
93 intList
*list
= NULL
, **tmp
= &list
;
98 for (i
= 0; i
< ARRAY_SIZE(value
); i
++) {
99 *tmp
= g_malloc0(sizeof(**tmp
));
100 (*tmp
)->value
= value
[i
];
104 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
105 g_assert(err
== NULL
);
107 str
= visitor_get(data
);
109 g_assert_cmpstr(str
, ==,
110 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
111 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
112 "0x7ffffffffffffffe-0x7fffffffffffffff)");
114 g_assert_cmpstr(str
, ==,
115 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
117 qapi_free_intList(list
);
120 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
127 visit_type_bool(data
->ov
, NULL
, &value
, &err
);
130 str
= visitor_get(data
);
131 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
= visitor_get(data
);
145 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
, NULL
, &string
, &err
);
159 str
= visitor_get(data
);
161 g_assert_cmpstr(str
, ==, string_human
);
163 g_assert_cmpstr(str
, ==, string
);
167 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
173 /* A null string should return "" */
174 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
176 str
= visitor_get(data
);
178 g_assert_cmpstr(str
, ==, "<null>");
180 g_assert_cmpstr(str
, ==, "");
184 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
190 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
191 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
193 str
= visitor_get(data
);
195 char *str_human
= g_strdup_printf("\"%s\"", EnumOne_str(i
));
197 g_assert_cmpstr(str
, ==, str_human
);
200 g_assert_cmpstr(str
, ==, EnumOne_str(i
));
207 output_visitor_test_add(const char *testpath
,
208 TestOutputVisitorData
*data
,
209 void (*test_func
)(TestOutputVisitorData
*data
,
210 const void *user_data
),
213 g_test_add(testpath
, TestOutputVisitorData
, data
,
214 human
? visitor_output_setup_human
: visitor_output_setup
,
215 test_func
, visitor_output_teardown
);
218 int main(int argc
, char **argv
)
220 TestOutputVisitorData out_visitor_data
;
222 g_test_init(&argc
, &argv
, NULL
);
224 output_visitor_test_add("/string-visitor/output/int",
225 &out_visitor_data
, test_visitor_out_int
, false);
226 output_visitor_test_add("/string-visitor/output/int-human",
227 &out_visitor_data
, test_visitor_out_int
, true);
228 output_visitor_test_add("/string-visitor/output/bool",
229 &out_visitor_data
, test_visitor_out_bool
, false);
230 output_visitor_test_add("/string-visitor/output/bool-human",
231 &out_visitor_data
, test_visitor_out_bool
, true);
232 output_visitor_test_add("/string-visitor/output/number",
233 &out_visitor_data
, test_visitor_out_number
, false);
234 output_visitor_test_add("/string-visitor/output/number-human",
235 &out_visitor_data
, test_visitor_out_number
, true);
236 output_visitor_test_add("/string-visitor/output/string",
237 &out_visitor_data
, test_visitor_out_string
, false);
238 output_visitor_test_add("/string-visitor/output/string-human",
239 &out_visitor_data
, test_visitor_out_string
, true);
240 output_visitor_test_add("/string-visitor/output/no-string",
241 &out_visitor_data
, test_visitor_out_no_string
,
243 output_visitor_test_add("/string-visitor/output/no-string-human",
244 &out_visitor_data
, test_visitor_out_no_string
,
246 output_visitor_test_add("/string-visitor/output/enum",
247 &out_visitor_data
, test_visitor_out_enum
, false);
248 output_visitor_test_add("/string-visitor/output/enum-human",
249 &out_visitor_data
, test_visitor_out_enum
, true);
250 output_visitor_test_add("/string-visitor/output/intList",
251 &out_visitor_data
, test_visitor_out_intList
, false);
252 output_visitor_test_add("/string-visitor/output/intList-human",
253 &out_visitor_data
, test_visitor_out_intList
, true);