diff options
author | Heinrich Schuchardt <xypron.glpk@gmx.de> | 2021-03-23 19:11:27 +0100 |
---|---|---|
committer | Leo Yu-Chi Liang <ycliang@andestech.com> | 2021-04-08 15:37:29 +0800 |
commit | f709a0b6f9c986e2a921435d095fc498949b53fa (patch) | |
tree | aead68051e472b49fbcaa740fb91cf45b100f0f1 /test/lib | |
parent | a718e2aed52a135f7a6a8745a49e4f5bdff49ecf (diff) | |
download | u-boot-f709a0b6f9c986e2a921435d095fc498949b53fa.tar.gz u-boot-f709a0b6f9c986e2a921435d095fc498949b53fa.tar.bz2 u-boot-f709a0b6f9c986e2a921435d095fc498949b53fa.zip |
test: unit test for longjmp
Provide a unit test for the longjmp() library function
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Acked-by: Sean Anderson <seanga2@gmail.com>
Diffstat (limited to 'test/lib')
-rw-r--r-- | test/lib/Makefile | 1 | ||||
-rw-r--r-- | test/lib/longjmp.c | 73 |
2 files changed, 74 insertions, 0 deletions
diff --git a/test/lib/Makefile b/test/lib/Makefile index 97c11e35a8..a30f615aa9 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_EFI_LOADER) += efi_device_path.o obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o obj-y += hexdump.o obj-y += lmb.o +obj-y += longjmp.o obj-$(CONFIG_CONSOLE_RECORD) += test_print.o obj-$(CONFIG_SSCANF) += sscanf.o obj-y += string.o diff --git a/test/lib/longjmp.c b/test/lib/longjmp.c new file mode 100644 index 0000000000..201367a5a3 --- /dev/null +++ b/test/lib/longjmp.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Test setjmp(), longjmp() + * + * Copyright (c) 2021, Heinrich Schuchardt <xypron.glpk@gmx.de> + */ + +#include <common.h> +#include <test/lib.h> +#include <test/test.h> +#include <test/ut.h> +#include <asm/setjmp.h> + +struct test_jmp_buf { + jmp_buf env; + int val; +}; + +/** + * test_longjmp() - test longjmp function + * + * @i is passed to longjmp. + * @i << 8 is set in the environment structure. + * + * @env: environment + * @i: value passed to longjmp() + */ +static noinline void test_longjmp(struct test_jmp_buf *env, int i) +{ + env->val = i << 8; + longjmp(env->env, i); +} + +/** + * test_setjmp() - test setjmp function + * + * setjmp() will return the value @i passed to longjmp() if @i is non-zero. + * For @i == 0 we expect return value 1. + * + * @i << 8 will be set by test_longjmp in the environment structure. + * This value can be used to check that the stack frame is restored. + * + * We return the XORed values to allow simply check both at once. + * + * @i: value passed to longjmp() + * Return: values return by longjmp() + */ +static int test_setjmp(int i) +{ + struct test_jmp_buf env; + int ret; + + env.val = -1; + ret = setjmp(env.env); + if (ret) + return ret ^ env.val; + test_longjmp(&env, i); + /* We should not arrive here */ + return 0x1000; +} + +static int lib_test_longjmp(struct unit_test_state *uts) +{ + int i; + + for (i = -3; i < 0; ++i) + ut_asserteq(i ^ (i << 8), test_setjmp(i)); + ut_asserteq(1, test_setjmp(0)); + for (i = 1; i < 4; ++i) + ut_asserteq(i ^ (i << 8), test_setjmp(i)); + return 0; +} +LIB_TEST(lib_test_longjmp, 0); |