Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / unlinkat_test.cpp
blob5953085bf12b66ca7379223f511a18378f5bf7f7
1 //===-- Unittests for unlinkat --------------------------------------------===//
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/fcntl/openat.h"
12 #include "src/unistd/close.h"
13 #include "src/unistd/unlinkat.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcUnlinkatTest, CreateAndDeleteTest) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
19 constexpr const char *TEST_DIR = "testdata";
20 constexpr const char *TEST_FILE = "openat.test";
21 int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
22 ASSERT_EQ(libc_errno, 0);
23 ASSERT_GT(dir_fd, 0);
24 int write_fd =
25 LIBC_NAMESPACE::openat(dir_fd, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
26 ASSERT_EQ(libc_errno, 0);
27 ASSERT_GT(write_fd, 0);
28 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
29 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd, TEST_FILE, 0), Succeeds(0));
30 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
33 TEST(LlvmLibcUnlinkatTest, UnlinkatNonExistentFile) {
34 constexpr const char *TEST_DIR = "testdata";
35 int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
36 ASSERT_EQ(libc_errno, 0);
37 ASSERT_GT(dir_fd, 0);
38 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
39 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
40 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd, "non-existent-file", 0),
41 Fails(ENOENT));
42 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));