test-qmp-output-visitor.c 29.4 KB
Newer Older
1 2 3
/*
 * QMP Output Visitor unit-tests.
 *
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 <glib.h>

16
#include "qemu-common.h"
17 18 19
#include "qapi/qmp-output-visitor.h"
#include "test-qapi-types.h"
#include "test-qapi-visit.h"
20
#include "qapi/qmp/types.h"
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

typedef struct TestOutputVisitorData {
    QmpOutputVisitor *qov;
    Visitor *ov;
} TestOutputVisitorData;

static void visitor_output_setup(TestOutputVisitorData *data,
                                 const void *unused)
{
    data->qov = qmp_output_visitor_new();
    g_assert(data->qov != NULL);

    data->ov = qmp_output_get_visitor(data->qov);
    g_assert(data->ov != NULL);
}

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

static void test_visitor_out_int(TestOutputVisitorData *data,
                                 const void *unused)
{
    int64_t value = -42;
    QObject *obj;

51
    visit_type_int(data->ov, NULL, &value, &error_abort);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
    g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);

    qobject_decref(obj);
}

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

67
    visit_type_bool(data->ov, NULL, &value, &error_abort);
68 69 70 71

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QBOOL);
E
Eric Blake 已提交
72
    g_assert(qbool_get_bool(qobject_to_qbool(obj)) == value);
73 74 75 76 77 78 79 80 81 82

    qobject_decref(obj);
}

static void test_visitor_out_number(TestOutputVisitorData *data,
                                    const void *unused)
{
    double value = 3.14;
    QObject *obj;

83
    visit_type_number(data->ov, NULL, &value, &error_abort);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QFLOAT);
    g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);

    qobject_decref(obj);
}

static void test_visitor_out_string(TestOutputVisitorData *data,
                                    const void *unused)
{
    char *string = (char *) "Q E M U";
    QObject *obj;

99
    visit_type_str(data->ov, NULL, &string, &error_abort);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QSTRING);
    g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);

    qobject_decref(obj);
}

static void test_visitor_out_no_string(TestOutputVisitorData *data,
                                       const void *unused)
{
    char *string = NULL;
    QObject *obj;

    /* A null string should return "" */
116
    visit_type_str(data->ov, NULL, &string, &error_abort);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QSTRING);
    g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");

    qobject_decref(obj);
}

static void test_visitor_out_enum(TestOutputVisitorData *data,
                                  const void *unused)
{
    QObject *obj;
    EnumOne i;

132
    for (i = 0; i < ENUM_ONE__MAX; i++) {
133
        visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
134 135 136 137 138 139 140 141 142 143 144 145 146

        obj = qmp_output_get_qobject(data->qov);
        g_assert(obj != NULL);
        g_assert(qobject_type(obj) == QTYPE_QSTRING);
        g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
                        EnumOne_lookup[i]);
        qobject_decref(obj);
    }
}

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

    for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
151
        err = NULL;
152
        visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
153 154
        g_assert(err);
        error_free(err);
155 156 157 158 159 160 161 162 163 164 165 166 167 168
    }
}


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;
    QObject *obj;
    QDict *qdict;

169
    visit_type_TestStruct(data->ov, NULL, &p, &error_abort);
170 171 172 173 174 175 176 177

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QDICT);

    qdict = qobject_to_qdict(obj);
    g_assert_cmpint(qdict_size(qdict), ==, 3);
    g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
E
Eric Blake 已提交
178
    g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, false);
179 180 181 182 183 184 185 186 187
    g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");

    QDECREF(qdict);
}

static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
                                           const void *unused)
{
    int64_t value = 42;
188
    UserDefTwo *ud2;
189 190 191
    QObject *obj;
    QDict *qdict, *dict1, *dict2, *dict3, *userdef;
    const char *string = "user def string";
S
Stefan Weil 已提交
192 193
    const char *strings[] = { "forty two", "forty three", "forty four",
                              "forty five" };
194 195 196 197

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

198 199 200 201 202 203
    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 已提交
204
    ud2->dict1->dict2->userdef->integer = value;
205 206 207 208 209 210
    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 已提交
211
    ud2->dict1->dict3->userdef->integer = value;
212
    ud2->dict1->dict3->string = g_strdup(strings[3]);
213

214
    visit_type_UserDefTwo(data->ov, "unused", &ud2, &error_abort);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QDICT);

    qdict = qobject_to_qdict(obj);
    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);
230 231
    g_assert_cmpstr(qdict_get_str(dict2, "string"), ==, strings[2]);
    userdef = qdict_get_qdict(dict2, "userdef");
232 233 234 235 236 237
    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);
238 239
    g_assert_cmpstr(qdict_get_str(dict3, "string"), ==, strings[3]);
    userdef = qdict_get_qdict(dict3, "userdef");
240 241 242 243 244
    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);

    QDECREF(qdict);
245
    qapi_free_UserDefTwo(ud2);
246 247
}

248 249 250
static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
                                           const void *unused)
{
251
    EnumOne bad_values[] = { ENUM_ONE__MAX, -1 };
E
Eric Blake 已提交
252 253
    UserDefOne u = {0};
    UserDefOne *pu = &u;
254
    Error *err;
255 256 257
    int i;

    for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
258
        err = NULL;
259 260
        u.has_enum1 = true;
        u.enum1 = bad_values[i];
261
        visit_type_UserDefOne(data->ov, "unused", &pu, &err);
262 263
        g_assert(err);
        error_free(err);
264 265 266
    }
}

267 268 269 270

static void test_visitor_out_list(TestOutputVisitorData *data,
                                  const void *unused)
{
271
    const char *value_str = "list value";
272 273 274 275 276 277 278 279 280
    TestStructList *p, *head = NULL;
    const int max_items = 10;
    bool value_bool = true;
    int value_int = 10;
    QListEntry *entry;
    QObject *obj;
    QList *qlist;
    int i;

281
    /* Build the list in reverse order... */
282 283 284
    for (i = 0; i < max_items; i++) {
        p = g_malloc0(sizeof(*p));
        p->value = g_malloc0(sizeof(*p->value));
285
        p->value->integer = value_int + (max_items - i - 1);
286
        p->value->boolean = value_bool;
287
        p->value->string = g_strdup(value_str);
288 289 290 291 292

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

293
    visit_type_TestStructList(data->ov, NULL, &head, &error_abort);
294 295 296 297 298 299 300 301

    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QLIST);

    qlist = qobject_to_qlist(obj);
    g_assert(!qlist_empty(qlist));

302
    /* ...and ensure that the visitor sees it in order */
303 304 305 306 307 308 309
    i = 0;
    QLIST_FOREACH_ENTRY(qlist, entry) {
        QDict *qdict;

        g_assert(qobject_type(entry->value) == QTYPE_QDICT);
        qdict = qobject_to_qdict(entry->value);
        g_assert_cmpint(qdict_size(qdict), ==, 3);
310
        g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int + i);
311 312 313 314 315 316 317
        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);

    QDECREF(qlist);
318
    qapi_free_TestStructList(head);
319 320 321 322 323
}

static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
                                            const void *unused)
{
324
    UserDefTwoList *p, *head = NULL;
325 326 327 328 329 330 331 332
    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);
333 334 335 336 337
        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 已提交
338
        p->value->dict1->dict2->userdef->integer = 42;
339 340
        p->value->dict1->dict2->string = g_strdup(string);
        p->value->dict1->has_dict3 = false;
341 342 343 344 345

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

346
    qapi_free_UserDefTwoList(head);
347 348
}

349 350 351 352 353 354 355 356 357 358 359
static void test_visitor_out_any(TestOutputVisitorData *data,
                                 const void *unused)
{
    QObject *qobj;
    QInt *qint;
    QBool *qbool;
    QString *qstring;
    QDict *qdict;
    QObject *obj;

    qobj = QOBJECT(qint_from_int(-42));
360
    visit_type_any(data->ov, NULL, &qobj, &error_abort);
361 362 363 364 365 366 367 368 369 370 371 372
    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    g_assert(qobject_type(obj) == QTYPE_QINT);
    g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, -42);
    qobject_decref(obj);
    qobject_decref(qobj);

    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);
373
    visit_type_any(data->ov, NULL, &qobj, &error_abort);
E
Eric Blake 已提交
374
    qobject_decref(qobj);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    obj = qmp_output_get_qobject(data->qov);
    g_assert(obj != NULL);
    qdict = qobject_to_qdict(obj);
    g_assert(qdict);
    qobj = qdict_get(qdict, "integer");
    g_assert(qobj);
    qint = qobject_to_qint(qobj);
    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);
    qobj = qdict_get(qdict, "string");
    g_assert(qobj);
    qstring = qobject_to_qstring(qobj);
    g_assert(qstring);
    g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
    qobject_decref(obj);
}

397 398 399 400 401 402 403
static void test_visitor_out_union_flat(TestOutputVisitorData *data,
                                        const void *unused)
{
    QObject *arg;
    QDict *qdict;

    UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
404
    tmp->enum1 = ENUM_ONE_VALUE1;
405
    tmp->string = g_strdup("str");
406 407 408
    tmp->u.value1 = g_malloc0(sizeof(UserDefA));
    tmp->integer = 41;
    tmp->u.value1->boolean = true;
409

410
    visit_type_UserDefFlatUnion(data->ov, NULL, &tmp, &error_abort);
411 412 413 414 415
    arg = qmp_output_get_qobject(data->qov);

    g_assert(qobject_type(arg) == QTYPE_QDICT);
    qdict = qobject_to_qdict(arg);

416 417
    g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
    g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
418
    g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
419 420 421 422 423 424
    g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);

    qapi_free_UserDefFlatUnion(tmp);
    QDECREF(qdict);
}

425 426
static void test_visitor_out_alternate(TestOutputVisitorData *data,
                                       const void *unused)
427 428
{
    QObject *arg;
E
Eric Blake 已提交
429
    UserDefAlternate *tmp;
430
    QDict *qdict;
431

E
Eric Blake 已提交
432
    tmp = g_new0(UserDefAlternate, 1);
433
    tmp->type = QTYPE_QINT;
434
    tmp->u.i = 42;
435

436
    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
437 438 439 440 441
    arg = qmp_output_get_qobject(data->qov);

    g_assert(qobject_type(arg) == QTYPE_QINT);
    g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);

442
    qapi_free_UserDefAlternate(tmp);
E
Eric Blake 已提交
443
    qobject_decref(arg);
E
Eric Blake 已提交
444 445

    tmp = g_new0(UserDefAlternate, 1);
446
    tmp->type = QTYPE_QSTRING;
E
Eric Blake 已提交
447 448
    tmp->u.s = g_strdup("hello");

449
    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
E
Eric Blake 已提交
450 451 452 453 454 455 456
    arg = qmp_output_get_qobject(data->qov);

    g_assert(qobject_type(arg) == QTYPE_QSTRING);
    g_assert_cmpstr(qstring_get_str(qobject_to_qstring(arg)), ==, "hello");

    qapi_free_UserDefAlternate(tmp);
    qobject_decref(arg);
457 458 459

    tmp = g_new0(UserDefAlternate, 1);
    tmp->type = QTYPE_QDICT;
460 461 462 463 464
    tmp->u.udfu.integer = 1;
    tmp->u.udfu.string = g_strdup("str");
    tmp->u.udfu.enum1 = ENUM_ONE_VALUE1;
    tmp->u.udfu.u.value1 = g_new0(UserDefA, 1);
    tmp->u.udfu.u.value1->boolean = true;
465 466 467 468 469 470 471 472 473 474 475 476 477 478

    visit_type_UserDefAlternate(data->ov, NULL, &tmp, &error_abort);
    arg = qmp_output_get_qobject(data->qov);

    g_assert_cmpint(qobject_type(arg), ==, QTYPE_QDICT);
    qdict = qobject_to_qdict(arg);
    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);
    qobject_decref(arg);
479 480
}

481 482 483 484 485 486
static void test_visitor_out_empty(TestOutputVisitorData *data,
                                   const void *unused)
{
    QObject *arg;

    arg = qmp_output_get_qobject(data->qov);
487
    g_assert(qobject_type(arg) == QTYPE_QNULL);
488 489
    /* Check that qnull reference counting is sane */
    g_assert(arg->refcnt == 2);
490
    qobject_decref(arg);
491 492
}

493 494 495
static void init_native_list(UserDefNativeListUnion *cvalue)
{
    int i;
496
    switch (cvalue->type) {
497
    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
498
        intList **list = &cvalue->u.integer;
499 500 501 502 503 504 505 506 507
        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: {
508
        int8List **list = &cvalue->u.s8;
509 510 511 512 513 514 515 516 517
        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: {
518
        int16List **list = &cvalue->u.s16;
519 520 521 522 523 524 525 526 527
        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: {
528
        int32List **list = &cvalue->u.s32;
529 530 531 532 533 534 535 536 537
        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: {
538
        int64List **list = &cvalue->u.s64;
539 540 541 542 543 544 545 546 547
        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: {
548
        uint8List **list = &cvalue->u.u8;
549 550 551 552 553 554 555 556 557
        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: {
558
        uint16List **list = &cvalue->u.u16;
559 560 561 562 563 564 565 566 567
        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: {
568
        uint32List **list = &cvalue->u.u32;
569 570 571 572 573 574 575 576 577
        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: {
578
        uint64List **list = &cvalue->u.u64;
579 580 581 582 583 584 585 586 587
        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: {
588
        boolList **list = &cvalue->u.boolean;
589 590 591 592 593 594 595 596 597
        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: {
598
        strList **list = &cvalue->u.string;
599 600 601 602 603 604 605 606 607
        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: {
608
        numberList **list = &cvalue->u.number;
609 610 611 612 613 614 615 616 617
        for (i = 0; i < 32; i++) {
            *list = g_new0(numberList, 1);
            (*list)->value = (double)i / 3;
            (*list)->next = NULL;
            list = &(*list)->next;
        }
        break;
    }
    default:
618
        g_assert_not_reached();
619 620 621 622 623 624 625 626 627 628 629 630 631 632 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
    }
}

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

    g_assert(qobj);
    g_assert(qobject_type(qobj) == QTYPE_QDICT);
    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 已提交
667
            g_assert_cmpint(qbool_get_bool(qvalue), ==, i % 3 == 0);
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
            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:
704
        g_assert_not_reached();
705 706 707 708 709 710 711 712 713 714 715
    }
    QDECREF(qlist);
}

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

716
    cvalue->type = kind;
717 718
    init_native_list(cvalue);

719
    visit_type_UserDefNativeListUnion(data->ov, NULL, &cvalue, &error_abort);
720 721

    obj = qmp_output_get_qobject(data->qov);
722
    check_native_list(obj, cvalue->type);
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 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 795 796 797 798
    qapi_free_UserDefNativeListUnion(cvalue);
    qobject_decref(obj);
}

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);
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
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);
831 832
    output_visitor_test_add("/visitor/output/struct-errors",
                            &out_visitor_data, test_visitor_out_struct_errors);
833 834
    output_visitor_test_add("/visitor/output/list",
                            &out_visitor_data, test_visitor_out_list);
835 836
    output_visitor_test_add("/visitor/output/any",
                            &out_visitor_data, test_visitor_out_any);
837 838
    output_visitor_test_add("/visitor/output/list-qapi-free",
                            &out_visitor_data, test_visitor_out_list_qapi_free);
839 840
    output_visitor_test_add("/visitor/output/union-flat",
                            &out_visitor_data, test_visitor_out_union_flat);
841 842
    output_visitor_test_add("/visitor/output/alternate",
                            &out_visitor_data, test_visitor_out_alternate);
843 844
    output_visitor_test_add("/visitor/output/empty",
                            &out_visitor_data, test_visitor_out_empty);
845
    output_visitor_test_add("/visitor/output/native_list/int",
846 847
                            &out_visitor_data,
                            test_visitor_out_native_list_int);
848
    output_visitor_test_add("/visitor/output/native_list/int8",
849 850
                            &out_visitor_data,
                            test_visitor_out_native_list_int8);
851
    output_visitor_test_add("/visitor/output/native_list/int16",
852 853
                            &out_visitor_data,
                            test_visitor_out_native_list_int16);
854
    output_visitor_test_add("/visitor/output/native_list/int32",
855 856
                            &out_visitor_data,
                            test_visitor_out_native_list_int32);
857
    output_visitor_test_add("/visitor/output/native_list/int64",
858 859
                            &out_visitor_data,
                            test_visitor_out_native_list_int64);
860
    output_visitor_test_add("/visitor/output/native_list/uint8",
861 862
                            &out_visitor_data,
                            test_visitor_out_native_list_uint8);
863
    output_visitor_test_add("/visitor/output/native_list/uint16",
864 865
                            &out_visitor_data,
                            test_visitor_out_native_list_uint16);
866
    output_visitor_test_add("/visitor/output/native_list/uint32",
867 868
                            &out_visitor_data,
                            test_visitor_out_native_list_uint32);
869
    output_visitor_test_add("/visitor/output/native_list/uint64",
870 871
                            &out_visitor_data,
                            test_visitor_out_native_list_uint64);
872
    output_visitor_test_add("/visitor/output/native_list/bool",
873 874
                            &out_visitor_data,
                            test_visitor_out_native_list_bool);
875
    output_visitor_test_add("/visitor/output/native_list/string",
876 877
                            &out_visitor_data,
                            test_visitor_out_native_list_str);
878
    output_visitor_test_add("/visitor/output/native_list/number",
879 880
                            &out_visitor_data,
                            test_visitor_out_native_list_number);
881 882 883 884 885

    g_test_run();

    return 0;
}