xref: /freebsd/contrib/netbsd-tests/lib/libc/time/t_strptime.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /* $NetBSD: t_strptime.c,v 1.1 2011/01/13 00:14:10 pgoyette Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by David Laight.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
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  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_strptime.c,v 1.1 2011/01/13 00:14:10 pgoyette Exp $");
36 
37 #include <time.h>
38 
39 #include <atf-c.h>
40 
41 static void
42 h_pass(const char *buf, const char *fmt, int len,
43     int tm_sec, int tm_min, int tm_hour, int tm_mday,
44     int tm_mon, int tm_year, int tm_wday, int tm_yday)
45 {
46 	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
47 	const char *ret, *exp;
48 
49 	exp = buf + len;
50 	ret = strptime(buf, fmt, &tm);
51 
52 #ifdef __FreeBSD__
53 	ATF_CHECK_MSG(ret == exp,
54 	    "strptime(\"%s\", \"%s\", tm): incorrect return code: "
55 	    "expected: %p, got: %p", buf, fmt, exp, ret);
56 
57 #define H_REQUIRE_FIELD(field)						\
58 		ATF_CHECK_MSG(tm.field == field,			\
59 		    "strptime(\"%s\", \"%s\", tm): incorrect %s: "	\
60 		    "expected: %d, but got: %d", buf, fmt,		\
61 		    ___STRING(field), field, tm.field)
62 #else
63 	ATF_REQUIRE_MSG(ret == exp,
64 	    "strptime(\"%s\", \"%s\", tm): incorrect return code: "
65 	    "expected: %p, got: %p", buf, fmt, exp, ret);
66 
67 #define H_REQUIRE_FIELD(field)						\
68 		ATF_REQUIRE_MSG(tm.field == field,			\
69 		    "strptime(\"%s\", \"%s\", tm): incorrect %s: "	\
70 		    "expected: %d, but got: %d", buf, fmt,		\
71 		    ___STRING(field), field, tm.field)
72 #endif
73 
74 	H_REQUIRE_FIELD(tm_sec);
75 	H_REQUIRE_FIELD(tm_min);
76 	H_REQUIRE_FIELD(tm_hour);
77 	H_REQUIRE_FIELD(tm_mday);
78 	H_REQUIRE_FIELD(tm_mon);
79 	H_REQUIRE_FIELD(tm_year);
80 	H_REQUIRE_FIELD(tm_wday);
81 	H_REQUIRE_FIELD(tm_yday);
82 
83 #undef H_REQUIRE_FIELD
84 }
85 
86 static void
87 h_fail(const char *buf, const char *fmt)
88 {
89 	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
90 
91 #ifdef __FreeBSD__
92 	ATF_CHECK_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
93 	    "\"%s\", &tm) should fail, but it didn't", buf, fmt);
94 #else
95 	ATF_REQUIRE_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
96 	    "\"%s\", &tm) should fail, but it didn't", buf, fmt);
97 #endif
98 }
99 
100 ATF_TC(common);
101 
102 ATF_TC_HEAD(common, tc)
103 {
104 
105 	atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
106 }
107 
108 ATF_TC_BODY(common, tc)
109 {
110 
111 #ifdef __FreeBSD__
112 	atf_tc_expect_fail("There are various issues with strptime on FreeBSD");
113 #endif
114 
115 	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
116 		24, 46, 27, 23, 20, 0, 98, 2, -1);
117 	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
118 		24, 46, 27, 23, 20, 0, 98, 2, -1);
119 	h_pass("Tue Jan 20 23:27:46 1998", "%c",
120 		24, 46, 27, 23, 20, 0, 98, 2, -1);
121 	h_pass("Fri Mar  4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
122 		24, 34, 5, 20, 4, 2, 105, 5, -1);
123 	h_pass("5\t3  4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
124 		21, 34, 5, 20, 4, 2, 105, 5, -1);
125 	h_pass("Fri Mar  4 20:05:34 2005", "%c",
126 		24, 34, 5, 20, 4, 2, 105, 5, -1);
127 
128 	h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
129 	h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
130 	h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
131 	h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
132 	h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
133 	h_fail("%", "%E%");
134 
135 	h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
136 	h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
137 
138 	h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
139 	h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
140 	h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
141 	h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
142 	h_fail("62", "%S");
143 }
144 
145 ATF_TC(day);
146 
147 ATF_TC_HEAD(day, tc)
148 {
149 
150 	atf_tc_set_md_var(tc, "descr", "Checks strptime(3): day names");
151 }
152 
153 ATF_TC_BODY(day, tc)
154 {
155 
156 	h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
157 	h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
158 	h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
159 	h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
160 	h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
161 	h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
162 	h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
163 	h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
164 	h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
165 	h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
166 	h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
167 	h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
168 	h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
169 	h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
170 	h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
171 	h_fail("Moon", "%a");
172 	h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
173 	h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
174 	h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
175 	h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
176 	h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
177 	h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
178 	h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
179 	h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
180 	h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
181 	h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
182 	h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
183 	h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
184 	h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
185 	h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
186 	h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
187 	h_fail("Moon", "%A");
188 
189 	h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
190 	h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
191 	h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
192 #ifdef __NetBSD__
193 	h_fail("sunday", "%EA");
194 #else
195 	h_pass("Sunday", "%EA", 6, -1, -1, -1, -1, -1, -1, 0, -1);
196 #endif
197 	h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
198 #ifdef __NetBSD__
199 	h_fail("SaturDay", "%OA");
200 #else
201 	h_pass("SaturDay", "%OA", 8, -1, -1, -1, -1, -1, -1, 6, -1);
202 #endif
203 }
204 
205 ATF_TC(month);
206 
207 ATF_TC_HEAD(month, tc)
208 {
209 
210 	atf_tc_set_md_var(tc, "descr", "Checks strptime(3): month names");
211 }
212 
213 ATF_TC_BODY(month, tc)
214 {
215 
216 	h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
217 	h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
218 	h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
219 	h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
220 	h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
221 	h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
222 	h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
223 	h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
224 	h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
225 	h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
226 	h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
227 	h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
228 	h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
229 	h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
230 	h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
231 	h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
232 	h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
233 	h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
234 	h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
235 	h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
236 	h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
237 	h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
238 	h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
239 	h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
240 	h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
241 	h_fail("Rover", "%b");
242 	h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
243 	h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
244 	h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
245 	h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
246 	h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
247 	h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
248 	h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
249 	h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
250 	h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
251 	h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
252 	h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
253 	h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
254 	h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
255 	h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
256 	h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
257 	h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
258 	h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
259 	h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
260 	h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
261 	h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
262 	h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
263 	h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
264 	h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
265 	h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
266 	h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
267 	h_fail("Rover", "%B");
268 
269 	h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
270 	h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
271 }
272 
273 ATF_TP_ADD_TCS(tp)
274 {
275 
276 	ATF_TP_ADD_TC(tp, common);
277 	ATF_TP_ADD_TC(tp, day);
278 	ATF_TP_ADD_TC(tp, month);
279 
280 	return atf_no_error();
281 }
282