Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / stdio / vfprintf_test.cpp
blob9bad2c831e5c42717bddd88254f1f14a22e5ee22
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 //===----------------------------------------------------------------------===//
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.
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
20 #include "src/stdio/vfprintf.h"
22 #include "test/UnitTest/Test.h"
24 #include <stdio.h>
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
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;
49 TEST(LlvmLibcVFPrintfTest, WriteToFile) {
50 const char *FILENAME = "vfprintf_output.test";
51 auto FILE_PATH = libc_make_test_file_path(FILENAME);
53 ::FILE *file = printf_test::fopen(FILE_PATH, "w");
54 ASSERT_FALSE(file == nullptr);
56 int written;
58 constexpr char simple[] = "A simple string with no conversions.\n";
59 written = call_vfprintf(file, simple);
60 EXPECT_EQ(written, 37);
62 constexpr char numbers[] = "1234567890\n";
63 written = call_vfprintf(file, "%s", numbers);
64 EXPECT_EQ(written, 11);
66 constexpr char format_more[] = "%s and more\n";
67 constexpr char short_numbers[] = "1234";
68 written = call_vfprintf(file, format_more, short_numbers);
69 EXPECT_EQ(written, 14);
71 ASSERT_EQ(0, printf_test::fclose(file));
73 file = printf_test::fopen(FILE_PATH, "r");
74 ASSERT_FALSE(file == nullptr);
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");
91 ASSERT_EQ(printf_test::ferror(file), 0);
93 written = call_vfprintf(file, "Writing to a read only file should fail.");
94 EXPECT_LT(written, 0);
96 ASSERT_EQ(printf_test::fclose(file), 0);