block: Remove dirty bitmaps from bdrv_move_feature_fields()
[qemu/ar7.git] / tests / test-string-output-visitor.c
blob1ecd75b8532d16a251f9c77b80e9db6442c96aaa
1 /*
2 * String Output Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Authors:
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.
13 #include "qemu/osdep.h"
14 #include <glib.h>
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/string-output-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
23 typedef struct TestOutputVisitorData {
24 StringOutputVisitor *sov;
25 Visitor *ov;
26 bool human;
27 } TestOutputVisitorData;
29 static void visitor_output_setup(TestOutputVisitorData *data,
30 const void *unused)
32 data->human = false;
33 data->sov = string_output_visitor_new(data->human);
34 g_assert(data->sov != NULL);
36 data->ov = string_output_get_visitor(data->sov);
37 g_assert(data->ov != NULL);
40 static void visitor_output_setup_human(TestOutputVisitorData *data,
41 const void *unused)
43 data->human = true;
44 data->sov = string_output_visitor_new(data->human);
45 g_assert(data->sov != NULL);
47 data->ov = string_output_get_visitor(data->sov);
48 g_assert(data->ov != NULL);
51 static void visitor_output_teardown(TestOutputVisitorData *data,
52 const void *unused)
54 string_output_visitor_cleanup(data->sov);
55 data->sov = NULL;
56 data->ov = NULL;
59 static void test_visitor_out_int(TestOutputVisitorData *data,
60 const void *unused)
62 int64_t value = 42;
63 Error *err = NULL;
64 char *str;
66 visit_type_int(data->ov, NULL, &value, &err);
67 g_assert(!err);
69 str = string_output_get_string(data->sov);
70 g_assert(str != NULL);
71 if (data->human) {
72 g_assert_cmpstr(str, ==, "42 (0x2a)");
73 } else {
74 g_assert_cmpstr(str, ==, "42");
76 g_free(str);
79 static void test_visitor_out_intList(TestOutputVisitorData *data,
80 const void *unused)
82 int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
83 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
84 intList *list = NULL, **tmp = &list;
85 int i;
86 Error *err = NULL;
87 char *str;
89 for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
90 *tmp = g_malloc0(sizeof(**tmp));
91 (*tmp)->value = value[i];
92 tmp = &(*tmp)->next;
95 visit_type_intList(data->ov, NULL, &list, &err);
96 g_assert(err == NULL);
98 str = string_output_get_string(data->sov);
99 g_assert(str != NULL);
100 if (data->human) {
101 g_assert_cmpstr(str, ==,
102 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
103 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
104 "0x7ffffffffffffffe-0x7fffffffffffffff)");
105 } else {
106 g_assert_cmpstr(str, ==,
107 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
109 g_free(str);
110 while (list) {
111 intList *tmp2;
112 tmp2 = list->next;
113 g_free(list);
114 list = tmp2;
118 static void test_visitor_out_bool(TestOutputVisitorData *data,
119 const void *unused)
121 Error *err = NULL;
122 bool value = true;
123 char *str;
125 visit_type_bool(data->ov, NULL, &value, &err);
126 g_assert(!err);
128 str = string_output_get_string(data->sov);
129 g_assert(str != NULL);
130 g_assert_cmpstr(str, ==, "true");
131 g_free(str);
134 static void test_visitor_out_number(TestOutputVisitorData *data,
135 const void *unused)
137 double value = 3.14;
138 Error *err = NULL;
139 char *str;
141 visit_type_number(data->ov, NULL, &value, &err);
142 g_assert(!err);
144 str = string_output_get_string(data->sov);
145 g_assert(str != NULL);
146 g_assert_cmpstr(str, ==, "3.140000");
147 g_free(str);
150 static void test_visitor_out_string(TestOutputVisitorData *data,
151 const void *unused)
153 char *string = (char *) "Q E M U";
154 const char *string_human = "\"Q E M U\"";
155 Error *err = NULL;
156 char *str;
158 visit_type_str(data->ov, NULL, &string, &err);
159 g_assert(!err);
161 str = string_output_get_string(data->sov);
162 g_assert(str != NULL);
163 if (data->human) {
164 g_assert_cmpstr(str, ==, string_human);
165 } else {
166 g_assert_cmpstr(str, ==, string);
168 g_free(str);
171 static void test_visitor_out_no_string(TestOutputVisitorData *data,
172 const void *unused)
174 char *string = NULL;
175 Error *err = NULL;
176 char *str;
178 /* A null string should return "" */
179 visit_type_str(data->ov, NULL, &string, &err);
180 g_assert(!err);
182 str = string_output_get_string(data->sov);
183 g_assert(str != NULL);
184 if (data->human) {
185 g_assert_cmpstr(str, ==, "<null>");
186 } else {
187 g_assert_cmpstr(str, ==, "");
189 g_free(str);
192 static void test_visitor_out_enum(TestOutputVisitorData *data,
193 const void *unused)
195 Error *err = NULL;
196 char *str;
197 EnumOne i;
199 for (i = 0; i < ENUM_ONE__MAX; i++) {
200 char *str_human;
202 visit_type_EnumOne(data->ov, "unused", &i, &err);
203 g_assert(!err);
205 str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
207 str = string_output_get_string(data->sov);
208 g_assert(str != NULL);
209 if (data->human) {
210 g_assert_cmpstr(str, ==, str_human);
211 } else {
212 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
214 g_free(str_human);
215 g_free(str);
219 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
220 const void *unused)
222 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
223 Error *err;
225 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
226 err = NULL;
227 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
228 g_assert(err);
229 error_free(err);
233 static void
234 output_visitor_test_add(const char *testpath,
235 TestOutputVisitorData *data,
236 void (*test_func)(TestOutputVisitorData *data,
237 const void *user_data),
238 bool human)
240 g_test_add(testpath, TestOutputVisitorData, data,
241 human ? visitor_output_setup_human : visitor_output_setup,
242 test_func, visitor_output_teardown);
245 int main(int argc, char **argv)
247 TestOutputVisitorData out_visitor_data;
249 g_test_init(&argc, &argv, NULL);
251 output_visitor_test_add("/string-visitor/output/int",
252 &out_visitor_data, test_visitor_out_int, false);
253 output_visitor_test_add("/string-visitor/output/int-human",
254 &out_visitor_data, test_visitor_out_int, true);
255 output_visitor_test_add("/string-visitor/output/bool",
256 &out_visitor_data, test_visitor_out_bool, false);
257 output_visitor_test_add("/string-visitor/output/bool-human",
258 &out_visitor_data, test_visitor_out_bool, true);
259 output_visitor_test_add("/string-visitor/output/number",
260 &out_visitor_data, test_visitor_out_number, false);
261 output_visitor_test_add("/string-visitor/output/number-human",
262 &out_visitor_data, test_visitor_out_number, true);
263 output_visitor_test_add("/string-visitor/output/string",
264 &out_visitor_data, test_visitor_out_string, false);
265 output_visitor_test_add("/string-visitor/output/string-human",
266 &out_visitor_data, test_visitor_out_string, true);
267 output_visitor_test_add("/string-visitor/output/no-string",
268 &out_visitor_data, test_visitor_out_no_string,
269 false);
270 output_visitor_test_add("/string-visitor/output/no-string-human",
271 &out_visitor_data, test_visitor_out_no_string,
272 true);
273 output_visitor_test_add("/string-visitor/output/enum",
274 &out_visitor_data, test_visitor_out_enum, false);
275 output_visitor_test_add("/string-visitor/output/enum-human",
276 &out_visitor_data, test_visitor_out_enum, true);
277 output_visitor_test_add("/string-visitor/output/enum-errors",
278 &out_visitor_data, test_visitor_out_enum_errors,
279 false);
280 output_visitor_test_add("/string-visitor/output/enum-errors-human",
281 &out_visitor_data, test_visitor_out_enum_errors,
282 true);
283 output_visitor_test_add("/string-visitor/output/intList",
284 &out_visitor_data, test_visitor_out_intList, false);
285 output_visitor_test_add("/string-visitor/output/intList-human",
286 &out_visitor_data, test_visitor_out_intList, true);
288 g_test_run();
290 return 0;