ast2400: Integrate the SCU model and set silicon revision
[qemu/ar7.git] / tests / test-string-input-visitor.c
blob7fe7a9c270e912fccc007a14b2084f4444a43b32
1 /*
2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-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-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData {
23 StringInputVisitor *siv;
24 } TestInputVisitorData;
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
29 if (data->siv) {
30 string_input_visitor_cleanup(data->siv);
31 data->siv = NULL;
35 /* This is provided instead of a test setup function so that the JSON
36 string used by the tests are kept in the test functions (and not
37 int main()) */
38 static
39 Visitor *visitor_input_test_init(TestInputVisitorData *data,
40 const char *string)
42 Visitor *v;
44 data->siv = string_input_visitor_new(string);
45 g_assert(data->siv != NULL);
47 v = string_input_get_visitor(data->siv);
48 g_assert(v != NULL);
50 return v;
53 static void test_visitor_in_int(TestInputVisitorData *data,
54 const void *unused)
56 int64_t res = 0, value = -42;
57 Error *err = NULL;
58 Visitor *v;
60 v = visitor_input_test_init(data, "-42");
62 visit_type_int(v, NULL, &res, &err);
63 g_assert(!err);
64 g_assert_cmpint(res, ==, value);
66 visitor_input_teardown(data, unused);
68 v = visitor_input_test_init(data, "not an int");
70 visit_type_int(v, NULL, &res, &err);
71 error_free_or_abort(&err);
74 static void test_visitor_in_intList(TestInputVisitorData *data,
75 const void *unused)
77 int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
78 int16List *res = NULL, *tmp;
79 Error *err = NULL;
80 Visitor *v;
81 int i = 0;
83 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
85 visit_type_int16List(v, NULL, &res, &error_abort);
86 tmp = res;
87 while (i < sizeof(value) / sizeof(value[0])) {
88 g_assert(tmp);
89 g_assert_cmpint(tmp->value, ==, value[i++]);
90 tmp = tmp->next;
92 g_assert(!tmp);
94 qapi_free_int16List(res);
96 visitor_input_teardown(data, unused);
98 v = visitor_input_test_init(data, "not an int list");
100 visit_type_int16List(v, NULL, &res, &err);
101 error_free_or_abort(&err);
102 g_assert(!res);
105 static void test_visitor_in_bool(TestInputVisitorData *data,
106 const void *unused)
108 Error *err = NULL;
109 bool res = false;
110 Visitor *v;
112 v = visitor_input_test_init(data, "true");
114 visit_type_bool(v, NULL, &res, &err);
115 g_assert(!err);
116 g_assert_cmpint(res, ==, true);
117 visitor_input_teardown(data, unused);
119 v = visitor_input_test_init(data, "yes");
121 visit_type_bool(v, NULL, &res, &err);
122 g_assert(!err);
123 g_assert_cmpint(res, ==, true);
124 visitor_input_teardown(data, unused);
126 v = visitor_input_test_init(data, "on");
128 visit_type_bool(v, NULL, &res, &err);
129 g_assert(!err);
130 g_assert_cmpint(res, ==, true);
131 visitor_input_teardown(data, unused);
133 v = visitor_input_test_init(data, "false");
135 visit_type_bool(v, NULL, &res, &err);
136 g_assert(!err);
137 g_assert_cmpint(res, ==, false);
138 visitor_input_teardown(data, unused);
140 v = visitor_input_test_init(data, "no");
142 visit_type_bool(v, NULL, &res, &err);
143 g_assert(!err);
144 g_assert_cmpint(res, ==, false);
145 visitor_input_teardown(data, unused);
147 v = visitor_input_test_init(data, "off");
149 visit_type_bool(v, NULL, &res, &err);
150 g_assert(!err);
151 g_assert_cmpint(res, ==, false);
154 static void test_visitor_in_number(TestInputVisitorData *data,
155 const void *unused)
157 double res = 0, value = 3.14;
158 Error *err = NULL;
159 Visitor *v;
161 v = visitor_input_test_init(data, "3.14");
163 visit_type_number(v, NULL, &res, &err);
164 g_assert(!err);
165 g_assert_cmpfloat(res, ==, value);
168 static void test_visitor_in_string(TestInputVisitorData *data,
169 const void *unused)
171 char *res = NULL, *value = (char *) "Q E M U";
172 Error *err = NULL;
173 Visitor *v;
175 v = visitor_input_test_init(data, value);
177 visit_type_str(v, NULL, &res, &err);
178 g_assert(!err);
179 g_assert_cmpstr(res, ==, value);
181 g_free(res);
184 static void test_visitor_in_enum(TestInputVisitorData *data,
185 const void *unused)
187 Error *err = NULL;
188 Visitor *v;
189 EnumOne i;
191 for (i = 0; EnumOne_lookup[i]; i++) {
192 EnumOne res = -1;
194 v = visitor_input_test_init(data, EnumOne_lookup[i]);
196 visit_type_EnumOne(v, NULL, &res, &err);
197 g_assert(!err);
198 g_assert_cmpint(i, ==, res);
200 visitor_input_teardown(data, NULL);
203 data->siv = NULL;
206 /* Try to crash the visitors */
207 static void test_visitor_in_fuzz(TestInputVisitorData *data,
208 const void *unused)
210 int64_t ires;
211 intList *ilres;
212 bool bres;
213 double nres;
214 char *sres;
215 EnumOne eres;
216 Visitor *v;
217 unsigned int i;
218 char buf[10000];
220 for (i = 0; i < 100; i++) {
221 unsigned int j;
223 j = g_test_rand_int_range(0, sizeof(buf) - 1);
225 buf[j] = '\0';
227 if (j != 0) {
228 for (j--; j != 0; j--) {
229 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
233 v = visitor_input_test_init(data, buf);
234 visit_type_int(v, NULL, &ires, NULL);
235 visitor_input_teardown(data, NULL);
237 v = visitor_input_test_init(data, buf);
238 visit_type_intList(v, NULL, &ilres, NULL);
239 visitor_input_teardown(data, NULL);
241 v = visitor_input_test_init(data, buf);
242 visit_type_bool(v, NULL, &bres, NULL);
243 visitor_input_teardown(data, NULL);
245 v = visitor_input_test_init(data, buf);
246 visit_type_number(v, NULL, &nres, NULL);
247 visitor_input_teardown(data, NULL);
249 v = visitor_input_test_init(data, buf);
250 sres = NULL;
251 visit_type_str(v, NULL, &sres, NULL);
252 g_free(sres);
253 visitor_input_teardown(data, NULL);
255 v = visitor_input_test_init(data, buf);
256 visit_type_EnumOne(v, NULL, &eres, NULL);
257 visitor_input_teardown(data, NULL);
261 static void input_visitor_test_add(const char *testpath,
262 TestInputVisitorData *data,
263 void (*test_func)(TestInputVisitorData *data, const void *user_data))
265 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
266 visitor_input_teardown);
269 int main(int argc, char **argv)
271 TestInputVisitorData in_visitor_data;
273 g_test_init(&argc, &argv, NULL);
275 input_visitor_test_add("/string-visitor/input/int",
276 &in_visitor_data, test_visitor_in_int);
277 input_visitor_test_add("/string-visitor/input/intList",
278 &in_visitor_data, test_visitor_in_intList);
279 input_visitor_test_add("/string-visitor/input/bool",
280 &in_visitor_data, test_visitor_in_bool);
281 input_visitor_test_add("/string-visitor/input/number",
282 &in_visitor_data, test_visitor_in_number);
283 input_visitor_test_add("/string-visitor/input/string",
284 &in_visitor_data, test_visitor_in_string);
285 input_visitor_test_add("/string-visitor/input/enum",
286 &in_visitor_data, test_visitor_in_enum);
287 input_visitor_test_add("/string-visitor/input/fuzz",
288 &in_visitor_data, test_visitor_in_fuzz);
290 g_test_run();
292 return 0;