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.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include "check.h"
#include "check_error.h"
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);
}
/* 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
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
"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",
-1,
-1,
-1,
+ -1,
+ -1,
+ -1,
+ -1,
-1};
for (i = 0; i < sub_nfailed; i++) {
CK_FAILURE,
CK_FAILURE,
CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
CK_ERROR,
CK_ERROR,
CK_ERROR,
"Simple Tests",
"Simple Tests",
"Simple Tests",
+ "Simple Tests",
+ "Simple Tests",
+ "Simple Tests",
+ "Simple Tests",
"Signal Tests",
"Signal Tests",
"Signal Tests",
"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",
CK_PASS,
CK_FAILURE,
CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
+ CK_FAILURE,
CK_ERROR,
CK_ERROR,
CK_ERROR,
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
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);