Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / stdio / remove_test.cpp
blobce4a1352d1cc1a55b3262962267357e0492cc488
1 //===-- Unittests for remove ----------------------------------------------===//
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/fcntl/open.h"
10 #include "src/stdio/remove.h"
11 #include "src/sys/stat/mkdirat.h"
12 #include "src/unistd/access.h"
13 #include "src/unistd/close.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 #include "src/errno/libc_errno.h"
18 #include <unistd.h>
20 TEST(LlvmLibcRemoveTest, CreateAndRemoveFile) {
21 // The test strategy is to create a file and remove it, and also verify that
22 // it was removed.
23 libc_errno = 0;
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
26 constexpr const char *TEST_FILE = "testdata/remove.test.file";
27 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
28 ASSERT_EQ(libc_errno, 0);
29 ASSERT_GT(fd, 0);
30 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
32 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Succeeds(0));
33 ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));
34 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Fails(ENOENT));
37 TEST(LlvmLibcRemoveTest, CreateAndRemoveDir) {
38 // The test strategy is to create a dir and remove it, and also verify that
39 // it was removed.
40 libc_errno = 0;
41 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
42 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
43 constexpr const char *TEST_DIR = "testdata/remove.test.dir";
44 ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU),
45 Succeeds(0));
47 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_DIR, F_OK), Succeeds(0));
48 ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_DIR), Succeeds(0));
49 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_DIR, F_OK), Fails(ENOENT));
52 TEST(LlvmLibcRemoveTest, RemoveNonExistent) {
53 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
54 ASSERT_THAT(LIBC_NAMESPACE::remove("testdata/non-existent"), Fails(ENOENT));