From: hugo303 Date: Tue, 3 Feb 2009 13:36:16 +0000 (+0000) Subject: * Applied patch #1726574, with some modifications. X-Git-Tag: 0.10.0~635 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aa58addeb0f8a1901433be2b13b4690a148d396c;p=check * Applied patch #1726574, with some modifications. The idea is to create a mechanism to verify that functions exit early with specific exit values, analogous to the existing signal test mechanism. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@529 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/src/check.c b/src/check.c index a00cd91..49f1b68 100644 --- a/src/check.c +++ b/src/check.c @@ -124,7 +124,7 @@ void suite_add_tcase (Suite *s, TCase *tc) list_add_end (s->tclst, tc); } -void _tcase_add_test (TCase *tc, TFun fn, const char *name, int _signal, int start, int end) +void _tcase_add_test (TCase *tc, TFun fn, const char *name, int _signal, int allowed_exit_value, int start, int end) { TF * tf; if (tc == NULL || fn == NULL || name == NULL) @@ -134,6 +134,7 @@ void _tcase_add_test (TCase *tc, TFun fn, const char *name, int _signal, int sta tf->loop_start = start; tf->loop_end = end; tf->signal = _signal; /* 0 means no signal expected */ + tf->allowed_exit_value = allowed_exit_value; /* 0 is default successful exit */ tf->name = name; list_add_end (tc->tflst, tf); } diff --git a/src/check.h.in b/src/check.h.in index 6a5f191..1127c2c 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -131,7 +131,11 @@ TCase * CK_EXPORT tcase_create (const char *name); /* Add a test function with signal handling to a test case (macro version) */ #define tcase_add_test_raise_signal(tc,tf,signal) \ - _tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 1) + _tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 0, 1) + +/* Add a test function with an expected exit value to a test case (macro version) */ +#define tcase_add_exit_test(tc, tf, expected_exit_value) \ + _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),0,1) /* Add a looping test function to a test case (macro version) @@ -140,18 +144,22 @@ TCase * CK_EXPORT tcase_create (const char *name); available in the test. */ #define tcase_add_loop_test(tc,tf,s,e) \ - _tcase_add_test((tc),(tf),"" # tf "",0,(s),(e)) + _tcase_add_test((tc),(tf),"" # tf "",0,0,(s),(e)) /* Signal version of loop test. FIXME: add a test case; this is untested as part of Check's tests. */ #define tcase_add_loop_test_raise_signal(tc,tf,signal,s,e) \ - _tcase_add_test((tc),(tf),"" # tf "",(signal),(s),(e)) + _tcase_add_test((tc),(tf),"" # tf "",(signal),0,(s),(e)) + +/* allowed exit value version of loop test. */ +#define tcase_add_loop_exit_test(tc,tf,expected_exit_value,s,e) \ + _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),(s),(e)) /* Add a test function to a test case (function version -- use this when the macro won't work */ -void CK_EXPORT _tcase_add_test (TCase *tc, TFun tf, const char *fname, int _signal, int start, int end); +void CK_EXPORT _tcase_add_test (TCase *tc, TFun tf, const char *fname, int _signal, int allowed_exit_value, int start, int end); /* Add unchecked fixture setup/teardown functions to a test case diff --git a/src/check_impl.h b/src/check_impl.h index c8293b5..d0afbf1 100644 --- a/src/check_impl.h +++ b/src/check_impl.h @@ -33,6 +33,7 @@ typedef struct TF { int loop_end; const char *name; int signal; + unsigned char allowed_exit_value; } TF; struct Suite { diff --git a/src/check_run.c b/src/check_run.c index 484ebc0..e30e657 100644 --- a/src/check_run.c +++ b/src/check_run.c @@ -75,8 +75,10 @@ static TestResult *tcase_run_tfun_fork (SRunner *sr, TCase *tc, TF *tf, int i); static TestResult *receive_result_info_fork (const char *tcname, const char *tname, int iter, - int status, int expected_signal); -static void set_fork_info (TestResult *tr, int status, int expected_signal); + int status, int expected_signal, + unsigned char allowed_exit_value); +static void set_fork_info (TestResult *tr, int status, int expected_signal, + unsigned char allowed_exit_value); static char *signal_msg (int sig); static char *signal_error_msg (int signal_received, int signal_expected); static char *exit_msg (int exitstatus); @@ -362,13 +364,14 @@ static TestResult *tcase_run_tfun_fork (SRunner *sr, TCase *tc, TF *tfun, int i) killpg(pid, SIGKILL); /* Kill remaining processes. */ - return receive_result_info_fork(tc->name, tfun->name, i, status, tfun->signal); + return receive_result_info_fork(tc->name, tfun->name, i, status, tfun->signal, tfun->allowed_exit_value); } static TestResult *receive_result_info_fork (const char *tcname, const char *tname, int iter, - int status, int expected_signal) + int status, int expected_signal, + unsigned char allowed_exit_value) { TestResult *tr; @@ -378,12 +381,12 @@ static TestResult *receive_result_info_fork (const char *tcname, tr->tcname = tcname; tr->tname = tname; tr->iter = iter; - set_fork_info(tr, status, expected_signal); + set_fork_info(tr, status, expected_signal, allowed_exit_value); return tr; } -static void set_fork_info (TestResult *tr, int status, int signal_expected) +static void set_fork_info (TestResult *tr, int status, int signal_expected, unsigned char allowed_exit_value) { int was_sig = WIFSIGNALED(status); int was_exit = WIFEXITED(status); @@ -410,10 +413,10 @@ static void set_fork_info (TestResult *tr, int status, int signal_expected) tr->msg = signal_msg(signal_received); } } else if (signal_expected == 0) { - if (was_exit && exit_status == 0) { + if (was_exit && exit_status == allowed_exit_value) { tr->rtype = CK_PASS; tr->msg = pass_msg(); - } else if (was_exit && exit_status != 0) { + } else if (was_exit && exit_status != allowed_exit_value) { if (tr->msg == NULL) { /* early exit */ tr->rtype = CK_ERROR; tr->msg = exit_msg(exit_status); @@ -424,7 +427,7 @@ static void set_fork_info (TestResult *tr, int status, int signal_expected) } else { /* a signal was expected and none raised */ if (was_exit) { tr->msg = exit_msg(exit_status); - if (exit_status == 0) + if (exit_status == allowed_exit_value) tr->rtype = CK_FAILURE; /* normal exit status */ else tr->rtype = CK_FAILURE; /* early exit */ diff --git a/tests/Makefile.am b/tests/Makefile.am index b7f3fdd..ebc7cb9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -47,6 +47,7 @@ check_check_SOURCES = \ check_check_fork.c \ check_check_fixture.c \ check_check_pack.c \ + check_check_exit.c \ check_check_main.c check_check_LDADD = $(top_builddir)/src/libcheckinternal.la $(top_builddir)/lib/libcompat.la diff --git a/tests/check_check.h b/tests/check_check.h index 13e0b80..6230093 100644 --- a/tests/check_check.h +++ b/tests/check_check.h @@ -24,6 +24,7 @@ Suite *make_limit_suite(void); Suite *make_fork_suite(void); Suite *make_fixture_suite(void); Suite *make_pack_suite(void); +Suite *make_exit_suite(void); extern int master_tests_lineno[]; void init_master_tests_lineno(void); diff --git a/tests/check_check_main.c b/tests/check_check_main.c index 04d2bb6..46e4d16 100644 --- a/tests/check_check_main.c +++ b/tests/check_check_main.c @@ -24,6 +24,7 @@ int main (void) srunner_add_suite(sr, make_fork_suite()); srunner_add_suite(sr, make_fixture_suite()); srunner_add_suite(sr, make_pack_suite()); + srunner_add_suite(sr, make_exit_suite()); printf ("Ran %d tests in subordinate suite\n", sub_ntests); srunner_run_all (sr, CK_VERBOSE);