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