test-qobject-output-visitor.c 28.4 KB
Newer Older
1
/*
2
 * QObject Output Visitor unit-tests.
3
 *
4
 * Copyright (C) 2011-2016 Red Hat Inc.
5 6 7 8 9 10 11 12
 *
 * Authors:
 *  Luiz Capitulino <lcapitulino@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14

15
#include "qemu-common.h"
16
#include "qapi/error.h"
17
#include "qapi/qobject-output-visitor.h"
18 19
#include "test-qapi-types.h"
#include "test-qapi-visit.h"
20
#include "qapi/qmp/types.h"
E
Eric Blake 已提交
21
#include "qapi/qmp/qjson.h"
22 23 24

typedef struct TestOutputVisitorData {
    Visitor *ov;
25
    QObject *obj;
26 27 28 29 30
} TestOutputVisitorData;

static void visitor_output_setup(TestOutputVisitorData *data,
                                 const void *unused)
{
31
    data->ov = qobject_output_visitor_new(&data->obj);
32
    g_assert(data->ov);
33 34 35 36 37
}

static void visitor_output_teardown(TestOutputVisitorData *data,
                                    const void *unused)
{
38
    visit_free(data->ov);
39
    data->ov = NULL;
40 41 42 43 44 45
    qobject_decref(data->obj);
    data->obj = NULL;
}

static QObject *visitor_get(TestOutputVisitorData *data)
{
46
    visit_complete(data->ov, &data->obj);
47 48
    g_assert(data->obj);
    return data->obj;
49 50
}

51 52 53 54 55 56
static void visitor_reset(TestOutputVisitorData *data)
{
    visitor_output_teardown(data, NULL);
    visitor_output_setup(data, NULL);
}

57 58 59 60
static void test_visitor_out_int(TestOutputVisitorData *data,
                                 const void *unused)
{
    int64_t value = -42;
61
    QInt *qint;
62

63
    visit_type_int(data->ov, NULL, &value, &error_abort);
64

65 66 67
    qint = qobject_to_qint(visitor_get(data));
    g_assert(qint);
    g_assert_cmpint(qint_get_int(qint), ==, value);
68 69 70 71 72 73 74 75
}

static void test_visitor_out_bool(TestOutputVisitorData *data,
                                  const void *unused)
{
    bool value = true;
    QObject *obj;

76
    visit_type_bool(data->ov, NULL, &value, &error_abort);
77

78
    obj = visitor_get(data);
79
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
E
Eric Blake 已提交
80
    g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
81 82 83 84 85 86
}

static void test_visitor_out_number(TestOutputVisitorData *data,
                                    const void *unused)
{
    double value = 3.14;
87
    QFloat *qfloat;
88

89
    visit_type_number(data->ov, NULL, &value, &error_abort);
90

91 92 93
    qfloat = qobject_to_qfloat(visitor_get(data));
    g_assert(qfloat);
    g_assert(qfloat_get_double(qfloat) == value);
94 95 96 97 98 99
}

static void test_visitor_out_string(TestOutputVisitorData *data,
                                    const void *unused)
{
    char *string = (char *) "Q E M U";
100
    QString *qstr;
101

102
    visit_type_str(data->ov, NULL, &string, &error_abort);
103

104 105 106
    qstr = qobject_to_qstring(visitor_get(data));
    g_assert(qstr);
    g_assert_cmpstr(qstring_get_str(qstr), ==, string);
107 108 109 110 111 112
}

static void test_visitor_out_no_string(TestOutputVisitorData *data,
                                       const void *unused)
{
    char *string = NULL;
113
    QString *qstr;
114 115

    /* A null string should return "" */
116
    visit_type_str(data->ov, NULL, &string, &error_abort);
117

118 119 120
    qstr = qobject_to_qstring(visitor_get(data));
    g_assert(qstr);
    g_assert_cmpstr(qstring_get_str(qstr), ==, "");
121 122 123 124 125 126
}

static void test_visitor_out_enum(TestOutputVisitorData *data,
                                  const void *unused)
{
    EnumOne i;
127
    QString *qstr;
128

129
    for (i = 0; i < ENUM_ONE__MAX; i++) {
130
        visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
131

132 133 134
        qstr = qobject_to_qstring(visitor_get(data));
        g_assert(qstr);
        g_assert_cmpstr(qstring_get_str(qstr), ==, EnumOne_lookup[i]);
135
        visitor_reset(data);
136 137 138 139 140 141
    }
}

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

    for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
146
        err = NULL;
147
        visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
148 149
        g_assert(err);
        error_free(err);
150
        visitor_reset(data);
151 152 153 154 155 156 157 158 159 160 161 162 163
    }
}


static void test_visitor_out_struct(TestOutputVisitorData *data,
                                    const void *unused)
{
    TestStruct test_struct = { .integer = 42,
                               .boolean = false,
                               .string = (char *) "foo"};
    TestStruct *p = &test_struct;
    QDict *qdict;

164
    visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
165

166 167
    qdict = qobject_to_qdict(visitor_get(data));
    g_assert(qdict);
168 169
    g_assert_cmpint(qdict_size(qdict), ==, 3);
    g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
E
Eric Blake 已提交
170
    g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
171 172 173 174 175 176 177
    g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
}

static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
                                           const void *unused)
{
    int64_t value = 42;
178
    UserDefTwo *ud2;
179 180
    QDict *qdict, *dict1, *dict2, *dict3, *userdef;
    const char *string = "user def string";
S
Stefan Weil 已提交
181 182
    const char *strings[] = { "forty two", "forty three", "forty four",
                              "forty five" };
183 184 185 186

    ud2 = g_malloc0(sizeof(*ud2));
    ud2->string0 = g_strdup(strings[0]);

187 188 189 190 191 192
    ud2->dict1 = g_malloc0(sizeof(*ud2->dict1));
    ud2->dict1->string1 = g_strdup(strings[1]);

    ud2->dict1->dict2 = g_malloc0(sizeof(*ud2->dict1->dict2));
    ud2->dict1->dict2->userdef = g_new0(UserDefOne, 1);
    ud2->dict1->dict2->userdef->string = g_strdup(string);
E
Eric Blake 已提交
193
    ud2->dict1->dict2->userdef->integer = value;
194 195 196 197 198 199
    ud2->dict1->dict2->string = g_strdup(strings[2]);

    ud2->dict1->dict3 = g_malloc0(sizeof(*ud2->dict1->dict3));
    ud2->dict1->has_dict3 = true;
    ud2->dict1->dict3->userdef = g_new0(UserDefOne, 1);
    ud2->dict1->dict3->userdef->string = g_strdup(string);
E
Eric Blake 已提交
200
    ud2->dict1->dict3->userdef->integer = value;
201
    ud2->dict1->dict3->string = g_strdup(strings[3]);
202

203
    visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
204

205 206
    qdict = qobject_to_qdict(visitor_get(data));
    g_assert(qdict);
207 208 209 210 211 212 213 214 215
    g_assert_cmpint(qdict_size(qdict), ==, 2);
    g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);

    dict1 = qdict_get_qdict(qdict, "dict1");
    g_assert_cmpint(qdict_size(dict1), ==, 3);
    g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);

    dict2 = qdict_get_qdict(dict1, "dict2");
    g_assert_cmpint(qdict_size(dict2), ==, 2);
216 217
    g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
    userdef = qdict_get_qdict(dict2, "userdef");
218 219 220 221 222 223
    g_assert_cmpint(qdict_size(userdef), ==, 2);
    g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
    g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

    dict3 = qdict_get_qdict(dict1, "dict3");
    g_assert_cmpint(qdict_size(dict3), ==, 2);
224 225
    g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
    userdef = qdict_get_qdict(dict3, "userdef");
226 227 228 229
    g_assert_cmpint(qdict_size(userdef), ==, 2);
    g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
    g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);

230
    qapi_free_UserDefTwo(ud2);
231 232
}

233 234 235
static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
                                           const void *unused)
{
236
    EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
E
Eric Blake 已提交
237 238
    UserDefOne u = {0};
    UserDefOne *pu = &u;
239
    Error *err;
240 241 242
    int i;

    for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
243
        err = NULL;
244 245
        u.has_enum1 = true;
        u.enum1 = bad_values[i];
246
        visit_type_UserDefOne(data->ov, "unused", &pu, &err);
247 248
        g_assert(err);
        error_free(err);
249
        visitor_reset(data);
250 251 252
    }
}

253 254 255 256

static void test_visitor_out_list(TestOutputVisitorData *data,
                                  const void *unused)
{
257
    const char *value_str = "list value";
258 259 260 261 262 263 264 265
    TestStructList *p, *head = NULL;
    const int max_items = 10;
    bool value_bool = true;
    int value_int = 10;
    QListEntry *entry;
    QList *qlist;
    int i;

266
    /* Build the list in reverse order... */
267 268 269
    for (i = 0; i < max_items; i++) {
        p = g_malloc0(sizeof(*p));
        p->value = g_malloc0(sizeof(*p->value));
270
        p->value->integer = value_int + (max_items - i - 1);
271
        p->value->boolean = value_bool;
272
        p->value->string = g_strdup(value_str);
273 274 275 276 277

        p->next = head;
        head = p;
    }

278
    visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
279

280 281
    qlist = qobject_to_qlist(visitor_get(data));
    g_assert(qlist);
282 283
    g_assert(!qlist_empty(qlist));

284
    /* ...and ensure that the visitor sees it in order */
285 286 287 288 289
    i = 0;
    QLIST_FOREACH_ENTRY(qlist, entry) {
        QDict *qdict;

        qdict = qobject_to_qdict(entry->value);
290
        g_assert(qdict);
291
        g_assert_cmpint(qdict_size(qdict), ==, 3);
292
        g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
293 294 295 296 297 298
        g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
        g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
        i++;
    }
    g_assert_cmpint(i, ==, max_items);

299
    qapi_free_TestStructList(head);
300 301 302 303 304
}

static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
                                            const void *unused)
{
305
    UserDefTwoList *p, *head = NULL;
306 307 308 309 310 311 312 313
    const char string[] = "foo bar";
    int i, max_count = 1024;

    for (i = 0; i < max_count; i++) {
        p = g_malloc0(sizeof(*p));
        p->value = g_malloc0(sizeof(*p->value));

        p->value->string0 = g_strdup(string);
314 315 316 317 318
        p->value->dict1 = g_new0(UserDefTwoDict, 1);
        p->value->dict1->string1 = g_strdup(string);
        p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
        p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1);
        p->value->dict1->dict2->userdef->string = g_strdup(string);
E
Eric Blake 已提交
319
        p->value->dict1->dict2->userdef->integer = 42;
320 321
        p->value->dict1->dict2->string = g_strdup(string);
        p->value->dict1->has_dict3 = false;
322 323 324 325 326

        p->next = head;
        head = p;
    }

327
    qapi_free_UserDefTwoList(head);
328 329
}

330 331 332 333 334 335 336 337 338 339
static void test_visitor_out_any(TestOutputVisitorData *data,
                                 const void *unused)
{
    QObject *qobj;
    QInt *qint;
    QBool *qbool;
    QString *qstring;
    QDict *qdict;

    qobj = QOBJECT(qint_from_int(-42));
340
    visit_type_any(data->ov, NULL, &qobj, &error_abort);
341 342 343
    qint = qobject_to_qint(visitor_get(data));
    g_assert(qint);
    g_assert_cmpint(qint_get_int(qint), ==, -42);
344 345
    qobject_decref(qobj);

346
    visitor_reset(data);
347 348 349 350 351
    qdict = qdict_new();
    qdict_put(qdict, "integer", qint_from_int(-42));
    qdict_put(qdict, "boolean", qbool_from_bool(true));
    qdict_put(qdict, "string", qstring_from_str("foo"));
    qobj = QOBJECT(qdict);
352
    visit_type_any(data->ov, NULL, &qobj, &error_abort);
E
Eric Blake 已提交
353
    qobject_decref(qobj);
354
    qdict = qobject_to_qdict(visitor_get(data));
355
    g_assert(qdict);
356
    qint = qobject_to_qint(qdict_get(qdict, "integer"));
357 358 359 360 361 362 363
    g_assert(qint);
    g_assert_cmpint(qint_get_int(qint), ==, -42);
    qobj = qdict_get(qdict, "boolean");
    g_assert(qobj);
    qbool = qobject_to_qbool(qobj);
    g_assert(qbool);
    g_assert(qbool_get_bool(qbool) == true);
364
    qstring = qobject_to_qstring(qdict_get(qdict, "string"));
365 366 367 368
    g_assert(qstring);
    g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
}

369 370 371 372 373 374
static void test_visitor_out_union_flat(TestOutputVisitorData *data,
                                        const void *unused)
{
    QDict *qdict;

    UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
375
    tmp->enum1 = ENUM_ONE_VALUE1;
376
    tmp->string = g_strdup("str");
377
    tmp->integer = 41;
378
    tmp->u.value1.boolean = true;
379

380
    visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
381 382
    qdict = qobject_to_qdict(visitor_get(data));
    g_assert(qdict);
383 384
    g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
    g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
385
    g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
386 387 388 389 390
    g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);

    qapi_free_UserDefFlatUnion(tmp);
}

391 392
static void test_visitor_out_alternate(TestOutputVisitorData *data,
                                       const void *unused)
393
{
E
Eric Blake 已提交
394
    UserDefAlternate *tmp;
395
    QInt *qint;
396
    QString *qstr;
397
    QDict *qdict;
398

E
Eric Blake 已提交
399
    tmp = g_new0(UserDefAlternate, 1);
400
    tmp->type = QTYPE_QINT;
401
    tmp->u.i = 42;
402

403
    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
404 405 406
    qint = qobject_to_qint(visitor_get(data));
    g_assert(qint);
    g_assert_cmpint(qint_get_int(qint), ==, 42);
407

408
    qapi_free_UserDefAlternate(tmp);
E
Eric Blake 已提交
409

410
    visitor_reset(data);
E
Eric Blake 已提交
411
    tmp = g_new0(UserDefAlternate, 1);
412
    tmp->type = QTYPE_QSTRING;
E
Eric Blake 已提交
413 414
    tmp->u.s = g_strdup("hello");

415
    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
416 417 418
    qstr = qobject_to_qstring(visitor_get(data));
    g_assert(qstr);
    g_assert_cmpstr(qstring_get_str(qstr), ==, "hello");
E
Eric Blake 已提交
419 420

    qapi_free_UserDefAlternate(tmp);
421

422
    visitor_reset(data);
423 424
    tmp = g_new0(UserDefAlternate, 1);
    tmp->type = QTYPE_QDICT;
425 426 427
    tmp->u.udfu.integer = 1;
    tmp->u.udfu.string = g_strdup("str");
    tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
428
    tmp->u.udfu.u.value1.boolean = true;
429 430

    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
431 432
    qdict = qobject_to_qdict(visitor_get(data));
    g_assert(qdict);
433 434 435 436 437 438 439
    g_assert_cmpint(qdict_size(qdict), ==, 4);
    g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 1);
    g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
    g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
    g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);

    qapi_free_UserDefAlternate(tmp);
440 441
}

442 443
static void test_visitor_out_null(TestOutputVisitorData *data,
                                  const void *unused)
444
{
445 446
    QDict *qdict;
    QObject *nil;
447

448 449
    visit_start_struct(data->ov, NULL, NULL, 0, &error_abort);
    visit_type_null(data->ov, "a", &error_abort);
450
    visit_check_struct(data->ov, &error_abort);
E
Eric Blake 已提交
451
    visit_end_struct(data->ov, NULL);
452 453
    qdict = qobject_to_qdict(visitor_get(data));
    g_assert(qdict);
454 455 456 457
    g_assert_cmpint(qdict_size(qdict), ==, 1);
    nil = qdict_get(qdict, "a");
    g_assert(nil);
    g_assert(qobject_type(nil) == QTYPE_QNULL);
458 459
}

460 461 462
static void init_native_list(UserDefNativeListUnion *cvalue)
{
    int i;
463
    switch (cvalue->type) {
464
    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
465
        intList **list = &cvalue->u.integer.data;
466 467 468 469 470 471 472 473 474
        for (i = 0; i < 32; i++) {
            *list = g_new0(intList, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
475
        int8List **list = &cvalue->u.s8.data;
476 477 478 479 480 481 482 483 484
        for (i = 0; i < 32; i++) {
            *list = g_new0(int8List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
485
        int16List **list = &cvalue->u.s16.data;
486 487 488 489 490 491 492 493 494
        for (i = 0; i < 32; i++) {
            *list = g_new0(int16List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
495
        int32List **list = &cvalue->u.s32.data;
496 497 498 499 500 501 502 503 504
        for (i = 0; i < 32; i++) {
            *list = g_new0(int32List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
505
        int64List **list = &cvalue->u.s64.data;
506 507 508 509 510 511 512 513 514
        for (i = 0; i < 32; i++) {
            *list = g_new0(int64List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
515
        uint8List **list = &cvalue->u.u8.data;
516 517 518 519 520 521 522 523 524
        for (i = 0; i < 32; i++) {
            *list = g_new0(uint8List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
525
        uint16List **list = &cvalue->u.u16.data;
526 527 528 529 530 531 532 533 534
        for (i = 0; i < 32; i++) {
            *list = g_new0(uint16List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
535
        uint32List **list = &cvalue->u.u32.data;
536 537 538 539 540 541 542 543 544
        for (i = 0; i < 32; i++) {
            *list = g_new0(uint32List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
545
        uint64List **list = &cvalue->u.u64.data;
546 547 548 549 550 551 552 553 554
        for (i = 0; i < 32; i++) {
            *list = g_new0(uint64List, 1);
            (*list)->value = i;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
555
        boolList **list = &cvalue->u.boolean.data;
556 557 558 559 560 561 562 563 564
        for (i = 0; i < 32; i++) {
            *list = g_new0(boolList, 1);
            (*list)->value = (i % 3 == 0);
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
565
        strList **list = &cvalue->u.string.data;
566 567 568 569 570 571 572 573 574
        for (i = 0; i < 32; i++) {
            *list = g_new0(strList, 1);
            (*list)->value = g_strdup_printf("%d", i);
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
575
        numberList **list = &cvalue->u.number.data;
576 577 578 579 580 581 582 583 584
        for (i = 0; i < 32; i++) {
            *list = g_new0(numberList, 1);
            (*list)->value = (double)i / 3;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    default:
585
        g_assert_not_reached();
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    }
}

static void check_native_list(QObject *qobj,
                              UserDefNativeListUnionKind kind)
{
    QDict *qdict;
    QList *qlist;
    int i;

    qdict = qobject_to_qdict(qobj);
    g_assert(qdict);
    g_assert(qdict_haskey(qdict, "data"));
    qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));

    switch (kind) {
    case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
    case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
    case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
    case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
    case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
    case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
    case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
    case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
        /* all integer elements in JSON arrays get stored into QInts when
         * we convert to QObjects, so we can check them all in the same
         * fashion, so simply fall through here
         */
    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
        for (i = 0; i < 32; i++) {
            QObject *tmp;
            QInt *qvalue;
            tmp = qlist_peek(qlist);
            g_assert(tmp);
            qvalue = qobject_to_qint(tmp);
            g_assert_cmpint(qint_get_int(qvalue), ==, i);
            qobject_decref(qlist_pop(qlist));
        }
        break;
    case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
        for (i = 0; i < 32; i++) {
            QObject *tmp;
            QBool *qvalue;
            tmp = qlist_peek(qlist);
            g_assert(tmp);
            qvalue = qobject_to_qbool(tmp);
E
Eric Blake 已提交
632
            g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
            qobject_decref(qlist_pop(qlist));
        }
        break;
    case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
        for (i = 0; i < 32; i++) {
            QObject *tmp;
            QString *qvalue;
            gchar str[8];
            tmp = qlist_peek(qlist);
            g_assert(tmp);
            qvalue = qobject_to_qstring(tmp);
            sprintf(str, "%d", i);
            g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
            qobject_decref(qlist_pop(qlist));
        }
        break;
    case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
        for (i = 0; i < 32; i++) {
            QObject *tmp;
            QFloat *qvalue;
            GString *double_expected = g_string_new("");
            GString *double_actual = g_string_new("");

            tmp = qlist_peek(qlist);
            g_assert(tmp);
            qvalue = qobject_to_qfloat(tmp);
            g_string_printf(double_expected, "%.6f", (double)i / 3);
            g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
            g_assert_cmpstr(double_actual->str, ==, double_expected->str);

            qobject_decref(qlist_pop(qlist));
            g_string_free(double_expected, true);
            g_string_free(double_actual, true);
        }
        break;
    default:
669
        g_assert_not_reached();
670 671 672 673 674 675 676 677 678 679 680
    }
    QDECREF(qlist);
}

static void test_native_list(TestOutputVisitorData *data,
                             const void *unused,
                             UserDefNativeListUnionKind kind)
{
    UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
    QObject *obj;

681
    cvalue->type = kind;
682 683
    init_native_list(cvalue);

684
    visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
685

686
    obj = visitor_get(data);
687
    check_native_list(obj, cvalue->type);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    qapi_free_UserDefNativeListUnion(cvalue);
}

static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
                                             const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
}

static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
                                              const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
}

static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
                                               const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
}

static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
                                               const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
}

static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
                                               const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
}

static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
                                               const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
}

static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
                                                const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
}

static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
                                                const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
}

static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
                                                const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
}

static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
                                              const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
}

static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
                                              const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
}

static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
                                                const void *unused)
{
    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
}

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
static void output_visitor_test_add(const char *testpath,
                                    TestOutputVisitorData *data,
                                    void (*test_func)(TestOutputVisitorData *data, const void *user_data))
{
    g_test_add(testpath, TestOutputVisitorData, data, 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("/visitor/output/int",
                            &out_visitor_data, test_visitor_out_int);
    output_visitor_test_add("/visitor/output/bool",
                            &out_visitor_data, test_visitor_out_bool);
    output_visitor_test_add("/visitor/output/number",
                            &out_visitor_data, test_visitor_out_number);
    output_visitor_test_add("/visitor/output/string",
                            &out_visitor_data, test_visitor_out_string);
    output_visitor_test_add("/visitor/output/no-string",
                            &out_visitor_data, test_visitor_out_no_string);
    output_visitor_test_add("/visitor/output/enum",
                            &out_visitor_data, test_visitor_out_enum);
    output_visitor_test_add("/visitor/output/enum-errors",
                            &out_visitor_data, test_visitor_out_enum_errors);
    output_visitor_test_add("/visitor/output/struct",
                            &out_visitor_data, test_visitor_out_struct);
    output_visitor_test_add("/visitor/output/struct-nested",
                            &out_visitor_data, test_visitor_out_struct_nested);
795 796
    output_visitor_test_add("/visitor/output/struct-errors",
                            &out_visitor_data, test_visitor_out_struct_errors);
797 798
    output_visitor_test_add("/visitor/output/list",
                            &out_visitor_data, test_visitor_out_list);
799 800
    output_visitor_test_add("/visitor/output/any",
                            &out_visitor_data, test_visitor_out_any);
801 802
    output_visitor_test_add("/visitor/output/list-qapi-free",
                            &out_visitor_data, test_visitor_out_list_qapi_free);
803 804
    output_visitor_test_add("/visitor/output/union-flat",
                            &out_visitor_data, test_visitor_out_union_flat);
805 806
    output_visitor_test_add("/visitor/output/alternate",
                            &out_visitor_data, test_visitor_out_alternate);
807 808
    output_visitor_test_add("/visitor/output/null",
                            &out_visitor_data, test_visitor_out_null);
809
    output_visitor_test_add("/visitor/output/native_list/int",
810 811
                            &out_visitor_data,
                            test_visitor_out_native_list_int);
812
    output_visitor_test_add("/visitor/output/native_list/int8",
813 814
                            &out_visitor_data,
                            test_visitor_out_native_list_int8);
815
    output_visitor_test_add("/visitor/output/native_list/int16",
816 817
                            &out_visitor_data,
                            test_visitor_out_native_list_int16);
818
    output_visitor_test_add("/visitor/output/native_list/int32",
819 820
                            &out_visitor_data,
                            test_visitor_out_native_list_int32);
821
    output_visitor_test_add("/visitor/output/native_list/int64",
822 823
                            &out_visitor_data,
                            test_visitor_out_native_list_int64);
824
    output_visitor_test_add("/visitor/output/native_list/uint8",
825 826
                            &out_visitor_data,
                            test_visitor_out_native_list_uint8);
827
    output_visitor_test_add("/visitor/output/native_list/uint16",
828 829
                            &out_visitor_data,
                            test_visitor_out_native_list_uint16);
830
    output_visitor_test_add("/visitor/output/native_list/uint32",
831 832
                            &out_visitor_data,
                            test_visitor_out_native_list_uint32);
833
    output_visitor_test_add("/visitor/output/native_list/uint64",
834 835
                            &out_visitor_data,
                            test_visitor_out_native_list_uint64);
836
    output_visitor_test_add("/visitor/output/native_list/bool",
837 838
                            &out_visitor_data,
                            test_visitor_out_native_list_bool);
839
    output_visitor_test_add("/visitor/output/native_list/string",
840 841
                            &out_visitor_data,
                            test_visitor_out_native_list_str);
842
    output_visitor_test_add("/visitor/output/native_list/number",
843 844
                            &out_visitor_data,
                            test_visitor_out_native_list_number);
845 846 847 848 849

    g_test_run();

    return 0;
}