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