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.
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"
21 typedef struct TestOutputVisitorData
{
22 StringOutputVisitor
*sov
;
25 } TestOutputVisitorData
;
27 static void visitor_output_setup(TestOutputVisitorData
*data
,
31 data
->sov
= string_output_visitor_new(data
->human
);
32 g_assert(data
->sov
!= NULL
);
34 data
->ov
= string_output_get_visitor(data
->sov
);
35 g_assert(data
->ov
!= NULL
);
38 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
42 data
->sov
= string_output_visitor_new(data
->human
);
43 g_assert(data
->sov
!= NULL
);
45 data
->ov
= string_output_get_visitor(data
->sov
);
46 g_assert(data
->ov
!= NULL
);
49 static void visitor_output_teardown(TestOutputVisitorData
*data
,
52 string_output_visitor_cleanup(data
->sov
);
57 static void test_visitor_out_int(TestOutputVisitorData
*data
,
64 visit_type_int(data
->ov
, &value
, NULL
, &err
);
67 str
= string_output_get_string(data
->sov
);
68 g_assert(str
!= NULL
);
70 g_assert_cmpstr(str
, ==, "42 (0x2a)");
72 g_assert_cmpstr(str
, ==, "42");
77 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
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
;
87 for (i
= 0; i
< sizeof(value
) / sizeof(value
[0]); i
++) {
88 *tmp
= g_malloc0(sizeof(**tmp
));
89 (*tmp
)->value
= value
[i
];
93 visit_type_intList(data
->ov
, &list
, NULL
, &errp
);
94 g_assert(errp
== NULL
);
96 str
= string_output_get_string(data
->sov
);
97 g_assert(str
!= NULL
);
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)");
104 g_assert_cmpstr(str
, ==,
105 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
116 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
123 visit_type_bool(data
->ov
, &value
, NULL
, &err
);
126 str
= string_output_get_string(data
->sov
);
127 g_assert(str
!= NULL
);
128 g_assert_cmpstr(str
, ==, "true");
132 static void test_visitor_out_number(TestOutputVisitorData
*data
,
139 visit_type_number(data
->ov
, &value
, NULL
, &err
);
142 str
= string_output_get_string(data
->sov
);
143 g_assert(str
!= NULL
);
144 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
, &string
, NULL
, &err
);
159 str
= string_output_get_string(data
->sov
);
160 g_assert(str
!= NULL
);
162 g_assert_cmpstr(str
, ==, string_human
);
164 g_assert_cmpstr(str
, ==, string
);
169 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
176 /* A null string should return "" */
177 visit_type_str(data
->ov
, &string
, NULL
, &err
);
180 str
= string_output_get_string(data
->sov
);
181 g_assert(str
!= NULL
);
183 g_assert_cmpstr(str
, ==, "<null>");
185 g_assert_cmpstr(str
, ==, "");
190 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
197 for (i
= 0; i
< ENUM_ONE_MAX
; i
++) {
201 visit_type_EnumOne(data
->ov
, &i
, "unused", &err
);
204 len
= strlen(EnumOne_lookup
[i
]) + 2;
205 str_human
= g_malloc0(len
);
207 strncpy(str_human
+ 1, EnumOne_lookup
[i
], strlen(EnumOne_lookup
[i
]));
208 str_human
[len
- 1] = '"';
210 str
= string_output_get_string(data
->sov
);
211 g_assert(str
!= NULL
);
213 g_assert_cmpstr(str
, ==, str_human
);
215 g_assert_cmpstr(str
, ==, EnumOne_lookup
[i
]);
222 static void test_visitor_out_enum_errors(TestOutputVisitorData
*data
,
225 EnumOne i
, bad_values
[] = { ENUM_ONE_MAX
, -1 };
228 for (i
= 0; i
< ARRAY_SIZE(bad_values
) ; i
++) {
230 visit_type_EnumOne(data
->ov
, &bad_values
[i
], "unused", &err
);
237 output_visitor_test_add(const char *testpath
,
238 TestOutputVisitorData
*data
,
239 void (*test_func
)(TestOutputVisitorData
*data
,
240 const void *user_data
),
243 g_test_add(testpath
, TestOutputVisitorData
, data
,
244 human
? visitor_output_setup_human
: visitor_output_setup
,
245 test_func
, visitor_output_teardown
);
248 int main(int argc
, char **argv
)
250 TestOutputVisitorData out_visitor_data
;
252 g_test_init(&argc
, &argv
, NULL
);
254 output_visitor_test_add("/string-visitor/output/int",
255 &out_visitor_data
, test_visitor_out_int
, false);
256 output_visitor_test_add("/string-visitor/output/int",
257 &out_visitor_data
, test_visitor_out_int
, true);
258 output_visitor_test_add("/string-visitor/output/bool",
259 &out_visitor_data
, test_visitor_out_bool
, false);
260 output_visitor_test_add("/string-visitor/output/bool",
261 &out_visitor_data
, test_visitor_out_bool
, true);
262 output_visitor_test_add("/string-visitor/output/number",
263 &out_visitor_data
, test_visitor_out_number
, false);
264 output_visitor_test_add("/string-visitor/output/number",
265 &out_visitor_data
, test_visitor_out_number
, true);
266 output_visitor_test_add("/string-visitor/output/string",
267 &out_visitor_data
, test_visitor_out_string
, false);
268 output_visitor_test_add("/string-visitor/output/string",
269 &out_visitor_data
, test_visitor_out_string
, true);
270 output_visitor_test_add("/string-visitor/output/no-string",
271 &out_visitor_data
, test_visitor_out_no_string
,
273 output_visitor_test_add("/string-visitor/output/no-string",
274 &out_visitor_data
, test_visitor_out_no_string
,
276 output_visitor_test_add("/string-visitor/output/enum",
277 &out_visitor_data
, test_visitor_out_enum
, false);
278 output_visitor_test_add("/string-visitor/output/enum",
279 &out_visitor_data
, test_visitor_out_enum
, true);
280 output_visitor_test_add("/string-visitor/output/enum-errors",
281 &out_visitor_data
, test_visitor_out_enum_errors
,
283 output_visitor_test_add("/string-visitor/output/enum-errors",
284 &out_visitor_data
, test_visitor_out_enum_errors
,
286 output_visitor_test_add("/string-visitor/output/intList",
287 &out_visitor_data
, test_visitor_out_intList
, false);
288 output_visitor_test_add("/string-visitor/output/intList",
289 &out_visitor_data
, test_visitor_out_intList
, true);