Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / isatty_test.cpp
blobfce4a3e5f5062aa383a8ff7ece89cf3644804a92
1 //===-- Unittests for isatty ----------------------------------------------===//
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 "src/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/isatty.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
16 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
17 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
19 TEST(LlvmLibcIsATTYTest, StdInOutTests) {
20 // If stdin is connected to a terminal, assume that all of the standard i/o
21 // fds are.
22 libc_errno = 0;
23 if (LIBC_NAMESPACE::isatty(0)) {
24 EXPECT_THAT(LIBC_NAMESPACE::isatty(0), Succeeds(1)); // stdin
25 EXPECT_THAT(LIBC_NAMESPACE::isatty(1), Succeeds(1)); // stdout
26 EXPECT_THAT(LIBC_NAMESPACE::isatty(2), Succeeds(1)); // stderr
27 } else {
28 EXPECT_THAT(LIBC_NAMESPACE::isatty(0), Fails(ENOTTY, 0)); // stdin
29 EXPECT_THAT(LIBC_NAMESPACE::isatty(1), Fails(ENOTTY, 0)); // stdout
30 EXPECT_THAT(LIBC_NAMESPACE::isatty(2), Fails(ENOTTY, 0)); // stderr
34 TEST(LlvmLibcIsATTYTest, BadFdTest) {
35 libc_errno = 0;
36 EXPECT_THAT(LIBC_NAMESPACE::isatty(-1), Fails(EBADF, 0)); // invalid fd
39 TEST(LlvmLibcIsATTYTest, DevTTYTest) {
40 constexpr const char *TTY_FILE = "/dev/tty";
41 libc_errno = 0;
42 int fd = LIBC_NAMESPACE::open(TTY_FILE, O_RDONLY);
43 if (fd > 0) {
44 ASSERT_EQ(libc_errno, 0);
45 EXPECT_THAT(LIBC_NAMESPACE::isatty(fd), Succeeds(1));
46 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
50 TEST(LlvmLibcIsATTYTest, FileTest) {
51 constexpr const char *TEST_FILE = "testdata/isatty.test";
52 libc_errno = 0;
53 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
54 ASSERT_EQ(libc_errno, 0);
55 ASSERT_GT(fd, 0);
56 EXPECT_THAT(LIBC_NAMESPACE::isatty(fd), Fails(ENOTTY, 0));
57 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));