diff --git a/tests/test.c b/tests/test.c index 8e37e936..a56cbcd6 100644 --- a/tests/test.c +++ b/tests/test.c @@ -32,15 +32,8 @@ static void test_loop_print_result(bool passed, unsigned int i, } } -/* - * lhs and rhs should be newly allocated strings so we can - * free them at the end of this function. - */ -void test_strings_equal(gchar *lhs, gchar *rhs, unsigned int line) +static void test_equal_common(gchar *lhs, gchar *rhs, bool passed) { - bool passed = strcmp(lhs, rhs) == 0; - - test_print_result(passed, line); if (!passed) { printf("\t\t Actual: %s\n", lhs); printf("\t\tExpected: %s\n", rhs); @@ -49,15 +42,32 @@ void test_strings_equal(gchar *lhs, gchar *rhs, unsigned int line) g_free(rhs); } +static void test_not_equal_common(gchar *lhs, gchar *rhs, bool passed) +{ + if (!passed) + printf("\t\tUnexpected: %s\n", lhs); + g_free(lhs); + g_free(rhs); +} + +/* + * lhs and rhs should be newly allocated strings so we can + * free them in test{_not}_equal_common(). + */ +void test_strings_equal(gchar *lhs, gchar *rhs, unsigned int line) +{ + bool passed = strcmp(lhs, rhs) == 0; + + test_print_result(passed, line); + test_equal_common(lhs, rhs, passed); +} + void test_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int line) { bool passed = strcmp(lhs, rhs) != 0; test_print_result(passed, line); - if (!passed) - printf("\t\tUnexpected: %s\n", lhs); - g_free(lhs); - g_free(rhs); + test_not_equal_common(lhs, rhs, passed); } void loop_strings_equal(gchar *lhs, gchar *rhs, unsigned int i, @@ -66,13 +76,7 @@ void loop_strings_equal(gchar *lhs, gchar *rhs, unsigned int i, bool passed = strcmp(lhs, rhs) == 0; test_loop_print_result(passed, i, line); - if (!passed) { - printf("\t\t Actual: %s\n", lhs); - printf("\t\tExpected: %s\n", rhs); - } - - g_free(lhs); - g_free(rhs); + test_equal_common(lhs, rhs, passed); } void loop_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int i, @@ -81,11 +85,7 @@ void loop_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int i, bool passed = strcmp(lhs, rhs) != 0; test_loop_print_result(passed, i, line); - if (!passed) - printf("\t\tUnexpected: %s\n", lhs); - - g_free(lhs); - g_free(rhs); + test_not_equal_common(lhs, rhs, passed); } static void run_test(struct UnitTest *test)