1//===-- Unittests for vfscanf ---------------------------------------------===//
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/vfscanf.h"
19
20#include "test/UnitTest/Test.h"
21
22namespace scanf_test {
23#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
24using 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_FILE
34} // namespace scanf_test
35
36static int call_vfscanf(::FILE *stream, const char *__restrict format, ...) {
37 va_list vlist;
38 va_start(vlist, format);
39 int ret = LIBC_NAMESPACE::vfscanf(stream, format, vlist);
40 va_end(vlist);
41 return ret;
42}
43
44TEST(LlvmLibcVFScanfTest, WriteToFile) {
45 const char *FILENAME = "vfscanf_output.test";
46 auto FILE_PATH = libc_make_test_file_path(FILENAME);
47 ::FILE *file = scanf_test::fopen(FILE_PATH, "w");
48 ASSERT_FALSE(file == nullptr);
49
50 int read;
51
52 constexpr char simple[] = "A simple string with no conversions.\n";
53
54 ASSERT_EQ(sizeof(simple) - 1,
55 scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file));
56
57 constexpr char numbers[] = "1234567890\n";
58
59 ASSERT_EQ(sizeof(numbers) - 1,
60 scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file));
61
62 constexpr char numbers_and_more[] = "1234 and more\n";
63
64 ASSERT_EQ(sizeof(numbers_and_more) - 1,
65 scanf_test::fwrite(numbers_and_more, 1,
66 sizeof(numbers_and_more) - 1, file));
67
68 read = call_vfscanf(file, "Reading from a write-only file should fail.");
69 EXPECT_LT(read, 0);
70
71 ASSERT_EQ(0, scanf_test::fclose(file));
72
73 file = scanf_test::fopen(FILE_PATH, "r");
74 ASSERT_FALSE(file == nullptr);
75
76 char data[50];
77 read = call_vfscanf(file, "%[A-Za-z .\n]", data);
78 ASSERT_EQ(read, 1);
79 ASSERT_STREQ(simple, data);
80
81 read = call_vfscanf(file, "%s", data);
82 ASSERT_EQ(read, 1);
83 ASSERT_EQ(LIBC_NAMESPACE::cpp::string_view(numbers, 10),
84 LIBC_NAMESPACE::cpp::string_view(data));
85
86 // The format string starts with a space to handle the fact that the %s leaves
87 // a trailing \n and %c doesn't strip leading whitespace.
88 read = call_vfscanf(file, " %50c", data);
89 ASSERT_EQ(read, 1);
90 ASSERT_EQ(
91 LIBC_NAMESPACE::cpp::string_view(numbers_and_more),
92 LIBC_NAMESPACE::cpp::string_view(data, sizeof(numbers_and_more) - 1));
93
94 ASSERT_EQ(scanf_test::ferror(file), 0);
95 ASSERT_EQ(scanf_test::fclose(file), 0);
96}
97

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