xref: /llvm-project/libc/test/src/stdio/fgetc_test.cpp (revision c63112a9118277a20ae440f3f69189c0937e8f4d)
10480b45eSSiva Chandra Reddy //===-- Unittests for fgetc -----------------------------------------------===//
20480b45eSSiva Chandra Reddy //
30480b45eSSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40480b45eSSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
50480b45eSSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60480b45eSSiva Chandra Reddy //
70480b45eSSiva Chandra Reddy //===----------------------------------------------------------------------===//
80480b45eSSiva Chandra Reddy 
90480b45eSSiva Chandra Reddy #include "src/stdio/clearerr.h"
100480b45eSSiva Chandra Reddy #include "src/stdio/fclose.h"
110480b45eSSiva Chandra Reddy #include "src/stdio/feof.h"
120480b45eSSiva Chandra Reddy #include "src/stdio/ferror.h"
130480b45eSSiva Chandra Reddy #include "src/stdio/fgetc.h"
140480b45eSSiva Chandra Reddy #include "src/stdio/fopen.h"
150480b45eSSiva Chandra Reddy #include "src/stdio/fwrite.h"
166a610195SSiva Chandra Reddy #include "src/stdio/getc.h"
17af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
180480b45eSSiva Chandra Reddy 
19*c63112a9Slntue #include "hdr/stdio_macros.h"
2004a9c625SMichael Jones #include "src/errno/libc_errno.h"
210480b45eSSiva Chandra Reddy 
22b6bc9d72SGuillaume Chatelet class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
236a610195SSiva Chandra Reddy public:
246a610195SSiva Chandra Reddy   using GetcFunc = int(FILE *);
256a610195SSiva Chandra Reddy   void test_with_func(GetcFunc *func, const char *filename) {
26b6bc9d72SGuillaume Chatelet     ::FILE *file = LIBC_NAMESPACE::fopen(filename, "w");
270480b45eSSiva Chandra Reddy     ASSERT_FALSE(file == nullptr);
280480b45eSSiva Chandra Reddy     constexpr char CONTENT[] = "123456789";
290480b45eSSiva Chandra Reddy     constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;
30b6bc9d72SGuillaume Chatelet     ASSERT_EQ(WRITE_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file));
310480b45eSSiva Chandra Reddy     // This is a write-only file so reads should fail.
326a610195SSiva Chandra Reddy     ASSERT_EQ(func(file), EOF);
330480b45eSSiva Chandra Reddy     // This is an error and not a real EOF.
34b6bc9d72SGuillaume Chatelet     ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
35b6bc9d72SGuillaume Chatelet     ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
363eb1e6d8Smichaelrj-google     LIBC_NAMESPACE::libc_errno = 0;
370480b45eSSiva Chandra Reddy 
38b6bc9d72SGuillaume Chatelet     ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
390480b45eSSiva Chandra Reddy 
40b6bc9d72SGuillaume Chatelet     file = LIBC_NAMESPACE::fopen(filename, "r");
410480b45eSSiva Chandra Reddy     ASSERT_FALSE(file == nullptr);
420480b45eSSiva Chandra Reddy 
430480b45eSSiva Chandra Reddy     for (size_t i = 0; i < WRITE_SIZE; ++i) {
446a610195SSiva Chandra Reddy       int c = func(file);
450480b45eSSiva Chandra Reddy       ASSERT_EQ(c, int('1' + i));
460480b45eSSiva Chandra Reddy     }
470480b45eSSiva Chandra Reddy     // Reading more should return EOF but not set error.
486a610195SSiva Chandra Reddy     ASSERT_EQ(func(file), EOF);
49b6bc9d72SGuillaume Chatelet     ASSERT_NE(LIBC_NAMESPACE::feof(file), 0);
50b6bc9d72SGuillaume Chatelet     ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
510480b45eSSiva Chandra Reddy 
52b6bc9d72SGuillaume Chatelet     ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
530480b45eSSiva Chandra Reddy   }
546a610195SSiva Chandra Reddy };
556a610195SSiva Chandra Reddy 
566a610195SSiva Chandra Reddy TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithFgetc) {
57b6bc9d72SGuillaume Chatelet   test_with_func(&LIBC_NAMESPACE::fgetc, "testdata/fgetc.test");
586a610195SSiva Chandra Reddy }
596a610195SSiva Chandra Reddy 
606a610195SSiva Chandra Reddy TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithGetc) {
61b6bc9d72SGuillaume Chatelet   test_with_func(&LIBC_NAMESPACE::getc, "testdata/getc.test");
626a610195SSiva Chandra Reddy }
63