[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / ErrnoTest.cpp
blob747d9dde0b73752e53c23e8fc750a3a0c64743fb
1 //===- ErrnoTest.cpp - Error handling unit tests --------------------------===//
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 "llvm/Support/Errno.h"
10 #include "gtest/gtest.h"
12 using namespace llvm::sys;
14 TEST(ErrnoTest, RetryAfterSignal) {
15 EXPECT_EQ(1, RetryAfterSignal(-1, [] { return 1; }));
17 EXPECT_EQ(-1, RetryAfterSignal(-1, [] {
18 errno = EAGAIN;
19 return -1;
20 }));
21 EXPECT_EQ(EAGAIN, errno);
23 unsigned calls = 0;
24 EXPECT_EQ(1, RetryAfterSignal(-1, [&calls] {
25 errno = EINTR;
26 ++calls;
27 return calls == 1 ? -1 : 1;
28 }));
29 EXPECT_EQ(2u, calls);
31 EXPECT_EQ(1, RetryAfterSignal(-1, [](int x) { return x; }, 1));
33 std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
34 EXPECT_EQ(47, *P);
36 errno = EINTR;
37 EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));