1 | //===-- Unittests for vfprintf --------------------------------------------===// |
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 | // These tests are copies of the non-v variants of the printf functions. This is |
10 | // because these functions are identical in every way except for how the varargs |
11 | // are passed. |
12 | |
13 | #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE |
14 | #include "src/stdio/fclose.h" |
15 | #include "src/stdio/ferror.h" |
16 | #include "src/stdio/fopen.h" |
17 | #include "src/stdio/fread.h" |
18 | #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE |
19 | |
20 | #include "src/stdio/vfprintf.h" |
21 | |
22 | #include "test/UnitTest/Test.h" |
23 | |
24 | #include <stdio.h> |
25 | |
26 | namespace printf_test { |
27 | #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE |
28 | using LIBC_NAMESPACE::fclose; |
29 | using LIBC_NAMESPACE::ferror; |
30 | using LIBC_NAMESPACE::fopen; |
31 | using LIBC_NAMESPACE::fread; |
32 | #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) |
33 | using ::fclose; |
34 | using ::ferror; |
35 | using ::fopen; |
36 | using ::fread; |
37 | #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE |
38 | } // namespace printf_test |
39 | |
40 | int call_vfprintf(::FILE *__restrict stream, const char *__restrict format, |
41 | ...) { |
42 | va_list vlist; |
43 | va_start(vlist, format); |
44 | int ret = LIBC_NAMESPACE::vfprintf(stream, format, vlist); |
45 | va_end(vlist); |
46 | return ret; |
47 | } |
48 | |
49 | TEST(LlvmLibcVFPrintfTest, WriteToFile) { |
50 | const char *FILENAME = "vfprintf_output.test" ; |
51 | auto FILE_PATH = libc_make_test_file_path(FILENAME); |
52 | |
53 | ::FILE *file = printf_test::fopen(filename: FILE_PATH, modes: "w" ); |
54 | ASSERT_FALSE(file == nullptr); |
55 | |
56 | int written; |
57 | |
58 | constexpr char simple[] = "A simple string with no conversions.\n" ; |
59 | written = call_vfprintf(stream: file, format: simple); |
60 | EXPECT_EQ(written, 37); |
61 | |
62 | constexpr char numbers[] = "1234567890\n" ; |
63 | written = call_vfprintf(stream: file, format: "%s" , numbers); |
64 | EXPECT_EQ(written, 11); |
65 | |
66 | constexpr char format_more[] = "%s and more\n" ; |
67 | constexpr char short_numbers[] = "1234" ; |
68 | written = call_vfprintf(stream: file, format: format_more, short_numbers); |
69 | EXPECT_EQ(written, 14); |
70 | |
71 | ASSERT_EQ(0, printf_test::fclose(file)); |
72 | |
73 | file = printf_test::fopen(filename: FILE_PATH, modes: "r" ); |
74 | ASSERT_FALSE(file == nullptr); |
75 | |
76 | char data[50]; |
77 | ASSERT_EQ(printf_test::fread(data, 1, sizeof(simple) - 1, file), |
78 | sizeof(simple) - 1); |
79 | data[sizeof(simple) - 1] = '\0'; |
80 | ASSERT_STREQ(data, simple); |
81 | ASSERT_EQ(printf_test::fread(data, 1, sizeof(numbers) - 1, file), |
82 | sizeof(numbers) - 1); |
83 | data[sizeof(numbers) - 1] = '\0'; |
84 | ASSERT_STREQ(data, numbers); |
85 | ASSERT_EQ(printf_test::fread( |
86 | data, 1, sizeof(format_more) + sizeof(short_numbers) - 4, file), |
87 | sizeof(format_more) + sizeof(short_numbers) - 4); |
88 | data[sizeof(format_more) + sizeof(short_numbers) - 4] = '\0'; |
89 | ASSERT_STREQ(data, "1234 and more\n" ); |
90 | |
91 | ASSERT_EQ(printf_test::ferror(file), 0); |
92 | |
93 | written = call_vfprintf(stream: file, format: "Writing to a read only file should fail." ); |
94 | EXPECT_LT(written, 0); |
95 | |
96 | ASSERT_EQ(printf_test::fclose(file), 0); |
97 | } |
98 | |