1 //===- ErrnoTest.cpp - Error handling unit tests --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/Errno.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm::sys;
14
TEST(ErrnoTest,RetryAfterSignal)15 TEST(ErrnoTest, RetryAfterSignal) {
16 EXPECT_EQ(1, RetryAfterSignal(-1, [] { return 1; }));
17
18 EXPECT_EQ(-1, RetryAfterSignal(-1, [] {
19 errno = EAGAIN;
20 return -1;
21 }));
22 EXPECT_EQ(EAGAIN, errno);
23
24 unsigned calls = 0;
25 EXPECT_EQ(1, RetryAfterSignal(-1, [&calls] {
26 errno = EINTR;
27 ++calls;
28 return calls == 1 ? -1 : 1;
29 }));
30 EXPECT_EQ(2u, calls);
31
32 EXPECT_EQ(1, RetryAfterSignal(-1, [](int x) { return x; }, 1));
33
34 std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
35 EXPECT_EQ(47, *P);
36
37 errno = EINTR;
38 EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));
39 }
40