From 4449bc3481ca92a1155c9aac972d6866362fd6b0 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Wed, 8 Apr 2015 09:43:45 +0200 Subject: [PATCH] tester: redefine CU_* macros to enable IF(! CU_macro()) { ... } syntax and goto end handling --- mediastreamer2 | 2 +- tester/common/bc_tester_utils.c | 52 +++++++++++--------- tester/common/bc_tester_utils.h | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 23 deletions(-) diff --git a/mediastreamer2 b/mediastreamer2 index 1f3306fb1..af994a131 160000 --- a/mediastreamer2 +++ b/mediastreamer2 @@ -1 +1 @@ -Subproject commit 1f3306fb1b243eb7e182c2f08b2dfc319c887b03 +Subproject commit af994a131d462d6b091be7f115a6341431a58de4 diff --git a/tester/common/bc_tester_utils.c b/tester/common/bc_tester_utils.c index 077ec0569..057217584 100644 --- a/tester/common/bc_tester_utils.c +++ b/tester/common/bc_tester_utils.c @@ -54,10 +54,8 @@ int xml_enabled = 0; char * suite_name; char * test_name; void (*tester_printf_va)(int level, const char *fmt, va_list args); -int verbosity_info; -int verbosity_error; -static void tester_printf(int level, const char *fmt, ...) { +void bc_tester_printf(int level, const char *fmt, ...) { va_list args; va_start (args, fmt); tester_printf_va(level, fmt, args); @@ -115,7 +113,7 @@ int bc_tester_nb_tests(const char *suite_name) { void bc_tester_list_suites() { int j; for(j=0;jpName); + bc_tester_printf(bc_printf_verbosity_error,"Suite initialization failed for [%s]", pSuite->pName); } static void suite_cleanup_failure_message_handler(const CU_pSuite pSuite) { - tester_printf(verbosity_error,"Suite cleanup failed for [%s]", pSuite->pName); + bc_tester_printf(bc_printf_verbosity_error,"Suite cleanup failed for [%s]", pSuite->pName); } #ifdef HAVE_CU_GET_SUITE static void suite_start_message_handler(const CU_pSuite pSuite) { - tester_printf(verbosity_info,"Suite [%s] started", pSuite->pName); + bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] started\n", pSuite->pName); } static void suite_complete_message_handler(const CU_pSuite pSuite, const CU_pFailureRecord pFailure) { - tester_printf(verbosity_info,"Suite [%s] ended", pSuite->pName); + bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] ended\n", pSuite->pName); } static void test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) { - tester_printf(verbosity_info,"Suite [%s] Test [%s] started", pSuite->pName,pTest->pName); + bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] Test [%s] started", pSuite->pName,pTest->pName); } /*derivated from cunit*/ @@ -176,7 +174,7 @@ static void test_complete_message_handler(const CU_pTest pTest, } else { strncat(result, " passed", strlen(" passed")); } - tester_printf(verbosity_info,"%s\n", result); + bc_tester_printf(bc_printf_verbosity_info,"%s\n", result); } #endif @@ -206,26 +204,26 @@ int bc_tester_run_tests(const char *suite_name, const char *test_name) { #ifndef HAVE_CU_GET_SUITE if( suite_name ){ - tester_printf(verbosity_info, "Tester compiled without CU_get_suite() function, running all tests instead of suite '%s'", suite_name); + bc_tester_printf(bc_printf_verbosity_info, "Tester compiled without CU_get_suite() function, running all tests instead of suite '%s'", suite_name); } #else if (suite_name){ CU_pSuite suite; suite=CU_get_suite(suite_name); if (!suite) { - tester_printf(verbosity_error, "Could not find suite '%s'. Available suites are:", suite_name); + bc_tester_printf(bc_printf_verbosity_error, "Could not find suite '%s'. Available suites are:", suite_name); bc_tester_list_suites(); return -1; } else if (test_name) { CU_pTest test=CU_get_test_by_name(test_name, suite); if (!test) { - tester_printf(verbosity_error, "Could not find test '%s' in suite '%s'. Available tests are:", test_name, suite_name); + bc_tester_printf(bc_printf_verbosity_error, "Could not find test '%s' in suite '%s'. Available tests are:", test_name, suite_name); // do not use suite_name here, since this method is case sensitive bc_tester_list_tests(suite->pName); return -2; } else { CU_ErrorCode err= CU_run_test(suite, test); - if (err != CUE_SUCCESS) tester_printf(verbosity_error, "CU_basic_run_test error %d", err); + if (err != CUE_SUCCESS) bc_tester_printf(bc_printf_verbosity_error, "CU_basic_run_test error %d", err); } } else { CU_run_suite(suite); @@ -253,7 +251,7 @@ int bc_tester_run_tests(const char *suite_name, const char *test_name) { void bc_tester_helper(const char *name, const char* additionnal_helper) { - tester_printf(verbosity_info,"%s --help\n" + bc_tester_printf(bc_printf_verbosity_info,"%s --help\n" "\t\t\t--list-suites\n" "\t\t\t--list-tests \n" "\t\t\t--suite \n" @@ -271,8 +269,8 @@ void bc_tester_helper(const char *name, const char* additionnal_helper) { void bc_tester_init(void (*ftester_printf)(int level, const char *fmt, va_list args), int iverbosity_info, int iverbosity_error) { tester_printf_va = ftester_printf; - verbosity_error = iverbosity_error; - verbosity_info = iverbosity_info; + bc_printf_verbosity_error = iverbosity_error; + bc_printf_verbosity_info = iverbosity_info; } int bc_tester_parse_args(int argc, char **argv, int argid) @@ -301,12 +299,12 @@ int bc_tester_parse_args(int argc, char **argv, int argid) } else if (strcmp(argv[i], "--xml") == 0){ xml_enabled = 1; }else { - tester_printf(verbosity_error, "Unknown option \"%s\"\n", argv[i]); + bc_tester_printf(bc_printf_verbosity_error, "Unknown option \"%s\"\n", argv[i]); return -1; } if( xml_enabled && (suite_name || test_name) ){ - tester_printf(verbosity_error, "Cannot use both XML and specific test suite\n"); + bc_tester_printf(bc_printf_verbosity_error, "Cannot use both XML and specific test suite\n"); return -1; } @@ -346,7 +344,7 @@ void bc_tester_uninit() { } CU_cleanup_registry(); /*add missing final newline*/ - tester_printf(verbosity_info,""); + bc_tester_printf(bc_printf_verbosity_info,""); if( xml_enabled ){ /*create real xml file only if tester did not crash*/ @@ -363,3 +361,13 @@ void bc_tester_uninit() { nb_test_suites = 0; } } + +char * bc_tester_res(const char *name) { + char* file = NULL; + if (name) { + size_t len = strlen(bc_tester_read_dir_prefix) + 1 + strlen(name) + 1; + file = malloc(len); + snprintf(file, len, "%s/%s", bc_tester_read_dir_prefix, name); + } + return file; +} diff --git a/tester/common/bc_tester_utils.h b/tester/common/bc_tester_utils.h index ef14d98fd..374e9fad0 100644 --- a/tester/common/bc_tester_utils.h +++ b/tester/common/bc_tester_utils.h @@ -26,6 +26,9 @@ extern const char *bc_tester_read_dir_prefix; extern const char *bc_tester_writable_dir_prefix; +int bc_printf_verbosity_info; +int bc_printf_verbosity_error; + typedef void (*test_function_t)(void); typedef int (*test_suite_function_t)(const char *name); @@ -60,6 +63,7 @@ int bc_tester_parse_args(int argc, char** argv, int argid); int bc_tester_start(); void bc_tester_add_suite(test_suite_t *suite); void bc_tester_uninit(); +void bc_tester_printf(int level, const char *fmt, ...); int bc_tester_nb_suites(); int bc_tester_nb_tests(const char* name); @@ -71,6 +75,86 @@ int bc_tester_run_suite(test_suite_t *suite); int bc_tester_run_tests(const char *suite_name, const char *test_name); int bc_tester_suite_index(const char *suite_name); + +/** + * Get full path to the given resource + * + * @param name relative resource path (relative to bc_tester_writable_dir_prefix) + * @return path to the resource. Must be freed by caller. +*/ +char * bc_tester_res(const char *name); + +/*Redefine the CU_... macros WITHOUT final ';' semicolon, to allow IF conditions */ +#define BC_ASSERT_EQUAL(actual, expected) CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_PASS(msg) CU_assertImplementation(CU_TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT(value) CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE) +#define BC_ASSERT_FATAL(value) CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE) +#define BC_TEST(value) CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE) +#define BC_TEST_FATAL(value) CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE) +#define BC_ASSERT_TRUE(value) CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_TRUE_FATAL(value) CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_FALSE(value) CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_FALSE_FATAL(value) CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_EQUAL(actual, expected) CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_EQUAL_FATAL(actual, expected) CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_NOT_EQUAL(actual, expected) CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_NOT_EQUAL_FATAL(actual, expected) CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_PTR_EQUAL(actual, expected) CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_PTR_EQUAL_FATAL(actual, expected) CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_PTR_NOT_EQUAL(actual, expected) CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_PTR_NULL(value) CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_PTR_NULL_FATAL(value) CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_PTR_NOT_NULL(value) CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_PTR_NOT_NULL_FATAL(value) CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_STRING_EQUAL(actual, expected) CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_STRING_EQUAL_FATAL(actual, expected) CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_STRING_NOT_EQUAL(actual, expected) CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_NSTRING_EQUAL(actual, expected, count) CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE) +#define BC_ASSERT_GREATER(actual, expected) CU_assertImplementation(((actual) >= (expected)), __LINE__, ("CU_ASSERT_GREATER(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) +#define BC_ASSERT_LOWER(actual, expected) CU_assertImplementation((actual) <= (expected)), __LINE__, ("CU_ASSERT_LOWER(" #actual "," #expected ")"), __FILE__, "", CU_FALSE) + +/*Add some custom defines with logs in case of fail*/ +#define BC_ASSERT_EQUAL_INT(actual, expected) { \ + int cactual = (actual), cexpected = (expected); \ + if (! BC_ASSERT_EQUAL(cactual, cexpected)) { \ + bc_tester_printf(bc_printf_verbosity_error, "%s:%d - Expected " #actual " = " #expected " but was %d != %d\n", __FILE__, __LINE__, cactual, cexpected); \ + } \ +} +#define BC_ASSERT_GREATER_INT(actual, expected) { \ + int cactual = (actual), cexpected = (expected); \ + if (! BC_ASSERT_LOWER(cactual, cexpected)) { \ + bc_tester_printf(bc_printf_verbosity_error, "%s:%d - Expected " #actual " >= " #expected " but was %d < %d\n", __FILE__, __LINE__, cactual, cexpected); \ + } \ +} +#define BC_ASSERT_LOWER_INT(actual, expected) { \ + int cactual = (actual), cexpected = (expected); \ + if (! BC_ASSERT_LOWER(cactual, cexpected)) { \ + bc_tester_printf(bc_printf_verbosity_error, "%s:%d - Expected " #actual " <= " #expected " but was %d > %d\n", __FILE__, __LINE__, cactual, cexpected); \ + } \ +} +#define BC_ASSERT_GREATER_UINT64_T(actual, expected) { \ + uint64_t cactual = (actual), cexpected = (expected); \ + if (! BC_ASSERT_GREATER(cactual, cexpected)) { \ + bc_tester_printf(bc_printf_verbosity_error, "%s:%d - Expected " #actual " >= " #expected " but was %lu < %lu\n", __FILE__, __LINE__, (long unsigned)cactual, (long unsigned)cexpected); \ + } \ +} +#define BC_ASSERT_LOWER_UINT64_T(actual, expected) { \ + uint64_t cactual = (actual), cexpected = (expected); \ + if (! BC_ASSERT_GREATER(cactual, cexpected)) { \ + bc_tester_printf(bc_printf_verbosity_error, "%s:%d - Expected " #actual " <= " #expected " but was %lu > %lu\n", __FILE__, __LINE__, (long unsigned)cactual, (long unsigned)cexpected); \ + } \ +} + + #ifdef __cplusplus } #endif