Revert "[lldb][test] Remove compiler version check and use regex" (#124101)
[llvm-project.git] / libc / test / src / stdio / unlocked_fileop_test.cpp
blob67f1b0ff513bc5921968180d0f2c708633cdcbbb
1 //===-- Unittests for f operations like fopen, flcose etc --------------===//
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/stdio/clearerr_unlocked.h"
10 #include "src/stdio/fclose.h"
11 #include "src/stdio/feof_unlocked.h"
12 #include "src/stdio/ferror_unlocked.h"
13 #include "src/stdio/flockfile.h"
14 #include "src/stdio/fopen.h"
15 #include "src/stdio/fread_unlocked.h"
16 #include "src/stdio/funlockfile.h"
17 #include "src/stdio/fwrite_unlocked.h"
18 #include "test/UnitTest/Test.h"
20 #include "src/errno/libc_errno.h"
22 TEST(LlvmLibcFILETest, UnlockedReadAndWrite) {
23 constexpr char fNAME[] = "testdata/unlocked_read_and_write.test";
24 ::FILE *f = LIBC_NAMESPACE::fopen(fNAME, "w");
25 ASSERT_FALSE(f == nullptr);
26 constexpr char CONTENT[] = "1234567890987654321";
27 LIBC_NAMESPACE::flockfile(f);
28 ASSERT_EQ(sizeof(CONTENT) - 1, LIBC_NAMESPACE::fwrite_unlocked(
29 CONTENT, 1, sizeof(CONTENT) - 1, f));
30 // Should be an error to read.
31 constexpr size_t READ_SIZE = 5;
32 char data[READ_SIZE * 2 + 1];
33 data[READ_SIZE * 2] = '\0';
35 ASSERT_EQ(size_t(0),
36 LIBC_NAMESPACE::fread_unlocked(data, 1, sizeof(READ_SIZE), f));
37 ASSERT_NE(LIBC_NAMESPACE::ferror_unlocked(f), 0);
38 ASSERT_ERRNO_FAILURE();
39 LIBC_NAMESPACE::libc_errno = 0;
41 LIBC_NAMESPACE::clearerr_unlocked(f);
42 ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(f), 0);
44 LIBC_NAMESPACE::funlockfile(f);
45 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(f));
47 f = LIBC_NAMESPACE::fopen(fNAME, "r");
48 ASSERT_FALSE(f == nullptr);
50 LIBC_NAMESPACE::flockfile(f);
51 ASSERT_EQ(LIBC_NAMESPACE::fread_unlocked(data, 1, READ_SIZE, f), READ_SIZE);
52 ASSERT_EQ(LIBC_NAMESPACE::fread_unlocked(data + READ_SIZE, 1, READ_SIZE, f),
53 READ_SIZE);
55 // Should be an error to write.
56 ASSERT_EQ(size_t(0),
57 LIBC_NAMESPACE::fwrite_unlocked(CONTENT, 1, sizeof(CONTENT), f));
58 ASSERT_NE(LIBC_NAMESPACE::ferror_unlocked(f), 0);
59 ASSERT_ERRNO_FAILURE();
60 LIBC_NAMESPACE::libc_errno = 0;
62 LIBC_NAMESPACE::clearerr_unlocked(f);
63 ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(f), 0);
65 // Reading more should trigger eof.
66 char large_data[sizeof(CONTENT)];
67 ASSERT_NE(sizeof(CONTENT),
68 LIBC_NAMESPACE::fread_unlocked(large_data, 1, sizeof(CONTENT), f));
69 ASSERT_NE(LIBC_NAMESPACE::feof_unlocked(f), 0);
71 LIBC_NAMESPACE::funlockfile(f);
72 ASSERT_STREQ(data, "1234567890");
74 ASSERT_EQ(LIBC_NAMESPACE::fclose(f), 0);