brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · d13bb8d Raw
52 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/setjmp/longjmp.h"10#include "src/setjmp/setjmp_impl.h"11#include "test/UnitTest/Test.h"12 13namespace {14 15constexpr int MAX_LOOP = 123;16int longjmp_called = 0;17 18void jump_back(jmp_buf buf, int n) {19  longjmp_called++;20  LIBC_NAMESPACE::longjmp(buf, n); // Will return |n| out of setjmp21}22 23TEST(LlvmLibcSetJmpTest, SetAndJumpBack) {24  jmp_buf buf;25  longjmp_called = 0;26 27  // Local variables in setjmp scope should be declared volatile.28  volatile int n = 0;29  // The first time setjmp is called, it should return 0.30  // Subsequent calls will return the value passed to jump_back below.31  if (LIBC_NAMESPACE::setjmp(buf) <= MAX_LOOP) {32    n = n + 1;33    jump_back(buf, n);34  }35  ASSERT_EQ(longjmp_called, n);36  ASSERT_EQ(n, MAX_LOOP + 1);37}38 39TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) {40  jmp_buf buf;41  longjmp_called = 0;42 43  int val = LIBC_NAMESPACE::setjmp(buf);44  if (val == 0)45    jump_back(buf, val);46 47  ASSERT_EQ(longjmp_called, 1);48  ASSERT_EQ(val, 1);49}50 51} // namespace52