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