Skip to content

Commit 5ce75b8

Browse files
authored
Cleanup strftime tests (#20657)
The core test now reports errors using `assert` which links directly the line number of the failure. The other test is now written in pure C (which I think makes it a lot easier to read).
1 parent 5043d63 commit 5ce75b8

File tree

3 files changed

+98
-109
lines changed

3 files changed

+98
-109
lines changed

test/core/test_strftime.cpp renamed to test/core/test_strftime.c

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6+
#include <assert.h>
67
#include <time.h>
78
#include <stdio.h>
89
#include <string.h>
910
#include <stdlib.h>
1011

11-
void test(int result, const char* comment, const char* parsed = "") {
12-
printf("%s: %d\n", comment, result);
13-
if (!result) {
14-
printf("\nERROR: %s (\"%s\")\n", comment, parsed);
15-
abort();
16-
}
17-
}
12+
#define TEST(result, comment, parsed) \
13+
printf("%s: %d\n", comment, result); \
14+
if (!(result)) printf("ERROR: %s (%s)\n", comment, parsed); \
15+
assert(result); \
1816

1917
int cmp(const char* s1, const char* s2) {
2018
for (; *s1 == *s2; s1++, s2++) {
@@ -50,100 +48,100 @@ int main() {
5048
printf("%s -> %s\n", fmt, s);
5149

5250
size = strftime(s, sizeof(s), "", &tm);
53-
test((size == 0) && (*s == '\0'), "strftime test #1", s);
51+
TEST((size == 0) && (*s == '\0'), "strftime test #1", s);
5452

5553
size = strftime(s, sizeof(s), "%a", &tm);
56-
test((size == 3) && !cmp(s, "Thu"), "strftime test #2", s);
54+
TEST((size == 3) && !cmp(s, "Thu"), "strftime test #2", s);
5755

5856
size = strftime(s, sizeof(s), "%A", &tm);
59-
test((size == 8) && !cmp(s, "Thursday"), "strftime test #3", s);
57+
TEST((size == 8) && !cmp(s, "Thursday"), "strftime test #3", s);
6058

6159
size = strftime(s, sizeof(s), "%b", &tm);
62-
test((size == 3) && !cmp(s, "Feb"), "strftime test #4", s);
60+
TEST((size == 3) && !cmp(s, "Feb"), "strftime test #4", s);
6361

6462
size = strftime(s, sizeof(s), "%B", &tm);
65-
test((size == 8) && !cmp(s, "February"), "strftime test #5", s);
63+
TEST((size == 8) && !cmp(s, "February"), "strftime test #5", s);
6664

6765
size = strftime(s, sizeof(s), "%d", &tm);
68-
test((size == 2) && !cmp(s, "21"), "strftime test #6", s);
66+
TEST((size == 2) && !cmp(s, "21"), "strftime test #6", s);
6967

7068
size = strftime(s, sizeof(s), "%Od", &tm);
71-
test((size == 2) && !cmp(s, "21"), "strftime test #6a", s);
69+
TEST((size == 2) && !cmp(s, "21"), "strftime test #6a", s);
7270

7371
size = strftime(s, sizeof(s), "%H", &tm);
74-
test((size == 2) && !cmp(s, "20"), "strftime test #7", s);
72+
TEST((size == 2) && !cmp(s, "20"), "strftime test #7", s);
7573

7674
size = strftime(s, sizeof(s), "%OH", &tm);
77-
test((size == 2) && !cmp(s, "20"), "strftime test #7a", s);
75+
TEST((size == 2) && !cmp(s, "20"), "strftime test #7a", s);
7876

7977
size = strftime(s, sizeof(s), "%I", &tm);
80-
test((size == 2) && !cmp(s, "08"), "strftime test #8", s);
78+
TEST((size == 2) && !cmp(s, "08"), "strftime test #8", s);
8179

8280
size = strftime(s, sizeof(s), "%OI", &tm);
83-
test((size == 2) && !cmp(s, "08"), "strftime test #8a", s);
81+
TEST((size == 2) && !cmp(s, "08"), "strftime test #8a", s);
8482

8583
size = strftime(s, sizeof(s), "%j", &tm);
86-
test((size == 3) && !cmp(s, "052"), "strftime test #9", s);
84+
TEST((size == 3) && !cmp(s, "052"), "strftime test #9", s);
8785

8886
size = strftime(s, sizeof(s), "%m", &tm);
89-
test((size == 2) && !cmp(s, "02"), "strftime test #10", s);
87+
TEST((size == 2) && !cmp(s, "02"), "strftime test #10", s);
9088

9189
size = strftime(s, sizeof(s), "%Om", &tm);
92-
test((size == 2) && !cmp(s, "02"), "strftime test #10a", s);
90+
TEST((size == 2) && !cmp(s, "02"), "strftime test #10a", s);
9391

9492
size = strftime(s, sizeof(s), "%M", &tm);
95-
test((size == 2) && !cmp(s, "23"), "strftime test #11", s);
93+
TEST((size == 2) && !cmp(s, "23"), "strftime test #11", s);
9694

9795
size = strftime(s, sizeof(s), "%OM", &tm);
98-
test((size == 2) && !cmp(s, "23"), "strftime test #11a", s);
96+
TEST((size == 2) && !cmp(s, "23"), "strftime test #11a", s);
9997

10098
size = strftime(s, sizeof(s), "%p", &tm);
101-
test((size == 2) && !cmp(s, "PM"), "strftime test #12", s);
99+
TEST((size == 2) && !cmp(s, "PM"), "strftime test #12", s);
102100

103101
size = strftime(s, sizeof(s), "%S", &tm);
104-
test((size == 2) && !cmp(s, "04"), "strftime test #13", s);
102+
TEST((size == 2) && !cmp(s, "04"), "strftime test #13", s);
105103

106104
size = strftime(s, sizeof(s), "%OS", &tm);
107-
test((size == 2) && !cmp(s, "04"), "strftime test #13a", s);
105+
TEST((size == 2) && !cmp(s, "04"), "strftime test #13a", s);
108106

109107
size = strftime(s, sizeof(s), "%U", &tm);
110-
test((size == 2) && !cmp(s, "07"), "strftime test #14", s);
108+
TEST((size == 2) && !cmp(s, "07"), "strftime test #14", s);
111109

112110
size = strftime(s, sizeof(s), "%OU", &tm);
113-
test((size == 2) && !cmp(s, "07"), "strftime test #14a", s);
111+
TEST((size == 2) && !cmp(s, "07"), "strftime test #14a", s);
114112

115113
size = strftime(s, sizeof(s), "%w", &tm);
116-
test((size == 1) && !cmp(s, "4"), "strftime test #15", s);
114+
TEST((size == 1) && !cmp(s, "4"), "strftime test #15", s);
117115

118116
size = strftime(s, sizeof(s), "%Ow", &tm);
119-
test((size == 1) && !cmp(s, "4"), "strftime test #15a", s);
117+
TEST((size == 1) && !cmp(s, "4"), "strftime test #15a", s);
120118

121119
size = strftime(s, sizeof(s), "%W", &tm);
122-
test((size == 2) && !cmp(s, "07"), "strftime test #16", s);
120+
TEST((size == 2) && !cmp(s, "07"), "strftime test #16", s);
123121

124122
size = strftime(s, sizeof(s), "%OW", &tm);
125-
test((size == 2) && !cmp(s, "07"), "strftime test #16a", s);
123+
TEST((size == 2) && !cmp(s, "07"), "strftime test #16a", s);
126124

127125
size = strftime(s, sizeof(s), "%y", &tm);
128-
test((size == 2) && !cmp(s, "74"), "strftime test #17", s);
126+
TEST((size == 2) && !cmp(s, "74"), "strftime test #17", s);
129127

130128
size = strftime(s, sizeof(s), "%Oy", &tm);
131-
test((size == 2) && !cmp(s, "74"), "strftime test #17a", s);
129+
TEST((size == 2) && !cmp(s, "74"), "strftime test #17a", s);
132130

133131
size = strftime(s, sizeof(s), "%Y", &tm);
134-
test((size == 4) && !cmp(s, "1974"), "strftime test #18", s);
132+
TEST((size == 4) && !cmp(s, "1974"), "strftime test #18", s);
135133

136134
size = strftime(s, sizeof(s), "%EY", &tm);
137-
test((size == 4) && !cmp(s, "1974"), "strftime test #18a", s);
135+
TEST((size == 4) && !cmp(s, "1974"), "strftime test #18a", s);
138136

139137
size = strftime(s, sizeof(s), "%%", &tm);
140-
test((size == 1) && !cmp(s, "%"), "strftime test #19", s);
138+
TEST((size == 1) && !cmp(s, "%"), "strftime test #19", s);
141139

142140
size = strftime(s, sizeof(s), "%Y", &tm);
143-
test((size == 4) && !cmp(s, "1974"), "strftime test #20", s);
141+
TEST((size == 4) && !cmp(s, "1974"), "strftime test #20", s);
144142

145143
size = strftime(s, 0, "%Y", &tm);
146-
test((size == 0), "strftime test #21", s);
144+
TEST((size == 0), "strftime test #21", s);
147145

148146
// Reset tm to Jan 1st
149147
tm.tm_mon = 0;
@@ -153,84 +151,84 @@ int main() {
153151
// 1/1/1972 was a Tuesday
154152
tm.tm_wday = 2;
155153
size = strftime(s, sizeof(s), "%U", &tm);
156-
test((size == 2) && !cmp(s, "00"), "strftime test #22", s);
154+
TEST((size == 2) && !cmp(s, "00"), "strftime test #22", s);
157155

158156
size = strftime(s, sizeof(s), "%W", &tm);
159-
test((size == 2) && !cmp(s, "00"), "strftime test #23", s);
157+
TEST((size == 2) && !cmp(s, "00"), "strftime test #23", s);
160158

161159
// 1/1/1973 was a Monday and is in CW 1
162160
tm.tm_year = 73;
163161
tm.tm_wday = 1;
164162
size = strftime(s, sizeof(s), "%W", &tm);
165-
test((size == 2) && !cmp(s, "01"), "strftime test #24", s);
163+
TEST((size == 2) && !cmp(s, "01"), "strftime test #24", s);
166164

167165
// 1/1/1978 was a Sunday and is in CW 1
168166
tm.tm_year = 78;
169167
tm.tm_wday = 0;
170168
size = strftime(s, sizeof(s), "%U", &tm);
171-
test((size == 2) && !cmp(s, "01"), "strftime test #25", s);
169+
TEST((size == 2) && !cmp(s, "01"), "strftime test #25", s);
172170

173171
// 2/1/1999 (was a Saturday)
174172
tm.tm_year = 99;
175173
tm.tm_yday = 1;
176174
tm.tm_wday = 6;
177175
size = strftime(s, sizeof(s), "%G (%V)", &tm);
178-
test((size == 9) && !cmp(s, "1998 (53)"), "strftime test #26", s);
176+
TEST((size == 9) && !cmp(s, "1998 (53)"), "strftime test #26", s);
179177

180178
size = strftime(s, sizeof(s), "%g", &tm);
181-
test((size == 2) && !cmp(s, "98"), "strftime test #27", s);
179+
TEST((size == 2) && !cmp(s, "98"), "strftime test #27", s);
182180

183181
// 30/12/1997 (was a Tuesday)
184182
tm.tm_year = 97;
185183
tm.tm_yday = 363;
186184
tm.tm_wday = 2;
187185
size = strftime(s, sizeof(s), "%G (%V)", &tm);
188-
test((size == 9) && !cmp(s, "1998 (01)"), "strftime test #28", s);
186+
TEST((size == 9) && !cmp(s, "1998 (01)"), "strftime test #28", s);
189187

190188
size = strftime(s, sizeof(s), "%g", &tm);
191-
test((size == 2) && !cmp(s, "98"), "strftime test #29", s);
189+
TEST((size == 2) && !cmp(s, "98"), "strftime test #29", s);
192190

193191
tm.tm_wday = 0;
194192
size = strftime(s, sizeof(s), "%w", &tm);
195-
test((size == 1) && !cmp(s, "0"), "strftime test #30", s);
193+
TEST((size == 1) && !cmp(s, "0"), "strftime test #30", s);
196194
size = strftime(s, sizeof(s), "%u", &tm);
197-
test((size == 1) && !cmp(s, "7"), "strftime test #31", s);
195+
TEST((size == 1) && !cmp(s, "7"), "strftime test #31", s);
198196

199197
tm.tm_wday = 1;
200198
size = strftime(s, sizeof(s), "%w", &tm);
201-
test((size == 1) && !cmp(s, "1"), "strftime test #32", s);
199+
TEST((size == 1) && !cmp(s, "1"), "strftime test #32", s);
202200
size = strftime(s, sizeof(s), "%u", &tm);
203-
test((size == 1) && !cmp(s, "1"), "strftime test #33", s);
201+
TEST((size == 1) && !cmp(s, "1"), "strftime test #33", s);
204202

205203
tm.tm_wday = 2;
206204
size = strftime(s, sizeof(s), "%w", &tm);
207-
test((size == 1) && !cmp(s, "2"), "strftime test #34", s);
205+
TEST((size == 1) && !cmp(s, "2"), "strftime test #34", s);
208206
size = strftime(s, sizeof(s), "%u", &tm);
209-
test((size == 1) && !cmp(s, "2"), "strftime test #35", s);
207+
TEST((size == 1) && !cmp(s, "2"), "strftime test #35", s);
210208

211209
tm.tm_wday = 3;
212210
size = strftime(s, sizeof(s), "%w", &tm);
213-
test((size == 1) && !cmp(s, "3"), "strftime test #36", s);
211+
TEST((size == 1) && !cmp(s, "3"), "strftime test #36", s);
214212
size = strftime(s, sizeof(s), "%u", &tm);
215-
test((size == 1) && !cmp(s, "3"), "strftime test #37", s);
213+
TEST((size == 1) && !cmp(s, "3"), "strftime test #37", s);
216214

217215
tm.tm_wday = 4;
218216
size = strftime(s, sizeof(s), "%w", &tm);
219-
test((size == 1) && !cmp(s, "4"), "strftime test #38", s);
217+
TEST((size == 1) && !cmp(s, "4"), "strftime test #38", s);
220218
size = strftime(s, sizeof(s), "%u", &tm);
221-
test((size == 1) && !cmp(s, "4"), "strftime test #39", s);
219+
TEST((size == 1) && !cmp(s, "4"), "strftime test #39", s);
222220

223221
tm.tm_wday = 5;
224222
size = strftime(s, sizeof(s), "%w", &tm);
225-
test((size == 1) && !cmp(s, "5"), "strftime test #40", s);
223+
TEST((size == 1) && !cmp(s, "5"), "strftime test #40", s);
226224
size = strftime(s, sizeof(s), "%u", &tm);
227-
test((size == 1) && !cmp(s, "5"), "strftime test #41", s);
225+
TEST((size == 1) && !cmp(s, "5"), "strftime test #41", s);
228226

229227
tm.tm_wday = 6;
230228
size = strftime(s, sizeof(s), "%w", &tm);
231-
test((size == 1) && !cmp(s, "6"), "strftime test #42", s);
229+
TEST((size == 1) && !cmp(s, "6"), "strftime test #42", s);
232230
size = strftime(s, sizeof(s), "%u", &tm);
233-
test((size == 1) && !cmp(s, "6"), "strftime test #43", s);
231+
TEST((size == 1) && !cmp(s, "6"), "strftime test #43", s);
234232

235233
// timezones
236234
time_t xmas2002 = 1040786563ll;
@@ -241,67 +239,67 @@ int main() {
241239
struct tm copy = tm;
242240
int ahead = timegm(&copy) >= summer2002;
243241
size = strftime(s, sizeof(s), "%z", &tm);
244-
test((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #1", s);
242+
TEST((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #1", s);
245243
size = strftime(s, sizeof(s), "%Z", &tm);
246-
test(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #2", s);
244+
TEST(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #2", s);
247245

248246
localtime_r(&xmas2002, &tm);
249247
copy = tm;
250248
ahead = timegm(&copy) >= xmas2002;
251249
size = strftime(s, sizeof(s), "%z", &tm);
252-
test((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #3", s);
250+
TEST((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #3", s);
253251
size = strftime(s, sizeof(s), "%Z", &tm);
254-
test(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #4", s);
252+
TEST(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #4", s);
255253

256254
// AM/PM
257255
tm.tm_sec = 0;
258256
tm.tm_min = 0;
259257

260258
tm.tm_hour = 0;
261259
size = strftime(s, sizeof(s), "%I %p", &tm);
262-
test(!cmp(s, "12 AM"), "strftime test #32", s);
260+
TEST(!cmp(s, "12 AM"), "strftime test #32", s);
263261

264262
tm.tm_hour = 12;
265263
size = strftime(s, sizeof(s), "%I %p", &tm);
266-
test(!cmp(s, "12 PM"), "strftime test #33", s);
264+
TEST(!cmp(s, "12 PM"), "strftime test #33", s);
267265

268266
tm.tm_min = 1;
269267

270268
tm.tm_hour = 0;
271269
size = strftime(s, sizeof(s), "%I %M %p", &tm);
272-
test(!cmp(s, "12 01 AM"), "strftime test #34", s);
270+
TEST(!cmp(s, "12 01 AM"), "strftime test #34", s);
273271

274272
tm.tm_hour = 12;
275273
size = strftime(s, sizeof(s), "%I %M %p", &tm);
276-
test(!cmp(s, "12 01 PM"), "strftime test #35", s);
274+
TEST(!cmp(s, "12 01 PM"), "strftime test #35", s);
277275

278276
// strftime week number edge case
279277
// 2013-01-06 Sunday
280278
time_t y2013 = 1357430400ll;
281279
gmtime_r(&y2013, &tm);
282280
size = strftime(s, sizeof(s), "%Y-%m-%d %W %U", &tm);
283-
test(!cmp(s, "2013-01-06 00 01"), "strftime test #36", s);
281+
TEST(!cmp(s, "2013-01-06 00 01"), "strftime test #36", s);
284282

285283
y2013 += 60 * 60 * 24;
286284
gmtime_r(&y2013, &tm);
287285
size = strftime(s, sizeof(s), "%Y-%m-%d %W %U", &tm);
288-
test(!cmp(s, "2013-01-07 01 01"), "strftime test #36a", s);
286+
TEST(!cmp(s, "2013-01-07 01 01"), "strftime test #36a", s);
289287

290288
// strftime %V (ISO 8601 week number) edge cases
291289
time_t dec17 = 1481932800;
292290
gmtime_r(&dec17, &tm);
293291
size = strftime(s, sizeof(s), "%Y-%m-%d %G %V %w", &tm);
294-
test(!cmp(s, "2016-12-17 2016 50 6"), "strftime test #37", s);
292+
TEST(!cmp(s, "2016-12-17 2016 50 6"), "strftime test #37", s);
295293

296294
dec17 = 1513468800;
297295
gmtime_r(&dec17, &tm);
298296
size = strftime(s, sizeof(s), "%Y-%m-%d %G %V %w", &tm);
299-
test(!cmp(s, "2017-12-17 2017 50 0"), "strftime test #37a", s);
297+
TEST(!cmp(s, "2017-12-17 2017 50 0"), "strftime test #37a", s);
300298

301299
dec17 = 1545004800;
302300
gmtime_r(&dec17, &tm);
303301
size = strftime(s, sizeof(s), "%Y-%m-%d %G %V %w", &tm);
304-
test(!cmp(s, "2018-12-17 2018 51 1"), "strftime test #37b", s);
302+
TEST(!cmp(s, "2018-12-17 2018 51 1"), "strftime test #37b", s);
305303

306304
return 0;
307305
}

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ def test_strptime_reentrant(self):
29712971
self.do_core_test('test_strptime_reentrant.c')
29722972

29732973
def test_strftime(self):
2974-
self.do_core_test('test_strftime.cpp')
2974+
self.do_core_test('test_strftime.c')
29752975

29762976
def test_trickystring(self):
29772977
self.do_core_test('test_trickystring.c')

0 commit comments

Comments
 (0)