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
,
76 visit_type_int(data
->ov
, NULL
, &value
, &error_abort
);
78 str
= visitor_get(data
);
80 g_assert_cmpstr(str
, ==, "42 (0x2a)");
82 g_assert_cmpstr(str
, ==, "42");
86 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
89 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
90 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
91 intList
*list
= NULL
, **tail
= &list
;
96 for (i
= 0; i
< ARRAY_SIZE(value
); i
++) {
97 QAPI_LIST_APPEND(tail
, value
[i
]);
100 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
101 g_assert(err
== NULL
);
103 str
= visitor_get(data
);
105 g_assert_cmpstr(str
, ==,
106 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
107 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
108 "0x7ffffffffffffffe-0x7fffffffffffffff)");
110 g_assert_cmpstr(str
, ==,
111 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
113 qapi_free_intList(list
);
116 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
122 visit_type_bool(data
->ov
, NULL
, &value
, &error_abort
);
124 str
= visitor_get(data
);
125 g_assert_cmpstr(str
, ==, "true");
128 static void test_visitor_out_number(TestOutputVisitorData
*data
,
131 double value
= 3.1415926535897932;
134 visit_type_number(data
->ov
, NULL
, &value
, &error_abort
);
136 str
= visitor_get(data
);
137 g_assert_cmpstr(str
, ==, "3.1415926535897931");
140 static void test_visitor_out_string(TestOutputVisitorData
*data
,
143 char *string
= (char *) "Q E M U";
144 const char *string_human
= "\"Q E M U\"";
147 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
149 str
= visitor_get(data
);
151 g_assert_cmpstr(str
, ==, string_human
);
153 g_assert_cmpstr(str
, ==, string
);
157 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
163 /* A null string should return "" */
164 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
166 str
= visitor_get(data
);
168 g_assert_cmpstr(str
, ==, "<null>");
170 g_assert_cmpstr(str
, ==, "");
174 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
180 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
181 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
183 str
= visitor_get(data
);
185 char *str_human
= g_strdup_printf("\"%s\"", EnumOne_str(i
));
187 g_assert_cmpstr(str
, ==, str_human
);
190 g_assert_cmpstr(str
, ==, EnumOne_str(i
));
197 output_visitor_test_add(const char *testpath
,
198 TestOutputVisitorData
*data
,
199 void (*test_func
)(TestOutputVisitorData
*data
,
200 const void *user_data
),
203 g_test_add(testpath
, TestOutputVisitorData
, data
,
204 human
? visitor_output_setup_human
: visitor_output_setup
,
205 test_func
, visitor_output_teardown
);
208 int main(int argc
, char **argv
)
210 TestOutputVisitorData out_visitor_data
;
212 g_test_init(&argc
, &argv
, NULL
);
214 output_visitor_test_add("/string-visitor/output/int",
215 &out_visitor_data
, test_visitor_out_int
, false);
216 output_visitor_test_add("/string-visitor/output/int-human",
217 &out_visitor_data
, test_visitor_out_int
, true);
218 output_visitor_test_add("/string-visitor/output/bool",
219 &out_visitor_data
, test_visitor_out_bool
, false);
220 output_visitor_test_add("/string-visitor/output/bool-human",
221 &out_visitor_data
, test_visitor_out_bool
, true);
222 output_visitor_test_add("/string-visitor/output/number",
223 &out_visitor_data
, test_visitor_out_number
, false);
224 output_visitor_test_add("/string-visitor/output/number-human",
225 &out_visitor_data
, test_visitor_out_number
, true);
226 output_visitor_test_add("/string-visitor/output/string",
227 &out_visitor_data
, test_visitor_out_string
, false);
228 output_visitor_test_add("/string-visitor/output/string-human",
229 &out_visitor_data
, test_visitor_out_string
, true);
230 output_visitor_test_add("/string-visitor/output/no-string",
231 &out_visitor_data
, test_visitor_out_no_string
,
233 output_visitor_test_add("/string-visitor/output/no-string-human",
234 &out_visitor_data
, test_visitor_out_no_string
,
236 output_visitor_test_add("/string-visitor/output/enum",
237 &out_visitor_data
, test_visitor_out_enum
, false);
238 output_visitor_test_add("/string-visitor/output/enum-human",
239 &out_visitor_data
, test_visitor_out_enum
, true);
240 output_visitor_test_add("/string-visitor/output/intList",
241 &out_visitor_data
, test_visitor_out_intList
, false);
242 output_visitor_test_add("/string-visitor/output/intList-human",
243 &out_visitor_data
, test_visitor_out_intList
, true);