17 changes: 16 additions & 1 deletion libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ add_libc_unittest(
libc.src.stdio.snprintf
)

# In fullbuild mode, fprintf's tests use the internal FILE for other functions.
if(LLVM_LIBC_FULL_BUILD)
add_libc_unittest(
fprintf_test
SUITE
Expand All @@ -147,7 +149,20 @@ add_libc_unittest(
libc.src.stdio.fopen
libc.src.stdio.fread
)

else()
# Else in overlay mode they use the system's FILE.
add_libc_unittest(
fprintf_test
SUITE
libc_stdio_unittests
SRCS
fprintf_test.cpp
DEPENDS
libc.src.stdio.fprintf
COMPILE_OPTIONS
-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE
)
endif()

add_libc_unittest(
printf_test
Expand Down
32 changes: 24 additions & 8 deletions libc/test/src/stdio/fprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
//
//===----------------------------------------------------------------------===//

#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE

#include "src/stdio/fprintf.h"

#include "test/UnitTest/Test.h"

#include <stdio.h>

namespace printf_test {
#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
using __llvm_libc::fclose;
using __llvm_libc::ferror;
using __llvm_libc::fopen;
using __llvm_libc::fread;
#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
using ::fread;
#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
} // namespace printf_test

TEST(LlvmLibcFPrintfTest, WriteToFile) {
constexpr char FILENAME[] = "testdata/fprintf_output.test";
::FILE *file = __llvm_libc::fopen(FILENAME, "w");
::FILE *file = printf_test::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);

int written;
Expand All @@ -37,31 +53,31 @@ TEST(LlvmLibcFPrintfTest, WriteToFile) {
written = __llvm_libc::fprintf(file, format_more, short_numbers);
EXPECT_EQ(written, 14);

ASSERT_EQ(0, __llvm_libc::fclose(file));
ASSERT_EQ(0, printf_test::fclose(file));

file = __llvm_libc::fopen(FILENAME, "r");
file = printf_test::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);

char data[50];
ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(simple) - 1, file),
ASSERT_EQ(printf_test::fread(data, 1, sizeof(simple) - 1, file),
sizeof(simple) - 1);
data[sizeof(simple) - 1] = '\0';
ASSERT_STREQ(data, simple);
ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(numbers) - 1, file),
ASSERT_EQ(printf_test::fread(data, 1, sizeof(numbers) - 1, file),
sizeof(numbers) - 1);
data[sizeof(numbers) - 1] = '\0';
ASSERT_STREQ(data, numbers);
ASSERT_EQ(__llvm_libc::fread(
ASSERT_EQ(printf_test::fread(
data, 1, sizeof(format_more) + sizeof(short_numbers) - 4, file),
sizeof(format_more) + sizeof(short_numbers) - 4);
data[sizeof(format_more) + sizeof(short_numbers) - 4] = '\0';
ASSERT_STREQ(data, "1234 and more\n");

ASSERT_EQ(__llvm_libc::ferror(file), 0);
ASSERT_EQ(printf_test::ferror(file), 0);

written =
__llvm_libc::fprintf(file, "Writing to a read only file should fail.");
EXPECT_LT(written, 0);

ASSERT_EQ(__llvm_libc::fclose(file), 0);
ASSERT_EQ(printf_test::fclose(file), 0);
}