net: xilinx_ethlite: Don't reset from init
[qemu/rayw.git] / tests / test-string-output-visitor.c
blob2af5a21ab5cc3b645aeb95490fbe06eaa58c0df4
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 "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"
21 typedef struct TestOutputVisitorData {
22 StringOutputVisitor *sov;
23 Visitor *ov;
24 } TestOutputVisitorData;
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
29 data->sov = string_output_visitor_new(false);
30 g_assert(data->sov != NULL);
32 data->ov = string_output_get_visitor(data->sov);
33 g_assert(data->ov != NULL);
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
39 string_output_visitor_cleanup(data->sov);
40 data->sov = NULL;
41 data->ov = NULL;
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
47 int64_t value = -42;
48 Error *err = NULL;
49 char *str;
51 visit_type_int(data->ov, &value, NULL, &err);
52 g_assert(!err);
54 str = string_output_get_string(data->sov);
55 g_assert(str != NULL);
56 g_assert_cmpstr(str, ==, "-42");
57 g_free(str);
60 static void test_visitor_out_bool(TestOutputVisitorData *data,
61 const void *unused)
63 Error *err = NULL;
64 bool value = true;
65 char *str;
67 visit_type_bool(data->ov, &value, NULL, &err);
68 g_assert(!err);
70 str = string_output_get_string(data->sov);
71 g_assert(str != NULL);
72 g_assert_cmpstr(str, ==, "true");
73 g_free(str);
76 static void test_visitor_out_number(TestOutputVisitorData *data,
77 const void *unused)
79 double value = 3.14;
80 Error *err = NULL;
81 char *str;
83 visit_type_number(data->ov, &value, NULL, &err);
84 g_assert(!err);
86 str = string_output_get_string(data->sov);
87 g_assert(str != NULL);
88 g_assert_cmpstr(str, ==, "3.140000");
89 g_free(str);
92 static void test_visitor_out_string(TestOutputVisitorData *data,
93 const void *unused)
95 char *string = (char *) "Q E M U";
96 Error *err = NULL;
97 char *str;
99 visit_type_str(data->ov, &string, NULL, &err);
100 g_assert(!err);
102 str = string_output_get_string(data->sov);
103 g_assert(str != NULL);
104 g_assert_cmpstr(str, ==, string);
105 g_free(str);
108 static void test_visitor_out_no_string(TestOutputVisitorData *data,
109 const void *unused)
111 char *string = NULL;
112 Error *err = NULL;
113 char *str;
115 /* A null string should return "" */
116 visit_type_str(data->ov, &string, NULL, &err);
117 g_assert(!err);
119 str = string_output_get_string(data->sov);
120 g_assert(str != NULL);
121 g_assert_cmpstr(str, ==, "");
122 g_free(str);
125 static void test_visitor_out_enum(TestOutputVisitorData *data,
126 const void *unused)
128 Error *err = NULL;
129 char *str;
130 EnumOne i;
132 for (i = 0; i < ENUM_ONE_MAX; i++) {
133 visit_type_EnumOne(data->ov, &i, "unused", &err);
134 g_assert(!err);
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);
143 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
144 const void *unused)
146 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
147 Error *err;
149 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
150 err = NULL;
151 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
152 g_assert(err);
153 error_free(err);
157 static void output_visitor_test_add(const char *testpath,
158 TestOutputVisitorData *data,
159 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
161 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
162 test_func, visitor_output_teardown);
165 int main(int argc, char **argv)
167 TestOutputVisitorData out_visitor_data;
169 g_test_init(&argc, &argv, NULL);
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);
186 g_test_run();
188 return 0;