Skip to content

Commit 6512bf0

Browse files
authored
[libc] Migrate dirent/fcntl/sched/signal tests to ErrnoCheckingTest. (#158700)
Remove direct libc_errno.h inclusion and manipulation of libc_errno in various unit tests, instead relying on ErrnoCheckingTest machinery. This is the final mechanical change of migrating libc unit tests to ErrnoCheckingTest - after it, all the unit tests relying on ASSERT_ERRNO_* macro will be using ErrnoCheckingTest.h
1 parent d9fa0de commit 6512bf0

18 files changed

+80
-81
lines changed

libc/test/src/dirent/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ add_libc_unittest(
1414
libc.src.dirent.opendir
1515
libc.src.dirent.readdir
1616
libc.src.errno.errno
17+
libc.test.UnitTest.ErrnoCheckingTest
1718
)
1819

libc/test/src/dirent/dirent_test.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/CPP/string_view.h"
10-
#include "src/__support/libc_errno.h"
1110
#include "src/dirent/closedir.h"
1211
#include "src/dirent/dirfd.h"
1312
#include "src/dirent/opendir.h"
1413
#include "src/dirent/readdir.h"
1514

15+
#include "test/UnitTest/ErrnoCheckingTest.h"
1616
#include "test/UnitTest/Test.h"
1717

1818
#include <dirent.h>
1919

20+
using LlvmLibcDirentTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
2021
using string_view = LIBC_NAMESPACE::cpp::string_view;
2122

22-
TEST(LlvmLibcDirentTest, SimpleOpenAndRead) {
23+
TEST_F(LlvmLibcDirentTest, SimpleOpenAndRead) {
2324
::DIR *dir = LIBC_NAMESPACE::opendir("testdata");
2425
ASSERT_TRUE(dir != nullptr);
2526
// The file descriptors 0, 1 and 2 are reserved for standard streams.
@@ -54,18 +55,14 @@ TEST(LlvmLibcDirentTest, SimpleOpenAndRead) {
5455
ASSERT_EQ(LIBC_NAMESPACE::closedir(dir), 0);
5556
}
5657

57-
TEST(LlvmLibcDirentTest, OpenNonExistentDir) {
58-
libc_errno = 0;
58+
TEST_F(LlvmLibcDirentTest, OpenNonExistentDir) {
5959
::DIR *dir = LIBC_NAMESPACE::opendir("___xyz123__.non_existent__");
6060
ASSERT_TRUE(dir == nullptr);
6161
ASSERT_ERRNO_EQ(ENOENT);
62-
libc_errno = 0;
6362
}
6463

65-
TEST(LlvmLibcDirentTest, OpenFile) {
66-
libc_errno = 0;
64+
TEST_F(LlvmLibcDirentTest, OpenFile) {
6765
::DIR *dir = LIBC_NAMESPACE::opendir("testdata/file1.txt");
6866
ASSERT_TRUE(dir == nullptr);
6967
ASSERT_ERRNO_EQ(ENOTDIR);
70-
libc_errno = 0;
7168
}

libc/test/src/fcntl/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_libc_unittest(
1414
libc.src.fcntl.creat
1515
libc.src.fcntl.open
1616
libc.src.unistd.close
17+
libc.test.UnitTest.ErrnoCheckingTest
1718
libc.test.UnitTest.ErrnoSetterMatcher
1819
)
1920

@@ -32,6 +33,7 @@ add_libc_unittest(
3233
libc.src.unistd.getpid
3334
libc.hdr.types.struct_flock
3435
libc.hdr.fcntl_macros
36+
libc.test.UnitTest.ErrnoCheckingTest
3537
libc.test.UnitTest.ErrnoSetterMatcher
3638
)
3739

@@ -48,5 +50,6 @@ add_libc_unittest(
4850
libc.src.fcntl.openat
4951
libc.src.unistd.close
5052
libc.src.unistd.read
53+
libc.test.UnitTest.ErrnoCheckingTest
5154
libc.test.UnitTest.ErrnoSetterMatcher
5255
)

libc/test/src/fcntl/creat_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/libc_errno.h"
109
#include "src/fcntl/creat.h"
1110
#include "src/fcntl/open.h"
1211
#include "src/unistd/close.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1313
#include "test/UnitTest/ErrnoSetterMatcher.h"
1414
#include "test/UnitTest/Test.h"
1515

1616
#include <sys/stat.h>
1717

18-
TEST(LlvmLibcCreatTest, CreatAndOpen) {
18+
using LlvmLibcCreatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
19+
20+
TEST_F(LlvmLibcCreatTest, CreatAndOpen) {
1921
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2022
constexpr const char *TEST_FILE = "testdata/creat.test";
2123
int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);

libc/test/src/fcntl/fcntl_test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
#include "hdr/fcntl_macros.h"
1010
#include "hdr/stdio_macros.h"
1111
#include "hdr/types/struct_flock.h"
12-
#include "src/__support/libc_errno.h"
1312
#include "src/fcntl/fcntl.h"
1413
#include "src/fcntl/open.h"
1514
#include "src/unistd/close.h"
1615
#include "src/unistd/getpid.h"
16+
#include "test/UnitTest/ErrnoCheckingTest.h"
1717
#include "test/UnitTest/ErrnoSetterMatcher.h"
1818
#include "test/UnitTest/Test.h"
1919

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

22-
TEST(LlvmLibcFcntlTest, FcntlDupfd) {
22+
using LlvmLibcFcntlTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
23+
24+
TEST_F(LlvmLibcFcntlTest, FcntlDupfd) {
2325
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2426
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_dup.test";
2527
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -41,7 +43,7 @@ TEST(LlvmLibcFcntlTest, FcntlDupfd) {
4143
ASSERT_THAT(LIBC_NAMESPACE::close(fd3), Succeeds(0));
4244
}
4345

44-
TEST(LlvmLibcFcntlTest, FcntlGetFl) {
46+
TEST_F(LlvmLibcFcntlTest, FcntlGetFl) {
4547
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
4648
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getfl.test";
4749
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -57,7 +59,7 @@ TEST(LlvmLibcFcntlTest, FcntlGetFl) {
5759
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
5860
}
5961

60-
TEST(LlvmLibcFcntlTest, FcntlSetFl) {
62+
TEST_F(LlvmLibcFcntlTest, FcntlSetFl) {
6163
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
6264
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_setfl.test";
6365
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -92,7 +94,7 @@ TEST(LlvmLibcFcntlTest, FcntlSetFl) {
9294
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
9395
}
9496

95-
TEST(LlvmLibcFcntlTest, FcntlGetLkRead) {
97+
TEST_F(LlvmLibcFcntlTest, FcntlGetLkRead) {
9698
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
9799
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getlkread.test";
98100
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -124,7 +126,7 @@ TEST(LlvmLibcFcntlTest, FcntlGetLkRead) {
124126
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
125127
}
126128

127-
TEST(LlvmLibcFcntlTest, FcntlGetLkWrite) {
129+
TEST_F(LlvmLibcFcntlTest, FcntlGetLkWrite) {
128130
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
129131
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_getlkwrite.test";
130132
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -155,7 +157,7 @@ TEST(LlvmLibcFcntlTest, FcntlGetLkWrite) {
155157
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
156158
}
157159

158-
TEST(LlvmLibcFcntlTest, UseAfterClose) {
160+
TEST_F(LlvmLibcFcntlTest, UseAfterClose) {
159161
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
160162
constexpr const char *TEST_FILE_NAME = "testdata/fcntl_use_after_close.test";
161163
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME);
@@ -165,8 +167,7 @@ TEST(LlvmLibcFcntlTest, UseAfterClose) {
165167
ASSERT_ERRNO_EQ(EBADF);
166168
}
167169

168-
TEST(LlvmLibcFcntlTest, SetGetOwnerTest) {
169-
libc_errno = 0;
170+
TEST_F(LlvmLibcFcntlTest, SetGetOwnerTest) {
170171
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
171172
pid_t pid = LIBC_NAMESPACE::getpid();
172173
ASSERT_GT(pid, -1);

libc/test/src/fcntl/openat_test.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/libc_errno.h"
109
#include "src/fcntl/open.h"
1110
#include "src/fcntl/openat.h"
1211
#include "src/unistd/close.h"
1312
#include "src/unistd/read.h"
13+
#include "test/UnitTest/ErrnoCheckingTest.h"
1414
#include "test/UnitTest/ErrnoSetterMatcher.h"
1515
#include "test/UnitTest/Test.h"
1616

1717
#include "hdr/fcntl_macros.h"
1818

19-
TEST(LlvmLibcUniStd, OpenAndReadTest) {
19+
using LlvmLibcOpenAtTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
20+
21+
TEST_F(LlvmLibcOpenAtTest, OpenAndReadTest) {
2022
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2123
constexpr const char *TEST_DIR = "testdata";
2224
constexpr const char *TEST_FILE = "openat.test";
@@ -36,7 +38,7 @@ TEST(LlvmLibcUniStd, OpenAndReadTest) {
3638
ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
3739
}
3840

39-
TEST(LlvmLibcUniStd, FailTest) {
41+
TEST_F(LlvmLibcOpenAtTest, FailTest) {
4042
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
4143
EXPECT_THAT(LIBC_NAMESPACE::openat(AT_FDCWD, "openat.test", O_RDONLY),
4244
Fails(ENOENT));

libc/test/src/sched/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_libc_unittest(
1414
libc.src.errno.errno
1515
libc.src.sched.sched_getaffinity
1616
libc.src.sched.sched_setaffinity
17+
libc.test.UnitTest.ErrnoCheckingTest
1718
libc.test.UnitTest.ErrnoSetterMatcher
1819
)
1920

@@ -26,6 +27,7 @@ add_libc_unittest(
2627
DEPENDS
2728
libc.src.errno.errno
2829
libc.src.sched.sched_yield
30+
libc.test.UnitTest.ErrnoCheckingTest
2931
)
3032

3133
add_libc_unittest(
@@ -39,6 +41,7 @@ add_libc_unittest(
3941
libc.src.errno.errno
4042
libc.src.sched.sched_get_priority_min
4143
libc.src.sched.sched_get_priority_max
44+
libc.test.UnitTest.ErrnoCheckingTest
4245
)
4346

4447
add_libc_unittest(
@@ -70,6 +73,7 @@ add_libc_unittest(
7073
libc.src.sched.sched_get_priority_min
7174
libc.src.sched.sched_get_priority_max
7275
libc.src.unistd.getuid
76+
libc.test.UnitTest.ErrnoCheckingTest
7377
)
7478

7579
add_libc_unittest(
@@ -87,6 +91,7 @@ add_libc_unittest(
8791
libc.src.sched.sched_get_priority_min
8892
libc.src.sched.sched_rr_get_interval
8993
libc.src.unistd.getuid
94+
libc.test.UnitTest.ErrnoCheckingTest
9095
)
9196

9297
add_libc_unittest(
@@ -104,5 +109,6 @@ add_libc_unittest(
104109
libc.src.errno.errno
105110
libc.src.sched.sched_getaffinity
106111
libc.src.sched.__sched_getcpucount
112+
libc.test.UnitTest.ErrnoCheckingTest
107113
libc.test.UnitTest.ErrnoSetterMatcher
108114
)

libc/test/src/sched/affinity_test.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/OSUtil/syscall.h"
10-
#include "src/__support/libc_errno.h"
1110
#include "src/sched/sched_getaffinity.h"
1211
#include "src/sched/sched_setaffinity.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1313
#include "test/UnitTest/ErrnoSetterMatcher.h"
1414

1515
#include "hdr/types/cpu_set_t.h"
1616
#include "hdr/types/pid_t.h"
1717
#include <sys/syscall.h>
1818

19-
TEST(LlvmLibcSchedAffinityTest, SmokeTest) {
19+
using LlvmLibcSchedAffinityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
20+
21+
TEST_F(LlvmLibcSchedAffinityTest, SmokeTest) {
2022
cpu_set_t mask;
21-
libc_errno = 0;
2223
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2324
pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
2425
ASSERT_GT(tid, pid_t(0));
@@ -29,19 +30,15 @@ TEST(LlvmLibcSchedAffinityTest, SmokeTest) {
2930
Succeeds(0));
3031
}
3132

32-
TEST(LlvmLibcSchedAffinityTest, BadMask) {
33+
TEST_F(LlvmLibcSchedAffinityTest, BadMask) {
3334
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
3435
pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
3536

36-
libc_errno = 0;
3737
ASSERT_THAT(
3838
LIBC_NAMESPACE::sched_getaffinity(tid, sizeof(cpu_set_t), nullptr),
3939
Fails(EFAULT));
4040

41-
libc_errno = 0;
4241
ASSERT_THAT(
4342
LIBC_NAMESPACE::sched_setaffinity(tid, sizeof(cpu_set_t), nullptr),
4443
Fails(EFAULT));
45-
46-
libc_errno = 0;
4744
}

libc/test/src/sched/cpu_count_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/OSUtil/syscall.h"
10-
#include "src/__support/libc_errno.h"
1110
#include "src/sched/sched_getaffinity.h"
1211
#include "src/sched/sched_getcpucount.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1313
#include "test/UnitTest/ErrnoSetterMatcher.h"
1414

1515
#include "hdr/sched_macros.h"
1616
#include "hdr/types/cpu_set_t.h"
1717
#include "hdr/types/pid_t.h"
1818

19-
TEST(LlvmLibcSchedCpuCountTest, SmokeTest) {
19+
using LlvmLibcSchedCpuCountTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
20+
21+
TEST_F(LlvmLibcSchedCpuCountTest, SmokeTest) {
2022
cpu_set_t mask;
21-
libc_errno = 0;
2223
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2324
pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
2425
ASSERT_GT(tid, pid_t(0));

libc/test/src/sched/get_priority_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/libc_errno.h"
109
#include "src/sched/sched_get_priority_max.h"
1110
#include "src/sched/sched_get_priority_min.h"
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
1212
#include "test/UnitTest/Test.h"
1313

1414
#include "hdr/sched_macros.h"
1515

16-
TEST(LlvmLibcSchedGetPriorityTest, HandleBadPolicyTest) {
16+
using LlvmLibcSchedGetPriorityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
17+
18+
TEST_F(LlvmLibcSchedGetPriorityTest, HandleBadPolicyTest) {
1719

1820
// Test arbitrary values for which there is no policy.
1921
{
@@ -57,9 +59,7 @@ TEST(LlvmLibcSchedGetPriorityTest, HandleBadPolicyTest) {
5759
}
5860
}
5961

60-
TEST(LlvmLibcSchedGetPriorityTest, SmokeTest) {
61-
libc_errno = 0;
62-
62+
TEST_F(LlvmLibcSchedGetPriorityTest, SmokeTest) {
6363
// We Test:
6464
// SCHED_OTHER, SCHED_FIFO, SCHED_RR
6565
// Linux specific test could also include:

0 commit comments

Comments
 (0)