Skip to content

Commit 385ea62

Browse files
vonosmastomtor
authored andcommitted
Fix/reapply "[libc] Migrate stdio tests to ErrnoCheckingTest." (llvm#143972)
This reverts commit a93e55e and fixes build and test failures: * Proper include added to setvbuf_test.cpp * fgetc/fgetc_unlocked/fgets tests are ported to ErrnoSetterMatcher and are made more precise. This fixes inconsistencies between expectations in regular and GPU builds - ErrnoSetterMatcher is configured to omit errno matching on GPUs, as fgetc implementation on GPU doesn't set errno, in contrast to Linux.
1 parent 25049df commit 385ea62

13 files changed

+84
-77
lines changed

libc/test/src/stdio/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_libc_test(
2020
libc.src.stdio.fread
2121
libc.src.stdio.fseek
2222
libc.src.stdio.fwrite
23+
libc.test.UnitTest.ErrnoCheckingTest
2324
)
2425

2526
add_libc_test(
@@ -68,6 +69,7 @@ add_libc_test(
6869
libc.src.stdio.fread
6970
libc.src.stdio.fwrite
7071
libc.src.stdio.setvbuf
72+
libc.test.UnitTest.ErrnoCheckingTest
7173
)
7274

7375
add_libc_test(
@@ -88,6 +90,7 @@ add_libc_test(
8890
libc.src.stdio.fread_unlocked
8991
libc.src.stdio.funlockfile
9092
libc.src.stdio.fwrite_unlocked
93+
libc.test.UnitTest.ErrnoCheckingTest
9194
)
9295

9396
add_libc_test(
@@ -109,6 +112,7 @@ add_libc_test(
109112
libc.src.stdio.fread
110113
libc.src.stdio.fseek
111114
libc.src.stdio.fwrite
115+
libc.test.UnitTest.ErrnoCheckingTest
112116
LINK_LIBRARIES
113117
LibcMemoryHelpers
114118
)
@@ -438,6 +442,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
438442
libc.src.sys.stat.mkdirat
439443
libc.src.unistd.access
440444
libc.src.unistd.close
445+
libc.test.UnitTest.ErrnoCheckingTest
441446
)
442447

443448
add_libc_test(
@@ -452,6 +457,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
452457
libc.src.stdio.rename
453458
libc.src.unistd.access
454459
libc.src.unistd.close
460+
libc.test.UnitTest.ErrnoCheckingTest
455461
libc.test.UnitTest.ErrnoSetterMatcher
456462
)
457463

@@ -468,6 +474,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
468474
libc.src.stdio.fgets
469475
libc.src.stdio.fputs
470476
libc.src.unistd.close
477+
libc.test.UnitTest.ErrnoCheckingTest
471478
libc.test.UnitTest.ErrnoSetterMatcher
472479
)
473480
endif()
@@ -488,6 +495,8 @@ add_libc_test(
488495
libc.src.stdio.fopen
489496
libc.src.stdio.fwrite
490497
libc.src.stdio.getc
498+
libc.test.UnitTest.ErrnoCheckingTest
499+
libc.test.UnitTest.ErrnoSetterMatcher
491500
)
492501

493502
add_libc_test(
@@ -510,6 +519,8 @@ add_libc_test(
510519
libc.src.stdio.funlockfile
511520
libc.src.stdio.fwrite
512521
libc.src.stdio.getc_unlocked
522+
libc.test.UnitTest.ErrnoCheckingTest
523+
libc.test.UnitTest.ErrnoSetterMatcher
513524
)
514525

515526
add_libc_test(
@@ -527,6 +538,8 @@ add_libc_test(
527538
libc.src.stdio.fgets
528539
libc.src.stdio.fopen
529540
libc.src.stdio.fwrite
541+
libc.test.UnitTest.ErrnoCheckingTest
542+
libc.test.UnitTest.ErrnoSetterMatcher
530543
)
531544

532545
add_libc_test(

libc/test/src/stdio/fdopen_test.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
#include "src/stdio/fdopen.h"
1010

1111
#include "hdr/fcntl_macros.h"
12-
#include "src/__support/libc_errno.h"
1312
#include "src/fcntl/open.h"
1413
#include "src/stdio/fclose.h"
1514
#include "src/stdio/fgets.h"
1615
#include "src/stdio/fputs.h"
1716
#include "src/unistd/close.h"
17+
#include "test/UnitTest/ErrnoCheckingTest.h"
1818
#include "test/UnitTest/ErrnoSetterMatcher.h"
1919
#include "test/UnitTest/Test.h"
2020

2121
#include <sys/stat.h> // For S_IRWXU
2222

23-
TEST(LlvmLibcStdioFdopenTest, WriteAppendRead) {
23+
using LlvmLibcStdioFdopenTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
24+
25+
TEST_F(LlvmLibcStdioFdopenTest, WriteAppendRead) {
2426
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
25-
libc_errno = 0;
2627
constexpr const char *TEST_FILE_NAME = "testdata/write_read_append.test";
2728
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
2829
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
@@ -52,8 +53,7 @@ TEST(LlvmLibcStdioFdopenTest, WriteAppendRead) {
5253
ASSERT_ERRNO_SUCCESS();
5354
}
5455

55-
TEST(LlvmLibcStdioFdopenTest, InvalidFd) {
56-
libc_errno = 0;
56+
TEST_F(LlvmLibcStdioFdopenTest, InvalidFd) {
5757
constexpr const char *TEST_FILE_NAME = "testdata/invalid_fd.test";
5858
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
5959
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_TRUNC);
@@ -64,8 +64,7 @@ TEST(LlvmLibcStdioFdopenTest, InvalidFd) {
6464
ASSERT_TRUE(nullptr == fp);
6565
}
6666

67-
TEST(LlvmLibcStdioFdopenTest, InvalidMode) {
68-
libc_errno = 0;
67+
TEST_F(LlvmLibcStdioFdopenTest, InvalidMode) {
6968
constexpr const char *TEST_FILE_NAME = "testdata/invalid_mode.test";
7069
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
7170
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_RDONLY, S_IRWXU);
@@ -83,7 +82,6 @@ TEST(LlvmLibcStdioFdopenTest, InvalidMode) {
8382
auto *fp2 = LIBC_NAMESPACE::fdopen(fd, "w");
8483
ASSERT_ERRNO_EQ(EINVAL);
8584
ASSERT_TRUE(nullptr == fp2);
86-
libc_errno = 0;
8785
LIBC_NAMESPACE::close(fd);
8886
ASSERT_ERRNO_SUCCESS();
8987
}

libc/test/src/stdio/fgetc_test.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,44 @@
1414
#include "src/stdio/fopen.h"
1515
#include "src/stdio/fwrite.h"
1616
#include "src/stdio/getc.h"
17+
#include "test/UnitTest/ErrnoCheckingTest.h"
18+
#include "test/UnitTest/ErrnoSetterMatcher.h"
1719
#include "test/UnitTest/Test.h"
1820

1921
#include "hdr/stdio_macros.h"
20-
#include "src/__support/libc_errno.h"
2122

22-
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
23+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
24+
25+
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
2326
public:
2427
using GetcFunc = int(FILE *);
2528
void test_with_func(GetcFunc *func, const char *filename) {
2629
::FILE *file = LIBC_NAMESPACE::fopen(filename, "w");
2730
ASSERT_FALSE(file == nullptr);
2831
constexpr char CONTENT[] = "123456789";
2932
constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;
30-
ASSERT_EQ(WRITE_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file));
33+
ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file),
34+
Succeeds(WRITE_SIZE));
3135
// This is a write-only file so reads should fail.
32-
ASSERT_EQ(func(file), EOF);
36+
ASSERT_THAT(func(file), Fails(EBADF, EOF));
3337
// This is an error and not a real EOF.
3438
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3539
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
36-
libc_errno = 0;
3740

38-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
41+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
3942

4043
file = LIBC_NAMESPACE::fopen(filename, "r");
4144
ASSERT_FALSE(file == nullptr);
4245

4346
for (size_t i = 0; i < WRITE_SIZE; ++i) {
44-
int c = func(file);
45-
ASSERT_EQ(c, int('1' + i));
47+
ASSERT_THAT(func(file), Succeeds(int('1' + i)));
4648
}
4749
// Reading more should return EOF but not set error.
48-
ASSERT_EQ(func(file), EOF);
50+
ASSERT_THAT(func(file), Succeeds(EOF));
4951
ASSERT_NE(LIBC_NAMESPACE::feof(file), 0);
5052
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
5153

52-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
54+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
5355
}
5456
};
5557

libc/test/src/stdio/fgetc_unlocked_test.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,46 @@
1717
#include "src/stdio/funlockfile.h"
1818
#include "src/stdio/fwrite.h"
1919
#include "src/stdio/getc_unlocked.h"
20+
#include "test/UnitTest/ErrnoCheckingTest.h"
21+
#include "test/UnitTest/ErrnoSetterMatcher.h"
2022
#include "test/UnitTest/Test.h"
2123

2224
#include "hdr/stdio_macros.h"
23-
#include "src/__support/libc_errno.h"
2425

25-
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
26+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
27+
28+
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
2629
public:
2730
using GetcFunc = int(FILE *);
2831
void test_with_func(GetcFunc *func, const char *filename) {
2932
::FILE *file = LIBC_NAMESPACE::fopen(filename, "w");
3033
ASSERT_FALSE(file == nullptr);
3134
constexpr char CONTENT[] = "123456789";
3235
constexpr size_t WRITE_SIZE = sizeof(CONTENT) - 1;
33-
ASSERT_EQ(WRITE_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file));
36+
ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file),
37+
Succeeds(WRITE_SIZE));
3438
// This is a write-only file so reads should fail.
35-
ASSERT_EQ(func(file), EOF);
39+
ASSERT_THAT(func(file), Fails(EBADF, EOF));
3640
// This is an error and not a real EOF.
3741
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3842
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
39-
libc_errno = 0;
4043

41-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
44+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
4245

4346
file = LIBC_NAMESPACE::fopen(filename, "r");
4447
ASSERT_FALSE(file == nullptr);
4548

4649
LIBC_NAMESPACE::flockfile(file);
4750
for (size_t i = 0; i < WRITE_SIZE; ++i) {
48-
int c = func(file);
49-
ASSERT_EQ(c, int('1' + i));
51+
ASSERT_THAT(func(file), Succeeds(int('1' + i)));
5052
}
5153
// Reading more should return EOF but not set error.
52-
ASSERT_EQ(func(file), EOF);
54+
ASSERT_THAT(func(file), Succeeds(EOF));
5355
ASSERT_NE(LIBC_NAMESPACE::feof_unlocked(file), 0);
5456
ASSERT_EQ(LIBC_NAMESPACE::ferror_unlocked(file), 0);
5557

5658
LIBC_NAMESPACE::funlockfile(file);
57-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
59+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
5860
}
5961
};
6062

libc/test/src/stdio/fgets_test.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#include "src/stdio/fgets.h"
1313
#include "src/stdio/fopen.h"
1414
#include "src/stdio/fwrite.h"
15+
#include "test/UnitTest/ErrnoCheckingTest.h"
16+
#include "test/UnitTest/ErrnoSetterMatcher.h"
1517
#include "test/UnitTest/Test.h"
1618

17-
#include "src/__support/libc_errno.h"
19+
using LlvmLibcFgetsTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
20+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
1821

19-
TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
22+
TEST_F(LlvmLibcFgetsTest, WriteAndReadCharacters) {
2023
constexpr char FILENAME[] = "testdata/fgets.test";
2124
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
2225
ASSERT_FALSE(file == nullptr);
@@ -29,15 +32,15 @@ TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
2932
char buff[8];
3033
char *output;
3134

32-
ASSERT_EQ(WRITE_SIZE, LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file));
35+
ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, WRITE_SIZE, file),
36+
Succeeds(WRITE_SIZE));
3337
// This is a write-only file so reads should fail.
34-
ASSERT_TRUE(LIBC_NAMESPACE::fgets(buff, 8, file) == nullptr);
38+
ASSERT_THAT(LIBC_NAMESPACE::fgets(buff, 8, file), Fails(EBADF, nullptr));
3539
// This is an error and not a real EOF.
3640
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3741
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
38-
libc_errno = 0;
3942

40-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
43+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
4144

4245
file = LIBC_NAMESPACE::fopen(FILENAME, "r");
4346
ASSERT_FALSE(file == nullptr);
@@ -55,6 +58,7 @@ TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
5558
// This is also implementation defined.
5659
output = LIBC_NAMESPACE::fgets(buff, 0, file);
5760
ASSERT_TRUE(output == nullptr);
61+
ASSERT_ERRNO_SUCCESS();
5862
#endif
5963

6064
const char *output_arr[] = {
@@ -86,5 +90,5 @@ TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
8690
ASSERT_NE(LIBC_NAMESPACE::feof(file), 0);
8791
ASSERT_ERRNO_SUCCESS();
8892

89-
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
93+
ASSERT_THAT(LIBC_NAMESPACE::fclose(file), Succeeds());
9094
}

0 commit comments

Comments
 (0)