Files
clang-p2996/libc/test/src/setjmp/setjmp_test.cpp
Siva Chandra Reddy 3b82b4fbd5 [libc] Add x86_64 implementation of setjmp and longjmp.
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D137147
2022-11-01 22:58:35 +00:00

31 lines
977 B
C++

//===-- Unittests for setjmp and longjmp ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/setjmp/longjmp.h"
#include "src/setjmp/setjmp_impl.h"
#include "utils/UnitTest/Test.h"
jmp_buf buf;
constexpr int MAX_LOOP = 123;
void jump_back(int n) {
__llvm_libc::longjmp(buf, n); // Will return |n| out of setjmp
}
TEST(LlvmLibcSetJmpTest, SetAndJumpBack) {
// Local variables in setjmp scope should be declared volatile.
volatile int n = 0;
// The first time setjmp is called, it should return 0.
// Subsequent calls will return the value passed to jump_back below.
if (__llvm_libc::setjmp(buf) <= MAX_LOOP) {
++n;
jump_back(n);
}
ASSERT_EQ(n, MAX_LOOP + 1);
}