strpftime-test.c revision 259065
1198956Srrs/*
2198956Srrs * Copyright (c) 1999 Kungliga Tekniska H��gskolan
3198956Srrs * (Royal Institute of Technology, Stockholm, Sweden).
4198956Srrs * All rights reserved.
5198956Srrs *
6198956Srrs * Redistribution and use in source and binary forms, with or without
7198956Srrs * modification, are permitted provided that the following conditions
8198956Srrs * are met:
9198956Srrs *
10198956Srrs * 1. Redistributions of source code must retain the above copyright
11198956Srrs *    notice, this list of conditions and the following disclaimer.
12198956Srrs *
13198956Srrs * 2. Redistributions in binary form must reproduce the above copyright
14198956Srrs *    notice, this list of conditions and the following disclaimer in the
15198956Srrs *    documentation and/or other materials provided with the distribution.
16198956Srrs *
17198956Srrs * 3. Neither the name of KTH nor the names of its contributors may be
18198956Srrs *    used to endorse or promote products derived from this software without
19198956Srrs *    specific prior written permission.
20198956Srrs *
21198956Srrs * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22198956Srrs * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23198956Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24198956Srrs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25198956Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26198956Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27198956Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28198956Srrs * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29198956Srrs * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30198956Srrs * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31198956Srrs * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32198956Srrs
33198956Srrs#include <config.h>
34198956Srrs#include <roken.h>
35204131Srrs#ifdef TEST_STRPFTIME
36198956Srrs#include "strpftime-test.h"
37198956Srrs#endif
38198956Srrs
39198956Srrsenum { MAXSIZE = 26 };
40198956Srrs
41198956Srrsstatic struct testcase {
42198956Srrs    time_t t;
43198956Srrs    struct {
44198956Srrs	const char *format;
45198956Srrs	const char *result;
46198956Srrs    } vals[MAXSIZE];
47198957Srrs} tests[] = {
48198957Srrs    {0,
49198957Srrs     {
50198957Srrs	 {"%A", "Thursday"},
51198956Srrs	 {"%a", "Thu"},
52198957Srrs	 {"%B", "January"},
53198957Srrs	 {"%b", "Jan"},
54198957Srrs	 {"%C", "19"},
55198956Srrs	 {"%d", "01"},
56198957Srrs	 {"%e", " 1"},
57198957Srrs	 {"%H", "00"},
58198957Srrs	 {"%I", "12"},
59198957Srrs	 {"%j", "001"},
60198957Srrs	 {"%k", " 0"},
61198956Srrs	 {"%l", "12"},
62198957Srrs	 {"%M", "00"},
63198957Srrs	 {"%m", "01"},
64198957Srrs	 {"%n", "\n"},
65198957Srrs	 {"%p", "AM"},
66198956Srrs	 {"%S", "00"},
67198957Srrs	 {"%t", "\t"},
68198957Srrs	 {"%w", "4"},
69198957Srrs	 {"%Y", "1970"},
70198957Srrs	 {"%y", "70"},
71198956Srrs	 {"%U", "00"},
72198957Srrs	 {"%W", "00"},
73198957Srrs	 {"%V", "01"},
74198957Srrs	 {"%%", "%"},
75198957Srrs	 {NULL, NULL}}
76198956Srrs    },
77198957Srrs    {90000,
78198957Srrs     {
79198957Srrs	 {"%A", "Friday"},
80198957Srrs	 {"%a", "Fri"},
81198957Srrs	 {"%B", "January"},
82198956Srrs	 {"%b", "Jan"},
83198957Srrs	 {"%C", "19"},
84198957Srrs	 {"%d", "02"},
85198957Srrs	 {"%e", " 2"},
86198957Srrs	 {"%H", "01"},
87198957Srrs	 {"%I", "01"},
88198956Srrs	 {"%j", "002"},
89198957Srrs	 {"%k", " 1"},
90198957Srrs	 {"%l", " 1"},
91198957Srrs	 {"%M", "00"},
92198957Srrs	 {"%m", "01"},
93198957Srrs	 {"%n", "\n"},
94198956Srrs	 {"%p", "AM"},
95198957Srrs	 {"%S", "00"},
96198957Srrs	 {"%t", "\t"},
97198957Srrs	 {"%w", "5"},
98198957Srrs	 {"%Y", "1970"},
99198957Srrs	 {"%y", "70"},
100198956Srrs	 {"%U", "00"},
101198957Srrs	 {"%W", "00"},
102198957Srrs	 {"%V", "01"},
103198957Srrs	 {"%%", "%"},
104198957Srrs	 {NULL, NULL}
105198957Srrs     }
106198956Srrs    },
107198957Srrs    {216306,
108198957Srrs     {
109198957Srrs	 {"%A", "Saturday"},
110198957Srrs	 {"%a", "Sat"},
111198957Srrs	 {"%B", "January"},
112198956Srrs	 {"%b", "Jan"},
113198957Srrs	 {"%C", "19"},
114198957Srrs	 {"%d", "03"},
115198957Srrs	 {"%e", " 3"},
116198957Srrs	 {"%H", "12"},
117198956Srrs	 {"%I", "12"},
118198957Srrs	 {"%j", "003"},
119198957Srrs	 {"%k", "12"},
120198957Srrs	 {"%l", "12"},
121198957Srrs	 {"%M", "05"},
122198956Srrs	 {"%m", "01"},
123198957Srrs	 {"%n", "\n"},
124198957Srrs	 {"%p", "PM"},
125198957Srrs	 {"%S", "06"},
126198957Srrs	 {"%t", "\t"},
127198956Srrs	 {"%w", "6"},
128198957Srrs	 {"%Y", "1970"},
129198957Srrs	 {"%y", "70"},
130198957Srrs	 {"%U", "00"},
131198957Srrs	 {"%W", "00"},
132198957Srrs	 {"%V", "01"},
133198957Srrs	 {"%%", "%"},
134204131Srrs	 {NULL, NULL}
135198957Srrs     }
136198957Srrs    },
137198957Srrs    {259200,
138198957Srrs     {
139198957Srrs	 {"%A", "Sunday"},
140198957Srrs	 {"%a", "Sun"},
141198956Srrs	 {"%B", "January"},
142198957Srrs	 {"%b", "Jan"},
143198957Srrs	 {"%C", "19"},
144198957Srrs	 {"%d", "04"},
145198957Srrs	 {"%e", " 4"},
146198957Srrs	 {"%H", "00"},
147198957Srrs	 {"%I", "12"},
148198956Srrs	 {"%j", "004"},
149198957Srrs	 {"%k", " 0"},
150198957Srrs	 {"%l", "12"},
151198957Srrs	 {"%M", "00"},
152198957Srrs	 {"%m", "01"},
153198957Srrs	 {"%n", "\n"},
154198957Srrs	 {"%p", "AM"},
155198956Srrs	 {"%S", "00"},
156198957Srrs	 {"%t", "\t"},
157198957Srrs	 {"%w", "0"},
158198957Srrs	 {"%Y", "1970"},
159198957Srrs	 {"%y", "70"},
160198957Srrs	 {"%U", "01"},
161198957Srrs	 {"%W", "00"},
162198956Srrs	 {"%V", "01"},
163198956Srrs	 {"%%", "%"},
164198957Srrs	 {NULL, NULL}
165198957Srrs     }
166198957Srrs    },
167198957Srrs    {915148800,
168198957Srrs     {
169198957Srrs	 {"%A", "Friday"},
170198957Srrs	 {"%a", "Fri"},
171198957Srrs	 {"%B", "January"},
172198957Srrs	 {"%b", "Jan"},
173198957Srrs	 {"%C", "19"},
174198956Srrs	 {"%d", "01"},
175198957Srrs	 {"%e", " 1"},
176198957Srrs	 {"%H", "00"},
177198957Srrs	 {"%I", "12"},
178198956Srrs	 {"%j", "001"},
179198957Srrs	 {"%k", " 0"},
180198957Srrs	 {"%l", "12"},
181198957Srrs	 {"%M", "00"},
182198957Srrs	 {"%m", "01"},
183198957Srrs	 {"%n", "\n"},
184198957Srrs	 {"%p", "AM"},
185198956Srrs	 {"%S", "00"},
186198957Srrs	 {"%t", "\t"},
187198957Srrs	 {"%w", "5"},
188198957Srrs	 {"%Y", "1999"},
189198956Srrs	 {"%y", "99"},
190198957Srrs	 {"%U", "00"},
191198957Srrs	 {"%W", "00"},
192198957Srrs	 {"%V", "53"},
193198956Srrs	 {"%%", "%"},
194198957Srrs	 {NULL, NULL}}
195198957Srrs    },
196198957Srrs    {942161105,
197198957Srrs     {
198198957Srrs
199198957Srrs	 {"%A", "Tuesday"},
200198957Srrs	 {"%a", "Tue"},
201198957Srrs	 {"%B", "November"},
202198956Srrs	 {"%b", "Nov"},
203198957Srrs	 {"%C", "19"},
204198957Srrs	 {"%d", "09"},
205198957Srrs	 {"%e", " 9"},
206198957Srrs	 {"%H", "15"},
207198957Srrs	 {"%I", "03"},
208198956Srrs	 {"%j", "313"},
209198957Srrs	 {"%k", "15"},
210198957Srrs	 {"%l", " 3"},
211198957Srrs	 {"%M", "25"},
212198957Srrs	 {"%m", "11"},
213198957Srrs	 {"%n", "\n"},
214198956Srrs	 {"%p", "PM"},
215198957Srrs	 {"%S", "05"},
216198957Srrs	 {"%t", "\t"},
217198957Srrs	 {"%w", "2"},
218198957Srrs	 {"%Y", "1999"},
219198957Srrs	 {"%y", "99"},
220198957Srrs	 {"%U", "45"},
221198956Srrs	 {"%W", "45"},
222198957Srrs	 {"%V", "45"},
223198957Srrs	 {"%%", "%"},
224198957Srrs	 {NULL, NULL}
225198956Srrs     }
226198957Srrs    }
227198957Srrs};
228198957Srrs
229198957Srrsint
230198957Srrsmain(int argc, char **argv)
231198957Srrs{
232198957Srrs    int i, j;
233198957Srrs    int ret = 0;
234198957Srrs
235198957Srrs    for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
236198957Srrs	struct tm *tm;
237198957Srrs
238198956Srrs	tm = gmtime (&tests[i].t);
239198957Srrs
240198957Srrs	for (j = 0; tests[i].vals[j].format != NULL; ++j) {
241198957Srrs	    char buf[128];
242198957Srrs	    size_t len;
243198957Srrs	    struct tm tm2;
244198957Srrs	    char *ptr;
245198956Srrs
246204131Srrs	    len = strftime (buf, sizeof(buf), tests[i].vals[j].format, tm);
247204131Srrs	    if (len != strlen (buf)) {
248198956Srrs		printf ("length of strftime(\"%s\") = %lu (\"%s\")\n",
249198956Srrs			tests[i].vals[j].format, (unsigned long)len,
250198957Srrs			buf);
251198956Srrs		++ret;
252198956Srrs		continue;
253198956Srrs	    }
254198956Srrs	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
255198956Srrs		printf ("result of strftime(\"%s\") = \"%s\" != \"%s\"\n",
256198956Srrs			tests[i].vals[j].format, buf,
257198956Srrs			tests[i].vals[j].result);
258198956Srrs		++ret;
259198956Srrs		continue;
260198956Srrs	    }
261198956Srrs	    memset (&tm2, 0, sizeof(tm2));
262198957Srrs	    ptr = strptime (tests[i].vals[j].result,
263198956Srrs			    tests[i].vals[j].format,
264198956Srrs			    &tm2);
265198956Srrs	    if (ptr == NULL || *ptr != '\0') {
266198956Srrs		printf ("bad return value from strptime("
267198956Srrs			"\"%s\", \"%s\")\n",
268198956Srrs			tests[i].vals[j].result,
269198956Srrs			tests[i].vals[j].format);
270198956Srrs		++ret;
271198956Srrs	    }
272198956Srrs	    strftime (buf, sizeof(buf), tests[i].vals[j].format, &tm2);
273198956Srrs	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
274198956Srrs		printf ("reverse of \"%s\" failed: \"%s\" vs \"%s\"\n",
275198956Srrs			tests[i].vals[j].format,
276198956Srrs			buf, tests[i].vals[j].result);
277198956Srrs		++ret;
278198956Srrs	    }
279198956Srrs	}
280198956Srrs    }
281198956Srrs    {
282198956Srrs	struct tm tm;
283198956Srrs	memset(&tm, 0, sizeof(tm));
284198956Srrs	strptime ("200505", "%Y%m", &tm);
285198956Srrs	if (tm.tm_year != 105)
286198956Srrs	    ++ret;
287198956Srrs	if (tm.tm_mon != 4)
288198956Srrs	    ++ret;
289198956Srrs    }
290198956Srrs    if (ret) {
291198956Srrs	printf ("%d errors\n", ret);
292198956Srrs	return 1;
293198956Srrs    } else
294198956Srrs	return 0;
295198956Srrs}
296198956Srrs