brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 451ff94 Raw
90 lines · cpp
1//===-- Unittests for fscanf ----------------------------------------------===//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/__support/CPP/string_view.h"10 11#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE12#include "src/stdio/fclose.h"13#include "src/stdio/ferror.h"14#include "src/stdio/fopen.h"15#include "src/stdio/fwrite.h"16#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE17 18#include "src/stdio/fscanf.h"19 20#include "test/UnitTest/Test.h"21 22namespace scanf_test {23#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE24using LIBC_NAMESPACE::fclose;25using LIBC_NAMESPACE::ferror;26using LIBC_NAMESPACE::fopen;27using LIBC_NAMESPACE::fwrite;28#else  // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)29using ::fclose;30using ::ferror;31using ::fopen;32using ::fwrite;33#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE34} // namespace scanf_test35 36TEST(LlvmLibcFScanfTest, WriteToFile) {37  const char *FILENAME = APPEND_LIBC_TEST("fscanf_output.test");38  auto FILE_PATH = libc_make_test_file_path(FILENAME);39  ::FILE *file = scanf_test::fopen(FILE_PATH, "w");40  ASSERT_FALSE(file == nullptr);41 42  int read;43 44  constexpr char simple[] = "A simple string with no conversions.\n";45 46  ASSERT_EQ(sizeof(simple) - 1,47            scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file));48 49  constexpr char numbers[] = "1234567890\n";50 51  ASSERT_EQ(sizeof(numbers) - 1,52            scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file));53 54  constexpr char numbers_and_more[] = "1234 and more\n";55 56  ASSERT_EQ(sizeof(numbers_and_more) - 1,57            scanf_test::fwrite(numbers_and_more, 1,58                               sizeof(numbers_and_more) - 1, file));59 60  read = LIBC_NAMESPACE::fscanf(file,61                                "Reading from a write-only file should fail.");62  EXPECT_LT(read, 0);63 64  ASSERT_EQ(0, scanf_test::fclose(file));65 66  file = scanf_test::fopen(FILE_PATH, "r");67  ASSERT_FALSE(file == nullptr);68 69  char data[50];70  read = LIBC_NAMESPACE::fscanf(file, "%[A-Za-z .\n]", data);71  ASSERT_EQ(read, 1);72  ASSERT_STREQ(simple, data);73 74  read = LIBC_NAMESPACE::fscanf(file, "%s", data);75  ASSERT_EQ(read, 1);76  ASSERT_EQ(LIBC_NAMESPACE::cpp::string_view(numbers, 10),77            LIBC_NAMESPACE::cpp::string_view(data));78 79  // The format string starts with a space to handle the fact that the %s leaves80  // a trailing \n and %c doesn't strip leading whitespace.81  read = LIBC_NAMESPACE::fscanf(file, " %50c", data);82  ASSERT_EQ(read, 1);83  ASSERT_EQ(84      LIBC_NAMESPACE::cpp::string_view(numbers_and_more),85      LIBC_NAMESPACE::cpp::string_view(data, sizeof(numbers_and_more) - 1));86 87  ASSERT_EQ(scanf_test::ferror(file), 0);88  ASSERT_EQ(scanf_test::fclose(file), 0);89}90