From: Bartosz Golaszewski Date: Sun, 5 Nov 2017 13:21:54 +0000 (+0100) Subject: tests: provide test_build_str() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=8cae086f0497ccd89f06a3568bce276002126635;p=qemu-gpiodev%2Flibgpiod.git tests: provide test_build_str() Many test cases use asprintf() to build custom strings. This routine can fail and the return value is tested using the TEST_ASSERT macros but this is not the subject of the actual testing. The test case must also free the string allocated by this function. As a simplification: pull this functionality into the testing framework so that we can build custom strings without keeping track of resources. The new function will also make the whole test suite die if the internal asprintf() call fails. Signed-off-by: Bartosz Golaszewski --- diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c index dcf3728..d64a24d 100644 --- a/tests/gpiod-test.c +++ b/tests/gpiod-test.c @@ -74,6 +74,7 @@ struct test_context { size_t num_chips; bool test_failed; char *failed_msg; + char *custom_str; struct event_thread event; struct gpiotool_proc tool_proc; bool running; @@ -976,6 +977,9 @@ static void teardown_test(void) free(globals.test_ctx.chips); + if (globals.test_ctx.custom_str) + free(globals.test_ctx.custom_str); + if (mockup_loaded()) { status = kmod_module_remove_module(globals.module, 0); if (status) @@ -1152,3 +1156,23 @@ bool test_regex_match(const char *str, const char *pattern) return ret; } + +const char *test_build_str(const char *fmt, ...) +{ + va_list va; + char *str; + int rv; + + if (globals.test_ctx.custom_str) + free(globals.test_ctx.custom_str); + + va_start(va, fmt); + rv = vasprintf(&str, fmt, va); + va_end(va); + if (rv < 0) + die_perr("error creating custom string"); + + globals.test_ctx.custom_str = str; + + return str; +} diff --git a/tests/gpiod-test.h b/tests/gpiod-test.h index f753171..0f45188 100644 --- a/tests/gpiod-test.h +++ b/tests/gpiod-test.h @@ -133,6 +133,12 @@ void test_free_chip_iter_noclose(struct gpiod_chip_iter **iter); bool test_regex_match(const char *str, const char *pattern); +/* + * Return a custom string built according to printf() formatting rules. The + * returned string is valid until the next call to this routine. + */ +const char *test_build_str(const char *fmt, ...) TEST_PRINTF(1, 2); + #define TEST_ASSERT(statement) \ do { \ if (!(statement)) { \