42 changes: 29 additions & 13 deletions libc/test/src/__support/File/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,34 @@ add_libc_test(
libc.src.__support.File.file
)

if (TARGET libc.src.__support.File.platform_file)
add_libc_test(
platform_file_test
SUITE
libc-support-tests
SRCS
platform_file_test.cpp
DEPENDS
libc.src.__support.File.file
libc.src.__support.File.platform_file
libc.include.stdio
)
endif()
add_libc_test(
platform_file_test
SUITE
libc-support-tests
SRCS
platform_file_test.cpp
DEPENDS
libc.src.__support.File.file
libc.src.__support.File.platform_file
libc.include.stdio
)

set(platform_stream_targets "platform_stdout;platform_stdin;platform_stderr")
foreach(target IN LISTS platform_file_targets)
if(TARGET libc.src.__support.File.${target})
add_libc_test(
${target}_test
HERMETIC_TEST_ONLY
SUITE
libc-support-tests
SRCS
${target}_test.cpp
DEPENDS
libc.src.__support.File.file
libc.src.__support.File.${target}
libc.include.stdio
)
endif()
endforeach()

add_subdirectory(testdata)
5 changes: 0 additions & 5 deletions libc/test/src/__support/File/platform_file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,3 @@ TEST(LlvmLibcPlatformFileTest, IncorrectOperation) {
ASSERT_TRUE(file->error());
ASSERT_EQ(file->close(), 0);
}

TEST(LlvmLibcPlatformFileTest, StdOutStdErrSmokeTest) {
EXPECT_FALSE(__llvm_libc::stdout == nullptr);
EXPECT_FALSE(__llvm_libc::stderr == nullptr);
}
12 changes: 12 additions & 0 deletions libc/test/src/__support/File/platform_stderr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcPlatformStreamTest, StdErrSmokeTest) {
EXPECT_FALSE(__llvm_libc::stderr == nullptr);
}
12 changes: 12 additions & 0 deletions libc/test/src/__support/File/platform_stdin_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcPlatformStreamTest, StdInSmokeTest) {
EXPECT_FALSE(__llvm_libc::stdin == nullptr);
}
12 changes: 12 additions & 0 deletions libc/test/src/__support/File/platform_stdout_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcPlatformStreamTest, StdOutSmokeTest) {
EXPECT_FALSE(__llvm_libc::stdout == nullptr);
}
23 changes: 15 additions & 8 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ add_libc_unittest(
)

if(LLVM_LIBC_FULL_BUILD)
# In fullbuild mode, fprintf's tests use the internal FILE for other functions.
list(APPEND fprintf_test_deps
libc.src.stdio.fclose
libc.src.stdio.ferror
libc.src.stdio.fopen
libc.src.stdio.fread
# In fullbuild mode, fprintf's tests use the internal FILE for other functions.
list(APPEND fprintf_test_deps
libc.src.stdio.fclose
libc.src.stdio.ferror
libc.src.stdio.fopen
libc.src.stdio.fread
)
# This is to be used for tests which write to libc's platform streams
# under full build but write to system-lib's streams otherwise.
set(hermetic_test_only HERMETIC_TEST_ONLY)
else()
# Else in overlay mode they use the system's FILE.
set(fprintf_test_copts "-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE")
Expand All @@ -161,8 +164,9 @@ add_libc_unittest(
${fprintf_test_copts}
)

add_libc_unittest(
add_libc_test(
printf_test
${hermetic_test_only}
SUITE
libc_stdio_unittests
SRCS
Expand Down Expand Up @@ -205,8 +209,9 @@ add_libc_unittest(
${fprintf_test_copts}
)

add_libc_unittest(
add_libc_test(
vprintf_test
${hermetic_test_only}
SUITE
libc_stdio_unittests
SRCS
Expand Down Expand Up @@ -244,6 +249,7 @@ add_libc_unittest(

add_libc_test(
puts_test
HERMETIC_TEST_ONLY # writes to libc's stdout
SUITE
libc_stdio_unittests
SRCS
Expand All @@ -254,6 +260,7 @@ add_libc_test(

add_libc_test(
fputs_test
HERMETIC_TEST_ONLY # writes to libc's stdout and stderr
SUITE
libc_stdio_unittests
SRCS
Expand Down
3 changes: 2 additions & 1 deletion libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ add_libc_unittest(
libc.src.unistd.sysconf
)

add_libc_unittest(
add_libc_test(
getopt_test
HERMETIC_TEST_ONLY # Uses libc's own stderr
SUITE
libc_unistd_unittests
SRCS
Expand Down
32 changes: 15 additions & 17 deletions libc/test/src/unistd/getopt_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,25 @@ void set_state(FILE *errstream) {
&test_globals::optpos, &test_globals::opterr, errstream);
}

static void my_memcpy(char *dest, const char *src, size_t size) {
for (size_t i = 0; i < size; i++)
dest[i] = src[i];
}

ssize_t cookie_write(void *cookie, const char *buf, size_t size) {
char **pos = static_cast<char **>(cookie);
my_memcpy(*pos, buf, size);
*pos += size;
return size;
}

static cookie_io_functions_t cookie{nullptr, &cookie_write, nullptr, nullptr};

// TODO: <stdio> could be either llvm-libc's or the system libc's. The former
// doesn't currently support fmemopen but does have fopencookie. In the future
// just use that instead. This memopen does no error checking for the size
// of the buffer, etc.
FILE *memopen(char **pos) {
static auto memcpy = [](char *dest, const char *src, size_t size) {
for (size_t i = 0; i < size; i++)
dest[i] = src[i];
};

static auto *write =
+[](void *cookie, const char *buf, size_t size) -> ssize_t {
char **pos = static_cast<char **>(cookie);
memcpy(*pos, buf, size);
*pos += size;
return size;
};

static cookie_io_functions_t cookie{nullptr, write, nullptr, nullptr};
return __llvm_libc::fopencookie(pos, "w", cookie);
}
FILE *memopen(char **pos) { return __llvm_libc::fopencookie(pos, "w", cookie); }

struct LlvmLibcGetoptTest : public __llvm_libc::testing::Test {
FILE *errstream;
Expand Down