[LTP] [PATCH v5 1/3] lib: TST_EXP_{FAIL,PASS}_PTR_{NULL,VOID}

Petr Vorel pvorel@suse.cz
Thu Mar 28 12:49:32 CET 2024


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,
};


More information about the ltp mailing list