Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / setjmp / setjmp_test.cpp
blob9e5f74a1734b3535e6aadd8645a29ab0ab516d22
1 //===-- Unittests for setjmp and longjmp ----------------------------------===//
2 //
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
6 //
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) {
17 longjmp_called++;
18 LIBC_NAMESPACE::longjmp(buf, n); // Will return |n| out of setjmp
21 TEST(LlvmLibcSetJmpTest, SetAndJumpBack) {
22 jmp_buf buf;
23 longjmp_called = 0;
25 // Local variables in setjmp scope should be declared volatile.
26 volatile int n = 0;
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) {
30 ++n;
31 jump_back(buf, n);
33 ASSERT_EQ(longjmp_called, n);
34 ASSERT_EQ(n, MAX_LOOP + 1);
37 TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) {
38 jmp_buf buf;
39 longjmp_called = 0;
41 int val = LIBC_NAMESPACE::setjmp(buf);
42 if (val == 0)
43 jump_back(buf, val);
45 ASSERT_EQ(longjmp_called, 1);
46 ASSERT_EQ(val, 1);