xref: /aosp_15_r20/external/llvm-libc/test/src/time/gmtime_r_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for gmtime_r --------------------------------------------===//
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/time/gmtime_r.h"
10 #include "src/time/time_utils.h"
11 #include "test/UnitTest/Test.h"
12 #include "test/src/time/TmMatcher.h"
13 
14 using LIBC_NAMESPACE::time_utils::TimeConstants;
15 
16 // gmtime and gmtime_r share the same code and thus didn't repeat all the tests
17 // from gmtime. Added couple of validation tests.
TEST(LlvmLibcGmTimeR,EndOf32BitEpochYear)18 TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
19   // Test for maximum value of a signed 32-bit integer.
20   // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
21   time_t seconds = 0x7FFFFFFF;
22   struct tm tm_data;
23   struct tm *tm_data_ptr;
24   tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data);
25   EXPECT_TM_EQ((tm{7,  // sec
26                    14, // min
27                    3,  // hr
28                    19, // day
29                    0,  // tm_mon starts with 0 for Jan
30                    2038 - TimeConstants::TIME_YEAR_BASE, // year
31                    2,                                    // wday
32                    7,                                    // yday
33                    0}),
34                *tm_data_ptr);
35   EXPECT_TM_EQ(*tm_data_ptr, tm_data);
36 }
37 
TEST(LlvmLibcGmTimeR,Max64BitYear)38 TEST(LlvmLibcGmTimeR, Max64BitYear) {
39   if (sizeof(time_t) == 4)
40     return;
41   // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
42   time_t seconds = 67767976202043050;
43   struct tm tm_data;
44   struct tm *tm_data_ptr;
45   tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data);
46   EXPECT_TM_EQ((tm{50, // sec
47                    50, // min
48                    12, // hr
49                    1,  // day
50                    0,  // tm_mon starts with 0 for Jan
51                    2147483647 - TimeConstants::TIME_YEAR_BASE, // year
52                    2,                                          // wday
53                    50,                                         // yday
54                    0}),
55                *tm_data_ptr);
56   EXPECT_TM_EQ(*tm_data_ptr, tm_data);
57 }
58