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