Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / time / gmtime_test.cpp
blob6b1d029a693c383219e829e01fb892023d34d972
1 //===-- Unittests for gmtime ----------------------------------------------===//
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/errno/libc_errno.h"
10 #include "src/time/gmtime.h"
11 #include "src/time/time_utils.h"
12 #include "test/UnitTest/ErrnoSetterMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "test/src/time/TmMatcher.h"
16 #include <limits.h>
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
19 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
20 using LIBC_NAMESPACE::time_utils::TimeConstants;
22 TEST(LlvmLibcGmTime, OutOfRange) {
23 if (sizeof(time_t) < sizeof(int64_t))
24 return;
25 time_t seconds =
26 1 + INT_MAX * static_cast<int64_t>(
27 TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);
28 struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds);
29 EXPECT_TRUE(tm_data == nullptr);
30 EXPECT_EQ(libc_errno, EOVERFLOW);
32 libc_errno = 0;
33 seconds = INT_MIN * static_cast<int64_t>(
34 TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) -
36 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
37 EXPECT_TRUE(tm_data == nullptr);
38 EXPECT_EQ(libc_errno, EOVERFLOW);
41 TEST(LlvmLibcGmTime, InvalidSeconds) {
42 time_t seconds = 0;
43 struct tm *tm_data = nullptr;
44 // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59.
45 seconds = -1;
46 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
47 EXPECT_TM_EQ((tm{59, // sec
48 59, // min
49 23, // hr
50 31, // day
51 12 - 1, // tm_mon starts with 0 for Jan
52 1969 - TimeConstants::TIME_YEAR_BASE, // year
53 3, // wday
54 364, // yday
55 0}),
56 *tm_data);
57 // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00.
58 seconds = 60;
59 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
60 EXPECT_TM_EQ((tm{0, // sec
61 1, // min
62 0, // hr
63 1, // day
64 0, // tm_mon starts with 0 for Jan
65 1970 - TimeConstants::TIME_YEAR_BASE, // year
66 4, // wday
67 0, // yday
68 0}),
69 *tm_data);
72 TEST(LlvmLibcGmTime, InvalidMinutes) {
73 time_t seconds = 0;
74 struct tm *tm_data = nullptr;
75 // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00.
76 seconds = -TimeConstants::SECONDS_PER_MIN;
77 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
78 EXPECT_TM_EQ((tm{0, // sec
79 59, // min
80 23, // hr
81 31, // day
82 11, // tm_mon starts with 0 for Jan
83 1969 - TimeConstants::TIME_YEAR_BASE, // year
84 3, // wday
85 0, // yday
86 0}),
87 *tm_data);
88 // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00.
89 seconds = 60 * TimeConstants::SECONDS_PER_MIN;
90 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
91 EXPECT_TM_EQ((tm{0, // sec
92 0, // min
93 1, // hr
94 1, // day
95 0, // tm_mon starts with 0 for Jan
96 1970 - TimeConstants::TIME_YEAR_BASE, // year
97 4, // wday
98 0, // yday
99 0}),
100 *tm_data);
103 TEST(LlvmLibcGmTime, InvalidHours) {
104 time_t seconds = 0;
105 struct tm *tm_data = nullptr;
106 // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00.
107 seconds = -TimeConstants::SECONDS_PER_HOUR;
108 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
109 EXPECT_TM_EQ((tm{0, // sec
110 0, // min
111 23, // hr
112 31, // day
113 11, // tm_mon starts with 0 for Jan
114 1969 - TimeConstants::TIME_YEAR_BASE, // year
115 3, // wday
116 0, // yday
117 0}),
118 *tm_data);
119 // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00.
120 seconds = 24 * TimeConstants::SECONDS_PER_HOUR;
121 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
122 EXPECT_TM_EQ((tm{0, // sec
123 0, // min
124 0, // hr
125 2, // day
126 0, // tm_mon starts with 0 for Jan
127 1970 - TimeConstants::TIME_YEAR_BASE, // year
128 5, // wday
129 0, // yday
130 0}),
131 *tm_data);
134 TEST(LlvmLibcGmTime, InvalidYear) {
135 // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00.
136 time_t seconds =
137 -TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY;
138 struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds);
139 EXPECT_TM_EQ((tm{0, // sec
140 0, // min
141 0, // hr
142 1, // day
143 0, // tm_mon starts with 0 for Jan
144 1969 - TimeConstants::TIME_YEAR_BASE, // year
145 3, // wday
146 0, // yday
147 0}),
148 *tm_data);
151 TEST(LlvmLibcGmTime, InvalidMonths) {
152 time_t seconds = 0;
153 struct tm *tm_data = nullptr;
154 // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00.
155 seconds = -31 * TimeConstants::SECONDS_PER_DAY;
156 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
157 EXPECT_TM_EQ((tm{0, // sec
158 0, // min
159 0, // hr
160 1, // day
161 12 - 1, // tm_mon starts with 0 for Jan
162 1969 - TimeConstants::TIME_YEAR_BASE, // year
163 1, // wday
164 0, // yday
165 0}),
166 *tm_data);
167 // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00.
168 seconds =
169 TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY;
170 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
171 EXPECT_TM_EQ((tm{0, // sec
172 0, // min
173 0, // hr
174 1, // day
175 0, // tm_mon starts with 0 for Jan
176 1971 - TimeConstants::TIME_YEAR_BASE, // year
177 5, // wday
178 0, // yday
179 0}),
180 *tm_data);
183 TEST(LlvmLibcGmTime, InvalidDays) {
184 time_t seconds = 0;
185 struct tm *tm_data = nullptr;
186 // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00.
187 seconds = -1 * TimeConstants::SECONDS_PER_DAY;
188 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
189 EXPECT_TM_EQ((tm{0, // sec
190 0, // min
191 0, // hr
192 31, // day
193 11, // tm_mon starts with 0 for Jan
194 1969 - TimeConstants::TIME_YEAR_BASE, // year
195 3, // wday
196 0, // yday
197 0}),
198 *tm_data);
200 // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00.
201 seconds = 31 * TimeConstants::SECONDS_PER_DAY;
202 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
203 EXPECT_TM_EQ((tm{0, // sec
204 0, // min
205 0, // hr
206 1, // day
207 0, // tm_mon starts with 0 for Jan
208 1970 - TimeConstants::TIME_YEAR_BASE, // year
209 0, // wday
210 0, // yday
211 0}),
212 *tm_data);
214 // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00.
215 seconds = 59 * TimeConstants::SECONDS_PER_DAY;
216 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
217 EXPECT_TM_EQ((tm{0, // sec
218 0, // min
219 0, // hr
220 1, // day
221 2, // tm_mon starts with 0 for Jan
222 1970 - TimeConstants::TIME_YEAR_BASE, // year
223 0, // wday
224 0, // yday
225 0}),
226 *tm_data);
228 // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00.
229 seconds = ((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) *
230 TimeConstants::SECONDS_PER_DAY;
231 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
232 EXPECT_TM_EQ((tm{0, // sec
233 0, // min
234 0, // hr
235 1, // day
236 2, // tm_mon starts with 0 for Jan
237 1972 - TimeConstants::TIME_YEAR_BASE, // year
238 3, // wday
239 0, // yday
240 0}),
241 *tm_data);
244 TEST(LlvmLibcGmTime, EndOf32BitEpochYear) {
245 // Test for maximum value of a signed 32-bit integer.
246 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
247 time_t seconds = 0x7FFFFFFF;
248 struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds);
249 EXPECT_TM_EQ((tm{7, // sec
250 14, // min
251 3, // hr
252 19, // day
253 0, // tm_mon starts with 0 for Jan
254 2038 - TimeConstants::TIME_YEAR_BASE, // year
255 2, // wday
256 7, // yday
257 0}),
258 *tm_data);
261 TEST(LlvmLibcGmTime, Max64BitYear) {
262 if (sizeof(time_t) == 4)
263 return;
264 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
265 time_t seconds = 6311479850;
266 struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds);
267 EXPECT_TM_EQ((tm{50, // sec
268 50, // min
269 12, // hr
270 1, // day
271 0, // tm_mon starts with 0 for Jan
272 2170 - TimeConstants::TIME_YEAR_BASE, // year
273 1, // wday
274 50, // yday
275 0}),
276 *tm_data);
278 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
279 seconds = 67767976202043050;
280 tm_data = LIBC_NAMESPACE::gmtime(&seconds);
281 EXPECT_TM_EQ((tm{50, // sec
282 50, // min
283 12, // hr
284 1, // day
285 0, // tm_mon starts with 0 for Jan
286 2147483647 - TimeConstants::TIME_YEAR_BASE, // year
287 2, // wday
288 50, // yday
289 0}),
290 *tm_data);