xref: /llvm-project/libc/test/src/stdio/fprintf_test.cpp (revision c63112a9118277a20ae440f3f69189c0937e8f4d)
1ba7e1cddSMichael Jones //===-- Unittests for fprintf ---------------------------------------------===//
2ba7e1cddSMichael Jones //
3ba7e1cddSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ba7e1cddSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5ba7e1cddSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ba7e1cddSMichael Jones //
7ba7e1cddSMichael Jones //===----------------------------------------------------------------------===//
8ba7e1cddSMichael Jones 
9a5a008ffSmichaelrj-google #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
10ba7e1cddSMichael Jones #include "src/stdio/fclose.h"
11ba7e1cddSMichael Jones #include "src/stdio/ferror.h"
12ba7e1cddSMichael Jones #include "src/stdio/fopen.h"
13ba7e1cddSMichael Jones #include "src/stdio/fread.h"
14a5a008ffSmichaelrj-google #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
15ba7e1cddSMichael Jones 
16ba7e1cddSMichael Jones #include "src/stdio/fprintf.h"
17ba7e1cddSMichael Jones 
18af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
19ba7e1cddSMichael Jones 
20de939c6cSMichael Jones namespace printf_test {
21a5a008ffSmichaelrj-google #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
22*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fclose;
23*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::ferror;
24*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fopen;
25*b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::fread;
26a5a008ffSmichaelrj-google #else  // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
27de939c6cSMichael Jones using ::fclose;
28de939c6cSMichael Jones using ::ferror;
29de939c6cSMichael Jones using ::fopen;
30de939c6cSMichael Jones using ::fread;
31a5a008ffSmichaelrj-google #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
32de939c6cSMichael Jones } // namespace printf_test
33de939c6cSMichael Jones 
34ba7e1cddSMichael Jones TEST(LlvmLibcFPrintfTest, WriteToFile) {
35d94fe972SMichael Jones   const char *FILENAME = "fprintf_output.test";
36d94fe972SMichael Jones   auto FILE_PATH = libc_make_test_file_path(FILENAME);
37d94fe972SMichael Jones 
38d94fe972SMichael Jones   ::FILE *file = printf_test::fopen(FILE_PATH, "w");
39ba7e1cddSMichael Jones   ASSERT_FALSE(file == nullptr);
40ba7e1cddSMichael Jones 
41ba7e1cddSMichael Jones   int written;
42ba7e1cddSMichael Jones 
43ba7e1cddSMichael Jones   constexpr char simple[] = "A simple string with no conversions.\n";
44*b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::fprintf(file, simple);
45ba7e1cddSMichael Jones   EXPECT_EQ(written, 37);
46ba7e1cddSMichael Jones 
47ba7e1cddSMichael Jones   constexpr char numbers[] = "1234567890\n";
48*b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::fprintf(file, "%s", numbers);
49ba7e1cddSMichael Jones   EXPECT_EQ(written, 11);
50ba7e1cddSMichael Jones 
51ba7e1cddSMichael Jones   constexpr char format_more[] = "%s and more\n";
52ba7e1cddSMichael Jones   constexpr char short_numbers[] = "1234";
53*b6bc9d72SGuillaume Chatelet   written = LIBC_NAMESPACE::fprintf(file, format_more, short_numbers);
54ba7e1cddSMichael Jones   EXPECT_EQ(written, 14);
55ba7e1cddSMichael Jones 
56de939c6cSMichael Jones   ASSERT_EQ(0, printf_test::fclose(file));
57ba7e1cddSMichael Jones 
58d94fe972SMichael Jones   file = printf_test::fopen(FILE_PATH, "r");
59ba7e1cddSMichael Jones   ASSERT_FALSE(file == nullptr);
60ba7e1cddSMichael Jones 
61ba7e1cddSMichael Jones   char data[50];
62de939c6cSMichael Jones   ASSERT_EQ(printf_test::fread(data, 1, sizeof(simple) - 1, file),
63ba7e1cddSMichael Jones             sizeof(simple) - 1);
64ba7e1cddSMichael Jones   data[sizeof(simple) - 1] = '\0';
65ba7e1cddSMichael Jones   ASSERT_STREQ(data, simple);
66de939c6cSMichael Jones   ASSERT_EQ(printf_test::fread(data, 1, sizeof(numbers) - 1, file),
67ba7e1cddSMichael Jones             sizeof(numbers) - 1);
68ba7e1cddSMichael Jones   data[sizeof(numbers) - 1] = '\0';
69ba7e1cddSMichael Jones   ASSERT_STREQ(data, numbers);
70de939c6cSMichael Jones   ASSERT_EQ(printf_test::fread(
71ba7e1cddSMichael Jones                 data, 1, sizeof(format_more) + sizeof(short_numbers) - 4, file),
72ba7e1cddSMichael Jones             sizeof(format_more) + sizeof(short_numbers) - 4);
73ba7e1cddSMichael Jones   data[sizeof(format_more) + sizeof(short_numbers) - 4] = '\0';
74ba7e1cddSMichael Jones   ASSERT_STREQ(data, "1234 and more\n");
75ba7e1cddSMichael Jones 
76de939c6cSMichael Jones   ASSERT_EQ(printf_test::ferror(file), 0);
77ba7e1cddSMichael Jones 
78ba7e1cddSMichael Jones   written =
79*b6bc9d72SGuillaume Chatelet       LIBC_NAMESPACE::fprintf(file, "Writing to a read only file should fail.");
802e6eccfeSMichael Jones   EXPECT_LT(written, 0);
81ba7e1cddSMichael Jones 
82de939c6cSMichael Jones   ASSERT_EQ(printf_test::fclose(file), 0);
83ba7e1cddSMichael Jones }
84