tcg/arm: Factor out code to emit immediate or reg-reg op
[qemu/pbrook.git] / tests / test-string-output-visitor.c
blob608f14a5deb313edde6977af9cc876b9e407ffe2
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-qmp-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 <glib.h>
15 #include "qapi/string-output-visitor.h"
16 #include "test-qapi-types.h"
17 #include "test-qapi-visit.h"
18 #include "qemu-objects.h"
20 typedef struct TestOutputVisitorData {
21 StringOutputVisitor *sov;
22 Visitor *ov;
23 } TestOutputVisitorData;
25 static void visitor_output_setup(TestOutputVisitorData *data,
26 const void *unused)
28 data->sov = string_output_visitor_new();
29 g_assert(data->sov != NULL);
31 data->ov = string_output_get_visitor(data->sov);
32 g_assert(data->ov != NULL);
35 static void visitor_output_teardown(TestOutputVisitorData *data,
36 const void *unused)
38 string_output_visitor_cleanup(data->sov);
39 data->sov = NULL;
40 data->ov = NULL;
43 static void test_visitor_out_int(TestOutputVisitorData *data,
44 const void *unused)
46 int64_t value = -42;
47 Error *errp = NULL;
48 char *str;
50 visit_type_int(data->ov, &value, NULL, &errp);
51 g_assert(error_is_set(&errp) == 0);
53 str = string_output_get_string(data->sov);
54 g_assert(str != NULL);
55 g_assert_cmpstr(str, ==, "-42");
56 g_free(str);
59 static void test_visitor_out_bool(TestOutputVisitorData *data,
60 const void *unused)
62 Error *errp = NULL;
63 bool value = true;
64 char *str;
66 visit_type_bool(data->ov, &value, NULL, &errp);
67 g_assert(error_is_set(&errp) == 0);
69 str = string_output_get_string(data->sov);
70 g_assert(str != NULL);
71 g_assert_cmpstr(str, ==, "true");
72 g_free(str);
75 static void test_visitor_out_number(TestOutputVisitorData *data,
76 const void *unused)
78 double value = 3.14;
79 Error *errp = NULL;
80 char *str;
82 visit_type_number(data->ov, &value, NULL, &errp);
83 g_assert(error_is_set(&errp) == 0);
85 str = string_output_get_string(data->sov);
86 g_assert(str != NULL);
87 g_assert_cmpstr(str, ==, "3.140000");
88 g_free(str);
91 static void test_visitor_out_string(TestOutputVisitorData *data,
92 const void *unused)
94 char *string = (char *) "Q E M U";
95 Error *errp = NULL;
96 char *str;
98 visit_type_str(data->ov, &string, NULL, &errp);
99 g_assert(error_is_set(&errp) == 0);
101 str = string_output_get_string(data->sov);
102 g_assert(str != NULL);
103 g_assert_cmpstr(str, ==, string);
104 g_free(str);
107 static void test_visitor_out_no_string(TestOutputVisitorData *data,
108 const void *unused)
110 char *string = NULL;
111 Error *errp = NULL;
112 char *str;
114 /* A null string should return "" */
115 visit_type_str(data->ov, &string, NULL, &errp);
116 g_assert(error_is_set(&errp) == 0);
118 str = string_output_get_string(data->sov);
119 g_assert(str != NULL);
120 g_assert_cmpstr(str, ==, "");
121 g_free(str);
124 static void test_visitor_out_enum(TestOutputVisitorData *data,
125 const void *unused)
127 Error *errp = NULL;
128 char *str;
129 EnumOne i;
131 for (i = 0; i < ENUM_ONE_MAX; i++) {
132 visit_type_EnumOne(data->ov, &i, "unused", &errp);
133 g_assert(!error_is_set(&errp));
135 str = string_output_get_string(data->sov);
136 g_assert(str != NULL);
137 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
138 g_free(str);
142 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
143 const void *unused)
145 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
146 Error *errp;
148 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
149 errp = NULL;
150 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
151 g_assert(error_is_set(&errp) == true);
152 error_free(errp);
156 static void output_visitor_test_add(const char *testpath,
157 TestOutputVisitorData *data,
158 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
160 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
161 test_func, visitor_output_teardown);
164 int main(int argc, char **argv)
166 TestOutputVisitorData out_visitor_data;
168 g_test_init(&argc, &argv, NULL);
170 output_visitor_test_add("/string-visitor/output/int",
171 &out_visitor_data, test_visitor_out_int);
172 output_visitor_test_add("/string-visitor/output/bool",
173 &out_visitor_data, test_visitor_out_bool);
174 output_visitor_test_add("/string-visitor/output/number",
175 &out_visitor_data, test_visitor_out_number);
176 output_visitor_test_add("/string-visitor/output/string",
177 &out_visitor_data, test_visitor_out_string);
178 output_visitor_test_add("/string-visitor/output/no-string",
179 &out_visitor_data, test_visitor_out_no_string);
180 output_visitor_test_add("/string-visitor/output/enum",
181 &out_visitor_data, test_visitor_out_enum);
182 output_visitor_test_add("/string-visitor/output/enum-errors",
183 &out_visitor_data, test_visitor_out_enum_errors);
185 g_test_run();
187 return 0;