blob: 747d9dde0b73752e53c23e8fc750a3a0c64743fb [file] [log] [blame]
Pavel Labathfe09f502017-06-29 13:15:31 +00001//===- ErrnoTest.cpp - Error handling unit tests --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Pavel Labathfe09f502017-06-29 13:15:31 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/Errno.h"
10#include "gtest/gtest.h"
11
12using namespace llvm::sys;
13
14TEST(ErrnoTest, RetryAfterSignal) {
15 EXPECT_EQ(1, RetryAfterSignal(-1, [] { return 1; }));
16
17 EXPECT_EQ(-1, RetryAfterSignal(-1, [] {
18 errno = EAGAIN;
19 return -1;
20 }));
21 EXPECT_EQ(EAGAIN, errno);
22
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);
30
31 EXPECT_EQ(1, RetryAfterSignal(-1, [](int x) { return x; }, 1));
32
33 std::unique_ptr<int> P(RetryAfterSignal(nullptr, [] { return new int(47); }));
34 EXPECT_EQ(47, *P);
Chandler Carruth9659a122018-07-07 02:46:12 +000035
36 errno = EINTR;
37 EXPECT_EQ(-1, RetryAfterSignal(-1, [] { return -1; }));
Pavel Labathfe09f502017-06-29 13:15:31 +000038}