| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/__support/CPP/string_view.h" |
| 10 | |
| 11 | #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE |
| 12 | #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_FILE |
| 17 | |
| 18 | #include "src/stdio/fscanf.h" |
| 19 | |
| 20 | #include "test/UnitTest/Test.h" |
| 21 | |
| 22 | namespace scanf_test { |
| 23 | #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE |
| 24 | using LIBC_NAMESPACE::fclose; |
| 25 | using LIBC_NAMESPACE::ferror; |
| 26 | using LIBC_NAMESPACE::fopen; |
| 27 | using LIBC_NAMESPACE::fwrite; |
| 28 | #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) |
| 29 | using ::fclose; |
| 30 | using ::ferror; |
| 31 | using ::fopen; |
| 32 | using ::fwrite; |
| 33 | #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE |
| 34 | } // namespace scanf_test |
| 35 | |
| 36 | TEST(LlvmLibcFScanfTest, WriteToFile) { |
| 37 | const char *FILENAME = "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 leaves |
| 80 | // 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 | |