Skip to content

Commit b8e1cc5

Browse files
authored
[libc] Migrate some test/src/time tests to ErrnoCheckingTest. (#157960)
Use ErrnoCheckingTest harness to clear out / validate errno value before/after the test respectively. Clean up explicit libc_errno.h inclusions which is unnecessary, since no test modifies errno directly.
1 parent 0efa75d commit b8e1cc5

File tree

8 files changed

+64
-48
lines changed

8 files changed

+64
-48
lines changed

libc/test/src/time/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ add_libc_unittest(
1212
CXX_STANDARD
1313
20
1414
DEPENDS
15+
libc.hdr.errno_macros
1516
libc.src.time.asctime
1617
libc.hdr.types.struct_tm
1718
libc.src.time.time_constants
19+
libc.test.UnitTest.ErrnoCheckingTest
1820
)
1921

2022
add_libc_unittest(
@@ -29,9 +31,11 @@ add_libc_unittest(
2931
CXX_STANDARD
3032
20
3133
DEPENDS
34+
libc.hdr.errno_macros
3235
libc.src.time.asctime_r
3336
libc.hdr.types.struct_tm
3437
libc.src.time.time_constants
38+
libc.test.UnitTest.ErrnoCheckingTest
3539
)
3640

3741
add_libc_unittest(
@@ -51,6 +55,7 @@ add_libc_unittest(
5155
libc.src.time.ctime
5256
libc.src.time.time_constants
5357
libc.hdr.types.struct_tm
58+
libc.test.UnitTest.ErrnoCheckingTest
5459
)
5560

5661
add_libc_unittest(
@@ -70,6 +75,7 @@ add_libc_unittest(
7075
libc.src.time.ctime_r
7176
libc.src.time.time_constants
7277
libc.hdr.types.struct_tm
78+
libc.test.UnitTest.ErrnoCheckingTest
7379
)
7480

7581
add_libc_unittest(
@@ -151,10 +157,12 @@ add_libc_unittest(
151157
HDRS
152158
TmMatcher.h
153159
DEPENDS
160+
libc.hdr.errno_macros
154161
libc.src.time.gmtime
155162
libc.src.__support.CPP.limits
156163
libc.hdr.types.struct_tm
157164
libc.src.time.time_constants
165+
libc.test.UnitTest.ErrnoCheckingTest
158166
)
159167

160168
add_libc_unittest(
@@ -169,6 +177,7 @@ add_libc_unittest(
169177
libc.src.time.gmtime_r
170178
libc.hdr.types.struct_tm
171179
libc.src.time.time_constants
180+
libc.test.UnitTest.ErrnoCheckingTest
172181
)
173182

174183
add_libc_test(
@@ -197,9 +206,9 @@ add_libc_test(
197206
nanosleep_test.cpp
198207
DEPENDS
199208
libc.include.time
200-
libc.src.time.nanosleep
201-
libc.src.errno.errno
202209
libc.hdr.types.struct_timespec
210+
libc.src.time.nanosleep
211+
libc.test.UnitTest.ErrnoCheckingTest
203212
)
204213

205214
add_libc_test(

libc/test/src/time/asctime_r_test.cpp

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

9-
#include "src/__support/libc_errno.h"
9+
#include "hdr/errno_macros.h"
1010
#include "src/time/asctime_r.h"
1111
#include "src/time/time_constants.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1213
#include "test/UnitTest/Test.h"
1314
#include "test/src/time/TmHelper.h"
1415

16+
using LlvmLibcAsctimeR = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
17+
1518
static inline char *call_asctime_r(struct tm *tm_data, int year, int month,
1619
int mday, int hour, int min, int sec,
1720
int wday, int yday, char *buffer) {
@@ -22,7 +25,7 @@ static inline char *call_asctime_r(struct tm *tm_data, int year, int month,
2225

2326
// asctime and asctime_r share the same code and thus didn't repeat all the
2427
// tests from asctime. Added couple of validation tests.
25-
TEST(LlvmLibcAsctimeR, Nullptr) {
28+
TEST_F(LlvmLibcAsctimeR, Nullptr) {
2629
char *result;
2730
result = LIBC_NAMESPACE::asctime_r(nullptr, nullptr);
2831
ASSERT_ERRNO_EQ(EINVAL);
@@ -39,7 +42,7 @@ TEST(LlvmLibcAsctimeR, Nullptr) {
3942
ASSERT_STREQ(nullptr, result);
4043
}
4144

42-
TEST(LlvmLibcAsctimeR, ValidDate) {
45+
TEST_F(LlvmLibcAsctimeR, ValidDate) {
4346
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
4447
struct tm tm_data;
4548
char *result;

libc/test/src/time/asctime_test.cpp

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

9-
#include "src/__support/libc_errno.h"
9+
#include "hdr/errno_macros.h"
1010
#include "src/time/asctime.h"
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "test/src/time/TmHelper.h"
1314

15+
using LlvmLibcAsctime = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
16+
1417
static inline char *call_asctime(struct tm *tm_data, int year, int month,
1518
int mday, int hour, int min, int sec, int wday,
1619
int yday) {
@@ -19,15 +22,15 @@ static inline char *call_asctime(struct tm *tm_data, int year, int month,
1922
return LIBC_NAMESPACE::asctime(tm_data);
2023
}
2124

22-
TEST(LlvmLibcAsctime, Nullptr) {
25+
TEST_F(LlvmLibcAsctime, Nullptr) {
2326
char *result;
2427
result = LIBC_NAMESPACE::asctime(nullptr);
2528
ASSERT_ERRNO_EQ(EINVAL);
2629
ASSERT_STREQ(nullptr, result);
2730
}
2831

2932
// Weekdays are in the range 0 to 6. Test passing invalid value in wday.
30-
TEST(LlvmLibcAsctime, InvalidWday) {
33+
TEST_F(LlvmLibcAsctime, InvalidWday) {
3134
struct tm tm_data;
3235

3336
// Test with wday = -1.
@@ -56,7 +59,7 @@ TEST(LlvmLibcAsctime, InvalidWday) {
5659
}
5760

5861
// Months are from January to December. Test passing invalid value in month.
59-
TEST(LlvmLibcAsctime, InvalidMonth) {
62+
TEST_F(LlvmLibcAsctime, InvalidMonth) {
6063
struct tm tm_data;
6164

6265
// Test with month = 0.
@@ -84,7 +87,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) {
8487
ASSERT_ERRNO_EQ(EINVAL);
8588
}
8689

87-
TEST(LlvmLibcAsctime, ValidWeekdays) {
90+
TEST_F(LlvmLibcAsctime, ValidWeekdays) {
8891
struct tm tm_data;
8992
char *result;
9093
// 1970-01-01 00:00:00.
@@ -124,7 +127,7 @@ TEST(LlvmLibcAsctime, ValidWeekdays) {
124127
ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n", result);
125128
}
126129

127-
TEST(LlvmLibcAsctime, ValidMonths) {
130+
TEST_F(LlvmLibcAsctime, ValidMonths) {
128131
struct tm tm_data;
129132
char *result;
130133
// 1970-01-01 00:00:00.
@@ -164,7 +167,7 @@ TEST(LlvmLibcAsctime, ValidMonths) {
164167
ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result);
165168
}
166169

167-
TEST(LlvmLibcAsctime, EndOf32BitEpochYear) {
170+
TEST_F(LlvmLibcAsctime, EndOf32BitEpochYear) {
168171
struct tm tm_data;
169172
char *result;
170173
// Test for maximum value of a signed 32-bit integer.
@@ -181,7 +184,7 @@ TEST(LlvmLibcAsctime, EndOf32BitEpochYear) {
181184
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
182185
}
183186

184-
TEST(LlvmLibcAsctime, Max64BitYear) {
187+
TEST_F(LlvmLibcAsctime, Max64BitYear) {
185188
if (sizeof(time_t) == 4)
186189
return;
187190
// Mon Jan 1 12:50:50 2170 (200 years from 1970),

libc/test/src/time/ctime_r_test.cpp

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

9-
#include "src/__support/libc_errno.h"
109
#include "src/time/ctime_r.h"
1110
#include "src/time/time_constants.h"
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
1212
#include "test/UnitTest/Test.h"
1313
#include "test/src/time/TmHelper.h"
1414

15-
TEST(LlvmLibcCtimeR, Nullptr) {
15+
using LlvmLibcCtimeR = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
16+
17+
TEST_F(LlvmLibcCtimeR, Nullptr) {
1618
char *result;
1719
result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr);
1820
ASSERT_STREQ(nullptr, result);
@@ -26,7 +28,7 @@ TEST(LlvmLibcCtimeR, Nullptr) {
2628
ASSERT_STREQ(nullptr, result);
2729
}
2830

29-
TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) {
31+
TEST_F(LlvmLibcCtimeR, ValidUnixTimestamp0) {
3032
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
3133
time_t t;
3234
char *result;
@@ -36,7 +38,7 @@ TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) {
3638
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
3739
}
3840

39-
TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
41+
TEST_F(LlvmLibcCtimeR, ValidUnixTimestamp32Int) {
4042
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
4143
time_t t;
4244
char *result;
@@ -46,7 +48,7 @@ TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
4648
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
4749
}
4850

49-
TEST(LlvmLibcCtimeR, InvalidArgument) {
51+
TEST_F(LlvmLibcCtimeR, InvalidArgument) {
5052
char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE];
5153
time_t t;
5254
char *result;

libc/test/src/time/ctime_test.cpp

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

9-
#include "src/__support/libc_errno.h"
109
#include "src/time/ctime.h"
10+
#include "test/UnitTest/ErrnoCheckingTest.h"
1111
#include "test/UnitTest/Test.h"
1212
#include "test/src/time/TmHelper.h"
1313

14-
TEST(LlvmLibcCtime, nullptr) {
14+
using LlvmLibcCtime = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
15+
16+
TEST_F(LlvmLibcCtime, nullptr) {
1517
char *result;
1618
result = LIBC_NAMESPACE::ctime(nullptr);
1719
ASSERT_STREQ(nullptr, result);
1820
}
1921

20-
TEST(LlvmLibcCtime, ValidUnixTimestamp0) {
22+
TEST_F(LlvmLibcCtime, ValidUnixTimestamp0) {
2123
time_t t;
2224
char *result;
2325
t = 0;
2426
result = LIBC_NAMESPACE::ctime(&t);
2527
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
2628
}
2729

28-
TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
30+
TEST_F(LlvmLibcCtime, ValidUnixTimestamp32Int) {
2931
time_t t;
3032
char *result;
3133
t = 2147483647;
3234
result = LIBC_NAMESPACE::ctime(&t);
3335
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
3436
}
3537

36-
TEST(LlvmLibcCtime, InvalidArgument) {
38+
TEST_F(LlvmLibcCtime, InvalidArgument) {
3739
time_t t;
3840
char *result;
3941
t = 2147483648;

libc/test/src/time/gmtime_r_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
#include "src/time/gmtime_r.h"
1010
#include "src/time/time_constants.h"
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "test/src/time/TmMatcher.h"
1314

15+
using LlvmLibcGmTimeR = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
16+
1417
// gmtime and gmtime_r share the same code and thus didn't repeat all the tests
1518
// from gmtime. Added couple of validation tests.
16-
TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
19+
TEST_F(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
1720
// Test for maximum value of a signed 32-bit integer.
1821
// Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
1922
time_t seconds = 0x7FFFFFFF;
@@ -34,7 +37,7 @@ TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
3437
EXPECT_TM_EQ(*tm_data_ptr, tm_data);
3538
}
3639

37-
TEST(LlvmLibcGmTimeR, Max64BitYear) {
40+
TEST_F(LlvmLibcGmTimeR, Max64BitYear) {
3841
if (sizeof(time_t) == 4)
3942
return;
4043
// Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.

0 commit comments

Comments
 (0)