xref: /aosp_15_r20/external/llvm-libc/test/src/time/asctime_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for asctime ---------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
13 
call_asctime(struct tm * tm_data,int year,int month,int mday,int hour,int min,int sec,int wday,int yday)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,
16                                  int yday) {
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);
20 }
21 
TEST(LlvmLibcAsctime,Nullptr)22 TEST(LlvmLibcAsctime, Nullptr) {
23   char *result;
24   result = LIBC_NAMESPACE::asctime(nullptr);
25   ASSERT_ERRNO_EQ(EINVAL);
26   ASSERT_STREQ(nullptr, result);
27 }
28 
29 // Weekdays are in the range 0 to 6. Test passing invalid value in wday.
TEST(LlvmLibcAsctime,InvalidWday)30 TEST(LlvmLibcAsctime, InvalidWday) {
31   struct tm tm_data;
32 
33   // Test with wday = -1.
34   call_asctime(&tm_data,
35                1970, // year
36                1,    // month
37                1,    // day
38                0,    // hr
39                0,    // min
40                0,    // sec
41                -1,   // wday
42                0);   // yday
43   ASSERT_ERRNO_EQ(EINVAL);
44 
45   // Test with wday = 7.
46   call_asctime(&tm_data,
47                1970, // year
48                1,    // month
49                1,    // day
50                0,    // hr
51                0,    // min
52                0,    // sec
53                7,    // wday
54                0);   // yday
55   ASSERT_ERRNO_EQ(EINVAL);
56 }
57 
58 // Months are from January to December. Test passing invalid value in month.
TEST(LlvmLibcAsctime,InvalidMonth)59 TEST(LlvmLibcAsctime, InvalidMonth) {
60   struct tm tm_data;
61 
62   // Test with month = 0.
63   call_asctime(&tm_data,
64                1970, // year
65                0,    // month
66                1,    // day
67                0,    // hr
68                0,    // min
69                0,    // sec
70                4,    // wday
71                0);   // yday
72   ASSERT_ERRNO_EQ(EINVAL);
73 
74   // Test with month = 13.
75   call_asctime(&tm_data,
76                1970, // year
77                13,   // month
78                1,    // day
79                0,    // hr
80                0,    // min
81                0,    // sec
82                4,    // wday
83                0);   // yday
84   ASSERT_ERRNO_EQ(EINVAL);
85 }
86 
TEST(LlvmLibcAsctime,ValidWeekdays)87 TEST(LlvmLibcAsctime, ValidWeekdays) {
88   struct tm tm_data;
89   char *result;
90   // 1970-01-01 00:00:00.
91   result = call_asctime(&tm_data,
92                         1970, // year
93                         1,    // month
94                         1,    // day
95                         0,    // hr
96                         0,    // min
97                         0,    // sec
98                         4,    // wday
99                         0);   // yday
100   ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", result);
101 
102   // 1970-01-03 00:00:00.
103   result = call_asctime(&tm_data,
104                         1970, // year
105                         1,    // month
106                         3,    // day
107                         0,    // hr
108                         0,    // min
109                         0,    // sec
110                         6,    // wday
111                         0);   // yday
112   ASSERT_STREQ("Sat Jan  3 00:00:00 1970\n", result);
113 
114   // 1970-01-04 00:00:00.
115   result = call_asctime(&tm_data,
116                         1970, // year
117                         1,    // month
118                         4,    // day
119                         0,    // hr
120                         0,    // min
121                         0,    // sec
122                         0,    // wday
123                         0);   // yday
124   ASSERT_STREQ("Sun Jan  4 00:00:00 1970\n", result);
125 }
126 
TEST(LlvmLibcAsctime,ValidMonths)127 TEST(LlvmLibcAsctime, ValidMonths) {
128   struct tm tm_data;
129   char *result;
130   // 1970-01-01 00:00:00.
131   result = call_asctime(&tm_data,
132                         1970, // year
133                         1,    // month
134                         1,    // day
135                         0,    // hr
136                         0,    // min
137                         0,    // sec
138                         4,    // wday
139                         0);   // yday
140   ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", result);
141 
142   // 1970-02-01 00:00:00.
143   result = call_asctime(&tm_data,
144                         1970, // year
145                         2,    // month
146                         1,    // day
147                         0,    // hr
148                         0,    // min
149                         0,    // sec
150                         0,    // wday
151                         0);   // yday
152   ASSERT_STREQ("Sun Feb  1 00:00:00 1970\n", result);
153 
154   // 1970-12-31 23:59:59.
155   result = call_asctime(&tm_data,
156                         1970, // year
157                         12,   // month
158                         31,   // day
159                         23,   // hr
160                         59,   // min
161                         59,   // sec
162                         4,    // wday
163                         0);   // yday
164   ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result);
165 }
166 
TEST(LlvmLibcAsctime,EndOf32BitEpochYear)167 TEST(LlvmLibcAsctime, EndOf32BitEpochYear) {
168   struct tm tm_data;
169   char *result;
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,
173                         2038, // year
174                         1,    // month
175                         19,   // day
176                         3,    // hr
177                         14,   // min
178                         7,    // sec
179                         2,    // wday
180                         7);   // yday
181   ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
182 }
183 
TEST(LlvmLibcAsctime,Max64BitYear)184 TEST(LlvmLibcAsctime, Max64BitYear) {
185   if (sizeof(time_t) == 4)
186     return;
187   // Mon Jan 1 12:50:50 2170 (200 years from 1970),
188   struct tm tm_data;
189   char *result;
190   result = call_asctime(&tm_data,
191                         2170, // year
192                         1,    // month
193                         1,    // day
194                         12,   // hr
195                         50,   // min
196                         50,   // sec
197                         1,    // wday
198                         50);  // yday
199   ASSERT_STREQ("Mon Jan  1 12:50:50 2170\n", result);
200 
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,
204                         2147483647, // year
205                         1,          // month
206                         1,          // day
207                         12,         // hr
208                         50,         // min
209                         50,         // sec
210                         2,          // wday
211                         50);        // yday
212   ASSERT_ERRNO_EQ(EOVERFLOW);
213   ASSERT_STREQ(nullptr, result);
214 }
215