trap signals for "-serial mon:stdio"
[qemu.git] / tests / test-string-output-visitor.c
1 /*
2 * String Output Visitor unit-tests.
3 *
4 * Copyright (C) 2012 Red Hat Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
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
13 #include <glib.h>
14
15 #include "qemu-common.h"
16 #include "qapi/string-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/qmp/types.h"
20
21 typedef struct TestOutputVisitorData {
22 StringOutputVisitor *sov;
23 Visitor *ov;
24 } TestOutputVisitorData;
25
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
28 {
29 data->sov = string_output_visitor_new();
30 g_assert(data->sov != NULL);
31
32 data->ov = string_output_get_visitor(data->sov);
33 g_assert(data->ov != NULL);
34 }
35
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
38 {
39 string_output_visitor_cleanup(data->sov);
40 data->sov = NULL;
41 data->ov = NULL;
42 }
43
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
46 {
47 int64_t value = -42;
48 Error *errp = NULL;
49 char *str;
50
51 visit_type_int(data->ov, &value, NULL, &errp);
52 g_assert(error_is_set(&errp) == 0);
53
54 str = string_output_get_string(data->sov);
55 g_assert(str != NULL);
56 g_assert_cmpstr(str, ==, "-42");
57 g_free(str);
58 }
59
60 static void test_visitor_out_bool(TestOutputVisitorData *data,
61 const void *unused)
62 {
63 Error *errp = NULL;
64 bool value = true;
65 char *str;
66
67 visit_type_bool(data->ov, &value, NULL, &errp);
68 g_assert(error_is_set(&errp) == 0);
69
70 str = string_output_get_string(data->sov);
71 g_assert(str != NULL);
72 g_assert_cmpstr(str, ==, "true");
73 g_free(str);
74 }
75
76 static void test_visitor_out_number(TestOutputVisitorData *data,
77 const void *unused)
78 {
79 double value = 3.14;
80 Error *errp = NULL;
81 char *str;
82
83 visit_type_number(data->ov, &value, NULL, &errp);
84 g_assert(error_is_set(&errp) == 0);
85
86 str = string_output_get_string(data->sov);
87 g_assert(str != NULL);
88 g_assert_cmpstr(str, ==, "3.140000");
89 g_free(str);
90 }
91
92 static void test_visitor_out_string(TestOutputVisitorData *data,
93 const void *unused)
94 {
95 char *string = (char *) "Q E M U";
96 Error *errp = NULL;
97 char *str;
98
99 visit_type_str(data->ov, &string, NULL, &errp);
100 g_assert(error_is_set(&errp) == 0);
101
102 str = string_output_get_string(data->sov);
103 g_assert(str != NULL);
104 g_assert_cmpstr(str, ==, string);
105 g_free(str);
106 }
107
108 static void test_visitor_out_no_string(TestOutputVisitorData *data,
109 const void *unused)
110 {
111 char *string = NULL;
112 Error *errp = NULL;
113 char *str;
114
115 /* A null string should return "" */
116 visit_type_str(data->ov, &string, NULL, &errp);
117 g_assert(error_is_set(&errp) == 0);
118
119 str = string_output_get_string(data->sov);
120 g_assert(str != NULL);
121 g_assert_cmpstr(str, ==, "");
122 g_free(str);
123 }
124
125 static void test_visitor_out_enum(TestOutputVisitorData *data,
126 const void *unused)
127 {
128 Error *errp = NULL;
129 char *str;
130 EnumOne i;
131
132 for (i = 0; i < ENUM_ONE_MAX; i++) {
133 visit_type_EnumOne(data->ov, &i, "unused", &errp);
134 g_assert(!error_is_set(&errp));
135
136 str = string_output_get_string(data->sov);
137 g_assert(str != NULL);
138 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
139 g_free(str);
140 }
141 }
142
143 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
144 const void *unused)
145 {
146 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
147 Error *errp;
148
149 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
150 errp = NULL;
151 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
152 g_assert(error_is_set(&errp) == true);
153 error_free(errp);
154 }
155 }
156
157 static void output_visitor_test_add(const char *testpath,
158 TestOutputVisitorData *data,
159 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
160 {
161 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
162 test_func, visitor_output_teardown);
163 }
164
165 int main(int argc, char **argv)
166 {
167 TestOutputVisitorData out_visitor_data;
168
169 g_test_init(&argc, &argv, NULL);
170
171 output_visitor_test_add("/string-visitor/output/int",
172 &out_visitor_data, test_visitor_out_int);
173 output_visitor_test_add("/string-visitor/output/bool",
174 &out_visitor_data, test_visitor_out_bool);
175 output_visitor_test_add("/string-visitor/output/number",
176 &out_visitor_data, test_visitor_out_number);
177 output_visitor_test_add("/string-visitor/output/string",
178 &out_visitor_data, test_visitor_out_string);
179 output_visitor_test_add("/string-visitor/output/no-string",
180 &out_visitor_data, test_visitor_out_no_string);
181 output_visitor_test_add("/string-visitor/output/enum",
182 &out_visitor_data, test_visitor_out_enum);
183 output_visitor_test_add("/string-visitor/output/enum-errors",
184 &out_visitor_data, test_visitor_out_enum_errors);
185
186 g_test_run();
187
188 return 0;
189 }