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
, **tmp
= &list
;
96 for (i
= 0; i
< ARRAY_SIZE(value
); i
++) {
97 *tmp
= g_malloc0(sizeof(**tmp
));
98 (*tmp
)->value
= value
[i
];
102 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
103 g_assert(err
== NULL
);
105 str
= visitor_get(data
);
107 g_assert_cmpstr(str
, ==,
108 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
109 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
110 "0x7ffffffffffffffe-0x7fffffffffffffff)");
112 g_assert_cmpstr(str
, ==,
113 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
115 qapi_free_intList(list
);
118 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
124 visit_type_bool(data
->ov
, NULL
, &value
, &error_abort
);
126 str
= visitor_get(data
);
127 g_assert_cmpstr(str
, ==, "true");
130 static void test_visitor_out_number(TestOutputVisitorData
*data
,
133 double value
= 3.1415926535897932;
136 visit_type_number(data
->ov
, NULL
, &value
, &error_abort
);
138 str
= visitor_get(data
);
139 g_assert_cmpstr(str
, ==, "3.1415926535897931");
142 static void test_visitor_out_string(TestOutputVisitorData
*data
,
145 char *string
= (char *) "Q E M U";
146 const char *string_human
= "\"Q E M U\"";
149 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
151 str
= visitor_get(data
);
153 g_assert_cmpstr(str
, ==, string_human
);
155 g_assert_cmpstr(str
, ==, string
);
159 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
165 /* A null string should return "" */
166 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
168 str
= visitor_get(data
);
170 g_assert_cmpstr(str
, ==, "<null>");
172 g_assert_cmpstr(str
, ==, "");
176 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
182 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
183 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
185 str
= visitor_get(data
);
187 char *str_human
= g_strdup_printf("\"%s\"", EnumOne_str(i
));
189 g_assert_cmpstr(str
, ==, str_human
);
192 g_assert_cmpstr(str
, ==, EnumOne_str(i
));
199 output_visitor_test_add(const char *testpath
,
200 TestOutputVisitorData
*data
,
201 void (*test_func
)(TestOutputVisitorData
*data
,
202 const void *user_data
),
205 g_test_add(testpath
, TestOutputVisitorData
, data
,
206 human
? visitor_output_setup_human
: visitor_output_setup
,
207 test_func
, visitor_output_teardown
);
210 int main(int argc
, char **argv
)
212 TestOutputVisitorData out_visitor_data
;
214 g_test_init(&argc
, &argv
, NULL
);
216 output_visitor_test_add("/string-visitor/output/int",
217 &out_visitor_data
, test_visitor_out_int
, false);
218 output_visitor_test_add("/string-visitor/output/int-human",
219 &out_visitor_data
, test_visitor_out_int
, true);
220 output_visitor_test_add("/string-visitor/output/bool",
221 &out_visitor_data
, test_visitor_out_bool
, false);
222 output_visitor_test_add("/string-visitor/output/bool-human",
223 &out_visitor_data
, test_visitor_out_bool
, true);
224 output_visitor_test_add("/string-visitor/output/number",
225 &out_visitor_data
, test_visitor_out_number
, false);
226 output_visitor_test_add("/string-visitor/output/number-human",
227 &out_visitor_data
, test_visitor_out_number
, true);
228 output_visitor_test_add("/string-visitor/output/string",
229 &out_visitor_data
, test_visitor_out_string
, false);
230 output_visitor_test_add("/string-visitor/output/string-human",
231 &out_visitor_data
, test_visitor_out_string
, true);
232 output_visitor_test_add("/string-visitor/output/no-string",
233 &out_visitor_data
, test_visitor_out_no_string
,
235 output_visitor_test_add("/string-visitor/output/no-string-human",
236 &out_visitor_data
, test_visitor_out_no_string
,
238 output_visitor_test_add("/string-visitor/output/enum",
239 &out_visitor_data
, test_visitor_out_enum
, false);
240 output_visitor_test_add("/string-visitor/output/enum-human",
241 &out_visitor_data
, test_visitor_out_enum
, true);
242 output_visitor_test_add("/string-visitor/output/intList",
243 &out_visitor_data
, test_visitor_out_intList
, false);
244 output_visitor_test_add("/string-visitor/output/intList-human",
245 &out_visitor_data
, test_visitor_out_intList
, true);