[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / time / asctime_test.cpp
blobe6a5040de235d46453d1ed7e3850124ac769f1df
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 //===----------------------------------------------------------------------===//
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,
15 int yday) {
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) {
22 char *result;
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) {
30 struct tm tm_data;
32 // Test with wday = -1.
33 call_asctime(&tm_data,
34 1970, // year
35 1, // month
36 1, // day
37 0, // hr
38 0, // min
39 0, // sec
40 -1, // wday
41 0); // yday
42 ASSERT_EQ(EINVAL, llvmlibc_errno);
44 // Test with wday = 7.
45 call_asctime(&tm_data,
46 1970, // year
47 1, // month
48 1, // day
49 0, // hr
50 0, // min
51 0, // sec
52 7, // wday
53 0); // yday
54 ASSERT_EQ(EINVAL, llvmlibc_errno);
57 // Months are from January to December. Test passing invalid value in month.
58 TEST(LlvmLibcAsctime, InvalidMonth) {
59 struct tm tm_data;
61 // Test with month = 0.
62 call_asctime(&tm_data,
63 1970, // year
64 0, // month
65 1, // day
66 0, // hr
67 0, // min
68 0, // sec
69 4, // wday
70 0); // yday
71 ASSERT_EQ(EINVAL, llvmlibc_errno);
73 // Test with month = 13.
74 call_asctime(&tm_data,
75 1970, // year
76 13, // month
77 1, // day
78 0, // hr
79 0, // min
80 0, // sec
81 4, // wday
82 0); // yday
83 ASSERT_EQ(EINVAL, llvmlibc_errno);
86 TEST(LlvmLibcAsctime, ValidWeekdays) {
87 struct tm tm_data;
88 char *result;
89 // 1970-01-01 00:00:00.
90 result = call_asctime(&tm_data,
91 1970, // year
92 1, // month
93 1, // day
94 0, // hr
95 0, // min
96 0, // sec
97 4, // wday
98 0); // yday
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,
103 1970, // year
104 1, // month
105 3, // day
106 0, // hr
107 0, // min
108 0, // sec
109 6, // wday
110 0); // yday
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,
115 1970, // year
116 1, // month
117 4, // day
118 0, // hr
119 0, // min
120 0, // sec
121 0, // wday
122 0); // yday
123 ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n", result);
126 TEST(LlvmLibcAsctime, ValidMonths) {
127 struct tm tm_data;
128 char *result;
129 // 1970-01-01 00:00:00.
130 result = call_asctime(&tm_data,
131 1970, // year
132 1, // month
133 1, // day
134 0, // hr
135 0, // min
136 0, // sec
137 4, // wday
138 0); // yday
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,
143 1970, // year
144 2, // month
145 1, // day
146 0, // hr
147 0, // min
148 0, // sec
149 0, // wday
150 0); // yday
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,
155 1970, // year
156 12, // month
157 31, // day
158 23, // hr
159 59, // min
160 59, // sec
161 4, // wday
162 0); // yday
163 ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result);
166 TEST(LlvmLibcAsctime, EndOf32BitEpochYear) {
167 struct tm tm_data;
168 char *result;
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,
172 2038, // year
173 1, // month
174 19, // day
175 3, // hr
176 14, // min
177 7, // sec
178 2, // wday
179 7); // yday
180 ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
183 TEST(LlvmLibcAsctime, Max64BitYear) {
184 if (sizeof(time_t) == 4)
185 return;
186 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
187 struct tm tm_data;
188 char *result;
189 result = call_asctime(&tm_data,
190 2170, // year
191 1, // month
192 1, // day
193 12, // hr
194 50, // min
195 50, // sec
196 1, // wday
197 50); // yday
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,
203 2147483647, // year
204 1, // month
205 1, // day
206 12, // hr
207 50, // min
208 50, // sec
209 2, // wday
210 50); // yday
211 ASSERT_EQ(EOVERFLOW, llvmlibc_errno);
212 ASSERT_STREQ(nullptr, result);