xref: /freebsd/contrib/netbsd-tests/lib/libc/locale/t_wcstod.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /* $NetBSD: t_wcstod.c,v 1.3 2011/10/01 17:56:11 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c)2005 Citrus Project,
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  */
55 
56 #include <sys/cdefs.h>
57 __COPYRIGHT("@(#) Copyright (c) 2011\
58  The NetBSD Foundation, inc. All rights reserved.");
59 __RCSID("$NetBSD: t_wcstod.c,v 1.3 2011/10/01 17:56:11 christos Exp $");
60 
61 #include <errno.h>
62 #include <math.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <wchar.h>
66 
67 #include <atf-c.h>
68 
69 #define	ALT_HUGE_VAL		-1
70 #define	ALT_MINUS_HUGE_VAL	-2
71 #define	ALT_NAN			-3
72 
73 #if !defined(__vax__)
74 static struct test {
75 	const wchar_t *wcs;
76 	size_t len;
77 	double val;
78 	int err;
79 } tests[] = {
80 { L"IN",		0,	0,			0 },
81 { L"+IN",		0,	0,			0 },
82 { L"-IN",		0,	0,			0 },
83 { L"INX",		0,	0,			0 },
84 { L"+INX",		0,	0,			0 },
85 { L"-INX",		0,	0,			0 },
86 { L"INF",		3,	ALT_HUGE_VAL,		0 },
87 { L"+INF",		4,	ALT_HUGE_VAL,		0 },
88 { L"-INF",		4,	ALT_MINUS_HUGE_VAL,	0 },
89 { L"INFX",		3,	ALT_HUGE_VAL,		0 },
90 { L"+INFX",		4,	ALT_HUGE_VAL,		0 },
91 { L"-INFX",		4,	ALT_MINUS_HUGE_VAL,	0 },
92 { L"     IN",		0,	0,			0 },
93 { L"     +IN",		0,	0,			0 },
94 { L"     -IN",		0,	0,			0 },
95 { L"     INX",		0,	0,			0 },
96 { L"     +INX",		0,	0,			0 },
97 { L"     -INX",		0,	0,			0 },
98 { L"+     INF",		0,	0,			0 },
99 { L"-     INF",		0,	0,			0 },
100 { L"     INF",		8,	ALT_HUGE_VAL,		0 },
101 { L"     +INF",		9,	ALT_HUGE_VAL,		0 },
102 { L"     -INF",		9,	ALT_MINUS_HUGE_VAL,	0 },
103 { L"     INFX",		8,	ALT_HUGE_VAL,		0 },
104 { L"     +INFX",	9,	ALT_HUGE_VAL,		0 },
105 { L"     -INFX",	9,	ALT_MINUS_HUGE_VAL,	0 },
106 { L"     INFINIT",	8,	ALT_HUGE_VAL,		0 },
107 { L"     +INFINIT",	9,	ALT_HUGE_VAL,		0 },
108 { L"     -INFINIT",	9,	ALT_MINUS_HUGE_VAL,	0 },
109 { L"     INFINITY",	13,	ALT_HUGE_VAL,		0 },
110 { L"     +INFINITY",	14,	ALT_HUGE_VAL,		0 },
111 { L"     -INFINITY",	14,	ALT_MINUS_HUGE_VAL,	0 },
112 { L"     INFINITYX",	13,	ALT_HUGE_VAL,		0 },
113 { L"     +INFINITYX",	14,	ALT_HUGE_VAL,		0 },
114 { L"     -INFINITYX",	14,	ALT_MINUS_HUGE_VAL,	0 },
115 
116 /* NAN */
117 { L"NA",		0,	0,			0 },
118 { L"+NA",		0,	0,			0 },
119 { L"-NA",		0,	0,			0 },
120 { L"NAX",		0,	0,			0 },
121 { L"+NAX",		0,	0,			0 },
122 { L"-NAX",		0,	0,			0 },
123 { L"NAN",		3,	ALT_NAN,		0 },
124 { L"+NAN",		4,	ALT_NAN,		0 },
125 { L"-NAN",		4,	ALT_NAN,		0 },
126 { L"NANX",		3,	ALT_NAN,		0 },
127 { L"+NANX",		4,	ALT_NAN,		0 },
128 { L"-NANX",		4,	ALT_NAN,		0 },
129 { L"     NA",		0,	0,			0 },
130 { L"     +NA",		0,	0,			0 },
131 { L"     -NA",		0,	0,			0 },
132 { L"     NAX",		0,	0,			0 },
133 { L"     +NAX",		0,	0,			0 },
134 { L"     -NAX",		0,	0,			0 },
135 { L"+     NAN",		0,	0,			0 },
136 { L"-     NAN",		0,	0,			0 },
137 { L"     NAN",		8,	ALT_NAN,		0 },
138 { L"     +NAN",		9,	ALT_NAN,		0 },
139 { L"     -NAN",		9,	ALT_NAN,		0 },
140 { L"     NANX",		8,	ALT_NAN,		0 },
141 { L"     +NANX",	9,	ALT_NAN,		0 },
142 { L"     -NANX",	9,	ALT_NAN,		0 },
143 
144 { L"0",			1,	0,			0 },
145 { L"+0",		2,	0,			0 },
146 { L"-0",		2,	0,			0 },
147 { L"          0",	11,	0,			0 },
148 { L"          +0",	12,	0,			0 },
149 { L"          -0",	12,	0,			0 },
150 { L"+          0",	0,	0,			0 },
151 { L"-          0",	0,	0,			0 },
152 
153 { L".",			0,	0,			0 },
154 { L".0",		2,	0,			0 },
155 { L".00",		3,	0,			0 },
156 { L".000",		4,	0,			0 },
157 
158 { L"0.",		2,	0,			0 },
159 { L"+0.",		3,	0,			0 },
160 { L"-0.",		3,	0,			0 },
161 { L"          0.",	12,	0,			0 },
162 { L"          +0.",	13,	0,			0 },
163 { L"          -0.",	13,	0,			0 },
164 
165 { L"0.0",		3,	0,			0 },
166 { L"+0.0",		4,	0,			0 },
167 { L"-0.0",		4,	0,			0 },
168 { L"          0.0",	13,	0,			0 },
169 { L"          +0.0",	14,	0,			0 },
170 { L"          -0.0",	14,	0,			0 },
171 
172 { L"000",		3,	0,			0 },
173 { L"+000",		4,	0,			0 },
174 { L"-000",		4,	0,			0 },
175 { L"          000",	13,	0,			0 },
176 { L"          +000",	14,	0,			0 },
177 { L"          -000",	14,	0,			0 },
178 
179 { L"000.",		4,	0,			0 },
180 { L"+000.",		5,	0,			0 },
181 { L"-000.",		5,	0,			0 },
182 { L"          000.",	14,	0,			0 },
183 { L"          +000.",	15,	0,			0 },
184 { L"          -000.",	15,	0,			0 },
185 
186 { L"000.0",		5,	0,			0 },
187 { L"+000.0",		6,	0,			0 },
188 { L"-000.0",		6,	0,			0 },
189 { L"          000.0",	15,	0,			0 },
190 { L"          +000.0",	16,	0,			0 },
191 { L"          -000.0",	16,	0,			0 },
192 
193 
194 { L"0.0.",		3,	0,			0 },
195 { L"+0.0.",		4,	0,			0 },
196 { L"-0.0.",		4,	0,			0 },
197 { L"          0.0.",	13,	0,			0 },
198 { L"          +0.0.",	14,	0,			0 },
199 { L"          -0.0.",	14,	0,			0 },
200 
201 { L"0.0.0",		3,	0,			0 },
202 { L"+0.0.0",		4,	0,			0 },
203 { L"-0.0.0",		4,	0,			0 },
204 { L"          0.0.0",	13,	0,			0 },
205 { L"          +0.0.0",	14,	0,			0 },
206 { L"          -0.0.0",	14,	0,			0 },
207 
208 /* XXX: FIXME */
209 #if defined(__linux__)
210 { L"0X",		2,	0,			0 },
211 { L"+0X",		3,	0,			0 },
212 { L"-0X",		3,	0,			0 },
213 #else
214 { L"0X",		1,	0,			0 },
215 { L"+0X",		2,	0,			0 },
216 { L"-0X",		2,	0,			0 },
217 #endif
218 
219 /* XXX: SunOS 5.8's wcstod(3) doesn't accept hex */
220 #if !defined(__SunOS__)
221 #if defined(__linux__)
222 { L"0X.",		3,	0,			0 },
223 { L"+0X.",		4,	0,			0 },
224 { L"-0X.",		4,	0,			0 },
225 { L"          0X.",	13,	0,			0 },
226 { L"          +0X.",	14,	0,			0 },
227 { L"          -0X.",	14,	0,			0 },
228 #else
229 { L"0X.",		1,	0,			0 },
230 { L"+0X.",		2,	0,			0 },
231 { L"-0X.",		2,	0,			0 },
232 { L"          0X.",	11,	0,			0 },
233 { L"          +0X.",	12,	0,			0 },
234 { L"          -0X.",	12,	0,			0 },
235 #endif
236 /* XXX: FIXME */
237 #if defined(__NetBSD__) || defined(__linux__)
238 { L"0X.0",		4,	0,			0 },
239 { L"+0X.0",		5,	0,			0 },
240 { L"-0X.0",		5,	0,			0 },
241 { L"          0X.0",	14,	0,			0 },
242 { L"          +0X.0",	15,	0,			0 },
243 { L"          -0X.0",	15,	0,			0 },
244 
245 { L"0X.0P",		4,	0,			0 },
246 { L"+0X.0P",		5,	0,			0 },
247 { L"-0X.0P",		5,	0,			0 },
248 { L"          0X.0P",	14,	0,			0 },
249 { L"          +0X.0P",	15,	0,			0 },
250 { L"          -0X.0P",	15,	0,			0 },
251 #else
252 { L"0X.0",		1,	0,			0 },
253 { L"+0X.0",		2,	0,			0 },
254 { L"-0X.0",		2,	0,			0 },
255 { L"          0X.0",	11,	0,			0 },
256 { L"          +0X.0",	12,	0,			0 },
257 { L"          -0X.0",	12,	0,			0 },
258 
259 { L"0X.0P",		1,	0,			0 },
260 { L"+0X.0P",		2,	0,			0 },
261 { L"-0X.0P",		2,	0,			0 },
262 { L"          0X.0P",	11,	0,			0 },
263 { L"          +0X.0P",	12,	0,			0 },
264 { L"          -0X.0P",	12,	0,			0 },
265 #endif
266 
267 { L"0X0",		3,	0,			0 },
268 { L"+0X0",		4,	0,			0 },
269 { L"-0X0",		4,	0,			0 },
270 { L"          0X0",	13,	0,			0 },
271 { L"          +0X0",	14,	0,			0 },
272 { L"          -0X0",	14,	0,			0 },
273 
274 { L"00X0.0",		2,	0,			0 },
275 { L"+00X0.0",		3,	0,			0 },
276 { L"-00X0.0",		3,	0,			0 },
277 { L"          00X0.0",	12,	0,			0 },
278 { L"          +00X0.0",	13,	0,			0 },
279 { L"          -00X0.0",	13,	0,			0 },
280 
281 { L"0X0P",		3,	0,			0 },
282 { L"+0X0P",		4,	0,			0 },
283 { L"-0X0P",		4,	0,			0 },
284 { L"          0X0P",	13,	0,			0 },
285 { L"          +0X0P",	14,	0,			0 },
286 { L"          -0X0P",	14,	0,			0 },
287 
288 { L"0X0.",		4,	0,			0 },
289 { L"+0X0.",		5,	0,			0 },
290 { L"-0X0.",		5,	0,			0 },
291 { L"          0X0.",	14,	0,			0 },
292 { L"          +0X0.",	15,	0,			0 },
293 { L"          -0X0.",	15,	0,			0 },
294 
295 { L"0X0.0",		5,	0,			0 },
296 { L"+0X0.0",		6,	0,			0 },
297 { L"-0X0.0",		6,	0,			0 },
298 { L"          0X0.0",	15,	0,			0 },
299 { L"          +0X0.0",	16,	0,			0 },
300 { L"          -0X0.0",	16,	0,			0 },
301 
302 { L"0X0.P",		4,	0,			0 },
303 { L"+0X0.P",		5,	0,			0 },
304 { L"-0X0.P",		5,	0,			0 },
305 { L"          0X0.P",	14,	0,			0 },
306 { L"          +0X0.P",	15,	0,			0 },
307 { L"          -0X0.P",	15,	0,			0 },
308 
309 { L"0X0.P",		4,	0,			0 },
310 { L"+0X0.P",		5,	0,			0 },
311 { L"-0X0.P",		5,	0,			0 },
312 { L"          0X0.P",	14,	0,			0 },
313 { L"          +0X0.P",	15,	0,			0 },
314 { L"          -0X0.P",	15,	0,			0 },
315 
316 #endif
317 { L"0.12345678",	10,	0.12345678,		0 },
318 { L"+0.12345678",	11,	+0.12345678,		0 },
319 { L"-0.12345678",	11,	-0.12345678,		0 },
320 { L"     0.12345678",	15,	0.12345678,		0 },
321 { L"     +0.12345678",	16,	+0.12345678,		0 },
322 { L"     -0.12345678",	16,	-0.12345678,		0 },
323 
324 { L"0.12345E67",	10,	0.12345E67,		0 },
325 { L"+0.12345E67",	11,	+0.12345E67,		0 },
326 { L"-0.12345E67",	11,	-0.12345E67,		0 },
327 { L"     0.12345E67",	15,	0.12345E67,		0 },
328 { L"     +0.12345E67",	16,	+0.12345E67,		0 },
329 { L"     -0.12345E67",	16,	-0.12345E67,		0 },
330 
331 { L"0.12345E+6",	10,	0.12345E+6,		0 },
332 { L"+0.12345E+6",	11,	+0.12345E+6,		0 },
333 { L"-0.12345E+6",	11,	-0.12345E+6,		0 },
334 { L"     0.12345E+6",	15,	0.12345E+6,		0 },
335 { L"     +0.12345E+6",	16,	+0.12345E+6,		0 },
336 { L"     -0.12345E+6",	16,	-0.12345E+6,		0 },
337 
338 { L"0.98765E-4",	10,	0.98765E-4,		0 },
339 { L"+0.98765E-4",	11,	+0.98765E-4,		0 },
340 { L"-0.98765E-4",	11,	-0.98765E-4,		0 },
341 { L"     0.98765E-4",	15,	0.98765E-4,		0 },
342 { L"     +0.98765E-4",	16,	+0.98765E-4,		0 },
343 { L"     -0.98765E-4",	16,	-0.98765E-4,		0 },
344 
345 { L"12345678E9",	10,	12345678E9,		0 },
346 { L"+12345678E9",	11,	+12345678E9,		0 },
347 { L"-12345678E9",	11,	-12345678E9,		0 },
348 { L"     12345678E9",	15,	12345678E9,		0 },
349 { L"     +12345678E9",	16,	+12345678E9,		0 },
350 { L"     -12345678E9",	16,	-12345678E9,		0 },
351 
352 /* XXX: SunOS 5.8's wcstod(3) doesn't accept hex */
353 #if !defined(__SunOS__)
354 { L"0x1P+2",		6,	4,			0 },
355 { L"+0x1P+2",		7,	+4,			0 },
356 { L"-0x1P+2",		7,	-4,			0 },
357 { L"     0x1P+2",	11,	4,			0 },
358 { L"     +0x1P+2",	12,	+4,			0 },
359 { L"     -0x1P+2",	12,	-4,			0 },
360 
361 { L"0x1.0P+2",		8,	4,			0 },
362 { L"+0x1.0P+2",		9,	+4,			0 },
363 { L"-0x1.0P+2",		9,	-4,			0 },
364 { L"     0x1.0P+2",	13,	4,			0 },
365 { L"     +0x1.0P+2",	14,	+4,			0 },
366 { L"     -0x1.0P+2",	14,	-4,			0 },
367 
368 { L"0x1P-2",		6,	0.25,			0 },
369 { L"+0x1P-2",		7,	+0.25,			0 },
370 { L"-0x1P-2",		7,	-0.25,			0 },
371 { L"     0x1P-2",	11,	0.25,			0 },
372 { L"     +0x1P-2",	12,	+0.25,			0 },
373 { L"     -0x1P-2",	12,	-0.25,			0 },
374 
375 { L"0x1.0P-2",		8,	0.25,			0 },
376 { L"+0x1.0P-2",		9,	+0.25,			0 },
377 { L"-0x1.0P-2",		9,	-0.25,			0 },
378 { L"     0x1.0P-2",	13,	0.25,			0 },
379 { L"     +0x1.0P-2",	14,	+0.25,			0 },
380 { L"     -0x1.0P-2",	14,	-0.25,			0 },
381 #endif
382 
383 { NULL, 0, 0, 0 }
384 };
385 #endif /* !defined(__vax__) */
386 
387 ATF_TC(wcstod);
388 ATF_TC_HEAD(wcstod, tc)
389 {
390 	atf_tc_set_md_var(tc, "descr", "Checks wcstod(3)");
391 }
392 ATF_TC_BODY(wcstod, tc)
393 {
394 #if defined(__vax__)
395 #else
396 	struct test *t;
397 #endif
398 
399 #if !defined(__vax__)
400 	for (t = &tests[0]; t->wcs != NULL; ++t) {
401 		double d;
402 		size_t n;
403 		wchar_t *tail;
404 		char *buf;
405 
406 		/* we do not supported %ls nor %S yet. */
407 		n = wcstombs(NULL, t->wcs, 0);
408 		ATF_REQUIRE((buf = (void *)malloc(n + 1)) != NULL);
409 		(void)wcstombs(buf, t->wcs, n + 1);
410 		(void)printf("Checking wcstod(\"%s\", &tail):\n", buf);
411 		free(buf);
412 
413 		errno = 0;
414 		d = wcstod(t->wcs, &tail);
415 		(void)printf("[errno]\n");
416 		(void)printf("  got     : %s\n", strerror(errno));
417 		(void)printf("  expected: %s\n", strerror(t->err));
418 		ATF_REQUIRE_EQ(errno, t->err);
419 
420 		n = (size_t)(tail - t->wcs);
421 		(void)printf("[endptr - nptr]\n");
422 		(void)printf("  got     : %zd\n", n);
423 		(void)printf("  expected: %zd\n", t->len);
424 		ATF_REQUIRE_EQ(n, t->len);
425 
426 		(void)printf("[result]\n");
427 		(void)printf("  real:     %F\n", d);
428 		if (t->val == ALT_HUGE_VAL) {
429 			(void)printf("  expected: %F\n", HUGE_VAL);
430 			ATF_REQUIRE(isinf(d));
431 			ATF_REQUIRE_EQ(d, HUGE_VAL);
432 		} else if (t->val == ALT_MINUS_HUGE_VAL) {
433 			(void)printf("  expected: %F\n", -HUGE_VAL);
434 			ATF_REQUIRE(isinf(d));
435 			ATF_REQUIRE_EQ(d, -HUGE_VAL);
436 		} else if (t->val == ALT_NAN) {
437 			(void)printf("  expected: %F\n", NAN);
438 			ATF_REQUIRE(isnan(d));
439 		} else {
440 			(void)printf("  expected: %F\n", t->val);
441 			ATF_REQUIRE_EQ(d, t->val);
442 		}
443 
444 		(void)printf("\n");
445 	}
446 #else /* !defined(__vax__) */
447 	atf_tc_skip("Test is unavailable on vax.");
448 #endif /* !defined(__vax__) */
449 }
450 
451 ATF_TP_ADD_TCS(tp)
452 {
453 	ATF_TP_ADD_TC(tp, wcstod);
454 
455 	return atf_no_error();
456 }
457