Skip to content

Commit b78b16b

Browse files
authored
[libc][stdio] Separate temporary files for unit test and hermetic test in stdio test suite. (#149740)
1 parent 2b826df commit b78b16b

21 files changed

+67
-38
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ endfunction()
7171

7272
function(_get_hermetic_test_compile_options output_var)
7373
_get_common_test_compile_options(compile_options "" "")
74+
list(APPEND compile_options "-DLIBC_TEST=HERMETIC")
7475

7576
# null check tests are death tests, remove from hermetic tests for now.
7677
if(LIBC_ADD_NULL_CHECKS)
@@ -232,6 +233,7 @@ function(create_libc_unittest fq_target_name)
232233

233234
_get_common_test_compile_options(compile_options "${LIBC_UNITTEST_C_TEST}"
234235
"${LIBC_UNITTEST_FLAGS}")
236+
list(APPEND compile_options "-DLIBC_TEST=UNIT")
235237
# TODO: Ideally we would have a separate function for link options.
236238
set(link_options
237239
${compile_options}

libc/test/UnitTest/Test.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@
5252
libc_errno = 0; \
5353
} while (0)
5454

55+
// Some macro utility to append file names with LIBC_TEST macro's value to be
56+
// used in stdio tests.
57+
#undef STR
58+
#undef EVAL_THEN_STR
59+
#define STR(X) #X
60+
#define EVAL_THEN_STR(X) STR(X)
61+
62+
#define APPEND_LIBC_TEST(X) X "." EVAL_THEN_STR(LIBC_TEST)
63+
5564
#endif // LLVM_LIBC_TEST_UNITTEST_TEST_H

libc/test/src/__support/File/platform_file_test.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ LIBC_INLINE File *openfile(const char *file_name, const char *mode) {
2121
}
2222

2323
TEST(LlvmLibcPlatformFileTest, CreateWriteCloseAndReadBack) {
24-
constexpr char FILENAME[] = "testdata/create_write_close_and_readback.test";
24+
constexpr char FILENAME[] =
25+
APPEND_LIBC_TEST("testdata/create_write_close_and_readback.test");
2526
File *file = openfile(FILENAME, "w");
2627
ASSERT_FALSE(file == nullptr);
2728
ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
@@ -42,7 +43,8 @@ TEST(LlvmLibcPlatformFileTest, CreateWriteCloseAndReadBack) {
4243
}
4344

4445
TEST(LlvmLibcPlatformFileTest, CreateWriteSeekAndReadBack) {
45-
constexpr char FILENAME[] = "testdata/create_write_seek_and_readback.test";
46+
constexpr char FILENAME[] =
47+
APPEND_LIBC_TEST("testdata/create_write_seek_and_readback.test");
4648
File *file = openfile(FILENAME, "w+");
4749
ASSERT_FALSE(file == nullptr);
4850
ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
@@ -62,7 +64,8 @@ TEST(LlvmLibcPlatformFileTest, CreateWriteSeekAndReadBack) {
6264
}
6365

6466
TEST(LlvmLibcPlatformFileTest, CreateAppendCloseAndReadBack) {
65-
constexpr char FILENAME[] = "testdata/create_append_close_and_readback.test";
67+
constexpr char FILENAME[] =
68+
APPEND_LIBC_TEST("testdata/create_append_close_and_readback.test");
6669
File *file = openfile(FILENAME, "w");
6770
ASSERT_FALSE(file == nullptr);
6871
ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
@@ -91,7 +94,8 @@ TEST(LlvmLibcPlatformFileTest, CreateAppendCloseAndReadBack) {
9194
}
9295

9396
TEST(LlvmLibcPlatformFileTest, CreateAppendSeekAndReadBack) {
94-
constexpr char FILENAME[] = "testdata/create_append_seek_and_readback.test";
97+
constexpr char FILENAME[] =
98+
APPEND_LIBC_TEST("testdata/create_append_seek_and_readback.test");
9599
File *file = openfile(FILENAME, "w");
96100
ASSERT_FALSE(file == nullptr);
97101
ASSERT_EQ(file->write(TEXT, TEXT_SIZE).value, TEXT_SIZE);
@@ -124,7 +128,7 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
124128
for (size_t i = 0; i < DATA_SIZE; ++i)
125129
write_data[i] = BYTE;
126130

127-
constexpr char FILENAME[] = "testdata/large_file.test";
131+
constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/large_file.test");
128132
File *file = openfile(FILENAME, "w");
129133
ASSERT_FALSE(file == nullptr);
130134

@@ -151,7 +155,8 @@ TEST(LlvmLibcPlatformFileTest, LargeFile) {
151155
}
152156

153157
TEST(LlvmLibcPlatformFileTest, ReadSeekCurAndRead) {
154-
constexpr char FILENAME[] = "testdata/read_seek_cur_and_read.test";
158+
constexpr char FILENAME[] =
159+
APPEND_LIBC_TEST("testdata/read_seek_cur_and_read.test");
155160
File *file = openfile(FILENAME, "w");
156161
ASSERT_FALSE(file == nullptr);
157162
constexpr char CONTENT[] = "1234567890987654321";
@@ -178,7 +183,8 @@ TEST(LlvmLibcPlatformFileTest, ReadSeekCurAndRead) {
178183
}
179184

180185
TEST(LlvmLibcPlatformFileTest, IncorrectOperation) {
181-
constexpr char FILENAME[] = "testdata/incorrect_operation.test";
186+
constexpr char FILENAME[] =
187+
APPEND_LIBC_TEST("testdata/incorrect_operation.test");
182188
char data[1] = {123};
183189

184190
File *file = openfile(FILENAME, "w");

libc/test/src/stdio/fdopen_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ using LlvmLibcStdioFdopenTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
2424

2525
TEST_F(LlvmLibcStdioFdopenTest, WriteAppendRead) {
2626
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
27-
constexpr const char *TEST_FILE_NAME = "testdata/write_read_append.test";
27+
constexpr const char *TEST_FILE_NAME =
28+
APPEND_LIBC_TEST("testdata/write_read_append.test");
2829
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
2930
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
3031
auto *fp = LIBC_NAMESPACE::fdopen(fd, "w");
@@ -54,7 +55,8 @@ TEST_F(LlvmLibcStdioFdopenTest, WriteAppendRead) {
5455
}
5556

5657
TEST_F(LlvmLibcStdioFdopenTest, InvalidFd) {
57-
constexpr const char *TEST_FILE_NAME = "testdata/invalid_fd.test";
58+
constexpr const char *TEST_FILE_NAME =
59+
APPEND_LIBC_TEST("testdata/invalid_fd.test");
5860
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
5961
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC);
6062
LIBC_NAMESPACE::close(fd);
@@ -65,7 +67,8 @@ TEST_F(LlvmLibcStdioFdopenTest, InvalidFd) {
6567
}
6668

6769
TEST_F(LlvmLibcStdioFdopenTest, InvalidMode) {
68-
constexpr const char *TEST_FILE_NAME = "testdata/invalid_mode.test";
70+
constexpr const char *TEST_FILE_NAME =
71+
APPEND_LIBC_TEST("testdata/invalid_mode.test");
6972
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
7073
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_RDONLY, S_IRWXU);
7174
ASSERT_ERRNO_SUCCESS();

libc/test/src/stdio/fgetc_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
5656
};
5757

5858
TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithFgetc) {
59-
test_with_func(&LIBC_NAMESPACE::fgetc, "testdata/fgetc.test");
59+
test_with_func(&LIBC_NAMESPACE::fgetc,
60+
APPEND_LIBC_TEST("testdata/fgetc.test"));
6061
}
6162

6263
TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithGetc) {
63-
test_with_func(&LIBC_NAMESPACE::getc, "testdata/getc.test");
64+
test_with_func(&LIBC_NAMESPACE::getc, APPEND_LIBC_TEST("testdata/getc.test"));
6465
}

libc/test/src/stdio/fgetc_unlocked_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
6262

6363
TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithFgetcUnlocked) {
6464
test_with_func(&LIBC_NAMESPACE::fgetc_unlocked,
65-
"testdata/fgetc_unlocked.test");
65+
APPEND_LIBC_TEST("testdata/fgetc_unlocked.test"));
6666
}
6767

6868
TEST_F(LlvmLibcGetcTest, WriteAndReadCharactersWithGetcUnlocked) {
69-
test_with_func(&LIBC_NAMESPACE::getc_unlocked, "testdata/getc_unlocked.test");
69+
test_with_func(&LIBC_NAMESPACE::getc_unlocked,
70+
APPEND_LIBC_TEST("testdata/getc_unlocked.test"));
7071
}

libc/test/src/stdio/fgets_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using LlvmLibcFgetsTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
2020
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
2121

2222
TEST_F(LlvmLibcFgetsTest, WriteAndReadCharacters) {
23-
constexpr char FILENAME[] = "testdata/fgets.test";
23+
constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/fgets.test");
2424
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
2525
ASSERT_FALSE(file == nullptr);
2626
constexpr char CONTENT[] = "123456789\n"

libc/test/src/stdio/fileop_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
2929
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
3030

3131
TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
32-
constexpr char FILENAME[] = "testdata/simple_operations.test";
32+
constexpr char FILENAME[] =
33+
APPEND_LIBC_TEST("testdata/simple_operations.test");
3334
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
3435
ASSERT_FALSE(file == nullptr);
3536
ASSERT_GE(LIBC_NAMESPACE::fileno(file), 0);
@@ -127,7 +128,7 @@ TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
127128
}
128129

129130
TEST_F(LlvmLibcFILETest, FFlush) {
130-
constexpr char FILENAME[] = "testdata/fflush.test";
131+
constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/fflush.test");
131132
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w+");
132133
ASSERT_FALSE(file == nullptr);
133134
constexpr char CONTENT[] = "1234567890987654321";
@@ -154,7 +155,7 @@ TEST_F(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
154155
};
155156
constexpr MyStruct WRITE_DATA[] = {{'a', 1}, {'b', 2}, {'c', 3}};
156157
constexpr size_t WRITE_NMEMB = sizeof(WRITE_DATA) / sizeof(MyStruct);
157-
constexpr char FILENAME[] = "testdata/fread_fwrite.test";
158+
constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/fread_fwrite.test");
158159

159160
FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
160161
ASSERT_FALSE(file == nullptr);

libc/test/src/stdio/fopen_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
TEST(LlvmLibcFOpenTest, PrintToFile) {
1818
int result;
1919

20-
FILE *file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "w");
20+
FILE *file =
21+
LIBC_NAMESPACE::fopen(APPEND_LIBC_TEST("testdata/test.txt"), "w");
2122
ASSERT_FALSE(file == nullptr);
2223

2324
static constexpr char STRING[] = "A simple string written to a file\n";
@@ -26,7 +27,8 @@ TEST(LlvmLibcFOpenTest, PrintToFile) {
2627

2728
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
2829

29-
FILE *new_file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "r");
30+
FILE *new_file =
31+
LIBC_NAMESPACE::fopen(APPEND_LIBC_TEST("testdata/test.txt"), "r");
3032
ASSERT_FALSE(new_file == nullptr);
3133

3234
static char data[64] = {0};

libc/test/src/stdio/fprintf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using ::fread;
3232
} // namespace printf_test
3333

3434
TEST(LlvmLibcFPrintfTest, WriteToFile) {
35-
const char *FILENAME = "fprintf_output.test";
35+
const char *FILENAME = APPEND_LIBC_TEST("fprintf_output.test");
3636
auto FILE_PATH = libc_make_test_file_path(FILENAME);
3737

3838
::FILE *file = printf_test::fopen(FILE_PATH, "w");

0 commit comments

Comments
 (0)