[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v5 10/15] tests: Clean up test-string-output-visitor
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v5 10/15] tests: Clean up test-string-output-visitor |
Date: |
Thu, 9 Jun 2016 10:48:41 -0600 |
Use &error_abort and error_free_or_abort() in more places, use
the generated qapi_free_intList() instead of open-coding it,
reduce the scope of some variables, avoid code duplication
during test setup with visitor_output_setup_internal(), and
copy the visitor_reset() concept from the qmp-output test to
the string-output test.
Signed-off-by: Eric Blake <address@hidden>
---
v5: new patch, split off independent cleanups from v4 11/28
---
tests/test-string-output-visitor.c | 61 ++++++++++++++++++--------------------
1 file changed, 29 insertions(+), 32 deletions(-)
diff --git a/tests/test-string-output-visitor.c
b/tests/test-string-output-visitor.c
index bfa3e8e..d57a4d3 100644
--- a/tests/test-string-output-visitor.c
+++ b/tests/test-string-output-visitor.c
@@ -25,26 +25,26 @@ typedef struct TestOutputVisitorData {
bool human;
} TestOutputVisitorData;
+static void visitor_output_setup_internal(TestOutputVisitorData *data,
+ bool human)
+{
+ data->human = human;
+ data->sov = string_output_visitor_new(human);
+ g_assert(data->sov);
+ data->ov = string_output_get_visitor(data->sov);
+ g_assert(data->ov);
+}
+
static void visitor_output_setup(TestOutputVisitorData *data,
const void *unused)
{
- data->human = false;
- data->sov = string_output_visitor_new(data->human);
- g_assert(data->sov != NULL);
-
- data->ov = string_output_get_visitor(data->sov);
- g_assert(data->ov != NULL);
+ return visitor_output_setup_internal(data, false);
}
static void visitor_output_setup_human(TestOutputVisitorData *data,
const void *unused)
{
- data->human = true;
- data->sov = string_output_visitor_new(data->human);
- g_assert(data->sov != NULL);
-
- data->ov = string_output_get_visitor(data->sov);
- g_assert(data->ov != NULL);
+ return visitor_output_setup_internal(data, true);
}
static void visitor_output_teardown(TestOutputVisitorData *data,
@@ -55,6 +55,14 @@ static void visitor_output_teardown(TestOutputVisitorData
*data,
data->ov = NULL;
}
+static void visitor_reset(TestOutputVisitorData *data)
+{
+ bool human = data->human;
+
+ visitor_output_teardown(data, NULL);
+ visitor_output_setup_internal(data, human);
+}
+
static void test_visitor_out_int(TestOutputVisitorData *data,
const void *unused)
{
@@ -106,12 +114,7 @@ static void test_visitor_out_intList(TestOutputVisitorData
*data,
"0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
}
g_free(str);
- while (list) {
- intList *tmp2;
- tmp2 = list->next;
- g_free(list);
- list = tmp2;
- }
+ qapi_free_intList(list);
}
static void test_visitor_out_bool(TestOutputVisitorData *data,
@@ -171,12 +174,10 @@ static void
test_visitor_out_no_string(TestOutputVisitorData *data,
const void *unused)
{
char *string = NULL;
- Error *err = NULL;
char *str;
/* A null string should return "" */
- visit_type_str(data->ov, NULL, &string, &err);
- g_assert(!err);
+ visit_type_str(data->ov, NULL, &string, &error_abort);
str = string_output_get_string(data->sov);
g_assert(str != NULL);
@@ -191,27 +192,24 @@ static void
test_visitor_out_no_string(TestOutputVisitorData *data,
static void test_visitor_out_enum(TestOutputVisitorData *data,
const void *unused)
{
- Error *err = NULL;
char *str;
EnumOne i;
for (i = 0; i < ENUM_ONE__MAX; i++) {
- char *str_human;
-
- visit_type_EnumOne(data->ov, "unused", &i, &err);
- g_assert(!err);
-
- str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
+ visit_type_EnumOne(data->ov, "unused", &i, &error_abort);
str = string_output_get_string(data->sov);
g_assert(str != NULL);
if (data->human) {
+ char *str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
+
g_assert_cmpstr(str, ==, str_human);
+ g_free(str_human);
} else {
g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
}
- g_free(str_human);
- g_free(str);
+ g_free(str);
+ visitor_reset(data);
}
}
@@ -224,8 +222,7 @@ static void
test_visitor_out_enum_errors(TestOutputVisitorData *data,
for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
err = NULL;
visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
- g_assert(err);
- error_free(err);
+ error_free_or_abort(&err);
}
}
--
2.5.5
- [Qemu-devel] [PATCH v5 00/15] Add clone visitor, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 08/15] string-output-visitor: Favor new visit_free() function, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 01/15] qapi: Improve use of qmp/types.h, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 10/15] tests: Clean up test-string-output-visitor,
Eric Blake <=
- [Qemu-devel] [PATCH v5 04/15] qapi: Add new visit_free() function, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 11/15] tests: Factor out common code in qapi output tests, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 15/15] replay: Use new QAPI cloning, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 06/15] string-input-visitor: Favor new visit_free() function, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 07/15] qmp-input-visitor: Favor new visit_free() function, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 14/15] sockets: Use new QAPI cloning, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 13/15] qapi: Add new clone visitor, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 02/15] qemu-img: Don't leak errors when outputting JSON, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 09/15] qmp-output-visitor: Favor new visit_free() function, Eric Blake, 2016/06/09
- [Qemu-devel] [PATCH v5 03/15] qapi: Add parameter to visit_end_*, Eric Blake, 2016/06/09