tests: use test_build_str() in test cases
authorBartosz Golaszewski <bartekgola@gmail.com>
Sun, 5 Nov 2017 13:31:46 +0000 (14:31 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Sun, 5 Nov 2017 13:31:46 +0000 (14:31 +0100)
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 <bartekgola@gmail.com>
tests/tests-chip.c
tests/tests-gpiodetect.c
tests/tests-gpiofind.c
tests/tests-gpioinfo.c

index e7527ecb3d6cce94d8cb8f7aa3159a6f628aa8bb..4f5821843862f9ab9d4c7aea17940adadd21adf6 100644 (file)
@@ -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);
index a5b96718de92a1ee0c9452c536779ee21a5bbfef..b56db2a428c5b2b8b9debc98f711c989bb0f9d36 100644 (file)
 
 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,
index 6faec575690f0e929117e95258f06ca91ad3db24..c825c00ef0fdde2273208a8acd26b8bba54df647 100644 (file)
 
 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,
index d94c4b2d4aff9bd66b467050e2c6da2c78c26a31..8f0babe4c0ff44b328fb973f204acb208f57a89d 100644 (file)
 
 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(),