Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / lseek_test.cpp
blob1a13d54dbc2328cebe9c738dcc5fcd97707144c4
1 //===-- Unittests for lseek -----------------------------------------------===//
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/lseek.h"
13 #include "src/unistd/read.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 #include <unistd.h>
19 TEST(LlvmLibcUniStd, LseekTest) {
20 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21 constexpr const char *TEST_FILE = "testdata/lseek.test";
22 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
23 ASSERT_EQ(libc_errno, 0);
24 ASSERT_GT(fd, 0);
25 constexpr const char LSEEK_TEST[] = "lseek test";
26 constexpr int LSEEK_TEST_SIZE = sizeof(LSEEK_TEST) - 1;
28 char read_buf[20];
29 ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
30 Succeeds(LSEEK_TEST_SIZE));
31 read_buf[LSEEK_TEST_SIZE] = '\0';
32 EXPECT_STREQ(read_buf, LSEEK_TEST);
34 // Seek to the beginning of the file and re-read.
35 ASSERT_THAT(LIBC_NAMESPACE::lseek(fd, 0, SEEK_SET), Succeeds(0));
36 ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
37 Succeeds(LSEEK_TEST_SIZE));
38 read_buf[LSEEK_TEST_SIZE] = '\0';
39 EXPECT_STREQ(read_buf, LSEEK_TEST);
41 // Seek to the beginning of the file from the end and re-read.
42 ASSERT_THAT(LIBC_NAMESPACE::lseek(fd, -LSEEK_TEST_SIZE, SEEK_END),
43 Succeeds(0));
44 ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
45 Succeeds(LSEEK_TEST_SIZE));
46 read_buf[LSEEK_TEST_SIZE] = '\0';
47 EXPECT_STREQ(read_buf, LSEEK_TEST);
49 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
52 TEST(LlvmLibcUniStd, LseekFailsTest) {
53 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
54 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
55 constexpr const char *TEST_FILE = "testdata/lseek.test";
56 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
57 ASSERT_EQ(libc_errno, 0);
58 ASSERT_GT(fd, 0);
59 EXPECT_THAT(LIBC_NAMESPACE::lseek(fd, -1, SEEK_CUR), Fails(EINVAL));
60 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));