Blame SOURCES/kvm-qapi-add-native-list-coverage-for-QMP-input-visitor-.patch

0a122b
From e88b00eb5e1f4785827445fe5b06b590ebae7d7b Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <e88b00eb5e1f4785827445fe5b06b590ebae7d7b.1387369730.git.minovotn@redhat.com>
0a122b
In-Reply-To: <091eecc4fa42754760dfff393dabcc2b444e9693.1387369730.git.minovotn@redhat.com>
0a122b
References: <091eecc4fa42754760dfff393dabcc2b444e9693.1387369730.git.minovotn@redhat.com>
0a122b
From: Markus Armbruster <armbru@redhat.com>
0a122b
Date: Tue, 10 Dec 2013 15:29:09 +0100
0a122b
Subject: [PATCH 09/21] qapi: add native list coverage for QMP input visitor
0a122b
 tests
0a122b
0a122b
RH-Author: Markus Armbruster <armbru@redhat.com>
0a122b
Message-id: <1386689361-30281-7-git-send-email-armbru@redhat.com>
0a122b
Patchwork-id: 56125
0a122b
O-Subject: [PATCH 7.0 qemu-kvm 06/18] qapi: add native list coverage for QMP input visitor tests
0a122b
Bugzilla: 997915
0a122b
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
0a122b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
0a122b
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
0a122b
0a122b
From: Michael Roth <mdroth@linux.vnet.ibm.com>
0a122b
0a122b
This exercises schema-generated visitors for native list types and does
0a122b
some sanity checking on validity of deserialized data.
0a122b
0a122b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
0a122b
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
0a122b
Reviewed-by: Amos Kong <akong@redhat.com>
0a122b
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
0a122b
(cherry picked from commit 199e0f17f23a68c8d619e9e623d970324ed5efc1)
0a122b
Signed-off-by: Markus Armbruster <armbru@redhat.com>
0a122b
---
0a122b
 tests/test-qmp-input-visitor.c | 338 +++++++++++++++++++++++++++++++++++++++++
0a122b
 1 file changed, 338 insertions(+)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 tests/test-qmp-input-visitor.c | 338 +++++++++++++++++++++++++++++++++++++++++
0a122b
 1 file changed, 338 insertions(+)
0a122b
0a122b
diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c
0a122b
index b308cf9..2741eef 100644
0a122b
--- a/tests/test-qmp-input-visitor.c
0a122b
+++ b/tests/test-qmp-input-visitor.c
0a122b
@@ -61,6 +61,31 @@ Visitor *visitor_input_test_init(TestInputVisitorData *data,
0a122b
     return v;
0a122b
 }
0a122b
 
0a122b
+/* similar to visitor_input_test_init(), but does not expect a string
0a122b
+ * literal/format json_string argument and so can be used for
0a122b
+ * programatically generated strings (and we can't pass in programatically
0a122b
+ * generated strings via %s format parameters since qobject_from_jsonv()
0a122b
+ * will wrap those in double-quotes and treat the entire object as a
0a122b
+ * string)
0a122b
+ */
0a122b
+static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
0a122b
+                                            const char *json_string)
0a122b
+{
0a122b
+    Visitor *v;
0a122b
+
0a122b
+    data->obj = qobject_from_json(json_string);
0a122b
+
0a122b
+    g_assert(data->obj != NULL);
0a122b
+
0a122b
+    data->qiv = qmp_input_visitor_new(data->obj);
0a122b
+    g_assert(data->qiv != NULL);
0a122b
+
0a122b
+    v = qmp_input_get_visitor(data->qiv);
0a122b
+    g_assert(v != NULL);
0a122b
+
0a122b
+    return v;
0a122b
+}
0a122b
+
0a122b
 static void test_visitor_in_int(TestInputVisitorData *data,
0a122b
                                 const void *unused)
0a122b
 {
0a122b
@@ -277,6 +302,287 @@ static void test_visitor_in_union(TestInputVisitorData *data,
0a122b
     qapi_free_UserDefUnion(tmp);
0a122b
 }
0a122b
 
0a122b
+static void test_native_list_integer_helper(TestInputVisitorData *data,
0a122b
+                                            const void *unused,
0a122b
+                                            UserDefNativeListUnionKind kind)
0a122b
+{
0a122b
+    UserDefNativeListUnion *cvalue = NULL;
0a122b
+    Error *err = NULL;
0a122b
+    Visitor *v;
0a122b
+    GString *gstr_list = g_string_new("");
0a122b
+    GString *gstr_union = g_string_new("");
0a122b
+    int i;
0a122b
+
0a122b
+    for (i = 0; i < 32; i++) {
0a122b
+        g_string_append_printf(gstr_list, "%d", i);
0a122b
+        if (i != 31) {
0a122b
+            g_string_append(gstr_list, ", ");
0a122b
+        }
0a122b
+    }
0a122b
+    g_string_append_printf(gstr_union,  "{ 'type': '%s', 'data': [ %s ] }",
0a122b
+                           UserDefNativeListUnionKind_lookup[kind],
0a122b
+                           gstr_list->str);
0a122b
+    v = visitor_input_test_init_raw(data,  gstr_union->str);
0a122b
+
0a122b
+    visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err;;
0a122b
+    g_assert(err == NULL);
0a122b
+    g_assert(cvalue != NULL);
0a122b
+    g_assert_cmpint(cvalue->kind, ==, kind);
0a122b
+
0a122b
+    switch (kind) {
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
0a122b
+        intList *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
0a122b
+        int8List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
0a122b
+        int16List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
0a122b
+        int32List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
0a122b
+        int64List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
0a122b
+        uint8List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
0a122b
+        uint16List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
0a122b
+        uint32List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
0a122b
+        uint64List *elem = NULL;
0a122b
+        for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) {
0a122b
+            g_assert_cmpint(elem->value, ==, i);
0a122b
+        }
0a122b
+        break;
0a122b
+    }
0a122b
+    default:
0a122b
+        g_assert(false);
0a122b
+    }
0a122b
+
0a122b
+    g_string_free(gstr_union, true);
0a122b
+    g_string_free(gstr_list, true);
0a122b
+    qapi_free_UserDefNativeListUnion(cvalue);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_int(TestInputVisitorData *data,
0a122b
+                                            const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
0a122b
+                                             const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_S8);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
0a122b
+                                              const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_S16);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
0a122b
+                                              const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_S32);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
0a122b
+                                              const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_S64);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
0a122b
+                                             const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_U8);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
0a122b
+                                               const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_U16);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
0a122b
+                                               const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_U32);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
0a122b
+                                               const void *unused)
0a122b
+{
0a122b
+    test_native_list_integer_helper(data, unused,
0a122b
+                                    USER_DEF_NATIVE_LIST_UNION_KIND_U64);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
0a122b
+                                            const void *unused)
0a122b
+{
0a122b
+    UserDefNativeListUnion *cvalue = NULL;
0a122b
+    boolList *elem = NULL;
0a122b
+    Error *err = NULL;
0a122b
+    Visitor *v;
0a122b
+    GString *gstr_list = g_string_new("");
0a122b
+    GString *gstr_union = g_string_new("");
0a122b
+    int i;
0a122b
+
0a122b
+    for (i = 0; i < 32; i++) {
0a122b
+        g_string_append_printf(gstr_list, "%s",
0a122b
+                               (i % 3 == 0) ? "true" : "false");
0a122b
+        if (i != 31) {
0a122b
+            g_string_append(gstr_list, ", ");
0a122b
+        }
0a122b
+    }
0a122b
+    g_string_append_printf(gstr_union,  "{ 'type': 'boolean', 'data': [ %s ] }",
0a122b
+                           gstr_list->str);
0a122b
+    v = visitor_input_test_init_raw(data,  gstr_union->str);
0a122b
+
0a122b
+    visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err;;
0a122b
+    g_assert(err == NULL);
0a122b
+    g_assert(cvalue != NULL);
0a122b
+    g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
0a122b
+
0a122b
+    for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) {
0a122b
+        g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
0a122b
+    }
0a122b
+
0a122b
+    g_string_free(gstr_union, true);
0a122b
+    g_string_free(gstr_list, true);
0a122b
+    qapi_free_UserDefNativeListUnion(cvalue);
0a122b
+}
0a122b
+
0a122b
+static void test_visitor_in_native_list_string(TestInputVisitorData *data,
0a122b
+                                               const void *unused)
0a122b
+{
0a122b
+    UserDefNativeListUnion *cvalue = NULL;
0a122b
+    strList *elem = NULL;
0a122b
+    Error *err = NULL;
0a122b
+    Visitor *v;
0a122b
+    GString *gstr_list = g_string_new("");
0a122b
+    GString *gstr_union = g_string_new("");
0a122b
+    int i;
0a122b
+
0a122b
+    for (i = 0; i < 32; i++) {
0a122b
+        g_string_append_printf(gstr_list, "'%d'", i);
0a122b
+        if (i != 31) {
0a122b
+            g_string_append(gstr_list, ", ");
0a122b
+        }
0a122b
+    }
0a122b
+    g_string_append_printf(gstr_union,  "{ 'type': 'string', 'data': [ %s ] }",
0a122b
+                           gstr_list->str);
0a122b
+    v = visitor_input_test_init_raw(data,  gstr_union->str);
0a122b
+
0a122b
+    visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err;;
0a122b
+    g_assert(err == NULL);
0a122b
+    g_assert(cvalue != NULL);
0a122b
+    g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
0a122b
+
0a122b
+    for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) {
0a122b
+        gchar str[8];
0a122b
+        sprintf(str, "%d", i);
0a122b
+        g_assert_cmpstr(elem->value, ==, str);
0a122b
+    }
0a122b
+
0a122b
+    g_string_free(gstr_union, true);
0a122b
+    g_string_free(gstr_list, true);
0a122b
+    qapi_free_UserDefNativeListUnion(cvalue);
0a122b
+}
0a122b
+
0a122b
+#define DOUBLE_STR_MAX 16
0a122b
+
0a122b
+static void test_visitor_in_native_list_number(TestInputVisitorData *data,
0a122b
+                                               const void *unused)
0a122b
+{
0a122b
+    UserDefNativeListUnion *cvalue = NULL;
0a122b
+    numberList *elem = NULL;
0a122b
+    Error *err = NULL;
0a122b
+    Visitor *v;
0a122b
+    GString *gstr_list = g_string_new("");
0a122b
+    GString *gstr_union = g_string_new("");
0a122b
+    int i;
0a122b
+
0a122b
+    for (i = 0; i < 32; i++) {
0a122b
+        g_string_append_printf(gstr_list, "%f", (double)i / 3);
0a122b
+        if (i != 31) {
0a122b
+            g_string_append(gstr_list, ", ");
0a122b
+        }
0a122b
+    }
0a122b
+    g_string_append_printf(gstr_union,  "{ 'type': 'number', 'data': [ %s ] }",
0a122b
+                           gstr_list->str);
0a122b
+    v = visitor_input_test_init_raw(data,  gstr_union->str);
0a122b
+
0a122b
+    visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err;;
0a122b
+    g_assert(err == NULL);
0a122b
+    g_assert(cvalue != NULL);
0a122b
+    g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
0a122b
+
0a122b
+    for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) {
0a122b
+        GString *double_expected = g_string_new("");
0a122b
+        GString *double_actual = g_string_new("");
0a122b
+
0a122b
+        g_string_printf(double_expected, "%.6f", (double)i / 3);
0a122b
+        g_string_printf(double_actual, "%.6f", elem->value);
0a122b
+        g_assert_cmpstr(double_expected->str, ==, double_actual->str);
0a122b
+
0a122b
+        g_string_free(double_expected, true);
0a122b
+        g_string_free(double_actual, true);
0a122b
+    }
0a122b
+
0a122b
+    g_string_free(gstr_union, true);
0a122b
+    g_string_free(gstr_list, true);
0a122b
+    qapi_free_UserDefNativeListUnion(cvalue);
0a122b
+}
0a122b
+
0a122b
 static void input_visitor_test_add(const char *testpath,
0a122b
                                    TestInputVisitorData *data,
0a122b
                                    void (*test_func)(TestInputVisitorData *data, const void *user_data))
0a122b
@@ -330,6 +636,38 @@ int main(int argc, char **argv)
0a122b
                             &in_visitor_data, test_visitor_in_union);
0a122b
     input_visitor_test_add("/visitor/input/errors",
0a122b
                             &in_visitor_data, test_visitor_in_errors);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/int",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_int);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/int8",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_int8);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/int16",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_int16);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/int32",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_int32);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/int64",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_int64);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/uint8",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_uint8);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/uint16",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_uint16);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/uint32",
0a122b
+                            &in_visitor_data,
0a122b
+                            test_visitor_in_native_list_uint32);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/uint64",
0a122b
+                            &in_visitor_data, test_visitor_in_native_list_uint64);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/bool",
0a122b
+                            &in_visitor_data, test_visitor_in_native_list_bool);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/str",
0a122b
+                            &in_visitor_data, test_visitor_in_native_list_string);
0a122b
+    input_visitor_test_add("/visitor/input/native_list/number",
0a122b
+                            &in_visitor_data, test_visitor_in_native_list_number);
0a122b
 
0a122b
     g_test_run();
0a122b
 
0a122b
-- 
0a122b
1.7.11.7
0a122b