Revert "[lldb][test] Remove compiler version check and use regex" (#124101)
[llvm-project.git] / libc / test / src / stdio / fscanf_test.cpp
blobe5b8c4f422bacd4c583942e29894f7c4ecb9923b
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/string_view.h"
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
18 #include "src/stdio/fscanf.h"
20 #include "test/UnitTest/Test.h"
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
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);
42 int read;
44 constexpr char simple[] = "A simple string with no conversions.\n";
46 ASSERT_EQ(sizeof(simple) - 1,
47 scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file));
49 constexpr char numbers[] = "1234567890\n";
51 ASSERT_EQ(sizeof(numbers) - 1,
52 scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file));
54 constexpr char numbers_and_more[] = "1234 and more\n";
56 ASSERT_EQ(sizeof(numbers_and_more) - 1,
57 scanf_test::fwrite(numbers_and_more, 1,
58 sizeof(numbers_and_more) - 1, file));
60 read = LIBC_NAMESPACE::fscanf(file,
61 "Reading from a write-only file should fail.");
62 EXPECT_LT(read, 0);
64 ASSERT_EQ(0, scanf_test::fclose(file));
66 file = scanf_test::fopen(FILE_PATH, "r");
67 ASSERT_FALSE(file == nullptr);
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);
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));
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));
87 ASSERT_EQ(scanf_test::ferror(file), 0);
88 ASSERT_EQ(scanf_test::fclose(file), 0);