Skip to content

Commit 3987c1d

Browse files
committed
Fix START_TEST to look like valid C code.
Instead of exporting the defined name as a bare function, export a struct that has a pointer to the function, but also its name, file and line number where it is defined. Store that information into a new `struct TTest`. After this commit, START_TEST(<testname>) will create three definitions: - <testname>_fn: The actual function; - <testname>_ttest: A `struct TTest` with the information about it; - <testname>: A pointer to <testname>_ttest. Functions `tcase_add_test()` and friends are updated to take a `TTest *` argument rather than a `TFun` and separate name. The runners are updated to find that information inside the linked `tc->ttest`. The call to `tcase_fn_start()` is moved from the defined functions to the runners (both the "fork" and the "nofork" one) which call it just before invoking the test function. A nice side-effect is that END_TEST is now optional, though the empty `#define` is kept for backwards compability. v2: Initialize the struct TTest by position to be compatible with older compilers that do not recognize named fields (e.g. VS 2010, VS 2012.) Tested: - `make check` still passes. - Removing END_TEST from test cases still produces valid code that builds and passes tests.
1 parent 65e8c5e commit 3987c1d

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

Diff for: src/check.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,21 @@ void suite_add_tcase(Suite * s, TCase * tc)
247247
check_list_add_end(s->tclst, tc);
248248
}
249249

250-
void _tcase_add_test(TCase * tc, TFun fn, const char *name, int _signal,
251-
int allowed_exit_value, int start, int end)
250+
void _tcase_add_test(TCase * tc, const TTest * ttest,
251+
int _signal, int allowed_exit_value,
252+
int start, int end)
252253
{
253254
TF *tf;
254255

255-
if(tc == NULL || fn == NULL || name == NULL)
256+
if(tc == NULL || ttest == NULL)
256257
return;
257258
tf = (TF *)emalloc(sizeof(TF)); /* freed in tcase_free */
258-
tf->fn = fn;
259+
tf->ttest = ttest;
259260
tf->loop_start = start;
260261
tf->loop_end = end;
261262
tf->signal = _signal; /* 0 means no signal expected */
262263
tf->allowed_exit_value =
263264
(WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */
264-
tf->name = name;
265265
check_list_add_end(tc->tflst, tf);
266266
}
267267

Diff for: src/check.h.in

+28-18
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ typedef void (*SFun) (void);
121121
*/
122122
typedef struct Suite Suite;
123123

124+
/**
125+
* Type for a test, which wraps a test function
126+
*/
127+
typedef struct TTest {
128+
const char *name;
129+
TFun fn;
130+
const char *file;
131+
int line;
132+
} TTest;
133+
124134
/**
125135
* Creates a test suite with the given name.
126136
*
@@ -214,8 +224,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
214224
*
215225
* @since 0.9.2
216226
* */
217-
#define tcase_add_test_raise_signal(tc,tf,signal) \
218-
_tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 0, 1)
227+
#define tcase_add_test_raise_signal(tc,ttest,signal) \
228+
_tcase_add_test((tc),(ttest),(signal), 0, 0, 1)
219229

220230
/**
221231
* Add a test function with an expected exit value to a test case
@@ -229,8 +239,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
229239
*
230240
* @since 0.9.7
231241
*/
232-
#define tcase_add_exit_test(tc, tf, expected_exit_value) \
233-
_tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),0,1)
242+
#define tcase_add_exit_test(tc, ttest, expected_exit_value) \
243+
_tcase_add_test((tc),(ttest),0,(expected_exit_value),0,1)
234244

235245
/**
236246
* Add a looping test function to a test case
@@ -246,8 +256,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
246256
*
247257
* @since 0.9.4
248258
*/
249-
#define tcase_add_loop_test(tc,tf,s,e) \
250-
_tcase_add_test((tc),(tf),"" # tf "",0,0,(s),(e))
259+
#define tcase_add_loop_test(tc,ttest,s,e) \
260+
_tcase_add_test((tc),(ttest),0,0,(s),(e))
251261

252262
/**
253263
* Add a looping test function with signal handling to a test case
@@ -267,8 +277,8 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
267277
*
268278
* @since 0.9.5
269279
*/
270-
#define tcase_add_loop_test_raise_signal(tc,tf,signal,s,e) \
271-
_tcase_add_test((tc),(tf),"" # tf "",(signal),0,(s),(e))
280+
#define tcase_add_loop_test_raise_signal(tc,ttest,signal,s,e) \
281+
_tcase_add_test((tc),(ttest),(signal),0,(s),(e))
272282

273283
/**
274284
* Add a looping test function with an expected exit value to a test case
@@ -288,16 +298,15 @@ CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
288298
*
289299
* @since 0.9.7
290300
*/
291-
#define tcase_add_loop_exit_test(tc,tf,expected_exit_value,s,e) \
292-
_tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),(s),(e))
301+
#define tcase_add_loop_exit_test(tc,ttest,expected_exit_value,s,e) \
302+
_tcase_add_test((tc),(ttest),0,(expected_exit_value),(s),(e))
293303

294304
/* Add a test function to a test case
295305
(function version -- use this when the macro won't work
296306
*/
297-
CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, TFun tf,
298-
const char *fname, int _signal,
299-
int allowed_exit_value, int start,
300-
int end);
307+
CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, const TTest * ttest,
308+
int _signal, int allowed_exit_value,
309+
int start, int end);
301310

302311
/**
303312
* Add unchecked fixture setup/teardown functions to a test case
@@ -400,16 +409,17 @@ CK_DLL_EXP const char* CK_EXPORT tcase_name(void);
400409
* @since 0.6.0
401410
*/
402411
#define START_TEST(__testname)\
403-
static void __testname (int _i CK_ATTRIBUTE_UNUSED)\
404-
{\
405-
tcase_fn_start (""# __testname, __FILE__, __LINE__);
412+
static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED);\
413+
static const TTest __testname ## _ttest = {""# __testname, __testname ## _fn, __FILE__, __LINE__};\
414+
static const TTest * __testname = & __testname ## _ttest;\
415+
static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED)
406416

407417
/**
408418
* End a unit test
409419
*
410420
* @since 0.6.0
411421
*/
412-
#define END_TEST }
422+
#define END_TEST
413423

414424
/*
415425
* Fail the test case unless expr is false

Diff for: src/check_impl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636

3737
typedef struct TF
3838
{
39-
TFun fn;
39+
const TTest * ttest;
4040
int loop_start;
4141
int loop_end;
42-
const char *name;
4342
int signal;
4443
signed char allowed_exit_value;
4544
} TF;

Diff for: src/check_log.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void log_test_start(SRunner * sr, TCase * tc, TF * tfun)
152152
{
153153
char buffer[100];
154154

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

Diff for: src/check_run.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,12 @@ static TestResult *tcase_run_tfun_nofork(SRunner * sr, TCase * tc, TF * tfun,
415415
clock_gettime(check_get_clockid(), &ts_start);
416416
if(0 == setjmp(error_jmp_buffer))
417417
{
418-
tfun->fn(i);
418+
tcase_fn_start(tfun->ttest->name, tfun->ttest->file, tfun->ttest->line);
419+
tfun->ttest->fn(i);
419420
}
420421
clock_gettime(check_get_clockid(), &ts_end);
421422
tcase_run_checked_teardown(tc);
422-
return receive_result_info_nofork(tc->name, tfun->name, i,
423+
return receive_result_info_nofork(tc->name, tfun->ttest->name, i,
423424
DIFF_IN_USEC(ts_start, ts_end));
424425
}
425426

@@ -491,7 +492,8 @@ static TestResult *tcase_run_tfun_fork(SRunner * sr, TCase * tc, TF * tfun,
491492
tr = tcase_run_checked_setup(sr, tc);
492493
free(tr);
493494
clock_gettime(check_get_clockid(), &ts_start);
494-
tfun->fn(i);
495+
tcase_fn_start(tfun->ttest->name, tfun->ttest->file, tfun->ttest->line);
496+
tfun->ttest->fn(i);
495497
clock_gettime(check_get_clockid(), &ts_end);
496498
tcase_run_checked_teardown(tc);
497499
send_duration_info(DIFF_IN_USEC(ts_start, ts_end));
@@ -535,7 +537,7 @@ static TestResult *tcase_run_tfun_fork(SRunner * sr, TCase * tc, TF * tfun,
535537

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

538-
return receive_result_info_fork(tc->name, tfun->name, i, status,
540+
return receive_result_info_fork(tc->name, tfun->ttest->name, i, status,
539541
tfun->signal, tfun->allowed_exit_value);
540542
}
541543

0 commit comments

Comments
 (0)