1 //===-- Unittests for setjmp and longjmp ----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/setjmp/longjmp.h"
10 #include "src/setjmp/setjmp_impl.h"
11 #include "test/UnitTest/Test.h"
13 constexpr int MAX_LOOP
= 123;
14 int longjmp_called
= 0;
16 void jump_back(jmp_buf buf
, int n
) {
18 LIBC_NAMESPACE::longjmp(buf
, n
); // Will return |n| out of setjmp
21 TEST(LlvmLibcSetJmpTest
, SetAndJumpBack
) {
25 // Local variables in setjmp scope should be declared volatile.
27 // The first time setjmp is called, it should return 0.
28 // Subsequent calls will return the value passed to jump_back below.
29 if (LIBC_NAMESPACE::setjmp(buf
) <= MAX_LOOP
) {
33 ASSERT_EQ(longjmp_called
, n
);
34 ASSERT_EQ(n
, MAX_LOOP
+ 1);
37 TEST(LlvmLibcSetJmpTest
, SetAndJumpBackValOne
) {
41 int val
= LIBC_NAMESPACE::setjmp(buf
);
45 ASSERT_EQ(longjmp_called
, 1);