xref: /minix3/crypto/external/bsd/heimdal/dist/lib/roken/strpftime-test.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: strpftime-test.c,v 1.1.1.1 2011/04/13 18:15:43 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 1999 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of KTH nor the names of its contributors may be
20*ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21*ebfedea0SLionel Sambuc  *    specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24*ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27*ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30*ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31*ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32*ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33*ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34*ebfedea0SLionel Sambuc 
35*ebfedea0SLionel Sambuc #include <config.h>
36*ebfedea0SLionel Sambuc #include <krb5/roken.h>
37*ebfedea0SLionel Sambuc #ifdef TEST_STRPFTIME
38*ebfedea0SLionel Sambuc #include "strpftime-test.h"
39*ebfedea0SLionel Sambuc #endif
40*ebfedea0SLionel Sambuc 
41*ebfedea0SLionel Sambuc enum { MAXSIZE = 26 };
42*ebfedea0SLionel Sambuc 
43*ebfedea0SLionel Sambuc static struct testcase {
44*ebfedea0SLionel Sambuc     time_t t;
45*ebfedea0SLionel Sambuc     struct {
46*ebfedea0SLionel Sambuc 	const char *format;
47*ebfedea0SLionel Sambuc 	const char *result;
48*ebfedea0SLionel Sambuc     } vals[MAXSIZE];
49*ebfedea0SLionel Sambuc } tests[] = {
50*ebfedea0SLionel Sambuc     {0,
51*ebfedea0SLionel Sambuc      {
52*ebfedea0SLionel Sambuc 	 {"%A", "Thursday"},
53*ebfedea0SLionel Sambuc 	 {"%a", "Thu"},
54*ebfedea0SLionel Sambuc 	 {"%B", "January"},
55*ebfedea0SLionel Sambuc 	 {"%b", "Jan"},
56*ebfedea0SLionel Sambuc 	 {"%C", "19"},
57*ebfedea0SLionel Sambuc 	 {"%d", "01"},
58*ebfedea0SLionel Sambuc 	 {"%e", " 1"},
59*ebfedea0SLionel Sambuc 	 {"%H", "00"},
60*ebfedea0SLionel Sambuc 	 {"%I", "12"},
61*ebfedea0SLionel Sambuc 	 {"%j", "001"},
62*ebfedea0SLionel Sambuc 	 {"%k", " 0"},
63*ebfedea0SLionel Sambuc 	 {"%l", "12"},
64*ebfedea0SLionel Sambuc 	 {"%M", "00"},
65*ebfedea0SLionel Sambuc 	 {"%m", "01"},
66*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
67*ebfedea0SLionel Sambuc 	 {"%p", "AM"},
68*ebfedea0SLionel Sambuc 	 {"%S", "00"},
69*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
70*ebfedea0SLionel Sambuc 	 {"%w", "4"},
71*ebfedea0SLionel Sambuc 	 {"%Y", "1970"},
72*ebfedea0SLionel Sambuc 	 {"%y", "70"},
73*ebfedea0SLionel Sambuc 	 {"%U", "00"},
74*ebfedea0SLionel Sambuc 	 {"%W", "00"},
75*ebfedea0SLionel Sambuc 	 {"%V", "01"},
76*ebfedea0SLionel Sambuc 	 {"%%", "%"},
77*ebfedea0SLionel Sambuc 	 {NULL, NULL}}
78*ebfedea0SLionel Sambuc     },
79*ebfedea0SLionel Sambuc     {90000,
80*ebfedea0SLionel Sambuc      {
81*ebfedea0SLionel Sambuc 	 {"%A", "Friday"},
82*ebfedea0SLionel Sambuc 	 {"%a", "Fri"},
83*ebfedea0SLionel Sambuc 	 {"%B", "January"},
84*ebfedea0SLionel Sambuc 	 {"%b", "Jan"},
85*ebfedea0SLionel Sambuc 	 {"%C", "19"},
86*ebfedea0SLionel Sambuc 	 {"%d", "02"},
87*ebfedea0SLionel Sambuc 	 {"%e", " 2"},
88*ebfedea0SLionel Sambuc 	 {"%H", "01"},
89*ebfedea0SLionel Sambuc 	 {"%I", "01"},
90*ebfedea0SLionel Sambuc 	 {"%j", "002"},
91*ebfedea0SLionel Sambuc 	 {"%k", " 1"},
92*ebfedea0SLionel Sambuc 	 {"%l", " 1"},
93*ebfedea0SLionel Sambuc 	 {"%M", "00"},
94*ebfedea0SLionel Sambuc 	 {"%m", "01"},
95*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
96*ebfedea0SLionel Sambuc 	 {"%p", "AM"},
97*ebfedea0SLionel Sambuc 	 {"%S", "00"},
98*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
99*ebfedea0SLionel Sambuc 	 {"%w", "5"},
100*ebfedea0SLionel Sambuc 	 {"%Y", "1970"},
101*ebfedea0SLionel Sambuc 	 {"%y", "70"},
102*ebfedea0SLionel Sambuc 	 {"%U", "00"},
103*ebfedea0SLionel Sambuc 	 {"%W", "00"},
104*ebfedea0SLionel Sambuc 	 {"%V", "01"},
105*ebfedea0SLionel Sambuc 	 {"%%", "%"},
106*ebfedea0SLionel Sambuc 	 {NULL, NULL}
107*ebfedea0SLionel Sambuc      }
108*ebfedea0SLionel Sambuc     },
109*ebfedea0SLionel Sambuc     {216306,
110*ebfedea0SLionel Sambuc      {
111*ebfedea0SLionel Sambuc 	 {"%A", "Saturday"},
112*ebfedea0SLionel Sambuc 	 {"%a", "Sat"},
113*ebfedea0SLionel Sambuc 	 {"%B", "January"},
114*ebfedea0SLionel Sambuc 	 {"%b", "Jan"},
115*ebfedea0SLionel Sambuc 	 {"%C", "19"},
116*ebfedea0SLionel Sambuc 	 {"%d", "03"},
117*ebfedea0SLionel Sambuc 	 {"%e", " 3"},
118*ebfedea0SLionel Sambuc 	 {"%H", "12"},
119*ebfedea0SLionel Sambuc 	 {"%I", "12"},
120*ebfedea0SLionel Sambuc 	 {"%j", "003"},
121*ebfedea0SLionel Sambuc 	 {"%k", "12"},
122*ebfedea0SLionel Sambuc 	 {"%l", "12"},
123*ebfedea0SLionel Sambuc 	 {"%M", "05"},
124*ebfedea0SLionel Sambuc 	 {"%m", "01"},
125*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
126*ebfedea0SLionel Sambuc 	 {"%p", "PM"},
127*ebfedea0SLionel Sambuc 	 {"%S", "06"},
128*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
129*ebfedea0SLionel Sambuc 	 {"%w", "6"},
130*ebfedea0SLionel Sambuc 	 {"%Y", "1970"},
131*ebfedea0SLionel Sambuc 	 {"%y", "70"},
132*ebfedea0SLionel Sambuc 	 {"%U", "00"},
133*ebfedea0SLionel Sambuc 	 {"%W", "00"},
134*ebfedea0SLionel Sambuc 	 {"%V", "01"},
135*ebfedea0SLionel Sambuc 	 {"%%", "%"},
136*ebfedea0SLionel Sambuc 	 {NULL, NULL}
137*ebfedea0SLionel Sambuc      }
138*ebfedea0SLionel Sambuc     },
139*ebfedea0SLionel Sambuc     {259200,
140*ebfedea0SLionel Sambuc      {
141*ebfedea0SLionel Sambuc 	 {"%A", "Sunday"},
142*ebfedea0SLionel Sambuc 	 {"%a", "Sun"},
143*ebfedea0SLionel Sambuc 	 {"%B", "January"},
144*ebfedea0SLionel Sambuc 	 {"%b", "Jan"},
145*ebfedea0SLionel Sambuc 	 {"%C", "19"},
146*ebfedea0SLionel Sambuc 	 {"%d", "04"},
147*ebfedea0SLionel Sambuc 	 {"%e", " 4"},
148*ebfedea0SLionel Sambuc 	 {"%H", "00"},
149*ebfedea0SLionel Sambuc 	 {"%I", "12"},
150*ebfedea0SLionel Sambuc 	 {"%j", "004"},
151*ebfedea0SLionel Sambuc 	 {"%k", " 0"},
152*ebfedea0SLionel Sambuc 	 {"%l", "12"},
153*ebfedea0SLionel Sambuc 	 {"%M", "00"},
154*ebfedea0SLionel Sambuc 	 {"%m", "01"},
155*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
156*ebfedea0SLionel Sambuc 	 {"%p", "AM"},
157*ebfedea0SLionel Sambuc 	 {"%S", "00"},
158*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
159*ebfedea0SLionel Sambuc 	 {"%w", "0"},
160*ebfedea0SLionel Sambuc 	 {"%Y", "1970"},
161*ebfedea0SLionel Sambuc 	 {"%y", "70"},
162*ebfedea0SLionel Sambuc 	 {"%U", "01"},
163*ebfedea0SLionel Sambuc 	 {"%W", "00"},
164*ebfedea0SLionel Sambuc 	 {"%V", "01"},
165*ebfedea0SLionel Sambuc 	 {"%%", "%"},
166*ebfedea0SLionel Sambuc 	 {NULL, NULL}
167*ebfedea0SLionel Sambuc      }
168*ebfedea0SLionel Sambuc     },
169*ebfedea0SLionel Sambuc     {915148800,
170*ebfedea0SLionel Sambuc      {
171*ebfedea0SLionel Sambuc 	 {"%A", "Friday"},
172*ebfedea0SLionel Sambuc 	 {"%a", "Fri"},
173*ebfedea0SLionel Sambuc 	 {"%B", "January"},
174*ebfedea0SLionel Sambuc 	 {"%b", "Jan"},
175*ebfedea0SLionel Sambuc 	 {"%C", "19"},
176*ebfedea0SLionel Sambuc 	 {"%d", "01"},
177*ebfedea0SLionel Sambuc 	 {"%e", " 1"},
178*ebfedea0SLionel Sambuc 	 {"%H", "00"},
179*ebfedea0SLionel Sambuc 	 {"%I", "12"},
180*ebfedea0SLionel Sambuc 	 {"%j", "001"},
181*ebfedea0SLionel Sambuc 	 {"%k", " 0"},
182*ebfedea0SLionel Sambuc 	 {"%l", "12"},
183*ebfedea0SLionel Sambuc 	 {"%M", "00"},
184*ebfedea0SLionel Sambuc 	 {"%m", "01"},
185*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
186*ebfedea0SLionel Sambuc 	 {"%p", "AM"},
187*ebfedea0SLionel Sambuc 	 {"%S", "00"},
188*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
189*ebfedea0SLionel Sambuc 	 {"%w", "5"},
190*ebfedea0SLionel Sambuc 	 {"%Y", "1999"},
191*ebfedea0SLionel Sambuc 	 {"%y", "99"},
192*ebfedea0SLionel Sambuc 	 {"%U", "00"},
193*ebfedea0SLionel Sambuc 	 {"%W", "00"},
194*ebfedea0SLionel Sambuc 	 {"%V", "53"},
195*ebfedea0SLionel Sambuc 	 {"%%", "%"},
196*ebfedea0SLionel Sambuc 	 {NULL, NULL}}
197*ebfedea0SLionel Sambuc     },
198*ebfedea0SLionel Sambuc     {942161105,
199*ebfedea0SLionel Sambuc      {
200*ebfedea0SLionel Sambuc 
201*ebfedea0SLionel Sambuc 	 {"%A", "Tuesday"},
202*ebfedea0SLionel Sambuc 	 {"%a", "Tue"},
203*ebfedea0SLionel Sambuc 	 {"%B", "November"},
204*ebfedea0SLionel Sambuc 	 {"%b", "Nov"},
205*ebfedea0SLionel Sambuc 	 {"%C", "19"},
206*ebfedea0SLionel Sambuc 	 {"%d", "09"},
207*ebfedea0SLionel Sambuc 	 {"%e", " 9"},
208*ebfedea0SLionel Sambuc 	 {"%H", "15"},
209*ebfedea0SLionel Sambuc 	 {"%I", "03"},
210*ebfedea0SLionel Sambuc 	 {"%j", "313"},
211*ebfedea0SLionel Sambuc 	 {"%k", "15"},
212*ebfedea0SLionel Sambuc 	 {"%l", " 3"},
213*ebfedea0SLionel Sambuc 	 {"%M", "25"},
214*ebfedea0SLionel Sambuc 	 {"%m", "11"},
215*ebfedea0SLionel Sambuc 	 {"%n", "\n"},
216*ebfedea0SLionel Sambuc 	 {"%p", "PM"},
217*ebfedea0SLionel Sambuc 	 {"%S", "05"},
218*ebfedea0SLionel Sambuc 	 {"%t", "\t"},
219*ebfedea0SLionel Sambuc 	 {"%w", "2"},
220*ebfedea0SLionel Sambuc 	 {"%Y", "1999"},
221*ebfedea0SLionel Sambuc 	 {"%y", "99"},
222*ebfedea0SLionel Sambuc 	 {"%U", "45"},
223*ebfedea0SLionel Sambuc 	 {"%W", "45"},
224*ebfedea0SLionel Sambuc 	 {"%V", "45"},
225*ebfedea0SLionel Sambuc 	 {"%%", "%"},
226*ebfedea0SLionel Sambuc 	 {NULL, NULL}
227*ebfedea0SLionel Sambuc      }
228*ebfedea0SLionel Sambuc     }
229*ebfedea0SLionel Sambuc };
230*ebfedea0SLionel Sambuc 
231*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)232*ebfedea0SLionel Sambuc main(int argc, char **argv)
233*ebfedea0SLionel Sambuc {
234*ebfedea0SLionel Sambuc     int i, j;
235*ebfedea0SLionel Sambuc     int ret = 0;
236*ebfedea0SLionel Sambuc 
237*ebfedea0SLionel Sambuc     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
238*ebfedea0SLionel Sambuc 	struct tm *tm;
239*ebfedea0SLionel Sambuc 
240*ebfedea0SLionel Sambuc 	tm = gmtime (&tests[i].t);
241*ebfedea0SLionel Sambuc 
242*ebfedea0SLionel Sambuc 	for (j = 0; tests[i].vals[j].format != NULL; ++j) {
243*ebfedea0SLionel Sambuc 	    char buf[128];
244*ebfedea0SLionel Sambuc 	    size_t len;
245*ebfedea0SLionel Sambuc 	    struct tm tm2;
246*ebfedea0SLionel Sambuc 	    char *ptr;
247*ebfedea0SLionel Sambuc 
248*ebfedea0SLionel Sambuc 	    len = strftime (buf, sizeof(buf), tests[i].vals[j].format, tm);
249*ebfedea0SLionel Sambuc 	    if (len != strlen (buf)) {
250*ebfedea0SLionel Sambuc 		printf ("length of strftime(\"%s\") = %lu (\"%s\")\n",
251*ebfedea0SLionel Sambuc 			tests[i].vals[j].format, (unsigned long)len,
252*ebfedea0SLionel Sambuc 			buf);
253*ebfedea0SLionel Sambuc 		++ret;
254*ebfedea0SLionel Sambuc 		continue;
255*ebfedea0SLionel Sambuc 	    }
256*ebfedea0SLionel Sambuc 	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
257*ebfedea0SLionel Sambuc 		printf ("result of strftime(\"%s\") = \"%s\" != \"%s\"\n",
258*ebfedea0SLionel Sambuc 			tests[i].vals[j].format, buf,
259*ebfedea0SLionel Sambuc 			tests[i].vals[j].result);
260*ebfedea0SLionel Sambuc 		++ret;
261*ebfedea0SLionel Sambuc 		continue;
262*ebfedea0SLionel Sambuc 	    }
263*ebfedea0SLionel Sambuc 	    memset (&tm2, 0, sizeof(tm2));
264*ebfedea0SLionel Sambuc 	    ptr = strptime (tests[i].vals[j].result,
265*ebfedea0SLionel Sambuc 			    tests[i].vals[j].format,
266*ebfedea0SLionel Sambuc 			    &tm2);
267*ebfedea0SLionel Sambuc 	    if (ptr == NULL || *ptr != '\0') {
268*ebfedea0SLionel Sambuc 		printf ("bad return value from strptime("
269*ebfedea0SLionel Sambuc 			"\"%s\", \"%s\")\n",
270*ebfedea0SLionel Sambuc 			tests[i].vals[j].result,
271*ebfedea0SLionel Sambuc 			tests[i].vals[j].format);
272*ebfedea0SLionel Sambuc 		++ret;
273*ebfedea0SLionel Sambuc 	    }
274*ebfedea0SLionel Sambuc 	    strftime (buf, sizeof(buf), tests[i].vals[j].format, &tm2);
275*ebfedea0SLionel Sambuc 	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
276*ebfedea0SLionel Sambuc 		printf ("reverse of \"%s\" failed: \"%s\" vs \"%s\"\n",
277*ebfedea0SLionel Sambuc 			tests[i].vals[j].format,
278*ebfedea0SLionel Sambuc 			buf, tests[i].vals[j].result);
279*ebfedea0SLionel Sambuc 		++ret;
280*ebfedea0SLionel Sambuc 	    }
281*ebfedea0SLionel Sambuc 	}
282*ebfedea0SLionel Sambuc     }
283*ebfedea0SLionel Sambuc     {
284*ebfedea0SLionel Sambuc 	struct tm tm;
285*ebfedea0SLionel Sambuc 	memset(&tm, 0, sizeof(tm));
286*ebfedea0SLionel Sambuc 	strptime ("200505", "%Y%m", &tm);
287*ebfedea0SLionel Sambuc 	if (tm.tm_year != 105)
288*ebfedea0SLionel Sambuc 	    ++ret;
289*ebfedea0SLionel Sambuc 	if (tm.tm_mon != 4)
290*ebfedea0SLionel Sambuc 	    ++ret;
291*ebfedea0SLionel Sambuc     }
292*ebfedea0SLionel Sambuc     if (ret) {
293*ebfedea0SLionel Sambuc 	printf ("%d errors\n", ret);
294*ebfedea0SLionel Sambuc 	return 1;
295*ebfedea0SLionel Sambuc     } else
296*ebfedea0SLionel Sambuc 	return 0;
297*ebfedea0SLionel Sambuc }
298