tst_test: Add support for array of test functions
RFC PATCH 1/1 tst_test: Add support for array of test functions
This commits add third option for specifying the test functions in an NULL terminated array of function pointers + converts two testcases.
--- include/tst_test.h | 11 ++++---- lib/tst_test.c | 38 ++++++++++++++++++++++----- testcases/kernel/syscalls/syscall/syscall01.c | 20 +++++--------- testcases/kernel/syscalls/waitpid/waitpid09.c | 12 +++------ 4 files changed, 47 insertions(+), 34 deletions(-)
include/tst_test.h +6 -5
@@ -140,17 +140,18 @@ struct tst_test {/* override default timeout per test run */ unsigned int timeout; +/* NULL terminated array of resource file names */ +const char *const *resource_files; +void (*setup)(void );void (*cleanup)(void ); -void (*test)(unsigned int test_nr); -void (*test_all)(void ); -/* Sampling function for timer measurement testcases */ int (*sample)(int clk_id,long long usec); -/* NULL terminated array of resource file names */ -const char *const *resource_files; +void (*test)(unsigned int test_nr); +void (*test_all)(void ); +void (**tests)(void ); };/*
lib/tst_test.c +32 -6
@@ -649,6 +649,15 @@ static void assert_test_fn(void)if (tst_test->sample) cnt++; +if (tst_test->tests) { + cnt++; + +if (!tst_test->tests[0 ]) { +tst_brk (TBROK , +"The tests[] must have at least one function" ); + } + } +if (!cnt)tst_brk (TBROK ,"No test function speficied" );
@@ -763,22 +772,39 @@ static void do_cleanup(void)static void run_tests (void ) { -unsigned int i; +unsigned int i =0 , go =1 ;struct results saved_results; -if (!tst_test->test) { + +while (go) { saved_results = *results; - tst_test->test_all (); -if (getpid () != main_pid) { -exit (0 ); +if (tst_test->test_all) { + tst_test->test_all (); + go =0 ; } +if (tst_test->test) { + tst_test->test (i); + +if (++i >= tst_test->tcnt) + go =0 ; + } + +if (tst_test->tests) { + tst_test->tests[i](); + +if (!tst_test->tests[++i]) + go =0 ; + } + +if (getpid () != main_pid) +exit (0 ); +tst_reap_children ();if (results_equal (&saved_results, results))tst_brk (TBROK ,"Test haven't reported results!" ); -return ; }for (i =0 ; i < tst_test->tcnt; i++) {
testcases/kernel/syscalls/syscall/syscall01.c +6 -14
@@ -75,20 +75,12 @@ static void verify_getgid(void) } } - -static void (*tcases[])(void ) = { - verify_getpid, - verify_getuid, - verify_getgid, -}; - -static void verify_syscall (unsigned int n) -{ - tcases[n](); -} -static struct tst_test test = { - .test = verify_syscall, - .tcnt =ARRAY_SIZE (tcases), + .tests = (void (*[])(void )) { + verify_getpid, + verify_getuid, + verify_getgid, +NULL , + } };
testcases/kernel/syscalls/waitpid/waitpid09.c +3 -9
@@ -162,16 +162,10 @@ static void case3(void)tst_res (TPASS ,"waitpid(-1, WNOHANG) = -1 with ECHILD if no children" ); } -static void (*tests[])(void ) = { case0, case1, case2, case3 }; - -static void waitpid09_test (unsigned int id) -{ - tests[id](); -} -static struct tst_test test = { .forks_child =1 , .needs_checkpoints =1 , - .test = waitpid09_test, - .tcnt =ARRAY_SIZE (tests), + .tests = (void (*[])(void )) { + case0, case1, case2, case3,NULL , + } };
Signature · 1 line
2.13.0
RFC PATCH 1/1 tst_test: Add support for array of test functions
Hi!
Ping?
Signature · 2 lines
Cyril Hrubis [email protected]
RFC PATCH 1/1 tst_test: Add support for array of test functions
In reply to Cyril Hrubis----- Original Message -----
Quoted 2 lines
This commits add third option for specifying the test functions in an NULL terminated array of function pointers + converts two testcases.
Hi,
What would be the benefits?
- it's almost same amount of code
- it makes tst_test interface bit more complicated
- as a user I'd probably go with test_all(), since it's most straight-forward
- if those test funcs have parameter other than void, you still need to use the old way
Regards,
Jan
RFC PATCH 1/1 tst_test: Add support for array of test functions
Hi!
Quoted 1 line
What would be the benefits?
I was aiming to avoid the need to have a switch () in each testcase that implements a similar tests but cannot be easily data driven (as we do for most of tests that loop over an array of structures describing the test data).
The question is, of course, if the interface is worth the effort.
Signature · 2 lines
Cyril Hrubis [email protected]