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/time/asctime.h"
10 #include "test/UnitTest/Test.h"
11 #include "test/src/time/TmHelper.h"
13 static inline char *call_asctime(struct tm
*tm_data
, int year
, int month
,
14 int mday
, int hour
, int min
, int sec
, int wday
,
16 __llvm_libc::tmhelper::testing::initialize_tm_data(
17 tm_data
, year
, month
, mday
, hour
, min
, sec
, wday
, yday
);
18 return __llvm_libc::asctime(tm_data
);
21 TEST(LlvmLibcAsctime
, Nullptr
) {
23 result
= __llvm_libc::asctime(nullptr);
24 ASSERT_EQ(EINVAL
, llvmlibc_errno
);
25 ASSERT_STREQ(nullptr, result
);
28 // Weekdays are in the range 0 to 6. Test passing invalid value in wday.
29 TEST(LlvmLibcAsctime
, InvalidWday
) {
32 // Test with wday = -1.
33 call_asctime(&tm_data
,
42 ASSERT_EQ(EINVAL
, llvmlibc_errno
);
44 // Test with wday = 7.
45 call_asctime(&tm_data
,
54 ASSERT_EQ(EINVAL
, llvmlibc_errno
);
57 // Months are from January to December. Test passing invalid value in month.
58 TEST(LlvmLibcAsctime
, InvalidMonth
) {
61 // Test with month = 0.
62 call_asctime(&tm_data
,
71 ASSERT_EQ(EINVAL
, llvmlibc_errno
);
73 // Test with month = 13.
74 call_asctime(&tm_data
,
83 ASSERT_EQ(EINVAL
, llvmlibc_errno
);
86 TEST(LlvmLibcAsctime
, ValidWeekdays
) {
89 // 1970-01-01 00:00:00.
90 result
= call_asctime(&tm_data
,
99 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result
);
101 // 1970-01-03 00:00:00.
102 result
= call_asctime(&tm_data
,
111 ASSERT_STREQ("Sat Jan 3 00:00:00 1970\n", result
);
113 // 1970-01-04 00:00:00.
114 result
= call_asctime(&tm_data
,
123 ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n", result
);
126 TEST(LlvmLibcAsctime
, ValidMonths
) {
129 // 1970-01-01 00:00:00.
130 result
= call_asctime(&tm_data
,
139 ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result
);
141 // 1970-02-01 00:00:00.
142 result
= call_asctime(&tm_data
,
151 ASSERT_STREQ("Sun Feb 1 00:00:00 1970\n", result
);
153 // 1970-12-31 23:59:59.
154 result
= call_asctime(&tm_data
,
163 ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result
);
166 TEST(LlvmLibcAsctime
, EndOf32BitEpochYear
) {
169 // Test for maximum value of a signed 32-bit integer.
170 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
171 result
= call_asctime(&tm_data
,
180 ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result
);
183 TEST(LlvmLibcAsctime
, Max64BitYear
) {
184 if (sizeof(time_t) == 4)
186 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
189 result
= call_asctime(&tm_data
,
198 ASSERT_STREQ("Mon Jan 1 12:50:50 2170\n", result
);
200 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
201 // This test would cause buffer overflow and thus asctime returns nullptr.
202 result
= call_asctime(&tm_data
,
211 ASSERT_EQ(EOVERFLOW
, llvmlibc_errno
);
212 ASSERT_STREQ(nullptr, result
);