Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / strptime / strptimetest.c
blob7862a0d07b199580bb9303485a203efbeff9e03a
1 /* $NetBSD: strptimetest.c,v 1.2 2005/03/05 07:48:47 martin Exp $ */
3 /*
4 * This file placed in the public domain.
5 * David Laight March 2005
6 */
8 #include <stddef.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <time.h>
14 // #include <errno.h>
16 #define XSTR(x) #x
17 #define STR(x) XSTR(x)
19 typedef struct tm tm_t;
20 int verbose;
22 const static struct test_list {
23 const char *buf;
24 const char *fmt;
25 int len;
26 tm_t want;
27 } std_tests[] = {
28 { "mon", "%a", 3, { .tm_wday = 1 } },
29 { "tueSDay", "%A", 7, { .tm_wday = 2 } },
30 { "september", "%b", 9, { .tm_mon = 8 } },
31 { "septembe", "%B", 3, { .tm_mon = 8 } },
32 { "Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y", 24,
33 { .tm_wday = 5, .tm_mon=2, .tm_mday = 4, .tm_hour=20,
34 .tm_min=5, .tm_sec=34, .tm_year=105 } },
36 #if 1
37 /* The pre-1.23 versions get some of these wrong! */
38 { "5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y", 21,
39 { .tm_wday = 5, .tm_mon=2, .tm_mday = 4, .tm_hour=20,
40 .tm_min=5, .tm_sec=34, .tm_year=105 } },
41 #endif
42 { "Fri Mar 4 20:05:34 2005", "%c", 24,
43 { .tm_wday = 5, .tm_mon=2, .tm_mday = 4, .tm_hour=20,
44 .tm_min=5, .tm_sec=34, .tm_year=105 } },
45 { "x20y", "x%Cy", 4, { .tm_year = 100 } },
46 { "x84y", "x%yy", 4, { .tm_year = 84 } },
47 { "x2084y", "x%C%yy", 6, { .tm_year = 184 } },
48 { "x8420y", "x%y%Cy", 6, { .tm_year = 184 } },
49 { "%20845", "%%%C%y5", 6, { .tm_year = 184 } },
50 { "%", "%E%", -1 },
52 { "1980", "%Y", 4, { .tm_year = 80 } },
53 { "1980", "%EY", 4, { .tm_year = 80 } },
54 { "sunday", "%A", 6 },
55 { "sunday", "%EA", -1 },
56 { "SaturDay", "%A", 8, { .tm_wday = 6 } },
57 { "SaturDay", "%OA", -1 },
59 { "0", "%S", 1 },
60 { "59", "%S", 2, { .tm_sec = 59 } },
61 { "60", "%S", 2, { .tm_sec = 60 } },
62 { "61", "%S", 2, { .tm_sec = 61 } },
63 { "62", "%S", -1 },
64 { 0 },
68 static void
69 usage(void)
71 fprintf(stderr, "usage: strptimetest [-v] [[-Ssec] [-Mmin] [-Hhour] [-dmday] [-mmon] [-Yyear] [-uwday] [-jyday] buffer format length]\n");
72 exit(1);
75 static void
76 mismatch(const char *buf, const char *fmt, int len,
77 const char *tm_x, int got_tm_x, int want_tm_x)
79 fprintf(stderr,
80 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed %s %d != %d\n",
81 len, buf, buf + len, fmt, tm_x, got_tm_x, want_tm_x);
84 static int
85 test(const char *buf, const char *fmt, int len, const tm_t *want)
87 tm_t got;
88 const char *np;
90 if (verbose)
91 printf("strptime(\"%.*s\" \"%s\", \"%s\", ...)\n",
92 len, buf, buf + len, fmt);
94 memset(&got, 0, sizeof got);
95 np = strptime(buf, fmt, &got);
96 if (!np) {
97 /* strptime failed */
98 if (len == -1)
99 return 0;
100 fprintf(stderr,
101 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed\n",
102 len, buf, buf + len, fmt);
103 return 2;
105 /* strptime ok */
106 if (len == -1) {
107 fprintf(stderr,
108 "strptime(\"%.*s\" \"%s\", \"%s\", ...) ok so failed\n",
109 (int)(np - buf), buf, np, fmt);
110 return 2;
112 if (np - buf != len) {
113 fprintf(stderr,
114 "strptime(\"%.*s\" \"%s\", \"%s\", ...) failed len %d\n",
115 (int)(np - buf), buf, np, fmt, len);
116 return 2;
119 #define CHECK(tm_x) \
120 if (got.tm_x != want->tm_x) { \
121 mismatch(buf, fmt, len, STR(tm_x), got.tm_x, want->tm_x); \
122 return 2; \
126 CHECK(tm_sec);
127 CHECK(tm_min);
128 CHECK(tm_hour);
129 CHECK(tm_mday);
130 CHECK(tm_mon);
131 CHECK(tm_year);
132 CHECK(tm_wday);
133 CHECK(tm_yday);
134 #undef CHECK
135 return 0;
138 static int
139 std_test(void)
141 const struct test_list *t;
142 int rval;
144 for (t = std_tests; t->fmt; t++) {
145 rval = test(t->buf, t->fmt, t->len, &t->want);
146 if (rval != 0)
147 return rval;
149 return 0;
152 static int
153 argint(void)
155 char *ep;
156 int v;
158 v = strtoul(optarg, &ep, 0);
159 if (*ep || ep == optarg)
160 usage();
161 return v;
165 main(int argc, char *argv[])
167 tm_t tm_want;
169 memset(&tm_want, 0, sizeof tm_want);
170 for (;;) {
171 switch (getopt(argc, argv, "S:M:H:d:m:Y:u:j:v")) {
172 default:
173 usage();
174 case -1:
175 break;
176 case 'v':
177 verbose++;
178 continue;
179 case 'S': tm_want.tm_sec = argint(); continue;
180 case 'M': tm_want.tm_min = argint(); continue;
181 case 'H': tm_want.tm_hour = argint(); continue;
182 case 'd': tm_want.tm_mday = argint(); continue;
183 case 'm': tm_want.tm_mon = argint(); continue;
184 case 'Y': tm_want.tm_year = argint() - 1900; continue;
185 case 'u': tm_want.tm_wday = argint(); continue;
186 case 'j': tm_want.tm_yday = argint(); continue;
188 break;
190 argv += optind;
191 argc -= optind;
193 if (argc <= 1)
194 return std_test();
196 if (argc != 3)
197 usage();
198 return test(argv[0], argv[1], atoi(argv[2]), &tm_want);