136991d83SMichael Jones //===-- Unittests for fscanf ----------------------------------------------===// 236991d83SMichael Jones // 336991d83SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 436991d83SMichael Jones // See https://llvm.org/LICENSE.txt for license information. 536991d83SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 636991d83SMichael Jones // 736991d83SMichael Jones //===----------------------------------------------------------------------===// 836991d83SMichael Jones 936991d83SMichael Jones #include "src/__support/CPP/string_view.h" 10a5a008ffSmichaelrj-google 11a5a008ffSmichaelrj-google #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE 1236991d83SMichael Jones #include "src/stdio/fclose.h" 1336991d83SMichael Jones #include "src/stdio/ferror.h" 1436991d83SMichael Jones #include "src/stdio/fopen.h" 1536991d83SMichael Jones #include "src/stdio/fwrite.h" 16a5a008ffSmichaelrj-google #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE 1736991d83SMichael Jones 1836991d83SMichael Jones #include "src/stdio/fscanf.h" 1936991d83SMichael Jones 20af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h" 2136991d83SMichael Jones 22a5a008ffSmichaelrj-google namespace scanf_test { 23a5a008ffSmichaelrj-google #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE 24*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fclose; 25*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::ferror; 26*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fopen; 27*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fwrite; 28a5a008ffSmichaelrj-google #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) 29a5a008ffSmichaelrj-google using ::fclose; 30a5a008ffSmichaelrj-google using ::ferror; 31a5a008ffSmichaelrj-google using ::fopen; 32a5a008ffSmichaelrj-google using ::fwrite; 33a5a008ffSmichaelrj-google #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE 34a5a008ffSmichaelrj-google } // namespace scanf_test 35a5a008ffSmichaelrj-google 3636991d83SMichael Jones TEST(LlvmLibcFScanfTest, WriteToFile) { 37a5a008ffSmichaelrj-google const char *FILENAME = "fscanf_output.test"; 38a5a008ffSmichaelrj-google auto FILE_PATH = libc_make_test_file_path(FILENAME); 39a5a008ffSmichaelrj-google ::FILE *file = scanf_test::fopen(FILE_PATH, "w"); 4036991d83SMichael Jones ASSERT_FALSE(file == nullptr); 4136991d83SMichael Jones 4236991d83SMichael Jones int read; 4336991d83SMichael Jones 4436991d83SMichael Jones constexpr char simple[] = "A simple string with no conversions.\n"; 4536991d83SMichael Jones 4636991d83SMichael Jones ASSERT_EQ(sizeof(simple) - 1, 47a5a008ffSmichaelrj-google scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file)); 4836991d83SMichael Jones 4936991d83SMichael Jones constexpr char numbers[] = "1234567890\n"; 5036991d83SMichael Jones 5136991d83SMichael Jones ASSERT_EQ(sizeof(numbers) - 1, 52a5a008ffSmichaelrj-google scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file)); 5336991d83SMichael Jones 5436991d83SMichael Jones constexpr char numbers_and_more[] = "1234 and more\n"; 5536991d83SMichael Jones 5636991d83SMichael Jones ASSERT_EQ(sizeof(numbers_and_more) - 1, 57a5a008ffSmichaelrj-google scanf_test::fwrite(numbers_and_more, 1, 5836991d83SMichael Jones sizeof(numbers_and_more) - 1, file)); 5936991d83SMichael Jones 60*b6bc9d72SGuillaume Chatelet read = LIBC_NAMESPACE::fscanf(file, 61*b6bc9d72SGuillaume Chatelet "Reading from a write-only file should fail."); 6236991d83SMichael Jones EXPECT_LT(read, 0); 6336991d83SMichael Jones 64a5a008ffSmichaelrj-google ASSERT_EQ(0, scanf_test::fclose(file)); 6536991d83SMichael Jones 66a5a008ffSmichaelrj-google file = scanf_test::fopen(FILE_PATH, "r"); 6736991d83SMichael Jones ASSERT_FALSE(file == nullptr); 6836991d83SMichael Jones 6936991d83SMichael Jones char data[50]; 70*b6bc9d72SGuillaume Chatelet read = LIBC_NAMESPACE::fscanf(file, "%[A-Za-z .\n]", data); 7136991d83SMichael Jones ASSERT_EQ(read, 1); 7236991d83SMichael Jones ASSERT_STREQ(simple, data); 7336991d83SMichael Jones 74*b6bc9d72SGuillaume Chatelet read = LIBC_NAMESPACE::fscanf(file, "%s", data); 7536991d83SMichael Jones ASSERT_EQ(read, 1); 76*b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::cpp::string_view(numbers, 10), 77*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::cpp::string_view(data)); 7836991d83SMichael Jones 7936991d83SMichael Jones // The format string starts with a space to handle the fact that the %s leaves 8036991d83SMichael Jones // a trailing \n and %c doesn't strip leading whitespace. 81*b6bc9d72SGuillaume Chatelet read = LIBC_NAMESPACE::fscanf(file, " %50c", data); 8236991d83SMichael Jones ASSERT_EQ(read, 1); 83*b6bc9d72SGuillaume Chatelet ASSERT_EQ( 84*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::cpp::string_view(numbers_and_more), 85*b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::cpp::string_view(data, sizeof(numbers_and_more) - 1)); 8636991d83SMichael Jones 87a5a008ffSmichaelrj-google ASSERT_EQ(scanf_test::ferror(file), 0); 88a5a008ffSmichaelrj-google ASSERT_EQ(scanf_test::fclose(file), 0); 8936991d83SMichael Jones } 90