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 "qapi/error.h"
16 #include "qapi/string-output-visitor.h"
17 #include "test-qapi-visit.h"
19 typedef struct TestOutputVisitorData
{
23 } TestOutputVisitorData
;
25 static void visitor_output_setup_internal(TestOutputVisitorData
*data
,
29 data
->ov
= string_output_visitor_new(human
, &data
->str
);
33 static void visitor_output_setup(TestOutputVisitorData
*data
,
36 return visitor_output_setup_internal(data
, false);
39 static void visitor_output_setup_human(TestOutputVisitorData
*data
,
42 return visitor_output_setup_internal(data
, true);
45 static void visitor_output_teardown(TestOutputVisitorData
*data
,
54 static char *visitor_get(TestOutputVisitorData
*data
)
56 visit_complete(data
->ov
, &data
->str
);
61 static void visitor_reset(TestOutputVisitorData
*data
)
63 bool human
= data
->human
;
65 visitor_output_teardown(data
, NULL
);
66 visitor_output_setup_internal(data
, human
);
69 static void test_visitor_out_int(TestOutputVisitorData
*data
,
75 visit_type_int(data
->ov
, NULL
, &value
, &error_abort
);
77 str
= visitor_get(data
);
79 g_assert_cmpstr(str
, ==, "42 (0x2a)");
81 g_assert_cmpstr(str
, ==, "42");
85 static void test_visitor_out_intList(TestOutputVisitorData
*data
,
88 int64_t value
[] = {0, 1, 9, 10, 16, 15, 14,
89 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX
- 1, INT64_MAX
};
90 intList
*list
= NULL
, **tail
= &list
;
95 for (i
= 0; i
< ARRAY_SIZE(value
); i
++) {
96 QAPI_LIST_APPEND(tail
, value
[i
]);
99 visit_type_intList(data
->ov
, NULL
, &list
, &err
);
100 g_assert(err
== NULL
);
102 str
= visitor_get(data
);
104 g_assert_cmpstr(str
, ==,
105 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
106 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
107 "0x7ffffffffffffffe-0x7fffffffffffffff)");
109 g_assert_cmpstr(str
, ==,
110 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
112 qapi_free_intList(list
);
115 static void test_visitor_out_bool(TestOutputVisitorData
*data
,
121 visit_type_bool(data
->ov
, NULL
, &value
, &error_abort
);
123 str
= visitor_get(data
);
124 g_assert_cmpstr(str
, ==, "true");
127 static void test_visitor_out_number(TestOutputVisitorData
*data
,
130 double value
= 3.1415926535897932;
133 visit_type_number(data
->ov
, NULL
, &value
, &error_abort
);
135 str
= visitor_get(data
);
136 g_assert_cmpstr(str
, ==, "3.1415926535897931");
139 static void test_visitor_out_string(TestOutputVisitorData
*data
,
142 char *string
= (char *) "Q E M U";
143 const char *string_human
= "\"Q E M U\"";
146 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
148 str
= visitor_get(data
);
150 g_assert_cmpstr(str
, ==, string_human
);
152 g_assert_cmpstr(str
, ==, string
);
156 static void test_visitor_out_no_string(TestOutputVisitorData
*data
,
162 /* A null string should return "" */
163 visit_type_str(data
->ov
, NULL
, &string
, &error_abort
);
165 str
= visitor_get(data
);
167 g_assert_cmpstr(str
, ==, "<null>");
169 g_assert_cmpstr(str
, ==, "");
173 static void test_visitor_out_enum(TestOutputVisitorData
*data
,
179 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
180 visit_type_EnumOne(data
->ov
, "unused", &i
, &error_abort
);
182 str
= visitor_get(data
);
184 char *str_human
= g_strdup_printf("\"%s\"", EnumOne_str(i
));
186 g_assert_cmpstr(str
, ==, str_human
);
189 g_assert_cmpstr(str
, ==, EnumOne_str(i
));
196 output_visitor_test_add(const char *testpath
,
197 TestOutputVisitorData
*data
,
198 void (*test_func
)(TestOutputVisitorData
*data
,
199 const void *user_data
),
202 g_test_add(testpath
, TestOutputVisitorData
, data
,
203 human
? visitor_output_setup_human
: visitor_output_setup
,
204 test_func
, visitor_output_teardown
);
207 int main(int argc
, char **argv
)
209 TestOutputVisitorData out_visitor_data
;
211 g_test_init(&argc
, &argv
, NULL
);
213 output_visitor_test_add("/string-visitor/output/int",
214 &out_visitor_data
, test_visitor_out_int
, false);
215 output_visitor_test_add("/string-visitor/output/int-human",
216 &out_visitor_data
, test_visitor_out_int
, true);
217 output_visitor_test_add("/string-visitor/output/bool",
218 &out_visitor_data
, test_visitor_out_bool
, false);
219 output_visitor_test_add("/string-visitor/output/bool-human",
220 &out_visitor_data
, test_visitor_out_bool
, true);
221 output_visitor_test_add("/string-visitor/output/number",
222 &out_visitor_data
, test_visitor_out_number
, false);
223 output_visitor_test_add("/string-visitor/output/number-human",
224 &out_visitor_data
, test_visitor_out_number
, true);
225 output_visitor_test_add("/string-visitor/output/string",
226 &out_visitor_data
, test_visitor_out_string
, false);
227 output_visitor_test_add("/string-visitor/output/string-human",
228 &out_visitor_data
, test_visitor_out_string
, true);
229 output_visitor_test_add("/string-visitor/output/no-string",
230 &out_visitor_data
, test_visitor_out_no_string
,
232 output_visitor_test_add("/string-visitor/output/no-string-human",
233 &out_visitor_data
, test_visitor_out_no_string
,
235 output_visitor_test_add("/string-visitor/output/enum",
236 &out_visitor_data
, test_visitor_out_enum
, false);
237 output_visitor_test_add("/string-visitor/output/enum-human",
238 &out_visitor_data
, test_visitor_out_enum
, true);
239 output_visitor_test_add("/string-visitor/output/intList",
240 &out_visitor_data
, test_visitor_out_intList
, false);
241 output_visitor_test_add("/string-visitor/output/intList-human",
242 &out_visitor_data
, test_visitor_out_intList
, true);