aboutsummaryrefslogtreecommitdiff
path: root/tests/test-string-output-visitor.c
blob: e89e43c3efb30d7d8371b376df3eff9c16611fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * String Output Visitor unit-tests.
 *
 * Copyright (C) 2012 Red Hat Inc.
 *
 * Authors:
 *  Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include <glib.h>

#include "qemu-common.h"
#include "qapi/string-output-visitor.h"
#include "test-qapi-types.h"
#include "test-qapi-visit.h"
#include "qapi/qmp/types.h"

typedef struct TestOutputVisitorData {
    StringOutputVisitor *sov;
    Visitor *ov;
    bool human;
} TestOutputVisitorData;

static void visitor_output_setup(TestOutputVisitorData *data,
                                 const void *unused)
{
    data->human = false;
    data->sov = string_output_visitor_new(data->human);
    g_assert(data->sov != NULL);

    data->ov = string_output_get_visitor(data->sov);
    g_assert(data->ov != NULL);
}

static void visitor_output_setup_human(TestOutputVisitorData *data,
                                       const void *unused)
{
    data->human = true;
    data->sov = string_output_visitor_new(data->human);
    g_assert(data->sov != NULL);

    data->ov = string_output_get_visitor(data->sov);
    g_assert(data->ov != NULL);
}

static void visitor_output_teardown(TestOutputVisitorData *data,
                                    const void *unused)
{
    string_output_visitor_cleanup(data->sov);
    data->sov = NULL;
    data->ov = NULL;
}

static void test_visitor_out_int(TestOutputVisitorData *data,
                                 const void *unused)
{
    int64_t value = 42;
    Error *err = NULL;
    char *str;

    visit_type_int(data->ov, &value, NULL, &err);
    g_assert(!err);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    if (data->human) {
        g_assert_cmpstr(str, ==, "42 (0x2a)");
    } else {
        g_assert_cmpstr(str, ==, "42");
    }
    g_free(str);
}

static void test_visitor_out_intList(TestOutputVisitorData *data,
                                     const void *unused)
{
    int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
        3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
    intList *list = NULL, **tmp = &list;
    int i;
    Error *errp = NULL;
    char *str;

    for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
        *tmp = g_malloc0(sizeof(**tmp));
        (*tmp)->value = value[i];
        tmp = &(*tmp)->next;
    }

    visit_type_intList(data->ov, &list, NULL, &errp);
    g_assert(errp == NULL);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    if (data->human) {
        g_assert_cmpstr(str, ==,
            "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
            "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
            "0x7ffffffffffffffe-0x7fffffffffffffff)");
    } else {
        g_assert_cmpstr(str, ==,
            "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
    }
    g_free(str);
    while (list) {
        intList *tmp2;
        tmp2 = list->next;
        g_free(list);
        list = tmp2;
    }
}

static void test_visitor_out_bool(TestOutputVisitorData *data,
                                  const void *unused)
{
    Error *err = NULL;
    bool value = true;
    char *str;

    visit_type_bool(data->ov, &value, NULL, &err);
    g_assert(!err);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    g_assert_cmpstr(str, ==, "true");
    g_free(str);
}

static void test_visitor_out_number(TestOutputVisitorData *data,
                                    const void *unused)
{
    double value = 3.14;
    Error *err = NULL;
    char *str;

    visit_type_number(data->ov, &value, NULL, &err);
    g_assert(!err);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    g_assert_cmpstr(str, ==, "3.140000");
    g_free(str);
}

static void test_visitor_out_string(TestOutputVisitorData *data,
                                    const void *unused)
{
    char *string = (char *) "Q E M U";
    const char *string_human = "\"Q E M U\"";
    Error *err = NULL;
    char *str;

    visit_type_str(data->ov, &string, NULL, &err);
    g_assert(!err);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    if (data->human) {
        g_assert_cmpstr(str, ==, string_human);
    } else {
        g_assert_cmpstr(str, ==, string);
    }
    g_free(str);
}

static void test_visitor_out_no_string(TestOutputVisitorData *data,
                                       const void *unused)
{
    char *string = NULL;
    Error *err = NULL;
    char *str;

    /* A null string should return "" */
    visit_type_str(data->ov, &string, NULL, &err);
    g_assert(!err);

    str = string_output_get_string(data->sov);
    g_assert(str != NULL);
    if (data->human) {
        g_assert_cmpstr(str, ==, "<null>");
    } else {
        g_assert_cmpstr(str, ==, "");
    }
    g_free(str);
}

static void test_visitor_out_enum(TestOutputVisitorData *data,
                                  const void *unused)
{
    Error *err = NULL;
    char *str;
    EnumOne i;

    for (i = 0; i < ENUM_ONE_MAX; i++) {
        char *str_human;
        int len;

        visit_type_EnumOne(data->ov, &i, "unused", &err);
        g_assert(!err);

        len = strlen(EnumOne_lookup[i]) + 2;
        str_human = g_malloc0(len);
        str_human[0] = '"';
        strncpy(str_human + 1, EnumOne_lookup[i], strlen(EnumOne_lookup[i]));
        str_human[len - 1] = '"';

        str = string_output_get_string(data->sov);
        g_assert(str != NULL);
        if (data->human) {
            g_assert_cmpstr(str, ==, str_human);
        } else {
            g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
        }
        g_free(str_human);
	g_free(str);
    }
}

static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
                                         const void *unused)
{
    EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
    Error *err;

    for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
        err = NULL;
        visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
        g_assert(err);
        error_free(err);
    }
}

static void
output_visitor_test_add(const char *testpath,
                        TestOutputVisitorData *data,
                        void (*test_func)(TestOutputVisitorData *data,
                                          const void *user_data),
                        bool human)
{
    g_test_add(testpath, TestOutputVisitorData, data,
               human ? visitor_output_setup_human : visitor_output_setup,
               test_func, visitor_output_teardown);
}

int main(int argc, char **argv)
{
    TestOutputVisitorData out_visitor_data;

    g_test_init(&argc, &argv, NULL);

    output_visitor_test_add("/string-visitor/output/int",
                            &out_visitor_data, test_visitor_out_int, false);
    output_visitor_test_add("/string-visitor/output/int",
                            &out_visitor_data, test_visitor_out_int, true);
    output_visitor_test_add("/string-visitor/output/bool",
                            &out_visitor_data, test_visitor_out_bool, false);
    output_visitor_test_add("/string-visitor/output/bool",
                            &out_visitor_data, test_visitor_out_bool, true);
    output_visitor_test_add("/string-visitor/output/number",
                            &out_visitor_data, test_visitor_out_number, false);
    output_visitor_test_add("/string-visitor/output/number",
                            &out_visitor_data, test_visitor_out_number, true);
    output_visitor_test_add("/string-visitor/output/string",
                            &out_visitor_data, test_visitor_out_string, false);
    output_visitor_test_add("/string-visitor/output/string",
                            &out_visitor_data, test_visitor_out_string, true);
    output_visitor_test_add("/string-visitor/output/no-string",
                            &out_visitor_data, test_visitor_out_no_string,
                            false);
    output_visitor_test_add("/string-visitor/output/no-string",
                            &out_visitor_data, test_visitor_out_no_string,
                            true);
    output_visitor_test_add("/string-visitor/output/enum",
                            &out_visitor_data, test_visitor_out_enum, false);
    output_visitor_test_add("/string-visitor/output/enum",
                            &out_visitor_data, test_visitor_out_enum, true);
    output_visitor_test_add("/string-visitor/output/enum-errors",
                            &out_visitor_data, test_visitor_out_enum_errors,
                            false);
    output_visitor_test_add("/string-visitor/output/enum-errors",
                            &out_visitor_data, test_visitor_out_enum_errors,
                            true);
    output_visitor_test_add("/string-visitor/output/intList",
                            &out_visitor_data, test_visitor_out_intList, false);
    output_visitor_test_add("/string-visitor/output/intList",
                            &out_visitor_data, test_visitor_out_intList, true);

    g_test_run();

    return 0;
}