1 //===-- Unittests for asctime ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/errno/libc_errno.h"
10 #include "src/time/asctime.h"
11 #include "test/UnitTest/Test.h"
12 #include "test/src/time/TmHelper.h"
14 static inline char *call_asctime(struct tm
*tm_data
, int year
, int month
,
15 int mday
, int hour
, int min
, int sec
, int wday
,
17 LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data(
18 tm_data
, year
, month
, mday
, hour
, min
, sec
, wday
, yday
);
19 return LIBC_NAMESPACE::asctime(tm_data
);
22 TEST(LlvmLibcAsctime
, Nullptr
) {
24 result
= LIBC_NAMESPACE::asctime(nullptr);
25 ASSERT_EQ(EINVAL
, libc_errno
);
26 ASSERT_STREQ(nullptr, result
);
29 // Weekdays are in the range 0 to 6. Test passing invalid value in wday.
30 TEST(LlvmLibcAsctime
, InvalidWday
) {
33 // Test with wday = -1.
34 call_asctime(&tm_data
,
43 ASSERT_EQ(EINVAL
, libc_errno
);
45 // Test with wday = 7.
46 call_asctime(&tm_data
,
55 ASSERT_EQ(EINVAL
, libc_errno
);
58 // Months are from January to December. Test passing invalid value in month.
59 TEST(LlvmLibcAsctime
, InvalidMonth
) {
62 // Test with month = 0.
63 call_asctime(&tm_data
,
72 ASSERT_EQ(EINVAL
, libc_errno
);
74 // Test with month = 13.
75 call_asctime(&tm_data
,
84 ASSERT_EQ(EINVAL
, libc_errno
);
87 TEST(LlvmLibcAsctime
, ValidWeekdays
) {
90 // 1970-01-01 00:00:00.
91 result
= call_asctime(&tm_data
,
100 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result
);
102 // 1970-01-03 00:00:00.
103 result
= call_asctime(&tm_data
,
112 ASSERT_STREQ("Sat Jan 3 00:00:00 1970\n", result
);
114 // 1970-01-04 00:00:00.
115 result
= call_asctime(&tm_data
,
124 ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n", result
);
127 TEST(LlvmLibcAsctime
, ValidMonths
) {
130 // 1970-01-01 00:00:00.
131 result
= call_asctime(&tm_data
,
140 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result
);
142 // 1970-02-01 00:00:00.
143 result
= call_asctime(&tm_data
,
152 ASSERT_STREQ("Sun Feb 1 00:00:00 1970\n", result
);
154 // 1970-12-31 23:59:59.
155 result
= call_asctime(&tm_data
,
164 ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result
);
167 TEST(LlvmLibcAsctime
, EndOf32BitEpochYear
) {
170 // Test for maximum value of a signed 32-bit integer.
171 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
172 result
= call_asctime(&tm_data
,
181 ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result
);
184 TEST(LlvmLibcAsctime
, Max64BitYear
) {
185 if (sizeof(time_t) == 4)
187 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
190 result
= call_asctime(&tm_data
,
199 ASSERT_STREQ("Mon Jan 1 12:50:50 2170\n", result
);
201 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
202 // This test would cause buffer overflow and thus asctime returns nullptr.
203 result
= call_asctime(&tm_data
,
212 ASSERT_EQ(EOVERFLOW
, libc_errno
);
213 ASSERT_STREQ(nullptr, result
);