Skip to content

Commit 1ecd108

Browse files
authored
[libc] Migrate stdio tests to ErrnoCheckingTest. (#143802)
Reduce the direct use of libc_errno in stdio unit tests by adopting ErrnoCheckingTest where appropriate. Also removes the libc_errno.h inclusions from stdlib.h tests that were accidentally added in d87eea3
1 parent dc4335a commit 1ecd108

13 files changed

+52
-59
lines changed

libc/test/src/stdio/CMakeLists.txt

Lines changed: 10 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
)
@@ -426,6 +430,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
426430
libc.src.sys.stat.mkdirat
427431
libc.src.unistd.access
428432
libc.src.unistd.close
433+
libc.test.UnitTest.ErrnoCheckingTest
429434
)
430435

431436
add_libc_test(
@@ -440,6 +445,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
440445
libc.src.stdio.rename
441446
libc.src.unistd.access
442447
libc.src.unistd.close
448+
libc.test.UnitTest.ErrnoCheckingTest
443449
libc.test.UnitTest.ErrnoSetterMatcher
444450
)
445451

@@ -456,6 +462,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
456462
libc.src.stdio.fgets
457463
libc.src.stdio.fputs
458464
libc.src.unistd.close
465+
libc.test.UnitTest.ErrnoCheckingTest
459466
libc.test.UnitTest.ErrnoSetterMatcher
460467
)
461468
endif()
@@ -476,6 +483,7 @@ add_libc_test(
476483
libc.src.stdio.fopen
477484
libc.src.stdio.fwrite
478485
libc.src.stdio.getc
486+
libc.test.UnitTest.ErrnoCheckingTest
479487
)
480488

481489
add_libc_test(
@@ -498,6 +506,7 @@ add_libc_test(
498506
libc.src.stdio.funlockfile
499507
libc.src.stdio.fwrite
500508
libc.src.stdio.getc_unlocked
509+
libc.test.UnitTest.ErrnoCheckingTest
501510
)
502511

503512
add_libc_test(
@@ -515,6 +524,7 @@ add_libc_test(
515524
libc.src.stdio.fgets
516525
libc.src.stdio.fopen
517526
libc.src.stdio.fwrite
527+
libc.test.UnitTest.ErrnoCheckingTest
518528
)
519529

520530
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include "src/stdio/fopen.h"
1515
#include "src/stdio/fwrite.h"
1616
#include "src/stdio/getc.h"
17+
#include "test/UnitTest/ErrnoCheckingTest.h"
1718
#include "test/UnitTest/Test.h"
1819

1920
#include "hdr/stdio_macros.h"
20-
#include "src/__support/libc_errno.h"
2121

22-
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
22+
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
2323
public:
2424
using GetcFunc = int(FILE *);
2525
void test_with_func(GetcFunc *func, const char *filename) {
@@ -33,7 +33,6 @@ class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
3333
// This is an error and not a real EOF.
3434
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3535
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
36-
libc_errno = 0;
3736

3837
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
3938

libc/test/src/stdio/fgetc_unlocked_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
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"
2021
#include "test/UnitTest/Test.h"
2122

2223
#include "hdr/stdio_macros.h"
23-
#include "src/__support/libc_errno.h"
2424

25-
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
25+
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
2626
public:
2727
using GetcFunc = int(FILE *);
2828
void test_with_func(GetcFunc *func, const char *filename) {
@@ -36,7 +36,6 @@ class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
3636
// This is an error and not a real EOF.
3737
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3838
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
39-
libc_errno = 0;
4039

4140
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
4241

libc/test/src/stdio/fgets_test.cpp

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

17-
#include "src/__support/libc_errno.h"
18+
using LlvmLibcFgetsTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
1819

19-
TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
20+
TEST_F(LlvmLibcFgetsTest, WriteAndReadCharacters) {
2021
constexpr char FILENAME[] = "testdata/fgets.test";
2122
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
2223
ASSERT_FALSE(file == nullptr);
@@ -35,7 +36,6 @@ TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
3536
// This is an error and not a real EOF.
3637
ASSERT_EQ(LIBC_NAMESPACE::feof(file), 0);
3738
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
38-
libc_errno = 0;
3939

4040
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
4141

libc/test/src/stdio/fileop_test.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
#include "src/stdio/fread.h"
1818
#include "src/stdio/fseek.h"
1919
#include "src/stdio/fwrite.h"
20+
#include "test/UnitTest/ErrnoCheckingTest.h"
2021
#include "test/UnitTest/ErrnoSetterMatcher.h"
2122
#include "test/UnitTest/Test.h"
2223

2324
#include "hdr/stdio_macros.h"
24-
#include "src/__support/libc_errno.h"
2525

26+
using LlvmLibcFILETest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
2627
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
2728
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
2829
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
2930

30-
TEST(LlvmLibcFILETest, SimpleFileOperations) {
31+
TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
3132
constexpr char FILENAME[] = "testdata/simple_operations.test";
3233
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
3334
ASSERT_FALSE(file == nullptr);
@@ -41,7 +42,6 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
4142
ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(CONTENT), file),
4243
returns(EQ(size_t(0))).with_errno(NE(0)));
4344
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
44-
libc_errno = 0;
4545

4646
LIBC_NAMESPACE::clearerr(file);
4747
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
@@ -72,23 +72,19 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
7272
ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT), file),
7373
returns(EQ(size_t(0))).with_errno(NE(0)));
7474
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
75-
libc_errno = 0;
7675

7776
LIBC_NAMESPACE::clearerr(file);
7877

7978
// Should be an error to puts.
8079
ASSERT_THAT(LIBC_NAMESPACE::fputs(CONTENT, file),
8180
returns(EQ(EOF)).with_errno(NE(0)));
8281
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
83-
libc_errno = 0;
8482

8583
LIBC_NAMESPACE::clearerr(file);
8684
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
8785

88-
libc_errno = 0;
8986
ASSERT_THAT(LIBC_NAMESPACE::fwrite("nothing", 1, 1, file),
9087
returns(EQ(size_t(0))).with_errno(NE(0)));
91-
libc_errno = 0;
9288

9389
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
9490

@@ -103,10 +99,8 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
10399
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
104100

105101
// This is not a readable file.
106-
libc_errno = 0;
107102
ASSERT_THAT(LIBC_NAMESPACE::fread(data, 1, 1, file),
108103
returns(EQ(0)).with_errno(NE(0)));
109-
libc_errno = 0;
110104

111105
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
112106

@@ -121,21 +115,18 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
121115

122116
// Check that the other functions correctly set libc_errno.
123117

124-
// libc_errno = 0;
125118
// ASSERT_NE(LIBC_NAMESPACE::fseek(file, 0, SEEK_SET), 0);
126119
// ASSERT_ERRNO_FAILURE();
127120

128-
// libc_errno = 0;
129121
// ASSERT_NE(LIBC_NAMESPACE::fclose(file), 0);
130122
// ASSERT_ERRNO_FAILURE();
131123

132-
// libc_errno = 0;
133124
// ASSERT_EQ(LIBC_NAMESPACE::fopen("INVALID FILE NAME", "r"),
134125
// static_cast<FILE *>(nullptr));
135126
// ASSERT_ERRNO_FAILURE();
136127
}
137128

138-
TEST(LlvmLibcFILETest, FFlush) {
129+
TEST_F(LlvmLibcFILETest, FFlush) {
139130
constexpr char FILENAME[] = "testdata/fflush.test";
140131
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w+");
141132
ASSERT_FALSE(file == nullptr);
@@ -156,7 +147,7 @@ TEST(LlvmLibcFILETest, FFlush) {
156147
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
157148
}
158149

159-
TEST(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
150+
TEST_F(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
160151
using MyStruct = struct {
161152
char c;
162153
unsigned long long i;
@@ -165,7 +156,6 @@ TEST(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
165156
constexpr size_t WRITE_NMEMB = sizeof(WRITE_DATA) / sizeof(MyStruct);
166157
constexpr char FILENAME[] = "testdata/fread_fwrite.test";
167158

168-
libc_errno = 0;
169159
FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
170160
ASSERT_FALSE(file == nullptr);
171161
ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fwrite(WRITE_DATA, 0, 1, file));

libc/test/src/stdio/fopencookie_test.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
#include "src/stdio/fread.h"
1616
#include "src/stdio/fseek.h"
1717
#include "src/stdio/fwrite.h"
18+
#include "test/UnitTest/ErrnoCheckingTest.h"
1819
#include "test/UnitTest/MemoryMatcher.h"
1920
#include "test/UnitTest/Test.h"
2021

2122
#include "hdr/stdio_macros.h"
2223
#include "hdr/types/size_t.h"
2324
#include "src/__support/libc_errno.h"
2425

26+
using LlvmLibcFOpenCookieTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
2527
using MemoryView = LIBC_NAMESPACE::testing::MemoryView;
2628

2729
struct StringStream {
@@ -88,7 +90,7 @@ int close_ss(void *cookie) {
8890
constexpr cookie_io_functions_t STRING_STREAM_FUNCS = {&read_ss, &write_ss,
8991
&seek_ss, &close_ss};
9092

91-
TEST(LlvmLibcFOpenCookie, ReadOnlyCookieTest) {
93+
TEST_F(LlvmLibcFOpenCookieTest, ReadOnlyCookieTest) {
9294
constexpr char CONTENT[] = "Hello,readonly!";
9395
auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
9496
ss->buf = reinterpret_cast<char *>(malloc(sizeof(CONTENT)));
@@ -115,7 +117,6 @@ TEST(LlvmLibcFOpenCookie, ReadOnlyCookieTest) {
115117
ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT), f));
116118
ASSERT_NE(LIBC_NAMESPACE::ferror(f), 0);
117119
ASSERT_ERRNO_FAILURE();
118-
libc_errno = 0;
119120

120121
LIBC_NAMESPACE::clearerr(f);
121122
ASSERT_EQ(LIBC_NAMESPACE::ferror(f), 0);
@@ -124,7 +125,7 @@ TEST(LlvmLibcFOpenCookie, ReadOnlyCookieTest) {
124125
free(ss);
125126
}
126127

127-
TEST(LlvmLibcFOpenCookie, WriteOnlyCookieTest) {
128+
TEST_F(LlvmLibcFOpenCookieTest, WriteOnlyCookieTest) {
128129
size_t INIT_BUFSIZE = 32;
129130
auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
130131
ss->buf = reinterpret_cast<char *>(malloc(INIT_BUFSIZE));
@@ -149,7 +150,6 @@ TEST(LlvmLibcFOpenCookie, WriteOnlyCookieTest) {
149150
LIBC_NAMESPACE::fread(read_data, 1, sizeof(WRITE_DATA), f));
150151
ASSERT_NE(LIBC_NAMESPACE::ferror(f), 0);
151152
ASSERT_ERRNO_EQ(EBADF);
152-
libc_errno = 0;
153153

154154
LIBC_NAMESPACE::clearerr(f);
155155
ASSERT_EQ(LIBC_NAMESPACE::ferror(f), 0);
@@ -158,7 +158,7 @@ TEST(LlvmLibcFOpenCookie, WriteOnlyCookieTest) {
158158
free(ss);
159159
}
160160

161-
TEST(LlvmLibcFOpenCookie, AppendOnlyCookieTest) {
161+
TEST_F(LlvmLibcFOpenCookieTest, AppendOnlyCookieTest) {
162162
constexpr char INITIAL_CONTENT[] = "1234567890987654321";
163163
constexpr char WRITE_DATA[] = "append";
164164
auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
@@ -178,7 +178,6 @@ TEST(LlvmLibcFOpenCookie, AppendOnlyCookieTest) {
178178
ASSERT_EQ(LIBC_NAMESPACE::fread(read_data, 1, READ_SIZE, f), size_t(0));
179179
ASSERT_NE(LIBC_NAMESPACE::ferror(f), 0);
180180
ASSERT_ERRNO_FAILURE();
181-
libc_errno = 0;
182181

183182
LIBC_NAMESPACE::clearerr(f);
184183
ASSERT_EQ(LIBC_NAMESPACE::ferror(f), 0);
@@ -192,7 +191,7 @@ TEST(LlvmLibcFOpenCookie, AppendOnlyCookieTest) {
192191
free(ss);
193192
}
194193

195-
TEST(LlvmLibcFOpenCookie, ReadUpdateCookieTest) {
194+
TEST_F(LlvmLibcFOpenCookieTest, ReadUpdateCookieTest) {
196195
const char INITIAL_CONTENT[] = "1234567890987654321";
197196
auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
198197
ss->buf = reinterpret_cast<char *>(malloc(sizeof(INITIAL_CONTENT)));
@@ -223,7 +222,7 @@ TEST(LlvmLibcFOpenCookie, ReadUpdateCookieTest) {
223222
free(ss);
224223
}
225224

226-
TEST(LlvmLibcFOpenCookie, WriteUpdateCookieTest) {
225+
TEST_F(LlvmLibcFOpenCookieTest, WriteUpdateCookieTest) {
227226
constexpr char WRITE_DATA[] = "hello, file";
228227
auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream)));
229228
ss->buf = reinterpret_cast<char *>(malloc(sizeof(WRITE_DATA)));

0 commit comments

Comments
 (0)