Commit 3d089cea authored by Markus Armbruster's avatar Markus Armbruster
Browse files

test-string-input-visitor: Improve list coverage



Lists with elements above INT64_MAX don't work (known bug).  Empty
lists don't work (weird).

Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
Reviewed-by: default avatarEric Blake <eblake@redhat.com>
Message-Id: <1488544368-30622-23-git-send-email-armbru@redhat.com>
parent 0f721d16
Loading
Loading
Loading
Loading
+72 −13
Original line number Diff line number Diff line
@@ -65,31 +65,90 @@ static void test_visitor_in_int(TestInputVisitorData *data,
    error_free_or_abort(&err);
}

static void check_ilist(Visitor *v, int64_t *expected, size_t n)
{
    int64List *res = NULL;
    int64List *tail;
    int i;

    visit_type_int64List(v, NULL, &res, &error_abort);
    tail = res;
    for (i = 0; i < n; i++) {
        g_assert(tail);
        g_assert_cmpint(tail->value, ==, expected[i]);
        tail = tail->next;
    }
    g_assert(!tail);

    qapi_free_int64List(res);
}

static void check_ulist(Visitor *v, uint64_t *expected, size_t n)
{
    uint64List *res = NULL;
    uint64List *tail;
    int i;

    /* BUG: unsigned numbers above INT64_MAX don't work */
    for (i = 0; i < n; i++) {
        if (expected[i] > INT64_MAX) {
            Error *err = NULL;
            visit_type_uint64List(v, NULL, &res, &err);
            error_free_or_abort(&err);
            return;
        }
    }

    visit_type_uint64List(v, NULL, &res, &error_abort);
    tail = res;
    for (i = 0; i < n; i++) {
        g_assert(tail);
        g_assert_cmpuint(tail->value, ==, expected[i]);
        tail = tail->next;
    }
    g_assert(!tail);

    qapi_free_uint64List(res);
}

static void test_visitor_in_intList(TestInputVisitorData *data,
                                    const void *unused)
{
    int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
    int16List *res = NULL, *tmp;
    /* Note: the visitor *sorts* ranges *unsigned* */
    int64_t expect1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 };
    int64_t expect2[] = { 32767, -32768, -32767 };
    int64_t expect3[] = { INT64_MAX, INT64_MIN };
    uint64_t expect4[] = { UINT64_MAX };
    Error *err = NULL;
    int64List *res = NULL;
    Visitor *v;
    int i = 0;

    /* Valid lists */

    v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
    check_ilist(v, expect1, ARRAY_SIZE(expect1));

    visit_type_int16List(v, NULL, &res, &error_abort);
    tmp = res;
    while (i < sizeof(value) / sizeof(value[0])) {
        g_assert(tmp);
        g_assert_cmpint(tmp->value, ==, value[i++]);
        tmp = tmp->next;
    }
    g_assert(!tmp);
    v = visitor_input_test_init(data, "32767,-32768--32767");
    check_ilist(v, expect2, ARRAY_SIZE(expect2));

    v = visitor_input_test_init(data,
                                "-9223372036854775808,9223372036854775807");
    check_ilist(v, expect3, ARRAY_SIZE(expect3));

    v = visitor_input_test_init(data, "18446744073709551615");
    check_ulist(v, expect4, ARRAY_SIZE(expect4));

    /* Empty list is invalid (weird) */

    v = visitor_input_test_init(data, "");
    visit_type_int64List(v, NULL, &res, &err);
    error_free_or_abort(&err);

    qapi_free_int16List(res);
    /* Not a list */

    v = visitor_input_test_init(data, "not an int list");

    visit_type_int16List(v, NULL, &res, &err);
    visit_type_int64List(v, NULL, &res, &err);
    error_free_or_abort(&err);
    g_assert(!res);
}