From 19a9f62a6573691cd56ff3767a13262eb95f32b2 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Sun, 5 Nov 2017 14:31:46 +0100 Subject: [PATCH] tests: use test_build_str() in test cases Shrink the test code by using the newly provided helper function for building custom strings wherever asprintf() was used before. Signed-off-by: Bartosz Golaszewski --- tests/tests-chip.c | 6 +-- tests/tests-gpiodetect.c | 28 ++++-------- tests/tests-gpiofind.c | 9 +--- tests/tests-gpioinfo.c | 94 +++++++++++++++------------------------- 4 files changed, 48 insertions(+), 89 deletions(-) diff --git a/tests/tests-chip.c b/tests/tests-chip.c index e7527ec..4f58218 100644 --- a/tests/tests-chip.c +++ b/tests/tests-chip.c @@ -78,13 +78,11 @@ static void chip_open_lookup(void) TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip_by_name = NULL; TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip_by_path = NULL; TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip_by_num = NULL; - TEST_CLEANUP(test_free_str) char *chip_num = NULL; - - TEST_ASSERT(asprintf(&chip_num, "%u", test_chip_num(1)) > 0); chip_by_name = gpiod_chip_open_lookup(test_chip_name(1)); chip_by_path = gpiod_chip_open_lookup(test_chip_path(1)); - chip_by_num = gpiod_chip_open_lookup(chip_num); + chip_by_num = gpiod_chip_open_lookup( + test_build_str("%u", test_chip_num(1))); chip_by_label = gpiod_chip_open_lookup("gpio-mockup-B"); TEST_ASSERT_NOT_NULL(chip_by_name); diff --git a/tests/tests-gpiodetect.c b/tests/tests-gpiodetect.c index a5b9671..b56db2a 100644 --- a/tests/tests-gpiodetect.c +++ b/tests/tests-gpiodetect.c @@ -16,31 +16,21 @@ static void gpiodetect_simple(void) { - TEST_CLEANUP(test_free_str) char *pattern0 = NULL; - TEST_CLEANUP(test_free_str) char *pattern1 = NULL; - TEST_CLEANUP(test_free_str) char *pattern2 = NULL; - int ret0, ret1, ret2; - - ret0 = asprintf(&pattern0, - "%s [gpio-mockup-A] (4 lines)", test_chip_name(0)); - ret1 = asprintf(&pattern1, - "%s [gpio-mockup-B] (8 lines)", test_chip_name(1)); - ret2 = asprintf(&pattern2, - "%s [gpio-mockup-C] (16 lines)", test_chip_name(2)); - - TEST_ASSERT(ret0 > 0); - TEST_ASSERT(ret1 > 0); - TEST_ASSERT(ret2 > 0); - test_tool_run("gpiodetect", (char *)NULL); test_tool_wait(); TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), pattern0); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), pattern1); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), pattern2); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s [gpio-mockup-A] (4 lines)", + test_chip_name(0))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s [gpio-mockup-B] (8 lines)", + test_chip_name(1))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s [gpio-mockup-C] (16 lines)", + test_chip_name(2))); TEST_ASSERT_NULL(test_tool_stderr()); } TEST_DEFINE(gpiodetect_simple, diff --git a/tests/tests-gpiofind.c b/tests/tests-gpiofind.c index 6faec57..c825c00 100644 --- a/tests/tests-gpiofind.c +++ b/tests/tests-gpiofind.c @@ -16,19 +16,14 @@ static void gpiofind_found(void) { - TEST_CLEANUP(test_free_str) char *output = NULL; - int rv; - - rv = asprintf(&output, "%s 7\n", test_chip_name(1)); - TEST_ASSERT(rv > 0); - test_tool_run("gpiofind", "gpio-mockup-B-7", (char *)NULL); test_tool_wait(); TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_EQ(test_tool_stdout(), output); + TEST_ASSERT_STR_EQ(test_tool_stdout(), + test_build_str("%s 7\n", test_chip_name(1))); TEST_ASSERT_NULL(test_tool_stderr()); } TEST_DEFINE(gpiofind_found, diff --git a/tests/tests-gpioinfo.c b/tests/tests-gpioinfo.c index d94c4b2..8f0babe 100644 --- a/tests/tests-gpioinfo.c +++ b/tests/tests-gpioinfo.c @@ -16,23 +16,18 @@ static void gpioinfo_dump_all_chips(void) { - TEST_CLEANUP(test_free_str) char *ptrn_line0 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line1 = NULL; - int rv0, rv1; - - rv0 = asprintf(&ptrn_line0, "%s - 4 lines:", test_chip_name(0)); - rv1 = asprintf(&ptrn_line1, "%s - 8 lines:", test_chip_name(1)); - TEST_ASSERT(rv0 > 0); - TEST_ASSERT(rv1 > 0); - test_tool_run("gpioinfo", (char *)NULL); test_tool_wait(); TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line0); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line1); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(0))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 8 lines:", + test_chip_name(1))); TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+output\\s+active-high"); TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), @@ -45,20 +40,8 @@ TEST_DEFINE(gpioinfo_dump_all_chips, static void gpioinfo_dump_all_chips_one_exported(void) { TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line0 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line1 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_lineinfo = NULL; struct gpiod_line *line; - int rv0, rv1, rv2; - - rv0 = asprintf(&ptrn_line0, "%s - 4 lines:", test_chip_name(0)); - rv1 = asprintf(&ptrn_line1, "%s - 8 lines:", test_chip_name(1)); - rv2 = asprintf(&ptrn_lineinfo, - "\\s+line\\s+7:\\s+unnamed\\s+\\\"%s\\\"\\s+input\\s+active-low", - TEST_CONSUMER); - TEST_ASSERT(rv0 > 0); - TEST_ASSERT(rv1 > 0); - TEST_ASSERT(rv2 > 0); + int rv; chip = gpiod_chip_open(test_chip_path(1)); TEST_ASSERT_NOT_NULL(chip); @@ -66,9 +49,9 @@ static void gpioinfo_dump_all_chips_one_exported(void) line = gpiod_chip_get_line(chip, 7); TEST_ASSERT_NOT_NULL(line); - rv2 = gpiod_line_request_input_flags(line, TEST_CONSUMER, + rv = gpiod_line_request_input_flags(line, TEST_CONSUMER, GPIOD_LINE_REQUEST_ACTIVE_LOW); - TEST_ASSERT_RET_OK(rv2); + TEST_ASSERT_RET_OK(rv); test_tool_run("gpioinfo", (char *)NULL); test_tool_wait(); @@ -76,11 +59,16 @@ static void gpioinfo_dump_all_chips_one_exported(void) TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line0); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line1); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(0))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 8 lines:", + test_chip_name(1))); TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+output\\s+active-high"); - TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), ptrn_lineinfo); + TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), + "\\s+line\\s+7:\\s+unnamed\\s+\\\"" TEST_CONSUMER "\\\"\\s+input\\s+active-low"); } TEST_DEFINE(gpioinfo_dump_all_chips_one_exported, "tools: gpioinfo - dump all chips (one line exported)", @@ -88,23 +76,18 @@ TEST_DEFINE(gpioinfo_dump_all_chips_one_exported, static void gpioinfo_dump_one_chip(void) { - TEST_CLEANUP(test_free_str) char *ptrn_line0 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line1 = NULL; - int rv0, rv1; - - rv0 = asprintf(&ptrn_line0, "%s - 8 lines:", test_chip_name(0)); - rv1 = asprintf(&ptrn_line1, "%s - 4 lines:", test_chip_name(1)); - TEST_ASSERT(rv0 > 0); - TEST_ASSERT(rv1 > 0); - test_tool_run("gpioinfo", test_chip_name(1), (char *)NULL); test_tool_wait(); TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_NOTCONT(test_tool_stdout(), ptrn_line0); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line1); + TEST_ASSERT_STR_NOTCONT(test_tool_stdout(), + test_build_str("%s - 8 lines:", + test_chip_name(0))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(1))); TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+output\\s+active-high"); TEST_ASSERT_REGEX_NOMATCH(test_tool_stdout(), @@ -116,21 +99,6 @@ TEST_DEFINE(gpioinfo_dump_one_chip, static void gpioinfo_dump_all_but_one_chip(void) { - TEST_CLEANUP(test_free_str) char *ptrn_line0 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line1 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line2 = NULL; - TEST_CLEANUP(test_free_str) char *ptrn_line3 = NULL; - int rv0, rv1, rv2, rv3; - - rv0 = asprintf(&ptrn_line0, "%s - 4 lines:", test_chip_name(0)); - rv1 = asprintf(&ptrn_line1, "%s - 4 lines:", test_chip_name(1)); - rv2 = asprintf(&ptrn_line2, "%s - 8 lines:", test_chip_name(2)); - rv3 = asprintf(&ptrn_line3, "%s - 4 lines:", test_chip_name(3)); - TEST_ASSERT(rv0 > 0); - TEST_ASSERT(rv1 > 0); - TEST_ASSERT(rv2 > 0); - TEST_ASSERT(rv3 > 0); - test_tool_run("gpioinfo", test_chip_name(0), test_chip_name(1), test_chip_name(3), (char *)NULL); test_tool_wait(); @@ -138,10 +106,18 @@ static void gpioinfo_dump_all_but_one_chip(void) TEST_ASSERT(test_tool_exited()); TEST_ASSERT_RET_OK(test_tool_exit_status()); TEST_ASSERT_NOT_NULL(test_tool_stdout()); - TEST_ASSERT_STR_NOTCONT(test_tool_stdout(), ptrn_line2); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line0); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line1); - TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), ptrn_line3); + TEST_ASSERT_STR_NOTCONT(test_tool_stdout(), + test_build_str("%s - 8 lines:", + test_chip_name(2))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(0))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(1))); + TEST_ASSERT_STR_CONTAINS(test_tool_stdout(), + test_build_str("%s - 4 lines:", + test_chip_name(3))); TEST_ASSERT_REGEX_MATCH(test_tool_stdout(), "\\s+line\\s+0:\\s+unnamed\\s+unused\\s+output\\s+active-high"); TEST_ASSERT_REGEX_NOMATCH(test_tool_stdout(), -- 2.30.2