From patchwork Fri Nov 6 06:35:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 540817 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D802E1402C3 for ; Fri, 6 Nov 2015 17:40:16 +1100 (AEDT) Received: from localhost ([::1]:36921 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zuah4-0008Cf-Qz for incoming@patchwork.ozlabs.org; Fri, 06 Nov 2015 01:40:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45692) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zuad0-0008VN-L4 for qemu-devel@nongnu.org; Fri, 06 Nov 2015 01:36:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zuacy-000475-AW for qemu-devel@nongnu.org; Fri, 06 Nov 2015 01:36:02 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51845) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zuacy-00046s-17 for qemu-devel@nongnu.org; Fri, 06 Nov 2015 01:36:00 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id BC54E8F50E; Fri, 6 Nov 2015 06:35:59 +0000 (UTC) Received: from red.redhat.com (ovpn-113-80.phx2.redhat.com [10.3.113.80]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tA66Ztmk008894; Fri, 6 Nov 2015 01:35:59 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 5 Nov 2015 23:35:31 -0700 Message-Id: <1446791754-23823-8-git-send-email-eblake@redhat.com> In-Reply-To: <1446791754-23823-1-git-send-email-eblake@redhat.com> References: <1446791754-23823-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v10 07/30] qapi: Simplify error cleanup in test-qmp-* X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org By moving err into data, we can let test teardown take care of cleaning up any collected error; it also gives us fewer lines of code between repeated tests where init runs teardown on our behalf. Rather than duplicate code between .c files, I added a new test-qmp-common.h. I debated about putting error_free_or_abort() in error.h, but it seems like something that is only useful for tests. Signed-off-by: Eric Blake --- v10: split in two pieces v9: move earlier in series (was 14/17), reword commit message v8: no change v7: pick up whitespace changes dropped from earlier commit v6: new patch --- tests/test-qmp-commands.c | 8 ++++---- tests/test-qmp-common.h | 22 ++++++++++++++++++++++ tests/test-qmp-event.c | 1 + tests/test-qmp-input-strict.c | 22 ++++++++-------------- tests/test-qmp-input-visitor.c | 27 ++++++++------------------- 5 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 tests/test-qmp-common.h diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c index f23d8ea..9f65fc2 100644 --- a/tests/test-qmp-commands.c +++ b/tests/test-qmp-commands.c @@ -1,12 +1,13 @@ #include #include "qemu-common.h" +#include "test-qmp-common.h" #include "qapi/qmp/types.h" #include "test-qmp-commands.h" #include "qapi/qmp/dispatch.h" #include "qemu/module.h" #include "qapi/qmp-input-visitor.h" -#include "tests/test-qapi-types.h" -#include "tests/test-qapi-visit.h" +#include "test-qapi-types.h" +#include "test-qapi-visit.h" void qmp_user_def_cmd(Error **errp) { @@ -225,8 +226,7 @@ static void test_dealloc_partial(void) assert(ud2->dict1 == NULL); /* confirm & release construction error */ - assert(err != NULL); - error_free(err); + error_free_or_abort(&err); /* tear down partial object */ qapi_free_UserDefTwo(ud2); diff --git a/tests/test-qmp-common.h b/tests/test-qmp-common.h new file mode 100644 index 0000000..043f49c --- /dev/null +++ b/tests/test-qmp-common.h @@ -0,0 +1,22 @@ +/* + * Code common to qmp/qapi unit-tests. + * + * Copyright (C) 2015 Red Hat, Inc. + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#ifndef TEST_QMP_COMMON_H__ +#define TEST_QMP_COMMON_H__ + +/* Expect an error, abort() if there is none. */ +static inline void error_free_or_abort(Error **errp) +{ + g_assert(*errp); + error_free(*errp); + *errp = NULL; +} + +#endif diff --git a/tests/test-qmp-event.c b/tests/test-qmp-event.c index 035c65c..c0ac3ea 100644 --- a/tests/test-qmp-event.c +++ b/tests/test-qmp-event.c @@ -22,6 +22,7 @@ #include "qapi/qmp/qint.h" #include "qapi/qmp/qobject.h" #include "qapi/qmp-event.h" +#include "test-qmp-common.h" typedef struct TestEventData { QDict *expect; diff --git a/tests/test-qmp-input-strict.c b/tests/test-qmp-input-strict.c index 6226397..7e15b09 100644 --- a/tests/test-qmp-input-strict.c +++ b/tests/test-qmp-input-strict.c @@ -15,6 +15,7 @@ #include #include "qemu-common.h" +#include "test-qmp-common.h" #include "qapi/qmp-input-visitor.h" #include "test-qapi-types.h" #include "test-qapi-visit.h" @@ -180,8 +181,7 @@ static void test_validate_fail_struct(TestInputVisitorData *data, v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); visit_type_TestStruct(v, &p, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); if (p) { g_free(p->string); } @@ -198,8 +198,7 @@ static void test_validate_fail_struct_nested(TestInputVisitorData *data, v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); visit_type_UserDefTwo(v, &udp, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefTwo(udp); } @@ -213,8 +212,7 @@ static void test_validate_fail_list(TestInputVisitorData *data, v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); visit_type_UserDefOneList(v, &head, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefOneList(head); } @@ -229,8 +227,7 @@ static void test_validate_fail_union_native_list(TestInputVisitorData *data, "{ 'type': 'integer', 'data' : [ 'string' ] }"); visit_type_UserDefNativeListUnion(v, &tmp, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefNativeListUnion(tmp); } @@ -244,8 +241,7 @@ static void test_validate_fail_union_flat(TestInputVisitorData *data, v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefFlatUnion(tmp); } @@ -260,8 +256,7 @@ static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); visit_type_UserDefFlatUnion2(v, &tmp, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefFlatUnion2(tmp); } @@ -275,8 +270,7 @@ static void test_validate_fail_alternate(TestInputVisitorData *data, v = validate_test_init(data, "3.14"); visit_type_UserDefAlternate(v, &tmp, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); qapi_free_UserDefAlternate(tmp); } diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c index 46566bc..2b59b6f 100644 --- a/tests/test-qmp-input-visitor.c +++ b/tests/test-qmp-input-visitor.c @@ -14,6 +14,7 @@ #include #include "qemu-common.h" +#include "test-qmp-common.h" #include "qapi/qmp-input-visitor.h" #include "test-qapi-types.h" #include "test-qapi-visit.h" @@ -111,8 +112,7 @@ static void test_visitor_in_int_overflow(TestInputVisitorData *data, v = visitor_input_test_init(data, "%f", DBL_MAX); visit_type_int(v, &res, NULL, &err); - g_assert(err); - error_free(err); + error_free_or_abort(&err); } static void test_visitor_in_bool(TestInputVisitorData *data, @@ -319,9 +319,7 @@ static void test_visitor_in_alternate(TestInputVisitorData *data, v = visitor_input_test_init(data, "false"); visit_type_UserDefAlternate(v, &tmp, NULL, &err); - g_assert(err); - error_free(err); - err = NULL; + error_free_or_abort(&err); qapi_free_UserDefAlternate(tmp); } @@ -341,9 +339,7 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data, v = visitor_input_test_init(data, "42"); visit_type_AltStrBool(v, &asb, NULL, &err); - g_assert(err); - error_free(err); - err = NULL; + error_free_or_abort(&err); qapi_free_AltStrBool(asb); /* FIXME: Order of alternate should not affect semantics; asn should @@ -352,9 +348,7 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data, visit_type_AltStrNum(v, &asn, NULL, &err); /* FIXME g_assert_cmpint(asn->type, == ALT_STR_NUM_KIND_N); */ /* FIXME g_assert_cmpfloat(asn->u.n, ==, 42); */ - g_assert(err); - error_free(err); - err = NULL; + error_free_or_abort(&err); qapi_free_AltStrNum(asn); v = visitor_input_test_init(data, "42"); @@ -385,9 +379,7 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data, v = visitor_input_test_init(data, "42.5"); visit_type_AltStrBool(v, &asb, NULL, &err); - g_assert(err); - error_free(err); - err = NULL; + error_free_or_abort(&err); qapi_free_AltStrBool(asb); v = visitor_input_test_init(data, "42.5"); @@ -404,9 +396,7 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data, v = visitor_input_test_init(data, "42.5"); visit_type_AltStrInt(v, &asi, NULL, &err); - g_assert(err); - error_free(err); - err = NULL; + error_free_or_abort(&err); qapi_free_AltStrInt(asi); v = visitor_input_test_init(data, "42.5"); @@ -713,12 +703,11 @@ static void test_visitor_in_errors(TestInputVisitorData *data, v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }"); visit_type_TestStruct(v, &p, NULL, &err); - g_assert(err); + error_free_or_abort(&err); /* FIXME - a failed parse should not leave a partially-allocated p * for us to clean up; this could cause callers to leak memory. */ g_assert(p->string == NULL); - error_free(err); g_free(p->string); g_free(p); }