summaryrefslogtreecommitdiff
path: root/libc/test/src/time/asctime_test.cpp
blob: 3df2edf1f9c9a029fab57b631aa291d4d6401d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//===-- Unittests for asctime ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/asctime.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmHelper.h"

static inline char *call_asctime(struct tm *tm_data, int year, int month,
                                 int mday, int hour, int min, int sec, int wday,
                                 int yday) {
  __llvm_libc::tmhelper::testing::initialize_tm_data(
      tm_data, year, month, mday, hour, min, sec, wday, yday);
  return __llvm_libc::asctime(tm_data);
}

TEST(LlvmLibcAsctime, Nullptr) {
  char *result;
  result = __llvm_libc::asctime(nullptr);
  ASSERT_EQ(EINVAL, libc_errno);
  ASSERT_STREQ(nullptr, result);
}

// Weekdays are in the range 0 to 6. Test passing invalid value in wday.
TEST(LlvmLibcAsctime, InvalidWday) {
  struct tm tm_data;

  // Test with wday = -1.
  call_asctime(&tm_data,
               1970, // year
               1,    // month
               1,    // day
               0,    // hr
               0,    // min
               0,    // sec
               -1,   // wday
               0);   // yday
  ASSERT_EQ(EINVAL, libc_errno);

  // Test with wday = 7.
  call_asctime(&tm_data,
               1970, // year
               1,    // month
               1,    // day
               0,    // hr
               0,    // min
               0,    // sec
               7,    // wday
               0);   // yday
  ASSERT_EQ(EINVAL, libc_errno);
}

// Months are from January to December. Test passing invalid value in month.
TEST(LlvmLibcAsctime, InvalidMonth) {
  struct tm tm_data;

  // Test with month = 0.
  call_asctime(&tm_data,
               1970, // year
               0,    // month
               1,    // day
               0,    // hr
               0,    // min
               0,    // sec
               4,    // wday
               0);   // yday
  ASSERT_EQ(EINVAL, libc_errno);

  // Test with month = 13.
  call_asctime(&tm_data,
               1970, // year
               13,   // month
               1,    // day
               0,    // hr
               0,    // min
               0,    // sec
               4,    // wday
               0);   // yday
  ASSERT_EQ(EINVAL, libc_errno);
}

TEST(LlvmLibcAsctime, ValidWeekdays) {
  struct tm tm_data;
  char *result;
  // 1970-01-01 00:00:00.
  result = call_asctime(&tm_data,
                        1970, // year
                        1,    // month
                        1,    // day
                        0,    // hr
                        0,    // min
                        0,    // sec
                        4,    // wday
                        0);   // yday
  ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", result);

  // 1970-01-03 00:00:00.
  result = call_asctime(&tm_data,
                        1970, // year
                        1,    // month
                        3,    // day
                        0,    // hr
                        0,    // min
                        0,    // sec
                        6,    // wday
                        0);   // yday
  ASSERT_STREQ("Sat Jan  3 00:00:00 1970\n", result);

  // 1970-01-04 00:00:00.
  result = call_asctime(&tm_data,
                        1970, // year
                        1,    // month
                        4,    // day
                        0,    // hr
                        0,    // min
                        0,    // sec
                        0,    // wday
                        0);   // yday
  ASSERT_STREQ("Sun Jan  4 00:00:00 1970\n", result);
}

TEST(LlvmLibcAsctime, ValidMonths) {
  struct tm tm_data;
  char *result;
  // 1970-01-01 00:00:00.
  result = call_asctime(&tm_data,
                        1970, // year
                        1,    // month
                        1,    // day
                        0,    // hr
                        0,    // min
                        0,    // sec
                        4,    // wday
                        0);   // yday
  ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", result);

  // 1970-02-01 00:00:00.
  result = call_asctime(&tm_data,
                        1970, // year
                        2,    // month
                        1,    // day
                        0,    // hr
                        0,    // min
                        0,    // sec
                        0,    // wday
                        0);   // yday
  ASSERT_STREQ("Sun Feb  1 00:00:00 1970\n", result);

  // 1970-12-31 23:59:59.
  result = call_asctime(&tm_data,
                        1970, // year
                        12,   // month
                        31,   // day
                        23,   // hr
                        59,   // min
                        59,   // sec
                        4,    // wday
                        0);   // yday
  ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n", result);
}

TEST(LlvmLibcAsctime, EndOf32BitEpochYear) {
  struct tm tm_data;
  char *result;
  // Test for maximum value of a signed 32-bit integer.
  // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
  result = call_asctime(&tm_data,
                        2038, // year
                        1,    // month
                        19,   // day
                        3,    // hr
                        14,   // min
                        7,    // sec
                        2,    // wday
                        7);   // yday
  ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
}

TEST(LlvmLibcAsctime, Max64BitYear) {
  if (sizeof(time_t) == 4)
    return;
  // Mon Jan 1 12:50:50 2170 (200 years from 1970),
  struct tm tm_data;
  char *result;
  result = call_asctime(&tm_data,
                        2170, // year
                        1,    // month
                        1,    // day
                        12,   // hr
                        50,   // min
                        50,   // sec
                        1,    // wday
                        50);  // yday
  ASSERT_STREQ("Mon Jan  1 12:50:50 2170\n", result);

  // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
  // This test would cause buffer overflow and thus asctime returns nullptr.
  result = call_asctime(&tm_data,
                        2147483647, // year
                        1,          // month
                        1,          // day
                        12,         // hr
                        50,         // min
                        50,         // sec
                        2,          // wday
                        50);        // yday
  ASSERT_EQ(EOVERFLOW, libc_errno);
  ASSERT_STREQ(nullptr, result);
}