From fbd2458c560cc0f92590e69b9811a52a2d1b9c50 Mon Sep 17 00:00:00 2001 From: hugo303 Date: Tue, 17 Aug 2004 09:50:36 +0000 Subject: [PATCH] Applied varargs patch (#933411) and added test cases. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@180 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- check/ChangeLog | 3 ++ check/src/check.c | 12 ++++++-- check/src/check.h.in | 19 +++++++----- check/tests/check_check_master.c | 34 ++++++++++++++++++--- check/tests/check_check_sub.c | 52 ++++++++++++++++++++++++++------ 5 files changed, 97 insertions(+), 23 deletions(-) diff --git a/check/ChangeLog b/check/ChangeLog index c2d8634..61f9fca 100644 --- a/check/ChangeLog +++ b/check/ChangeLog @@ -1,5 +1,8 @@ 2004-08-17 hugo303 + * src/check.c, src/check.h.in, tests/check_check_master.c, + tests/check_check_sub.c, ChangeLog + Applied varargs patch (#933411) and added test cases. * src/check.h.in tests/check_check_master.c tests/check_check_sub.c: Applied fail_if (#709167) patch. diff --git a/check/src/check.c b/check/src/check.c index 42ab77c..2a3cf5d 100644 --- a/check/src/check.c +++ b/check/src/check.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "check.h" #include "check_error.h" @@ -168,14 +169,21 @@ void _mark_point (const char *file, int line) send_loc_info (get_send_key(), file, line); } -void _fail_unless (int result, const char *file, int line, const char * msg) +void _fail_unless (int result, const char *file, + int line, const char * msg, ...) { if (msg == NULL) eprintf ("_fail_unless() called with NULL msg",__FILE__,__LINE__); send_loc_info (get_send_key(), file, line); if (!result) { - send_failure_info (get_send_key(), msg); + va_list ap; + char buf[BUFSIZ]; + + va_start(ap,msg); + vsnprintf(buf, BUFSIZ, msg, ap); + va_end(ap); + send_failure_info (get_send_key(), buf); if (cur_fork_status() == CK_FORK) exit(1); } diff --git a/check/src/check.h.in b/check/src/check.h.in index bf77b52..746fee5 100644 --- a/check/src/check.h.in +++ b/check/src/check.h.in @@ -155,18 +155,21 @@ static void __testname (void)\ /* Fail the test case unless expr is true */ -#define fail_unless(expr,msg)\ - _fail_unless(expr,__FILE__,__LINE__, msg ? msg : "Assertion '"#expr"' failed") +#define fail_unless(expr, msg, args...)\ + _fail_unless(expr, __FILE__, __LINE__,\ + msg ? msg : "Assertion '"#expr"' failed", ##args) /* Fail the test case if expr is true */ -#define fail_if(expr, msg)\ - _fail_unless(!(expr),__FILE__,__LINE__,msg ?msg :"Assertion '"#expr"' failed") - -/* Non macro version of #fail_unless, with more complicated interface */ -void _fail_unless (int result, const char *file, int line, const char *msg); +#define fail_if(expr, msg, args...)\ + _fail_unless(!(expr), __FILE__, __LINE__,\ + msg ?msg :"Assertion '"#expr"' failed", ##args) /* Always fail */ -#define fail(msg) _fail_unless(0,__FILE__,__LINE__,msg) +#define fail(msg, args...) _fail_unless(0, __FILE__, __LINE__, msg, ##args) + +/* Non macro version of #fail_unless, with more complicated interface */ +void _fail_unless (int result, const char *file, + int line, const char *msg, ...); /* Mark the last point reached in a unit test diff --git a/check/tests/check_check_master.c b/check/tests/check_check_master.c index 5b96234..ab65326 100644 --- a/check/tests/check_check_master.c +++ b/check/tests/check_check_master.c @@ -14,13 +14,15 @@ TestResult **tr_all_array; START_TEST(test_check_nfailures) { - fail_unless (sub_nfailed == 12, "Unexpected number of failures received"); + fail_unless (sub_nfailed == 16, + "Unexpected number of failures received, %d.", sub_nfailed); } END_TEST START_TEST(test_check_ntests_run) { - fail_unless (sub_ntests == 14, "Unexpected number of tests run"); + fail_unless (sub_ntests == 18, + "Unexpected number of tests run, %d.", sub_ntests); } END_TEST @@ -34,7 +36,11 @@ START_TEST(test_check_failure_msgs) "This test should fail", /* "Test passed", */ "This test should fail", - "Assertion '2==3' failed", + "Assertion '2 == 3' failed", + "Assertion '2 != 3' failed", + "3 != 4", + "5 != 6", + "7 == 7", "Received signal 11", "Received signal 8", "Received signal 8", @@ -71,6 +77,10 @@ START_TEST(test_check_failure_lnos) -1, -1, -1, + -1, + -1, + -1, + -1, -1}; for (i = 0; i < sub_nfailed; i++) { @@ -95,6 +105,10 @@ START_TEST(test_check_failure_ftypes) CK_FAILURE, CK_FAILURE, CK_FAILURE, + CK_FAILURE, + CK_FAILURE, + CK_FAILURE, + CK_FAILURE, CK_ERROR, CK_ERROR, CK_ERROR, @@ -132,6 +146,10 @@ START_TEST(test_check_failure_tcnames) "Simple Tests", "Simple Tests", "Simple Tests", + "Simple Tests", + "Simple Tests", + "Simple Tests", + "Simple Tests", "Signal Tests", "Signal Tests", "Signal Tests", @@ -164,7 +182,11 @@ START_TEST(test_check_all_msgs) "This test should fail", "Passed", "This test should fail", - "Assertion '2==3' failed", + "Assertion '2 == 3' failed", + "Assertion '2 != 3' failed", + "3 != 4", + "5 != 6", + "7 == 7", "Received signal 11", "Received signal 8", "Received signal 8", @@ -197,6 +219,10 @@ START_TEST(test_check_all_ftypes) CK_PASS, CK_FAILURE, CK_FAILURE, + CK_FAILURE, + CK_FAILURE, + CK_FAILURE, + CK_FAILURE, CK_ERROR, CK_ERROR, CK_ERROR, diff --git a/check/tests/check_check_sub.c b/check/tests/check_check_sub.c index b062576..8c82e7d 100644 --- a/check/tests/check_check_sub.c +++ b/check/tests/check_check_sub.c @@ -18,33 +18,63 @@ END_TEST START_TEST(test_pass) { - fail_unless (1==1, "This test should pass"); - fail_unless (9999, "This test should pass"); + fail_unless(1 == 1, "This test should pass"); + fail_unless(9999, "This test should pass"); } END_TEST START_TEST(test_fail) { - fail_unless (1==2, "This test should fail"); + fail_unless(1 == 2, "This test should fail"); +} +END_TEST + +START_TEST(test_fail_if_pass) +{ + fail_if(1 == 2, "This test should pass"); + fail_if(0, "This test should pass"); +} +END_TEST + +START_TEST(test_fail_if_fail) +{ + fail_if(1 == 1, "This test should fail"); } END_TEST START_TEST(test_fail_null_msg) { - fail_unless (2==3, NULL); + fail_unless(2 == 3, NULL); } END_TEST -START_TEST(test_fail_if_pass) +START_TEST(test_fail_if_null_msg) { - fail_if(1==2, "This test should pass"); - fail_if(0, "This test should pass"); + fail_if(2 != 3, NULL); } END_TEST -START_TEST(test_fail_if_fail) +START_TEST(test_fail_vararg_msg_1) +{ + int x = 3; + int y = 4; + fail_unless(x == y, "%d != %d", x, y); +} +END_TEST + +START_TEST(test_fail_vararg_msg_2) +{ + int x = 5; + int y = 6; + fail_if(x != y, "%d != %d", x, y); +} +END_TEST + +START_TEST(test_fail_vararg_msg_3) { - fail_if(1==1, "This test should fail"); + int x = 7; + int y = 7; + fail("%d == %d", x, y); } END_TEST @@ -140,6 +170,10 @@ Suite *make_sub_suite(void) tcase_add_test (tc_simple, test_fail_if_pass); tcase_add_test (tc_simple, test_fail_if_fail); tcase_add_test (tc_simple, test_fail_null_msg); + tcase_add_test (tc_simple, test_fail_if_null_msg); + tcase_add_test (tc_simple, test_fail_vararg_msg_1); + tcase_add_test (tc_simple, test_fail_vararg_msg_2); + tcase_add_test (tc_simple, test_fail_vararg_msg_3); tcase_add_test (tc_signal, test_segv); tcase_add_test (tc_signal, test_fpe); tcase_add_test (tc_signal, test_mark_point); -- 2.50.0