1
2
3
4
5
6
7
8
9
10
11
12
13#include <glib.h>
14
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"
20
21typedef struct TestOutputVisitorData {
22 StringOutputVisitor *sov;
23 Visitor *ov;
24 bool human;
25} TestOutputVisitorData;
26
27static void visitor_output_setup(TestOutputVisitorData *data,
28 const void *unused)
29{
30 data->human = false;
31 data->sov = string_output_visitor_new(data->human);
32 g_assert(data->sov != NULL);
33
34 data->ov = string_output_get_visitor(data->sov);
35 g_assert(data->ov != NULL);
36}
37
38static void visitor_output_setup_human(TestOutputVisitorData *data,
39 const void *unused)
40{
41 data->human = true;
42 data->sov = string_output_visitor_new(data->human);
43 g_assert(data->sov != NULL);
44
45 data->ov = string_output_get_visitor(data->sov);
46 g_assert(data->ov != NULL);
47}
48
49static void visitor_output_teardown(TestOutputVisitorData *data,
50 const void *unused)
51{
52 string_output_visitor_cleanup(data->sov);
53 data->sov = NULL;
54 data->ov = NULL;
55}
56
57static void test_visitor_out_int(TestOutputVisitorData *data,
58 const void *unused)
59{
60 int64_t value = 42;
61 Error *err = NULL;
62 char *str;
63
64 visit_type_int(data->ov, &value, NULL, &err);
65 g_assert(!err);
66
67 str = string_output_get_string(data->sov);
68 g_assert(str != NULL);
69 if (data->human) {
70 g_assert_cmpstr(str, ==, "42 (0x2a)");
71 } else {
72 g_assert_cmpstr(str, ==, "42");
73 }
74 g_free(str);
75}
76
77static void test_visitor_out_intList(TestOutputVisitorData *data,
78 const void *unused)
79{
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;
83 int i;
84 Error *errp = NULL;
85 char *str;
86
87 for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
88 *tmp = g_malloc0(sizeof(**tmp));
89 (*tmp)->value = value[i];
90 tmp = &(*tmp)->next;
91 }
92
93 visit_type_intList(data->ov, &list, NULL, &errp);
94 g_assert(errp == NULL);
95
96 str = string_output_get_string(data->sov);
97 g_assert(str != NULL);
98 if (data->human) {
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)");
103 } else {
104 g_assert_cmpstr(str, ==,
105 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
106 }
107 g_free(str);
108 while (list) {
109 intList *tmp2;
110 tmp2 = list->next;
111 g_free(list);
112 list = tmp2;
113 }
114}
115
116static void test_visitor_out_bool(TestOutputVisitorData *data,
117 const void *unused)
118{
119 Error *err = NULL;
120 bool value = true;
121 char *str;
122
123 visit_type_bool(data->ov, &value, NULL, &err);
124 g_assert(!err);
125
126 str = string_output_get_string(data->sov);
127 g_assert(str != NULL);
128 g_assert_cmpstr(str, ==, "true");
129 g_free(str);
130}
131
132static void test_visitor_out_number(TestOutputVisitorData *data,
133 const void *unused)
134{
135 double value = 3.14;
136 Error *err = NULL;
137 char *str;
138
139 visit_type_number(data->ov, &value, NULL, &err);
140 g_assert(!err);
141
142 str = string_output_get_string(data->sov);
143 g_assert(str != NULL);
144 g_assert_cmpstr(str, ==, "3.140000");
145 g_free(str);
146}
147
148static void test_visitor_out_string(TestOutputVisitorData *data,
149 const void *unused)
150{
151 char *string = (char *) "Q E M U";
152 const char *string_human = "\"Q E M U\"";
153 Error *err = NULL;
154 char *str;
155
156 visit_type_str(data->ov, &string, NULL, &err);
157 g_assert(!err);
158
159 str = string_output_get_string(data->sov);
160 g_assert(str != NULL);
161 if (data->human) {
162 g_assert_cmpstr(str, ==, string_human);
163 } else {
164 g_assert_cmpstr(str, ==, string);
165 }
166 g_free(str);
167}
168
169static void test_visitor_out_no_string(TestOutputVisitorData *data,
170 const void *unused)
171{
172 char *string = NULL;
173 Error *err = NULL;
174 char *str;
175
176
177 visit_type_str(data->ov, &string, NULL, &err);
178 g_assert(!err);
179
180 str = string_output_get_string(data->sov);
181 g_assert(str != NULL);
182 if (data->human) {
183 g_assert_cmpstr(str, ==, "<null>");
184 } else {
185 g_assert_cmpstr(str, ==, "");
186 }
187 g_free(str);
188}
189
190static void test_visitor_out_enum(TestOutputVisitorData *data,
191 const void *unused)
192{
193 Error *err = NULL;
194 char *str;
195 EnumOne i;
196
197 for (i = 0; i < ENUM_ONE_MAX; i++) {
198 char *str_human;
199
200 visit_type_EnumOne(data->ov, &i, "unused", &err);
201 g_assert(!err);
202
203 str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
204
205 str = string_output_get_string(data->sov);
206 g_assert(str != NULL);
207 if (data->human) {
208 g_assert_cmpstr(str, ==, str_human);
209 } else {
210 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
211 }
212 g_free(str_human);
213 g_free(str);
214 }
215}
216
217static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
218 const void *unused)
219{
220 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
221 Error *err;
222
223 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
224 err = NULL;
225 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
226 g_assert(err);
227 error_free(err);
228 }
229}
230
231static void
232output_visitor_test_add(const char *testpath,
233 TestOutputVisitorData *data,
234 void (*test_func)(TestOutputVisitorData *data,
235 const void *user_data),
236 bool human)
237{
238 g_test_add(testpath, TestOutputVisitorData, data,
239 human ? visitor_output_setup_human : visitor_output_setup,
240 test_func, visitor_output_teardown);
241}
242
243int main(int argc, char **argv)
244{
245 TestOutputVisitorData out_visitor_data;
246
247 g_test_init(&argc, &argv, NULL);
248
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",
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",
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",
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",
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,
267 false);
268 output_visitor_test_add("/string-visitor/output/no-string",
269 &out_visitor_data, test_visitor_out_no_string,
270 true);
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",
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,
277 false);
278 output_visitor_test_add("/string-visitor/output/enum-errors",
279 &out_visitor_data, test_visitor_out_enum_errors,
280 true);
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",
284 &out_visitor_data, test_visitor_out_intList, true);
285
286 g_test_run();
287
288 return 0;
289}
290