Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / pread_pwrite_test.cpp
blob6819f559424f3e03eea69248552b013ce1e3974b
1 //===-- Unittests for pread and pwrite ------------------------------------===//
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/fsync.h"
13 #include "src/unistd/pread.h"
14 #include "src/unistd/pwrite.h"
15 #include "src/unistd/unlink.h"
16 #include "src/unistd/write.h"
17 #include "test/UnitTest/ErrnoSetterMatcher.h"
18 #include "test/UnitTest/Test.h"
20 TEST(LlvmLibcUniStd, PWriteAndPReadBackTest) {
21 // The strategy here is that we first create a file and write to it. Next,
22 // we open that file again and write at an offset. Finally, we open the
23 // file again and pread at an offset and make sure that only expected data
24 // is being read back. This also confirms that pwrite happened successfully.
25 constexpr const char HELLO[] = "hello";
26 constexpr int HELLO_SIZE = sizeof(HELLO);
27 constexpr off_t OFFSET = 3;
28 constexpr const char OFFSET_TEXT[] = "helhello";
29 constexpr int OFFSET_TEXT_SIZE = sizeof(OFFSET_TEXT);
31 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
33 constexpr const char *TEST_FILE = "testdata/pread_pwrite.test";
34 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
35 ASSERT_EQ(libc_errno, 0);
36 ASSERT_GT(fd, 0);
37 ASSERT_THAT(LIBC_NAMESPACE::write(fd, HELLO, HELLO_SIZE),
38 Succeeds(HELLO_SIZE));
39 ASSERT_THAT(LIBC_NAMESPACE::fsync(fd), Succeeds(0));
40 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
42 fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY);
43 ASSERT_EQ(libc_errno, 0);
44 ASSERT_GT(fd, 0);
45 ASSERT_THAT(LIBC_NAMESPACE::pwrite(fd, HELLO, HELLO_SIZE, OFFSET),
46 Succeeds(HELLO_SIZE));
47 ASSERT_THAT(LIBC_NAMESPACE::fsync(fd), Succeeds(0));
48 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
50 fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
51 ASSERT_EQ(libc_errno, 0);
52 ASSERT_GT(fd, 0);
53 char read_buf[OFFSET_TEXT_SIZE];
54 ASSERT_THAT(LIBC_NAMESPACE::pread(fd, read_buf, HELLO_SIZE, OFFSET),
55 Succeeds(HELLO_SIZE));
56 EXPECT_STREQ(read_buf, HELLO);
57 ASSERT_THAT(LIBC_NAMESPACE::pread(fd, read_buf, OFFSET_TEXT_SIZE, 0),
58 Succeeds(OFFSET_TEXT_SIZE));
59 EXPECT_STREQ(read_buf, OFFSET_TEXT);
60 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
62 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
65 TEST(LlvmLibcUniStd, PWriteFails) {
66 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
67 EXPECT_THAT(LIBC_NAMESPACE::pwrite(-1, "", 1, 0), Fails(EBADF));
70 TEST(LlvmLibcUniStd, PReadFails) {
71 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
72 EXPECT_THAT(LIBC_NAMESPACE::pread(-1, nullptr, 1, 0), Fails(EBADF));