brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 1d242a0 Raw
66 lines · cpp
1//===-- Unittests for fgetc -----------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/stdio/clearerr.h"10#include "src/stdio/fclose.h"11#include "src/stdio/feof.h"12#include "src/stdio/ferror.h"13#include "src/stdio/fgetc.h"14#include "src/stdio/fopen.h"15#include "src/stdio/fwrite.h"16#include "src/stdio/getc.h"17#include "test/UnitTest/ErrnoCheckingTest.h"18#include "test/UnitTest/ErrnoSetterMatcher.h"19#include "test/UnitTest/Test.h"20 21#include "hdr/stdio_macros.h"22 23using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;24 25class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {26public:27  using GetcFunc = int(FILE *);28  void test_with_func(GetcFunc *func, const char *filename) {29    ::FILE *file = LIBC_NAMESPACE::fopen(filename, "w");30    ASSERT_FALSE(file == nullptr);31    constexpr char CONTENT[] = "123456789";32    constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;33    ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file),34                Succeeds(WRITE_SIZE));35    // This is a write-only file so reads should fail.36    ASSERT_THAT(func(file), Fails(EBADF, EOF));37    // This is an error and not a real EOF.38    ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);39    ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);40 41    ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());42 43    file = LIBC_NAMESPACE::fopen(filename, "r");44    ASSERT_FALSE(file == nullptr);45 46    for (size_t i = 0; i < WRITE_SIZE; ++i) {47      ASSERT_THAT(func(file), Succeeds(int('1' + i)));48    }49    // Reading more should return EOF but not set error.50    ASSERT_THAT(func(file), Succeeds(EOF));51    ASSERT_NE(LIBC_NAMESPACE::feof(file), 0);52    ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);53 54    ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());55  }56};57 58TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithFgetc) {59  test_with_func(&LIBC_NAMESPACE::fgetc,60                 APPEND_LIBC_TEST("testdata/fgetc.test"));61}62 63TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithGetc) {64  test_with_func(&LIBC_NAMESPACE::getc, APPEND_LIBC_TEST("testdata/getc.test"));65}66