Docs/RCU: Correct sample code of qatomic_rcu_set
[qemu/ar7.git] / tests / test-string-output-visitor.c
blob0dae04b960303af84e30ef6d34ffe9de3b42e666
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-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 "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/string-output-visitor.h"
18 #include "test-qapi-visit.h"
20 typedef struct TestOutputVisitorData {
21 Visitor *ov;
22 char *str;
23 bool human;
24 } TestOutputVisitorData;
26 static void visitor_output_setup_internal(TestOutputVisitorData *data,
27 bool human)
29 data->human = human;
30 data->ov = string_output_visitor_new(human, &data->str);
31 g_assert(data->ov);
34 static void visitor_output_setup(TestOutputVisitorData *data,
35 const void *unused)
37 return visitor_output_setup_internal(data, false);
40 static void visitor_output_setup_human(TestOutputVisitorData *data,
41 const void *unused)
43 return visitor_output_setup_internal(data, true);
46 static void visitor_output_teardown(TestOutputVisitorData *data,
47 const void *unused)
49 visit_free(data->ov);
50 data->ov = NULL;
51 g_free(data->str);
52 data->str = NULL;
55 static char *visitor_get(TestOutputVisitorData *data)
57 visit_complete(data->ov, &data->str);
58 g_assert(data->str);
59 return data->str;
62 static void visitor_reset(TestOutputVisitorData *data)
64 bool human = data->human;
66 visitor_output_teardown(data, NULL);
67 visitor_output_setup_internal(data, human);
70 static void test_visitor_out_int(TestOutputVisitorData *data,
71 const void *unused)
73 int64_t value = 42;
74 char *str;
76 visit_type_int(data->ov, NULL, &value, &error_abort);
78 str = visitor_get(data);
79 if (data->human) {
80 g_assert_cmpstr(str, ==, "42 (0x2a)");
81 } else {
82 g_assert_cmpstr(str, ==, "42");
86 static void test_visitor_out_intList(TestOutputVisitorData *data,
87 const void *unused)
89 int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
90 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
91 intList *list = NULL, **tmp = &list;
92 int i;
93 Error *err = NULL;
94 char *str;
96 for (i = 0; i < ARRAY_SIZE(value); i++) {
97 *tmp = g_malloc0(sizeof(**tmp));
98 (*tmp)->value = value[i];
99 tmp = &(*tmp)->next;
102 visit_type_intList(data->ov, NULL, &list, &err);
103 g_assert(err == NULL);
105 str = visitor_get(data);
106 if (data->human) {
107 g_assert_cmpstr(str, ==,
108 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
109 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
110 "0x7ffffffffffffffe-0x7fffffffffffffff)");
111 } else {
112 g_assert_cmpstr(str, ==,
113 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
115 qapi_free_intList(list);
118 static void test_visitor_out_bool(TestOutputVisitorData *data,
119 const void *unused)
121 bool value = true;
122 char *str;
124 visit_type_bool(data->ov, NULL, &value, &error_abort);
126 str = visitor_get(data);
127 g_assert_cmpstr(str, ==, "true");
130 static void test_visitor_out_number(TestOutputVisitorData *data,
131 const void *unused)
133 double value = 3.1415926535897932;
134 char *str;
136 visit_type_number(data->ov, NULL, &value, &error_abort);
138 str = visitor_get(data);
139 g_assert_cmpstr(str, ==, "3.1415926535897931");
142 static void test_visitor_out_string(TestOutputVisitorData *data,
143 const void *unused)
145 char *string = (char *) "Q E M U";
146 const char *string_human = "\"Q E M U\"";
147 char *str;
149 visit_type_str(data->ov, NULL, &string, &error_abort);
151 str = visitor_get(data);
152 if (data->human) {
153 g_assert_cmpstr(str, ==, string_human);
154 } else {
155 g_assert_cmpstr(str, ==, string);
159 static void test_visitor_out_no_string(TestOutputVisitorData *data,
160 const void *unused)
162 char *string = NULL;
163 char *str;
165 /* A null string should return "" */
166 visit_type_str(data->ov, NULL, &string, &error_abort);
168 str = visitor_get(data);
169 if (data->human) {
170 g_assert_cmpstr(str, ==, "<null>");
171 } else {
172 g_assert_cmpstr(str, ==, "");
176 static void test_visitor_out_enum(TestOutputVisitorData *data,
177 const void *unused)
179 char *str;
180 EnumOne i;
182 for (i = 0; i < ENUM_ONE__MAX; i++) {
183 visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
185 str = visitor_get(data);
186 if (data->human) {
187 char *str_human = g_strdup_printf("\"%s\"", EnumOne_str(i));
189 g_assert_cmpstr(str, ==, str_human);
190 g_free(str_human);
191 } else {
192 g_assert_cmpstr(str, ==, EnumOne_str(i));
194 visitor_reset(data);
198 static void
199 output_visitor_test_add(const char *testpath,
200 TestOutputVisitorData *data,
201 void (*test_func)(TestOutputVisitorData *data,
202 const void *user_data),
203 bool human)
205 g_test_add(testpath, TestOutputVisitorData, data,
206 human ? visitor_output_setup_human : visitor_output_setup,
207 test_func, visitor_output_teardown);
210 int main(int argc, char **argv)
212 TestOutputVisitorData out_visitor_data;
214 g_test_init(&argc, &argv, NULL);
216 output_visitor_test_add("/string-visitor/output/int",
217 &out_visitor_data, test_visitor_out_int, false);
218 output_visitor_test_add("/string-visitor/output/int-human",
219 &out_visitor_data, test_visitor_out_int, true);
220 output_visitor_test_add("/string-visitor/output/bool",
221 &out_visitor_data, test_visitor_out_bool, false);
222 output_visitor_test_add("/string-visitor/output/bool-human",
223 &out_visitor_data, test_visitor_out_bool, true);
224 output_visitor_test_add("/string-visitor/output/number",
225 &out_visitor_data, test_visitor_out_number, false);
226 output_visitor_test_add("/string-visitor/output/number-human",
227 &out_visitor_data, test_visitor_out_number, true);
228 output_visitor_test_add("/string-visitor/output/string",
229 &out_visitor_data, test_visitor_out_string, false);
230 output_visitor_test_add("/string-visitor/output/string-human",
231 &out_visitor_data, test_visitor_out_string, true);
232 output_visitor_test_add("/string-visitor/output/no-string",
233 &out_visitor_data, test_visitor_out_no_string,
234 false);
235 output_visitor_test_add("/string-visitor/output/no-string-human",
236 &out_visitor_data, test_visitor_out_no_string,
237 true);
238 output_visitor_test_add("/string-visitor/output/enum",
239 &out_visitor_data, test_visitor_out_enum, false);
240 output_visitor_test_add("/string-visitor/output/enum-human",
241 &out_visitor_data, test_visitor_out_enum, true);
242 output_visitor_test_add("/string-visitor/output/intList",
243 &out_visitor_data, test_visitor_out_intList, false);
244 output_visitor_test_add("/string-visitor/output/intList-human",
245 &out_visitor_data, test_visitor_out_intList, true);
247 g_test_run();
249 return 0;