[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / stdio / fgets_test.cpp
blob1ef24150d944b70d6840b0e0a70c0202f0d4a1c5
1 //===-- Unittests for fgets -----------------------------------------------===//
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/fclose.h"
10 #include "src/stdio/feof.h"
11 #include "src/stdio/ferror.h"
12 #include "src/stdio/fgets.h"
13 #include "src/stdio/fopen.h"
14 #include "src/stdio/fwrite.h"
15 #include "test/UnitTest/Test.h"
17 #include <errno.h>
18 #include <stdio.h>
20 TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
21 constexpr char FILENAME[] = "testdata/fgets.test";
22 ::FILE *file = __llvm_libc::fopen(FILENAME, "w");
23 ASSERT_FALSE(file == nullptr);
24 constexpr char CONTENT[] = "123456789\n"
25 "1234567\n"
26 "123456\n"
27 "1";
28 constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;
30 char buff[8];
31 char *output;
33 ASSERT_EQ(WRITE_SIZE, __llvm_libc::fwrite(CONTENT, 1, WRITE_SIZE, file));
34 // This is a write-only file so reads should fail.
35 ASSERT_TRUE(__llvm_libc::fgets(buff, 8, file) == nullptr);
36 // This is an error and not a real EOF.
37 ASSERT_EQ(__llvm_libc::feof(file), 0);
38 ASSERT_NE(__llvm_libc::ferror(file), 0);
39 errno = 0;
41 ASSERT_EQ(0, __llvm_libc::fclose(file));
43 file = __llvm_libc::fopen(FILENAME, "r");
44 ASSERT_FALSE(file == nullptr);
46 // If we request just 1 byte, it should return just a null byte and not
47 // advance the read head. This is implementation defined.
48 output = __llvm_libc::fgets(buff, 1, file);
49 ASSERT_TRUE(output == buff);
50 ASSERT_EQ(buff[0], '\0');
51 ASSERT_EQ(errno, 0);
53 // If we request less than 1 byte, it should do nothing and return nullptr.
54 // This is also implementation defined.
55 output = __llvm_libc::fgets(buff, 0, file);
56 ASSERT_TRUE(output == nullptr);
58 const char *output_arr[] = {
59 "1234567", "89\n", "1234567", "\n", "123456\n", "1",
62 constexpr size_t ARR_SIZE = sizeof(output_arr) / sizeof(char *);
64 for (size_t i = 0; i < ARR_SIZE; ++i) {
65 output = __llvm_libc::fgets(buff, 8, file);
67 // This pointer comparison is intentional, fgets should return a pointer to
68 // buff when it succeeds.
69 ASSERT_TRUE(output == buff);
70 ASSERT_EQ(__llvm_libc::ferror(file), 0);
72 EXPECT_STREQ(buff, output_arr[i]);
75 // This should have hit the end of the file, but that isn't an error unless it
76 // fails to read anything.
77 ASSERT_NE(__llvm_libc::feof(file), 0);
78 ASSERT_EQ(__llvm_libc::ferror(file), 0);
79 ASSERT_EQ(errno, 0);
81 // Reading more should be an EOF, but not an error.
82 output = __llvm_libc::fgets(buff, 8, file);
83 ASSERT_TRUE(output == nullptr);
84 ASSERT_NE(__llvm_libc::feof(file), 0);
85 ASSERT_EQ(errno, 0);
87 ASSERT_EQ(0, __llvm_libc::fclose(file));