yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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