Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix START_TEST to look like valid C code. #158

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,21 @@ void suite_add_tcase(Suite * s, TCase * tc)
check_list_add_end(s->tclst, tc);
}

void _tcase_add_test(TCase * tc, TFun fn, const char *name, int _signal,
int allowed_exit_value, int start, int end)
void _tcase_add_test(TCase * tc, const TTest * ttest,
int _signal, int allowed_exit_value,
int start, int end)
{
TF *tf;

if(tc == NULL || fn == NULL || name == NULL)
if(tc == NULL || ttest == NULL)
return;
tf = (TF *)emalloc(sizeof(TF)); /* freed in tcase_free */
tf->fn = fn;
tf->ttest = ttest;
tf->loop_start = start;
tf->loop_end = end;
tf->signal = _signal; /* 0 means no signal expected */
tf->allowed_exit_value =
(WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */
tf->name = name;
check_list_add_end(tc->tflst, tf);
}

Expand Down
46 changes: 28 additions & 18 deletions src/check.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ typedef void (*SFun) (void);
*/
typedef struct Suite Suite;

/**
* Type for a test, which wraps a test function
*/
typedef struct TTest {
const char *name;
TFun fn;
const char *file;
int line;
} TTest;

/**
* Creates a test suite with the given name.
*
Expand Down Expand Up @@ -214,8 +224,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
*
* @since 0.9.2
* */
#define tcase_add_test_raise_signal(tc,tf,signal) \
_tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 0, 1)
#define tcase_add_test_raise_signal(tc,ttest,signal) \
_tcase_add_test((tc),(ttest),(signal), 0, 0, 1)

/**
* Add a test function with an expected exit value to a test case
Expand All @@ -229,8 +239,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
*
* @since 0.9.7
*/
#define tcase_add_exit_test(tc, tf, expected_exit_value) \
_tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),0,1)
#define tcase_add_exit_test(tc, ttest, expected_exit_value) \
_tcase_add_test((tc),(ttest),0,(expected_exit_value),0,1)

/**
* Add a looping test function to a test case
Expand All @@ -246,8 +256,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
*
* @since 0.9.4
*/
#define tcase_add_loop_test(tc,tf,s,e) \
_tcase_add_test((tc),(tf),"" # tf "",0,0,(s),(e))
#define tcase_add_loop_test(tc,ttest,s,e) \
_tcase_add_test((tc),(ttest),0,0,(s),(e))

/**
* Add a looping test function with signal handling to a test case
Expand All @@ -267,8 +277,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
*
* @since 0.9.5
*/
#define tcase_add_loop_test_raise_signal(tc,tf,signal,s,e) \
_tcase_add_test((tc),(tf),"" # tf "",(signal),0,(s),(e))
#define tcase_add_loop_test_raise_signal(tc,ttest,signal,s,e) \
_tcase_add_test((tc),(ttest),(signal),0,(s),(e))

/**
* Add a looping test function with an expected exit value to a test case
Expand All @@ -288,16 +298,15 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
*
* @since 0.9.7
*/
#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))
#define tcase_add_loop_exit_test(tc,ttest,expected_exit_value,s,e) \
_tcase_add_test((tc),(ttest),0,(expected_exit_value),(s),(e))

/* Add a test function to a test case
(function version -- use this when the macro won't work
*/
CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, TFun tf,
const char *fname, int _signal,
int allowed_exit_value, int start,
int end);
CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, const TTest * ttest,
int _signal, int allowed_exit_value,
int start, int end);

/**
* Add unchecked fixture setup/teardown functions to a test case
Expand Down Expand Up @@ -400,16 +409,17 @@ CK_DLL_EXP const char* CK_EXPORT tcase_name(void);
* @since 0.6.0
*/
#define START_TEST(__testname)\
static void __testname (int _i CK_ATTRIBUTE_UNUSED)\
{\
tcase_fn_start (""# __testname, __FILE__, __LINE__);
static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED);\
static const TTest __testname ## _ttest = {""# __testname, __testname ## _fn, __FILE__, __LINE__};\
static const TTest * __testname = & __testname ## _ttest;\
static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED)

/**
* End a unit test
*
* @since 0.6.0
*/
#define END_TEST }
#define END_TEST

/*
* Fail the test case unless expr is false
Expand Down
3 changes: 1 addition & 2 deletions src/check_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@

typedef struct TF
{
TFun fn;
const TTest * ttest;
int loop_start;
int loop_end;
const char *name;
int signal;
signed char allowed_exit_value;
} TF;
Expand Down
2 changes: 1 addition & 1 deletion src/check_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void log_test_start(SRunner * sr, TCase * tc, TF * tfun)
{
char buffer[100];

snprintf(buffer, 99, "%s:%s", tc->name, tfun->name);
snprintf(buffer, 99, "%s:%s", tc->name, tfun->ttest->name);
srunner_send_evt(sr, buffer, CLSTART_T);
}

Expand Down
10 changes: 6 additions & 4 deletions src/check_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,12 @@ static TestResult *tcase_run_tfun_nofork(SRunner * sr, TCase * tc, TF * tfun,
clock_gettime(check_get_clockid(), &ts_start);
if(0 == setjmp(error_jmp_buffer))
{
tfun->fn(i);
tcase_fn_start(tfun->ttest->name, tfun->ttest->file, tfun->ttest->line);
tfun->ttest->fn(i);
}
clock_gettime(check_get_clockid(), &ts_end);
tcase_run_checked_teardown(tc);
return receive_result_info_nofork(tc->name, tfun->name, i,
return receive_result_info_nofork(tc->name, tfun->ttest->name, i,
DIFF_IN_USEC(ts_start, ts_end));
}

Expand Down Expand Up @@ -491,7 +492,8 @@ static TestResult *tcase_run_tfun_fork(SRunner * sr, TCase * tc, TF * tfun,
tr = tcase_run_checked_setup(sr, tc);
free(tr);
clock_gettime(check_get_clockid(), &ts_start);
tfun->fn(i);
tcase_fn_start(tfun->ttest->name, tfun->ttest->file, tfun->ttest->line);
tfun->ttest->fn(i);
clock_gettime(check_get_clockid(), &ts_end);
tcase_run_checked_teardown(tc);
send_duration_info(DIFF_IN_USEC(ts_start, ts_end));
Expand Down Expand Up @@ -535,7 +537,7 @@ static TestResult *tcase_run_tfun_fork(SRunner * sr, TCase * tc, TF * tfun,

killpg(pid, SIGKILL); /* Kill remaining processes. */

return receive_result_info_fork(tc->name, tfun->name, i, status,
return receive_result_info_fork(tc->name, tfun->ttest->name, i, status,
tfun->signal, tfun->allowed_exit_value);
}

Expand Down