Message ID | 20240327034923.30987-2-wegao@suse.com |
---|---|
State | Superseded |
Headers | show |
Series | lib: TST_EXP_{FAIL,PASS}_PTR_{NULL,VOID} | expand |
Hi Wei, > --- /dev/null > +++ b/lib/newlib_tests/test_macros07.c nit: Instead of creating a new test I would add it to existing test_macros02.c (it would reduce some duplicity). Also having exp_errs_pass[] and exp_errs_fail[] static and put testing all 4 macros into it's own functions, I'll send a suggestion shortly. Kind regards, Petr
Hi Wei, > --- /dev/null > +++ b/lib/newlib_tests/test_macros07.c > @@ -0,0 +1,61 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2024 Wei Gao <wegao@suse.com> > + */ > + > +/* > + * Test TST_EXP_FAIL_PTR_{NULL,VOID} and TST_EXP_FAIL_PTR_ARR{NULL,VOID} macro. > + */ > + > +#include "tst_test.h" > + > +static char *fail_fn_null(void) > +{ > + errno = EINVAL; > + return NULL; > +} > + > +static char *fail_fn_void(void) > +{ > + errno = EINVAL; > + return (void *)-1; > +} > + > +static char *pass_fn(void) > +{ > + return "pass"; > +} > + > +static void do_test(void) > +{ > + const int exp_errs_pass[] = {ENOTTY, EINVAL}; > + const int exp_errs_fail[] = {ENOTTY, EISDIR}; > + > + tst_res(TINFO, "Testing TST_EXP_FAIL_PTR_NULL macro"); > + TST_EXP_FAIL_PTR_NULL(fail_fn_null(), EINVAL, "fail_fn_null()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_NULL(fail_fn_null(), ENOTTY, "fail_fn_null()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_NULL(pass_fn(), ENOTTY, "pass_fn()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_ARR_NULL(fail_fn_null(), exp_errs_pass, "fail_fn_null()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_ARR_NULL(fail_fn_null(), exp_errs_fail, "fail_fn()_null"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + > + tst_res(TINFO, "Testing TST_EXP_FAIL_PTR_VOID macro"); > + TST_EXP_FAIL_PTR_VOID(fail_fn_void(), EINVAL, "fail_fn_void()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_VOID(fail_fn_void(), ENOTTY, "fail_fn_void()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_VOID(pass_fn(), ENOTTY, "pass_fn()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_ARR_VOID(fail_fn_void(), exp_errs_pass, "fail_fn_void()"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > + TST_EXP_FAIL_PTR_ARR_VOID(fail_fn_void(), exp_errs_fail, "fail_fn()_void"); > + tst_res(TINFO, "TST_PASS = %i", TST_PASS); > +} > + > +static struct tst_test test = { > + .test_all = do_test, > +}; How about extend test_macros02.c (add testing macro, renaming functions to avoid clash, add inval_ret_fn_char() which was missing in your version): static int fail_fn(void) { errno = EINVAL; return -1; } static int pass_fn(void) { return 0; } static int inval_ret_fn(void) { return 42; } static char *fail_fn_null(void) { errno = EINVAL; return NULL; } static char *fail_fn_void(void) { errno = EINVAL; return (void *)-1; } static char *inval_ret_fn_char(void) { return (void *)-1; } static char *pass_fn_char(void) { return "pass"; } #define TEST_MACRO(macro, macro_arr, fail_fn, pass_fn, inval_fn) \ do { \ tst_res(TINFO, "Testing macro macro"); \ macro(fail_fn(), EINVAL, "fail_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(fail_fn(), ENOTTY, "fail_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(pass_fn(), ENOTTY, "pass_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(inval_fn(), ENOTTY, "inval_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro_arr(fail_fn(), exp_errs_pass, "fail_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro_arr(fail_fn(), exp_errs_fail, "fail_fn()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ } while (0) static void do_test(void) { const int exp_errs_pass[] = {ENOTTY, EINVAL}; const int exp_errs_fail[] = {ENOTTY, EISDIR}; TEST_MACRO(TST_EXP_FAIL, TST_EXP_FAIL_ARR, fail_fn, pass_fn, inval_ret_fn); TEST_MACRO(TST_EXP_FAIL2, TST_EXP_FAIL2_ARR, fail_fn, pass_fn, inval_ret_fn); TEST_MACRO(TST_EXP_FAIL_PTR_NULL, TST_EXP_FAIL_PTR_ARR_NULL, fail_fn_null, pass_fn_char, inval_ret_fn_char); TEST_MACRO(TST_EXP_FAIL_PTR_VOID, TST_EXP_FAIL_PTR_ARR_VOID, fail_fn_void, pass_fn_char, inval_ret_fn_char); } static struct tst_test test = { .test_all = do_test, };
Hi Wei, > Hi Wei, ... > #define TEST_MACRO(macro, macro_arr, fail_fn, pass_fn, inval_fn) \ > do { \ > tst_res(TINFO, "Testing macro macro"); \ > macro(fail_fn(), EINVAL, "fail_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > macro(fail_fn(), ENOTTY, "fail_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > macro(pass_fn(), ENOTTY, "pass_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > macro(inval_fn(), ENOTTY, "inval_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > macro_arr(fail_fn(), exp_errs_pass, "fail_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > macro_arr(fail_fn(), exp_errs_fail, "fail_fn()"); \ > tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ > } while (0) Actually this (print real function with #macro and #macro_arr, 2x skip message parameter (to test also macro without error parameter): #define TEST_MACRO(macro, macro_arr, fail_fn, pass_fn, inval_fn) \ do { \ tst_res(TINFO, "Testing " #macro " macro"); \ macro(fail_fn(), EINVAL, #fail_fn"()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(fail_fn(), ENOTTY); /* skip msg parameter */ \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(pass_fn(), ENOTTY, #pass_fn"()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro(inval_fn(), ENOTTY, #inval_fn"()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro_arr(fail_fn(), exp_errs_pass, #fail_fn"()"); \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ macro_arr(fail_fn(), exp_errs_fail); /* skip msg parameter */ \ tst_res(TINFO, "TST_PASS = %i", TST_PASS); \ } while (0) Kind regards, Petr
Hi Wei, ... > #define TST_EXP_FAIL_ARR_(SCALL, SSCALL, EXP_ERRS, EXP_ERRS_CNT, ...) \ > do { \ > TST_EXP_FAIL_SILENT_(TST_RET == 0, SCALL, SSCALL, \ > @@ -258,6 +293,32 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); > TST_EXP_FAIL2_ARR_(SCALL, #SCALL, EXP_ERRS, \ > ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); > +#define TST_EXP_FAIL_PTR_NULL(SCALL, EXP_ERR, ...) \ > + do { \ > + int tst_exp_err__ = EXP_ERR; \ > + TST_EXP_FAIL_PTR_(SCALL, #SCALL, NULL, \ > + &tst_exp_err__, 1, ##__VA_ARGS__); \ > + } while (0) > + > +#define TST_EXP_FAIL_PTR_ARR_NULL(SCALL, EXP_ERRS, ...) \ > + do { \ > + TST_EXP_FAIL_PTR_(SCALL, #SCALL, NULL, \ > + EXP_ERRS, ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); \ FYI I send a patch [1] (Cyril's suggestion) replacing ARRAY_SIZE() with just size parameter. I.e. moving ARRAY_SIZE() to the caller. This allows to use array of expected errnos more flexibly, e.g. fanotify14 [2]. Could you please send another version, which will be based on my v3 patchset or just wait before it's merged and rebase? Kind regards, Petr [1] https://lore.kernel.org/ltp/20240326144145.747735-3-pvorel@suse.cz/ [2] https://lore.kernel.org/ltp/20240326144145.747735-4-pvorel@suse.cz/ [3] https://patchwork.ozlabs.org/project/ltp/list/?series=400495 > + } while (0) > + > +#define TST_EXP_FAIL_PTR_VOID(SCALL, EXP_ERR, ...) \ > + do { \ > + int tst_exp_err__ = EXP_ERR; \ > + TST_EXP_FAIL_PTR_(SCALL, #SCALL, (void *)-1, \ > + &tst_exp_err__, 1, ##__VA_ARGS__); \ > + } while (0) > + > +#define TST_EXP_FAIL_PTR_ARR_VOID(SCALL, EXP_ERRS, ...) \ > + do { \ > + TST_EXP_FAIL_PTR_(SCALL, #SCALL, (void *)-1, \ > + EXP_ERRS, ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); \ > + } while (0) > +
diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h index d2e50a219..c70cad2f4 100644 --- a/include/tst_test_macros.h +++ b/include/tst_test_macros.h @@ -227,6 +227,41 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); } \ } while (0) +#define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, \ + ERRNOS, ERRNOS_CNT, ...) \ + do { \ + TESTPTR(SCALL); \ + \ + TST_PASS = 0; \ + \ + if (TST_RET_PTR != FAIL_PTR_VAL) { \ + TST_MSG_(TFAIL, " succeeded", SSCALL, ##__VA_ARGS__); \ + break; \ + } \ + \ + if (!tst_errno_in_set(TST_ERR, ERRNOS, ERRNOS_CNT)) { \ + char tst_str_buf__[ERRNOS_CNT * 20]; \ + TST_MSGP_(TFAIL | TTERRNO, " expected %s", \ + tst_errno_names(tst_str_buf__, \ + ERRNOS, ERRNOS_CNT), \ + SSCALL, ##__VA_ARGS__); \ + break; \ + } \ + \ + TST_PASS = 1; \ + \ + } while (0) + +#define TST_EXP_FAIL_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, \ + ERRNOS, ERRNOS_CNT, ...) \ + do { \ + TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, \ + ERRNOS, ERRNOS_CNT, ##__VA_ARGS__); \ + if (TST_PASS) \ + TST_MSG_(TPASS | TTERRNO, " ", SSCALL, ##__VA_ARGS__); \ + } while (0) + + #define TST_EXP_FAIL_ARR_(SCALL, SSCALL, EXP_ERRS, EXP_ERRS_CNT, ...) \ do { \ TST_EXP_FAIL_SILENT_(TST_RET == 0, SCALL, SSCALL, \ @@ -258,6 +293,32 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); TST_EXP_FAIL2_ARR_(SCALL, #SCALL, EXP_ERRS, \ ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); +#define TST_EXP_FAIL_PTR_NULL(SCALL, EXP_ERR, ...) \ + do { \ + int tst_exp_err__ = EXP_ERR; \ + TST_EXP_FAIL_PTR_(SCALL, #SCALL, NULL, \ + &tst_exp_err__, 1, ##__VA_ARGS__); \ + } while (0) + +#define TST_EXP_FAIL_PTR_ARR_NULL(SCALL, EXP_ERRS, ...) \ + do { \ + TST_EXP_FAIL_PTR_(SCALL, #SCALL, NULL, \ + EXP_ERRS, ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); \ + } while (0) + +#define TST_EXP_FAIL_PTR_VOID(SCALL, EXP_ERR, ...) \ + do { \ + int tst_exp_err__ = EXP_ERR; \ + TST_EXP_FAIL_PTR_(SCALL, #SCALL, (void *)-1, \ + &tst_exp_err__, 1, ##__VA_ARGS__); \ + } while (0) + +#define TST_EXP_FAIL_PTR_ARR_VOID(SCALL, EXP_ERRS, ...) \ + do { \ + TST_EXP_FAIL_PTR_(SCALL, #SCALL, (void *)-1, \ + EXP_ERRS, ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__); \ + } while (0) + #define TST_EXP_FAIL2(SCALL, EXP_ERR, ...) \ do { \ int tst_exp_err__ = EXP_ERR; \ diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore index a69b29e24..4f43899e5 100644 --- a/lib/newlib_tests/.gitignore +++ b/lib/newlib_tests/.gitignore @@ -40,6 +40,7 @@ test_macros03 test_macros04 test_macros05 test_macros06 +test_macros07 tst_fuzzy_sync01 tst_fuzzy_sync02 tst_fuzzy_sync03 diff --git a/lib/newlib_tests/test_macros07.c b/lib/newlib_tests/test_macros07.c new file mode 100644 index 000000000..ac361fd8e --- /dev/null +++ b/lib/newlib_tests/test_macros07.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Wei Gao <wegao@suse.com> + */ + +/* + * Test TST_EXP_FAIL_PTR_{NULL,VOID} and TST_EXP_FAIL_PTR_ARR{NULL,VOID} macro. + */ + +#include "tst_test.h" + +static char *fail_fn_null(void) +{ + errno = EINVAL; + return NULL; +} + +static char *fail_fn_void(void) +{ + errno = EINVAL; + return (void *)-1; +} + +static char *pass_fn(void) +{ + return "pass"; +} + +static void do_test(void) +{ + const int exp_errs_pass[] = {ENOTTY, EINVAL}; + const int exp_errs_fail[] = {ENOTTY, EISDIR}; + + tst_res(TINFO, "Testing TST_EXP_FAIL_PTR_NULL macro"); + TST_EXP_FAIL_PTR_NULL(fail_fn_null(), EINVAL, "fail_fn_null()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_NULL(fail_fn_null(), ENOTTY, "fail_fn_null()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_NULL(pass_fn(), ENOTTY, "pass_fn()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_ARR_NULL(fail_fn_null(), exp_errs_pass, "fail_fn_null()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_ARR_NULL(fail_fn_null(), exp_errs_fail, "fail_fn()_null"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + + tst_res(TINFO, "Testing TST_EXP_FAIL_PTR_VOID macro"); + TST_EXP_FAIL_PTR_VOID(fail_fn_void(), EINVAL, "fail_fn_void()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_VOID(fail_fn_void(), ENOTTY, "fail_fn_void()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_VOID(pass_fn(), ENOTTY, "pass_fn()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_ARR_VOID(fail_fn_void(), exp_errs_pass, "fail_fn_void()"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); + TST_EXP_FAIL_PTR_ARR_VOID(fail_fn_void(), exp_errs_fail, "fail_fn()_void"); + tst_res(TINFO, "TST_PASS = %i", TST_PASS); +} + +static struct tst_test test = { + .test_all = do_test, +};
Signed-off-by: Wei Gao <wegao@suse.com> --- include/tst_test_macros.h | 61 ++++++++++++++++++++++++++++++++ lib/newlib_tests/.gitignore | 1 + lib/newlib_tests/test_macros07.c | 61 ++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 lib/newlib_tests/test_macros07.c