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

source code of libc/test/src/stdio/fscanf_test.cpp