xref: /aosp_15_r20/external/llvm-libc/test/src/stdio/unlocked_fileop_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
8 
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"
19 
20 #include "src/errno/libc_errno.h"
21 
TEST(LlvmLibcFILETest,UnlockedReadAndWrite)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';
34 
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;
40 
41   LIBC_NAMESPACE::clearerr_unlocked(f);
42   ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(f), 0);
43 
44   LIBC_NAMESPACE::funlockfile(f);
45   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(f));
46 
47   f = LIBC_NAMESPACE::fopen(fNAME, "r");
48   ASSERT_FALSE(f == nullptr);
49 
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);
54 
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;
61 
62   LIBC_NAMESPACE::clearerr_unlocked(f);
63   ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(f), 0);
64 
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);
70 
71   LIBC_NAMESPACE::funlockfile(f);
72   ASSERT_STREQ(data, "1234567890");
73 
74   ASSERT_EQ(LIBC_NAMESPACE::fclose(f), 0);
75 }
76