Skip to content

Commit 984ebeb

Browse files
authored
[test] Cleanup strptime tests. NFC (#22339)
1 parent c5f8cb2 commit 984ebeb

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

test/core/test_strptime_reentrant.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
// glibc requires _XOPEN_SOURCE to be defined in order to get strptime.
99
#define _XOPEN_SOURCE
10+
#include <assert.h>
1011
#include <time.h>
1112
#include <stdio.h>
1213
#include <string.h>
1314
#include <stdlib.h>
1415

16+
#if __GLIBC__ || __EMSCRIPTEN__
17+
// Not all implementations support this (for example, upstream musl)
18+
#define HAVE_WDAY
19+
#endif
20+
1521
int main() {
1622
int result = 0;
1723
struct tm tm;
@@ -30,25 +36,31 @@ int main() {
3036
}
3137

3238
if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16 ||
33-
tm.tm_mday != 12 || tm.tm_mon != 1 || tm.tm_year != 107 ||
34-
tm.tm_wday != 1 || tm.tm_yday != 42) {
39+
tm.tm_mday != 12 || tm.tm_mon != 1 || tm.tm_year != 107) {
3540
printf("ERR: unexpected tm content (1) - %d/%d/%d %d:%d:%d\n", tm.tm_mon + 1,
3641
tm.tm_mday, tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
3742
exit(EXIT_FAILURE);
3843
}
44+
#ifdef HAVE_WDAY
45+
assert(tm.tm_wday == 1);
46+
assert(tm.tm_yday == 42);
47+
#endif
3948

4049
if (strptime("8", "%d", &tm) == NULL) {
4150
printf("ERR: strptime failed");
4251
exit(EXIT_FAILURE);
4352
}
4453

4554
if (tm.tm_sec != 21 || tm.tm_min != 13 || tm.tm_hour != 16 ||
46-
tm.tm_mday != 8 || tm.tm_mon != 1 || tm.tm_year != 107 ||
47-
tm.tm_wday != 4 || tm.tm_yday != 38) {
55+
tm.tm_mday != 8 || tm.tm_mon != 1 || tm.tm_year != 107) {
4856
printf("ERR: unexpected tm content (2) - %d/%d/%d %d:%d:%d\n", tm.tm_mon + 1,
4957
tm.tm_mday, tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
5058
exit(EXIT_FAILURE);
5159
}
60+
#ifdef HAVE_WDAY
61+
assert(tm.tm_wday == 4);
62+
assert(tm.tm_yday == 38);
63+
#endif
5264

5365
printf("OK\n");
5466
}

test/core/test_strptime_tm.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,36 @@
99
#define _XOPEN_SOURCE
1010
// glibc requires _DEFAULT_SOURCE to be defined in order to get tm_gmtoff.
1111
#define _DEFAULT_SOURCE
12+
13+
#include <assert.h>
1214
#include <time.h>
1315
#include <stdio.h>
1416
#include <string.h>
1517

18+
#if __GLIBC__ || __EMSCRIPTEN__
19+
// Not all implementations support these (for example, upstream musl)
20+
#define HAVE_WDAY
21+
#define HAVE_TIMEZONE
22+
#endif
23+
24+
#define STRPTIME(a, b, c) assert(strptime(a, b, c) != 0)
25+
1626
void ReadMonth(const char *month) {
1727
struct tm value = {0};
18-
if (strptime(month, "%b", &value)) {
19-
printf("%s: %d\n", month, value.tm_mon);
20-
}
28+
STRPTIME(month, "%b", &value);
29+
printf("%s: %d\n", month, value.tm_mon);
2130
}
2231

2332
int main() {
2433
struct tm tm;
25-
char *ptr = strptime("17410105012000", "%H%M%S%d%m%Y", &tm);
26-
27-
printf(
28-
"%s: %s, %d/%d/%d %d:%d:%d", (ptr != NULL && *ptr == '\0') ? "OK" : "ERR",
29-
tm.tm_wday == 0
30-
? "Sun"
31-
: (tm.tm_wday == 1
32-
? "Mon"
33-
: (tm.tm_wday == 2
34-
? "Tue"
35-
: (tm.tm_wday == 3
36-
? "Wed"
37-
: (tm.tm_wday == 4
38-
? "Thu"
39-
: (tm.tm_wday == 5
40-
? "Fri"
41-
: (tm.tm_wday == 6 ? "Sat"
42-
: "ERR")))))),
34+
STRPTIME("17410105012000", "%H%M%S%d%m%Y", &tm);
35+
36+
printf("%d/%d/%d %d:%d:%d\n",
4337
tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900, tm.tm_hour, tm.tm_min,
4438
tm.tm_sec);
45-
46-
printf("\n");
39+
#ifdef HAVE_WDAY
40+
assert(tm.tm_wday == 3);
41+
#endif
4742

4843
ReadMonth("jan");
4944
ReadMonth("january");
@@ -65,49 +60,54 @@ int main() {
6560

6661

6762
// check that %% is handled correctly for normal strings
68-
strptime("2020-05-01T00:01%z","%Y-%m-%dT%H:%M%%z",&tm);
63+
STRPTIME("2020-05-01T00:01%z","%Y-%m-%dT%H:%M%%z",&tm);
6964
printf("%d\n",tm.tm_min);
7065

7166
// check that %% is handled correctly even if the letter after it is
7267
// in EQUIVALENT_MATCHERS
73-
strptime("%D2020-05-01T00:01","%%D%Y-%m-%dT%H:%M",&tm);
68+
STRPTIME("%D2020-05-01T00:01","%%D%Y-%m-%dT%H:%M",&tm);
7469
printf("%d,%d\n",tm.tm_year+1900,tm.tm_min);
7570

7671

7772
// check that EQUIVALENT_MATCHERS works
7873
// %c == %a %b %d %H:%M:%S %Y
79-
strptime("Sun March 31 12:34:56 2345","%c",&tm);
74+
STRPTIME("Sun March 31 12:34:56 2345","%c",&tm);
8075
printf("%d,%d,%d,%d,%d,%d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
8176

8277
// check that EQUIVALENT_MATCHERS works twice
8378
// 'T': '%H\\:%M\\:%S',
8479
// 'D': '%m\\/%d\\/%y',
85-
strptime("12:34:56 01/02/03","%T %D",&tm);
80+
STRPTIME("12:34:56 01/02/03","%T %D",&tm);
8681
printf("%d,%d,%d,%d,%d,%d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
8782

8883
// check regex special characters don't break things
89-
strptime(".?12:34:56 01/02/03",".?%T %D",&tm);
84+
STRPTIME(".?12:34:56 01/02/03",".?%T %D",&tm);
9085
printf("%d,%d,%d,%d,%d,%d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
9186

9287

88+
#ifdef HAVE_TIMEZONE
9389
// check timezone offsets
94-
strptime("2020-05-01T00:00+0100","%Y-%m-%dT%H:%M%z",&tm);
95-
printf("%ld\n",tm.tm_gmtoff); // 3600
90+
STRPTIME("2020-05-01T00:00+0100","%Y-%m-%dT%H:%M%z",&tm);
91+
printf("tm_gmtoff: %ld\n",tm.tm_gmtoff);
92+
assert(tm.tm_gmtoff == 3600);
9693

97-
strptime("2020-05-01T00:00Z","%Y-%m-%dT%H:%M%z",&tm);
98-
printf("%ld\n",tm.tm_gmtoff); // 0
94+
STRPTIME("2020-05-01T00:00Z","%Y-%m-%dT%H:%M%z",&tm);
95+
printf("tm_gmtoff: %ld\n",tm.tm_gmtoff);
96+
assert(tm.tm_gmtoff == 0);
9997

100-
strptime("2020-05-01T00:00-02:30","%Y-%m-%dT%H:%M%z",&tm);
101-
printf("%ld\n",tm.tm_gmtoff); // -9000
98+
STRPTIME("2020-05-01T00:00-02:30","%Y-%m-%dT%H:%M%z",&tm);
99+
printf("tm_gmtoff: %ld\n",tm.tm_gmtoff);
100+
assert(tm.tm_gmtoff == -9000);
101+
#endif
102102

103103
// check that the numbers of spaces in format string are ignored
104-
strptime("12 34 56","%H %M %S",&tm);
104+
STRPTIME("12 34 56","%H %M %S",&tm);
105105
printf("%d,%d,%d\n",tm.tm_hour,tm.tm_min,tm.tm_sec);
106106

107-
strptime("123456","%H %M %S",&tm);
107+
STRPTIME("123456","%H %M %S",&tm);
108108
printf("%d,%d,%d\n",tm.tm_hour,tm.tm_min,tm.tm_sec);
109109

110-
strptime("12 34 56","%H %M %S",&tm);
110+
STRPTIME("12 34 56","%H %M %S",&tm);
111111
printf("%d,%d,%d\n",tm.tm_hour,tm.tm_min,tm.tm_sec);
112112

113113
return 0;

test/core/test_strptime_tm.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OK: Wed, 1/5/2000 17:41:1
1+
1/5/2000 17:41:1
22
jan: 0
33
january: 0
44
feb: 1
@@ -21,9 +21,9 @@ december: 11
2121
2345,3,31,12,34,56
2222
2003,1,2,12,34,56
2323
2003,1,2,12,34,56
24-
3600
25-
0
26-
-9000
24+
tm_gmtoff: 3600
25+
tm_gmtoff: 0
26+
tm_gmtoff: -9000
2727
12,34,56
2828
12,34,56
2929
12,34,56

0 commit comments

Comments
 (0)