9ae3a8
From 45c69be7596bc6d158f56ec94c5f465a2030a8e5 Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <45c69be7596bc6d158f56ec94c5f465a2030a8e5.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:08 +0100
9ae3a8
Subject: [PATCH 08/21] qapi: add native list coverage for QMP output visitor
9ae3a8
 tests
9ae3a8
9ae3a8
RH-Author: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: <1386689361-30281-6-git-send-email-armbru@redhat.com>
9ae3a8
Patchwork-id: 56135
9ae3a8
O-Subject: [PATCH 7.0 qemu-kvm 05/18] qapi: add native list coverage for QMP output 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 serialized 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 83c84667f57637fe5a7a6fc9905d6a9e9589d3e5)
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
---
9ae3a8
 qapi-schema-test.json           |  15 ++
9ae3a8
 tests/test-qmp-output-visitor.c | 332 ++++++++++++++++++++++++++++++++++++++++
9ae3a8
 2 files changed, 347 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 qapi-schema-test.json           |  15 ++
9ae3a8
 tests/test-qmp-output-visitor.c | 332 ++++++++++++++++++++++++++++++++++++++++
9ae3a8
 2 files changed, 347 insertions(+)
9ae3a8
9ae3a8
diff --git a/qapi-schema-test.json b/qapi-schema-test.json
9ae3a8
index 9eae350..4434fa3 100644
9ae3a8
--- a/qapi-schema-test.json
9ae3a8
+++ b/qapi-schema-test.json
9ae3a8
@@ -32,6 +32,21 @@
9ae3a8
 { 'union': 'UserDefUnion',
9ae3a8
   'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } }
9ae3a8
 
9ae3a8
+# for testing native lists
9ae3a8
+{ 'union': 'UserDefNativeListUnion',
9ae3a8
+  'data': { 'integer': ['int'],
9ae3a8
+            's8': ['int8'],
9ae3a8
+            's16': ['int16'],
9ae3a8
+            's32': ['int32'],
9ae3a8
+            's64': ['int64'],
9ae3a8
+            'u8': ['uint8'],
9ae3a8
+            'u16': ['uint16'],
9ae3a8
+            'u32': ['uint32'],
9ae3a8
+            'u64': ['uint64'],
9ae3a8
+            'number': ['number'],
9ae3a8
+            'boolean': ['bool'],
9ae3a8
+            'string': ['str'] } }
9ae3a8
+
9ae3a8
 # testing commands
9ae3a8
 { 'command': 'user_def_cmd', 'data': {} }
9ae3a8
 { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} }
9ae3a8
diff --git a/tests/test-qmp-output-visitor.c b/tests/test-qmp-output-visitor.c
9ae3a8
index 71367e6..0942a41 100644
9ae3a8
--- a/tests/test-qmp-output-visitor.c
9ae3a8
+++ b/tests/test-qmp-output-visitor.c
9ae3a8
@@ -431,6 +431,314 @@ static void test_visitor_out_union(TestOutputVisitorData *data,
9ae3a8
     QDECREF(qdict);
9ae3a8
 }
9ae3a8
 
9ae3a8
+static void init_native_list(UserDefNativeListUnion *cvalue)
9ae3a8
+{
9ae3a8
+    int i;
9ae3a8
+    switch (cvalue->kind) {
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
9ae3a8
+        intList **list = &cvalue->integer;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(intList, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
9ae3a8
+        int8List **list = &cvalue->s8;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(int8List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
9ae3a8
+        int16List **list = &cvalue->s16;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(int16List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
9ae3a8
+        int32List **list = &cvalue->s32;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(int32List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
9ae3a8
+        int64List **list = &cvalue->s64;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(int64List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
9ae3a8
+        uint8List **list = &cvalue->u8;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(uint8List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
9ae3a8
+        uint16List **list = &cvalue->u16;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(uint16List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
9ae3a8
+        uint32List **list = &cvalue->u32;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(uint32List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
9ae3a8
+        uint64List **list = &cvalue->u64;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(uint64List, 1);
9ae3a8
+            (*list)->value = i;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
9ae3a8
+        boolList **list = &cvalue->boolean;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(boolList, 1);
9ae3a8
+            (*list)->value = (i % 3 == 0);
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
9ae3a8
+        strList **list = &cvalue->string;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(strList, 1);
9ae3a8
+            (*list)->value = g_strdup_printf("%d", i);
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
9ae3a8
+        numberList **list = &cvalue->number;
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            *list = g_new0(numberList, 1);
9ae3a8
+            (*list)->value = (double)i / 3;
9ae3a8
+            (*list)->next = NULL;
9ae3a8
+            list = &(*list)->next;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    }
9ae3a8
+    default:
9ae3a8
+        g_assert(false);
9ae3a8
+    }
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void check_native_list(QObject *qobj,
9ae3a8
+                              UserDefNativeListUnionKind kind)
9ae3a8
+{
9ae3a8
+    QDict *qdict;
9ae3a8
+    QList *qlist;
9ae3a8
+    int i;
9ae3a8
+
9ae3a8
+    g_assert(qobj);
9ae3a8
+    g_assert(qobject_type(qobj) == QTYPE_QDICT);
9ae3a8
+    qdict = qobject_to_qdict(qobj);
9ae3a8
+    g_assert(qdict);
9ae3a8
+    g_assert(qdict_haskey(qdict, "data"));
9ae3a8
+    qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
9ae3a8
+
9ae3a8
+    switch (kind) {
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
9ae3a8
+        /* all integer elements in JSON arrays get stored into QInts when
9ae3a8
+         * we convert to QObjects, so we can check them all in the same
9ae3a8
+         * fashion, so simply fall through here
9ae3a8
+         */
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            QObject *tmp;
9ae3a8
+            QInt *qvalue;
9ae3a8
+            tmp = qlist_peek(qlist);
9ae3a8
+            g_assert(tmp);
9ae3a8
+            qvalue = qobject_to_qint(tmp);
9ae3a8
+            g_assert_cmpint(qint_get_int(qvalue), ==, i);
9ae3a8
+            qobject_decref(qlist_pop(qlist));
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            QObject *tmp;
9ae3a8
+            QBool *qvalue;
9ae3a8
+            tmp = qlist_peek(qlist);
9ae3a8
+            g_assert(tmp);
9ae3a8
+            qvalue = qobject_to_qbool(tmp);
9ae3a8
+            g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0);
9ae3a8
+            qobject_decref(qlist_pop(qlist));
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            QObject *tmp;
9ae3a8
+            QString *qvalue;
9ae3a8
+            gchar str[8];
9ae3a8
+            tmp = qlist_peek(qlist);
9ae3a8
+            g_assert(tmp);
9ae3a8
+            qvalue = qobject_to_qstring(tmp);
9ae3a8
+            sprintf(str, "%d", i);
9ae3a8
+            g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
9ae3a8
+            qobject_decref(qlist_pop(qlist));
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
9ae3a8
+        for (i = 0; i < 32; i++) {
9ae3a8
+            QObject *tmp;
9ae3a8
+            QFloat *qvalue;
9ae3a8
+            GString *double_expected = g_string_new("");
9ae3a8
+            GString *double_actual = g_string_new("");
9ae3a8
+
9ae3a8
+            tmp = qlist_peek(qlist);
9ae3a8
+            g_assert(tmp);
9ae3a8
+            qvalue = qobject_to_qfloat(tmp);
9ae3a8
+            g_string_printf(double_expected, "%.6f", (double)i / 3);
9ae3a8
+            g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
9ae3a8
+            g_assert_cmpstr(double_actual->str, ==, double_expected->str);
9ae3a8
+
9ae3a8
+            qobject_decref(qlist_pop(qlist));
9ae3a8
+            g_string_free(double_expected, true);
9ae3a8
+            g_string_free(double_actual, true);
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
+    default:
9ae3a8
+        g_assert(false);
9ae3a8
+    }
9ae3a8
+    QDECREF(qlist);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_native_list(TestOutputVisitorData *data,
9ae3a8
+                             const void *unused,
9ae3a8
+                             UserDefNativeListUnionKind kind)
9ae3a8
+{
9ae3a8
+    UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
9ae3a8
+    Error *err = NULL;
9ae3a8
+    QObject *obj;
9ae3a8
+
9ae3a8
+    cvalue->kind = kind;
9ae3a8
+    init_native_list(cvalue);
9ae3a8
+
9ae3a8
+    visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err;;
9ae3a8
+    g_assert(err == NULL);
9ae3a8
+
9ae3a8
+    obj = qmp_output_get_qobject(data->qov);
9ae3a8
+    check_native_list(obj, cvalue->kind);
9ae3a8
+    qapi_free_UserDefNativeListUnion(cvalue);
9ae3a8
+    qobject_decref(obj);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
9ae3a8
+                                             const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
9ae3a8
+                                              const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
9ae3a8
+                                               const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
9ae3a8
+                                               const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
9ae3a8
+                                               const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
9ae3a8
+                                               const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
9ae3a8
+                                                const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
9ae3a8
+                                                const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
9ae3a8
+                                                const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
9ae3a8
+                                              const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
9ae3a8
+                                              const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
9ae3a8
+}
9ae3a8
+
9ae3a8
+static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
9ae3a8
+                                                const void *unused)
9ae3a8
+{
9ae3a8
+    test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
9ae3a8
+}
9ae3a8
+
9ae3a8
 static void output_visitor_test_add(const char *testpath,
9ae3a8
                                     TestOutputVisitorData *data,
9ae3a8
                                     void (*test_func)(TestOutputVisitorData *data, const void *user_data))
9ae3a8
@@ -471,6 +779,30 @@ int main(int argc, char **argv)
9ae3a8
                             &out_visitor_data, test_visitor_out_list_qapi_free);
9ae3a8
     output_visitor_test_add("/visitor/output/union",
9ae3a8
                             &out_visitor_data, test_visitor_out_union);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/int",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_int);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/int8",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_int8);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/int16",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_int16);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/int32",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_int32);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/int64",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_int64);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/uint8",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_uint8);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/uint16",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_uint16);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/uint32",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_uint32);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/uint64",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_uint64);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/bool",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_bool);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/string",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_str);
9ae3a8
+    output_visitor_test_add("/visitor/output/native_list/number",
9ae3a8
+                            &out_visitor_data, test_visitor_out_native_list_number);
9ae3a8
 
9ae3a8
     g_test_run();
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8