]>
Commit | Line | Data |
---|---|---|
2d7799f2 PB |
1 | /* |
2 | * String Output Visitor unit-tests. | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
b3db211f | 7 | * Paolo Bonzini <[email protected]> (based on test-qobject-output-visitor) |
2d7799f2 PB |
8 | * |
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. | |
11 | */ | |
12 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
2d7799f2 | 14 | |
79ee7df8 | 15 | #include "qemu-common.h" |
da34e65c | 16 | #include "qapi/error.h" |
2d7799f2 PB |
17 | #include "qapi/string-output-visitor.h" |
18 | #include "test-qapi-types.h" | |
19 | #include "test-qapi-visit.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/types.h" |
2d7799f2 PB |
21 | |
22 | typedef struct TestOutputVisitorData { | |
2d7799f2 | 23 | Visitor *ov; |
23d1705f | 24 | char *str; |
b4900c0e | 25 | bool human; |
2d7799f2 PB |
26 | } TestOutputVisitorData; |
27 | ||
a8fff94d EB |
28 | static void visitor_output_setup_internal(TestOutputVisitorData *data, |
29 | bool human) | |
30 | { | |
31 | data->human = human; | |
3b098d56 | 32 | data->ov = string_output_visitor_new(human, &data->str); |
a8fff94d EB |
33 | g_assert(data->ov); |
34 | } | |
35 | ||
2d7799f2 PB |
36 | static void visitor_output_setup(TestOutputVisitorData *data, |
37 | const void *unused) | |
38 | { | |
a8fff94d | 39 | return visitor_output_setup_internal(data, false); |
b4900c0e HT |
40 | } |
41 | ||
42 | static void visitor_output_setup_human(TestOutputVisitorData *data, | |
43 | const void *unused) | |
44 | { | |
a8fff94d | 45 | return visitor_output_setup_internal(data, true); |
2d7799f2 PB |
46 | } |
47 | ||
48 | static void visitor_output_teardown(TestOutputVisitorData *data, | |
49 | const void *unused) | |
50 | { | |
e7ca5656 | 51 | visit_free(data->ov); |
2d7799f2 | 52 | data->ov = NULL; |
23d1705f EB |
53 | g_free(data->str); |
54 | data->str = NULL; | |
55 | } | |
56 | ||
57 | static char *visitor_get(TestOutputVisitorData *data) | |
58 | { | |
3b098d56 | 59 | visit_complete(data->ov, &data->str); |
23d1705f EB |
60 | g_assert(data->str); |
61 | return data->str; | |
2d7799f2 PB |
62 | } |
63 | ||
a8fff94d EB |
64 | static void visitor_reset(TestOutputVisitorData *data) |
65 | { | |
66 | bool human = data->human; | |
67 | ||
68 | visitor_output_teardown(data, NULL); | |
69 | visitor_output_setup_internal(data, human); | |
70 | } | |
71 | ||
2d7799f2 PB |
72 | static void test_visitor_out_int(TestOutputVisitorData *data, |
73 | const void *unused) | |
74 | { | |
69e25563 | 75 | int64_t value = 42; |
e940f543 | 76 | Error *err = NULL; |
2d7799f2 PB |
77 | char *str; |
78 | ||
51e72bc1 | 79 | visit_type_int(data->ov, NULL, &value, &err); |
e940f543 | 80 | g_assert(!err); |
2d7799f2 | 81 | |
23d1705f | 82 | str = visitor_get(data); |
b4900c0e HT |
83 | if (data->human) { |
84 | g_assert_cmpstr(str, ==, "42 (0x2a)"); | |
85 | } else { | |
86 | g_assert_cmpstr(str, ==, "42"); | |
87 | } | |
2d7799f2 PB |
88 | } |
89 | ||
69e25563 HT |
90 | static void test_visitor_out_intList(TestOutputVisitorData *data, |
91 | const void *unused) | |
92 | { | |
93 | int64_t value[] = {0, 1, 9, 10, 16, 15, 14, | |
94 | 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX}; | |
95 | intList *list = NULL, **tmp = &list; | |
96 | int i; | |
533fdaed | 97 | Error *err = NULL; |
69e25563 HT |
98 | char *str; |
99 | ||
100 | for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) { | |
101 | *tmp = g_malloc0(sizeof(**tmp)); | |
102 | (*tmp)->value = value[i]; | |
103 | tmp = &(*tmp)->next; | |
104 | } | |
105 | ||
51e72bc1 | 106 | visit_type_intList(data->ov, NULL, &list, &err); |
533fdaed | 107 | g_assert(err == NULL); |
69e25563 | 108 | |
23d1705f | 109 | str = visitor_get(data); |
b4900c0e HT |
110 | if (data->human) { |
111 | g_assert_cmpstr(str, ==, | |
112 | "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 " | |
113 | "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16," | |
114 | "0x7ffffffffffffffe-0x7fffffffffffffff)"); | |
115 | } else { | |
116 | g_assert_cmpstr(str, ==, | |
117 | "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807"); | |
118 | } | |
a8fff94d | 119 | qapi_free_intList(list); |
69e25563 HT |
120 | } |
121 | ||
2d7799f2 PB |
122 | static void test_visitor_out_bool(TestOutputVisitorData *data, |
123 | const void *unused) | |
124 | { | |
e940f543 | 125 | Error *err = NULL; |
2d7799f2 PB |
126 | bool value = true; |
127 | char *str; | |
128 | ||
51e72bc1 | 129 | visit_type_bool(data->ov, NULL, &value, &err); |
e940f543 | 130 | g_assert(!err); |
2d7799f2 | 131 | |
23d1705f | 132 | str = visitor_get(data); |
2d7799f2 | 133 | g_assert_cmpstr(str, ==, "true"); |
2d7799f2 PB |
134 | } |
135 | ||
136 | static void test_visitor_out_number(TestOutputVisitorData *data, | |
137 | const void *unused) | |
138 | { | |
139 | double value = 3.14; | |
e940f543 | 140 | Error *err = NULL; |
2d7799f2 PB |
141 | char *str; |
142 | ||
51e72bc1 | 143 | visit_type_number(data->ov, NULL, &value, &err); |
e940f543 | 144 | g_assert(!err); |
2d7799f2 | 145 | |
23d1705f | 146 | str = visitor_get(data); |
173bbb75 | 147 | g_assert_cmpstr(str, ==, "3.140000"); |
2d7799f2 PB |
148 | } |
149 | ||
150 | static void test_visitor_out_string(TestOutputVisitorData *data, | |
151 | const void *unused) | |
152 | { | |
153 | char *string = (char *) "Q E M U"; | |
b4900c0e | 154 | const char *string_human = "\"Q E M U\""; |
e940f543 | 155 | Error *err = NULL; |
2d7799f2 PB |
156 | char *str; |
157 | ||
51e72bc1 | 158 | visit_type_str(data->ov, NULL, &string, &err); |
e940f543 | 159 | g_assert(!err); |
2d7799f2 | 160 | |
23d1705f | 161 | str = visitor_get(data); |
b4900c0e HT |
162 | if (data->human) { |
163 | g_assert_cmpstr(str, ==, string_human); | |
164 | } else { | |
165 | g_assert_cmpstr(str, ==, string); | |
166 | } | |
2d7799f2 PB |
167 | } |
168 | ||
169 | static void test_visitor_out_no_string(TestOutputVisitorData *data, | |
170 | const void *unused) | |
171 | { | |
172 | char *string = NULL; | |
2d7799f2 PB |
173 | char *str; |
174 | ||
175 | /* A null string should return "" */ | |
a8fff94d | 176 | visit_type_str(data->ov, NULL, &string, &error_abort); |
2d7799f2 | 177 | |
23d1705f | 178 | str = visitor_get(data); |
b4900c0e HT |
179 | if (data->human) { |
180 | g_assert_cmpstr(str, ==, "<null>"); | |
181 | } else { | |
182 | g_assert_cmpstr(str, ==, ""); | |
183 | } | |
2d7799f2 PB |
184 | } |
185 | ||
186 | static void test_visitor_out_enum(TestOutputVisitorData *data, | |
187 | const void *unused) | |
188 | { | |
2d7799f2 PB |
189 | char *str; |
190 | EnumOne i; | |
191 | ||
7fb1cf16 | 192 | for (i = 0; i < ENUM_ONE__MAX; i++) { |
a8fff94d | 193 | visit_type_EnumOne(data->ov, "unused", &i, &error_abort); |
b4900c0e | 194 | |
23d1705f | 195 | str = visitor_get(data); |
b4900c0e | 196 | if (data->human) { |
a8fff94d EB |
197 | char *str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]); |
198 | ||
b4900c0e | 199 | g_assert_cmpstr(str, ==, str_human); |
a8fff94d | 200 | g_free(str_human); |
b4900c0e HT |
201 | } else { |
202 | g_assert_cmpstr(str, ==, EnumOne_lookup[i]); | |
203 | } | |
a8fff94d | 204 | visitor_reset(data); |
2d7799f2 PB |
205 | } |
206 | } | |
207 | ||
208 | static void test_visitor_out_enum_errors(TestOutputVisitorData *data, | |
209 | const void *unused) | |
210 | { | |
7fb1cf16 | 211 | EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 }; |
e940f543 | 212 | Error *err; |
2d7799f2 PB |
213 | |
214 | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { | |
e940f543 | 215 | err = NULL; |
51e72bc1 | 216 | visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err); |
a8fff94d | 217 | error_free_or_abort(&err); |
2d7799f2 PB |
218 | } |
219 | } | |
220 | ||
b4900c0e HT |
221 | static void |
222 | output_visitor_test_add(const char *testpath, | |
223 | TestOutputVisitorData *data, | |
224 | void (*test_func)(TestOutputVisitorData *data, | |
225 | const void *user_data), | |
226 | bool human) | |
2d7799f2 | 227 | { |
b4900c0e HT |
228 | g_test_add(testpath, TestOutputVisitorData, data, |
229 | human ? visitor_output_setup_human : visitor_output_setup, | |
2d7799f2 PB |
230 | test_func, visitor_output_teardown); |
231 | } | |
232 | ||
233 | int main(int argc, char **argv) | |
234 | { | |
235 | TestOutputVisitorData out_visitor_data; | |
236 | ||
237 | g_test_init(&argc, &argv, NULL); | |
238 | ||
239 | output_visitor_test_add("/string-visitor/output/int", | |
b4900c0e | 240 | &out_visitor_data, test_visitor_out_int, false); |
deb847bf | 241 | output_visitor_test_add("/string-visitor/output/int-human", |
b4900c0e HT |
242 | &out_visitor_data, test_visitor_out_int, true); |
243 | output_visitor_test_add("/string-visitor/output/bool", | |
244 | &out_visitor_data, test_visitor_out_bool, false); | |
deb847bf | 245 | output_visitor_test_add("/string-visitor/output/bool-human", |
b4900c0e HT |
246 | &out_visitor_data, test_visitor_out_bool, true); |
247 | output_visitor_test_add("/string-visitor/output/number", | |
248 | &out_visitor_data, test_visitor_out_number, false); | |
deb847bf | 249 | output_visitor_test_add("/string-visitor/output/number-human", |
b4900c0e | 250 | &out_visitor_data, test_visitor_out_number, true); |
2d7799f2 | 251 | output_visitor_test_add("/string-visitor/output/string", |
b4900c0e | 252 | &out_visitor_data, test_visitor_out_string, false); |
deb847bf | 253 | output_visitor_test_add("/string-visitor/output/string-human", |
b4900c0e HT |
254 | &out_visitor_data, test_visitor_out_string, true); |
255 | output_visitor_test_add("/string-visitor/output/no-string", | |
256 | &out_visitor_data, test_visitor_out_no_string, | |
257 | false); | |
deb847bf | 258 | output_visitor_test_add("/string-visitor/output/no-string-human", |
b4900c0e HT |
259 | &out_visitor_data, test_visitor_out_no_string, |
260 | true); | |
261 | output_visitor_test_add("/string-visitor/output/enum", | |
262 | &out_visitor_data, test_visitor_out_enum, false); | |
deb847bf | 263 | output_visitor_test_add("/string-visitor/output/enum-human", |
b4900c0e | 264 | &out_visitor_data, test_visitor_out_enum, true); |
2d7799f2 | 265 | output_visitor_test_add("/string-visitor/output/enum-errors", |
b4900c0e HT |
266 | &out_visitor_data, test_visitor_out_enum_errors, |
267 | false); | |
deb847bf | 268 | output_visitor_test_add("/string-visitor/output/enum-errors-human", |
b4900c0e HT |
269 | &out_visitor_data, test_visitor_out_enum_errors, |
270 | true); | |
271 | output_visitor_test_add("/string-visitor/output/intList", | |
272 | &out_visitor_data, test_visitor_out_intList, false); | |
deb847bf | 273 | output_visitor_test_add("/string-visitor/output/intList-human", |
b4900c0e | 274 | &out_visitor_data, test_visitor_out_intList, true); |
2d7799f2 PB |
275 | |
276 | g_test_run(); | |
277 | ||
278 | return 0; | |
279 | } |