brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 7af7eca Raw
76 lines · cpp
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-exception6//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/ErrnoCheckingTest.h"19#include "test/UnitTest/Test.h"20 21using LlvmLibcFILETest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;22 23TEST_F(LlvmLibcFILETest, UnlockedReadAndWrite) {24  constexpr char fNAME[] =25      APPEND_LIBC_TEST("testdata/unlocked_read_and_write.test");26  ::FILE *f = LIBC_NAMESPACE::fopen(fNAME, "w");27  ASSERT_FALSE(f == nullptr);28  constexpr char CONTENT[] = "1234567890987654321";29  LIBC_NAMESPACE::flockfile(f);30  ASSERT_EQ(sizeof(CONTENT) - 1, LIBC_NAMESPACE::fwrite_unlocked(31                                     CONTENT, 1, sizeof(CONTENT) - 1, f));32  // Should be an error to read.33  constexpr size_t READ_SIZE = 5;34  char data[READ_SIZE * 2 + 1];35  data[READ_SIZE * 2] = '\0';36 37  ASSERT_EQ(size_t(0),38            LIBC_NAMESPACE::fread_unlocked(data, 1, sizeof(READ_SIZE), f));39  ASSERT_NE(LIBC_NAMESPACE::ferror_unlocked(f), 0);40  ASSERT_ERRNO_FAILURE();41 42  LIBC_NAMESPACE::clearerr_unlocked(f);43  ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(f), 0);44 45  LIBC_NAMESPACE::funlockfile(f);46  ASSERT_EQ(0, LIBC_NAMESPACE::fclose(f));47 48  f = LIBC_NAMESPACE::fopen(fNAME, "r");49  ASSERT_FALSE(f == nullptr);50 51  LIBC_NAMESPACE::flockfile(f);52  ASSERT_EQ(LIBC_NAMESPACE::fread_unlocked(data, 1, READ_SIZE, f), READ_SIZE);53  ASSERT_EQ(LIBC_NAMESPACE::fread_unlocked(data + READ_SIZE, 1, READ_SIZE, f),54            READ_SIZE);55 56  // Should be an error to write.57  ASSERT_EQ(size_t(0),58            LIBC_NAMESPACE::fwrite_unlocked(CONTENT, 1, sizeof(CONTENT), f));59  ASSERT_NE(LIBC_NAMESPACE::ferror_unlocked(f), 0);60  ASSERT_ERRNO_FAILURE();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