reactos/modules/rostests/apitests/crt/setjmp.c

139 lines
2.6 KiB
C
Raw Normal View History

/*
* PROJECT: ReactOS CRT
* LICENSE: MIT (https://spdx.org/licenses/MIT)
2025-03-29 22:24:17 +00:00
* PURPOSE: Tests for setjmp and longjmp
* COPYRIGHT: Copyright 2025 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
* Copyright 2025 Serge Gautherie <reactos-git_serge_171003@gautherie.fr>
*/
#include <apitest.h>
#include <setjmp.h>
2025-03-29 10:48:42 +00:00
static jmp_buf g_jmp_buf;
2025-03-29 21:57:18 +00:00
static void TEST_setjmp_1(void)
{
2025-03-29 21:57:18 +00:00
volatile int x = 2, y = 3, z = 4;
2025-03-29 22:08:16 +00:00
memset(&g_jmp_buf, 0xCC, sizeof(g_jmp_buf));
if (setjmp(g_jmp_buf) == 0)
{
ok_int(TRUE, TRUE);
2025-03-29 21:57:18 +00:00
ok_int(x, 2);
2025-03-29 22:00:33 +00:00
ok_int(y, 3);
ok_int(z, 4);
}
else
{
ok_int(TRUE, FALSE);
2025-03-29 21:57:18 +00:00
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
}
}
2025-03-29 21:57:18 +00:00
static void TEST_setjmp_2(void)
{
2025-03-29 21:57:18 +00:00
volatile int x = 1001, y = 1002, z = 1003;
volatile int value;
2025-03-29 22:08:16 +00:00
memset(&g_jmp_buf, 0xCC, sizeof(g_jmp_buf));
2025-03-29 21:57:18 +00:00
value = setjmp(g_jmp_buf);
if (value == 0)
{
2025-03-29 21:57:18 +00:00
ok_int(TRUE, TRUE);
longjmp(g_jmp_buf, 999);
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
}
2025-03-29 21:57:18 +00:00
else if (value == 999)
{
ok_int(x, 1001);
ok_int(y, 1002);
ok_int(z, 1003);
}
else
{
2025-03-29 21:57:18 +00:00
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
}
}
2025-03-29 22:07:04 +00:00
static void TEST_longjmp(int value)
{
2025-03-29 21:57:18 +00:00
ok_int(TRUE, TRUE);
2025-03-29 22:07:04 +00:00
longjmp(g_jmp_buf, value);
2025-03-29 21:57:18 +00:00
ok_int(TRUE, FALSE);
}
static void TEST_setjmp_3(void)
{
volatile int x = 1001, y = 1002, z = 1003;
volatile int value;
2025-03-29 22:08:16 +00:00
memset(&g_jmp_buf, 0xCC, sizeof(g_jmp_buf));
2025-03-29 21:57:18 +00:00
value = setjmp(g_jmp_buf);
if (value == 0)
{
2025-03-29 10:35:06 +00:00
ok_int(TRUE, TRUE);
2025-03-29 21:57:18 +00:00
z = 9999;
2025-03-29 22:07:04 +00:00
TEST_longjmp(0xBEEFCAFE);
2025-03-29 21:57:18 +00:00
ok_int(TRUE, FALSE);
2025-03-29 10:35:06 +00:00
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
}
2025-03-29 21:57:18 +00:00
else if (value == 0xBEEFCAFE)
{
ok_int(x, 1001);
ok_int(y, 1002);
ok_int(z, 9999);
}
else
{
2025-03-29 21:57:18 +00:00
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
ok_int(TRUE, FALSE);
}
}
2025-03-29 22:07:04 +00:00
static void TEST_setjmp_4(void)
{
2025-03-29 22:27:47 +00:00
volatile int x = 0xFEEDF00D;
2025-03-29 22:07:04 +00:00
volatile int value;
2025-03-29 22:08:16 +00:00
memset(&g_jmp_buf, 0xCC, sizeof(g_jmp_buf));
2025-03-29 22:07:04 +00:00
value = setjmp(g_jmp_buf);
if (value == 0)
{
ok_int(TRUE, TRUE);
TEST_longjmp(0);
ok_int(TRUE, FALSE);
}
else if (value == 1)
{
2025-03-29 22:27:47 +00:00
ok_int(x, 0xFEEDF00D);
2025-03-29 22:07:04 +00:00
}
else
{
ok_int(TRUE, FALSE);
}
}
START_TEST(setjmp)
{
2025-03-29 21:57:18 +00:00
TEST_setjmp_1();
TEST_setjmp_2();
TEST_setjmp_3();
2025-03-29 22:07:04 +00:00
TEST_setjmp_4();
}