xref: /freebsd/contrib/netbsd-tests/lib/libc/locale/t_wcstod.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 #ifdef __FreeBSD__
70 #include <stdio.h>
71 #endif
72 
73 #define	ALT_HUGE_VAL		-1
74 #define	ALT_MINUS_HUGE_VAL	-2
75 #define	ALT_NAN			-3
76 
77 #if !defined(__vax__)
78 static struct test {
79 	const wchar_t *wcs;
80 	size_t len;
81 	double val;
82 	int err;
83 } tests[] = {
84 { L"IN",		0,	0,			0 },
85 { L"+IN",		0,	0,			0 },
86 { L"-IN",		0,	0,			0 },
87 { L"INX",		0,	0,			0 },
88 { L"+INX",		0,	0,			0 },
89 { L"-INX",		0,	0,			0 },
90 { L"INF",		3,	ALT_HUGE_VAL,		0 },
91 { L"+INF",		4,	ALT_HUGE_VAL,		0 },
92 { L"-INF",		4,	ALT_MINUS_HUGE_VAL,	0 },
93 { L"INFX",		3,	ALT_HUGE_VAL,		0 },
94 { L"+INFX",		4,	ALT_HUGE_VAL,		0 },
95 { L"-INFX",		4,	ALT_MINUS_HUGE_VAL,	0 },
96 { L"     IN",		0,	0,			0 },
97 { L"     +IN",		0,	0,			0 },
98 { L"     -IN",		0,	0,			0 },
99 { L"     INX",		0,	0,			0 },
100 { L"     +INX",		0,	0,			0 },
101 { L"     -INX",		0,	0,			0 },
102 { L"+     INF",		0,	0,			0 },
103 { L"-     INF",		0,	0,			0 },
104 { L"     INF",		8,	ALT_HUGE_VAL,		0 },
105 { L"     +INF",		9,	ALT_HUGE_VAL,		0 },
106 { L"     -INF",		9,	ALT_MINUS_HUGE_VAL,	0 },
107 { L"     INFX",		8,	ALT_HUGE_VAL,		0 },
108 { L"     +INFX",	9,	ALT_HUGE_VAL,		0 },
109 { L"     -INFX",	9,	ALT_MINUS_HUGE_VAL,	0 },
110 { L"     INFINIT",	8,	ALT_HUGE_VAL,		0 },
111 { L"     +INFINIT",	9,	ALT_HUGE_VAL,		0 },
112 { L"     -INFINIT",	9,	ALT_MINUS_HUGE_VAL,	0 },
113 { L"     INFINITY",	13,	ALT_HUGE_VAL,		0 },
114 { L"     +INFINITY",	14,	ALT_HUGE_VAL,		0 },
115 { L"     -INFINITY",	14,	ALT_MINUS_HUGE_VAL,	0 },
116 { L"     INFINITYX",	13,	ALT_HUGE_VAL,		0 },
117 { L"     +INFINITYX",	14,	ALT_HUGE_VAL,		0 },
118 { L"     -INFINITYX",	14,	ALT_MINUS_HUGE_VAL,	0 },
119 
120 /* NAN */
121 { L"NA",		0,	0,			0 },
122 { L"+NA",		0,	0,			0 },
123 { L"-NA",		0,	0,			0 },
124 { L"NAX",		0,	0,			0 },
125 { L"+NAX",		0,	0,			0 },
126 { L"-NAX",		0,	0,			0 },
127 { L"NAN",		3,	ALT_NAN,		0 },
128 { L"+NAN",		4,	ALT_NAN,		0 },
129 { L"-NAN",		4,	ALT_NAN,		0 },
130 { L"NANX",		3,	ALT_NAN,		0 },
131 { L"+NANX",		4,	ALT_NAN,		0 },
132 { L"-NANX",		4,	ALT_NAN,		0 },
133 { L"     NA",		0,	0,			0 },
134 { L"     +NA",		0,	0,			0 },
135 { L"     -NA",		0,	0,			0 },
136 { L"     NAX",		0,	0,			0 },
137 { L"     +NAX",		0,	0,			0 },
138 { L"     -NAX",		0,	0,			0 },
139 { L"+     NAN",		0,	0,			0 },
140 { L"-     NAN",		0,	0,			0 },
141 { L"     NAN",		8,	ALT_NAN,		0 },
142 { L"     +NAN",		9,	ALT_NAN,		0 },
143 { L"     -NAN",		9,	ALT_NAN,		0 },
144 { L"     NANX",		8,	ALT_NAN,		0 },
145 { L"     +NANX",	9,	ALT_NAN,		0 },
146 { L"     -NANX",	9,	ALT_NAN,		0 },
147 
148 { L"0",			1,	0,			0 },
149 { L"+0",		2,	0,			0 },
150 { L"-0",		2,	0,			0 },
151 { L"          0",	11,	0,			0 },
152 { L"          +0",	12,	0,			0 },
153 { L"          -0",	12,	0,			0 },
154 { L"+          0",	0,	0,			0 },
155 { L"-          0",	0,	0,			0 },
156 
157 { L".",			0,	0,			0 },
158 { L".0",		2,	0,			0 },
159 { L".00",		3,	0,			0 },
160 { L".000",		4,	0,			0 },
161 
162 { L"0.",		2,	0,			0 },
163 { L"+0.",		3,	0,			0 },
164 { L"-0.",		3,	0,			0 },
165 { L"          0.",	12,	0,			0 },
166 { L"          +0.",	13,	0,			0 },
167 { L"          -0.",	13,	0,			0 },
168 
169 { L"0.0",		3,	0,			0 },
170 { L"+0.0",		4,	0,			0 },
171 { L"-0.0",		4,	0,			0 },
172 { L"          0.0",	13,	0,			0 },
173 { L"          +0.0",	14,	0,			0 },
174 { L"          -0.0",	14,	0,			0 },
175 
176 { L"000",		3,	0,			0 },
177 { L"+000",		4,	0,			0 },
178 { L"-000",		4,	0,			0 },
179 { L"          000",	13,	0,			0 },
180 { L"          +000",	14,	0,			0 },
181 { L"          -000",	14,	0,			0 },
182 
183 { L"000.",		4,	0,			0 },
184 { L"+000.",		5,	0,			0 },
185 { L"-000.",		5,	0,			0 },
186 { L"          000.",	14,	0,			0 },
187 { L"          +000.",	15,	0,			0 },
188 { L"          -000.",	15,	0,			0 },
189 
190 { L"000.0",		5,	0,			0 },
191 { L"+000.0",		6,	0,			0 },
192 { L"-000.0",		6,	0,			0 },
193 { L"          000.0",	15,	0,			0 },
194 { L"          +000.0",	16,	0,			0 },
195 { L"          -000.0",	16,	0,			0 },
196 
197 
198 { L"0.0.",		3,	0,			0 },
199 { L"+0.0.",		4,	0,			0 },
200 { L"-0.0.",		4,	0,			0 },
201 { L"          0.0.",	13,	0,			0 },
202 { L"          +0.0.",	14,	0,			0 },
203 { L"          -0.0.",	14,	0,			0 },
204 
205 { L"0.0.0",		3,	0,			0 },
206 { L"+0.0.0",		4,	0,			0 },
207 { L"-0.0.0",		4,	0,			0 },
208 { L"          0.0.0",	13,	0,			0 },
209 { L"          +0.0.0",	14,	0,			0 },
210 { L"          -0.0.0",	14,	0,			0 },
211 
212 /* XXX: FIXME */
213 #if defined(__linux__)
214 { L"0X",		2,	0,			0 },
215 { L"+0X",		3,	0,			0 },
216 { L"-0X",		3,	0,			0 },
217 #else
218 { L"0X",		1,	0,			0 },
219 { L"+0X",		2,	0,			0 },
220 { L"-0X",		2,	0,			0 },
221 #endif
222 
223 /* XXX: SunOS 5.8's wcstod(3) doesn't accept hex */
224 #if !defined(__SunOS__)
225 #if defined(__linux__)
226 { L"0X.",		3,	0,			0 },
227 { L"+0X.",		4,	0,			0 },
228 { L"-0X.",		4,	0,			0 },
229 { L"          0X.",	13,	0,			0 },
230 { L"          +0X.",	14,	0,			0 },
231 { L"          -0X.",	14,	0,			0 },
232 #else
233 { L"0X.",		1,	0,			0 },
234 { L"+0X.",		2,	0,			0 },
235 { L"-0X.",		2,	0,			0 },
236 { L"          0X.",	11,	0,			0 },
237 { L"          +0X.",	12,	0,			0 },
238 { L"          -0X.",	12,	0,			0 },
239 #endif
240 /* XXX: FIXME */
241 #if defined(__NetBSD__) || defined(__linux__) || defined(__FreeBSD__)
242 { L"0X.0",		4,	0,			0 },
243 { L"+0X.0",		5,	0,			0 },
244 { L"-0X.0",		5,	0,			0 },
245 { L"          0X.0",	14,	0,			0 },
246 { L"          +0X.0",	15,	0,			0 },
247 { L"          -0X.0",	15,	0,			0 },
248 
249 { L"0X.0P",		4,	0,			0 },
250 { L"+0X.0P",		5,	0,			0 },
251 { L"-0X.0P",		5,	0,			0 },
252 { L"          0X.0P",	14,	0,			0 },
253 { L"          +0X.0P",	15,	0,			0 },
254 { L"          -0X.0P",	15,	0,			0 },
255 #else
256 { L"0X.0",		1,	0,			0 },
257 { L"+0X.0",		2,	0,			0 },
258 { L"-0X.0",		2,	0,			0 },
259 { L"          0X.0",	11,	0,			0 },
260 { L"          +0X.0",	12,	0,			0 },
261 { L"          -0X.0",	12,	0,			0 },
262 
263 { L"0X.0P",		1,	0,			0 },
264 { L"+0X.0P",		2,	0,			0 },
265 { L"-0X.0P",		2,	0,			0 },
266 { L"          0X.0P",	11,	0,			0 },
267 { L"          +0X.0P",	12,	0,			0 },
268 { L"          -0X.0P",	12,	0,			0 },
269 #endif
270 
271 { L"0X0",		3,	0,			0 },
272 { L"+0X0",		4,	0,			0 },
273 { L"-0X0",		4,	0,			0 },
274 { L"          0X0",	13,	0,			0 },
275 { L"          +0X0",	14,	0,			0 },
276 { L"          -0X0",	14,	0,			0 },
277 
278 { L"00X0.0",		2,	0,			0 },
279 { L"+00X0.0",		3,	0,			0 },
280 { L"-00X0.0",		3,	0,			0 },
281 { L"          00X0.0",	12,	0,			0 },
282 { L"          +00X0.0",	13,	0,			0 },
283 { L"          -00X0.0",	13,	0,			0 },
284 
285 { L"0X0P",		3,	0,			0 },
286 { L"+0X0P",		4,	0,			0 },
287 { L"-0X0P",		4,	0,			0 },
288 { L"          0X0P",	13,	0,			0 },
289 { L"          +0X0P",	14,	0,			0 },
290 { L"          -0X0P",	14,	0,			0 },
291 
292 { L"0X0.",		4,	0,			0 },
293 { L"+0X0.",		5,	0,			0 },
294 { L"-0X0.",		5,	0,			0 },
295 { L"          0X0.",	14,	0,			0 },
296 { L"          +0X0.",	15,	0,			0 },
297 { L"          -0X0.",	15,	0,			0 },
298 
299 { L"0X0.0",		5,	0,			0 },
300 { L"+0X0.0",		6,	0,			0 },
301 { L"-0X0.0",		6,	0,			0 },
302 { L"          0X0.0",	15,	0,			0 },
303 { L"          +0X0.0",	16,	0,			0 },
304 { L"          -0X0.0",	16,	0,			0 },
305 
306 { L"0X0.P",		4,	0,			0 },
307 { L"+0X0.P",		5,	0,			0 },
308 { L"-0X0.P",		5,	0,			0 },
309 { L"          0X0.P",	14,	0,			0 },
310 { L"          +0X0.P",	15,	0,			0 },
311 { L"          -0X0.P",	15,	0,			0 },
312 
313 { L"0X0.P",		4,	0,			0 },
314 { L"+0X0.P",		5,	0,			0 },
315 { L"-0X0.P",		5,	0,			0 },
316 { L"          0X0.P",	14,	0,			0 },
317 { L"          +0X0.P",	15,	0,			0 },
318 { L"          -0X0.P",	15,	0,			0 },
319 
320 #endif
321 { L"0.12345678",	10,	0.12345678,		0 },
322 { L"+0.12345678",	11,	+0.12345678,		0 },
323 { L"-0.12345678",	11,	-0.12345678,		0 },
324 { L"     0.12345678",	15,	0.12345678,		0 },
325 { L"     +0.12345678",	16,	+0.12345678,		0 },
326 { L"     -0.12345678",	16,	-0.12345678,		0 },
327 
328 { L"0.12345E67",	10,	0.12345E67,		0 },
329 { L"+0.12345E67",	11,	+0.12345E67,		0 },
330 { L"-0.12345E67",	11,	-0.12345E67,		0 },
331 { L"     0.12345E67",	15,	0.12345E67,		0 },
332 { L"     +0.12345E67",	16,	+0.12345E67,		0 },
333 { L"     -0.12345E67",	16,	-0.12345E67,		0 },
334 
335 { L"0.12345E+6",	10,	0.12345E+6,		0 },
336 { L"+0.12345E+6",	11,	+0.12345E+6,		0 },
337 { L"-0.12345E+6",	11,	-0.12345E+6,		0 },
338 { L"     0.12345E+6",	15,	0.12345E+6,		0 },
339 { L"     +0.12345E+6",	16,	+0.12345E+6,		0 },
340 { L"     -0.12345E+6",	16,	-0.12345E+6,		0 },
341 
342 { L"0.98765E-4",	10,	0.98765E-4,		0 },
343 { L"+0.98765E-4",	11,	+0.98765E-4,		0 },
344 { L"-0.98765E-4",	11,	-0.98765E-4,		0 },
345 { L"     0.98765E-4",	15,	0.98765E-4,		0 },
346 { L"     +0.98765E-4",	16,	+0.98765E-4,		0 },
347 { L"     -0.98765E-4",	16,	-0.98765E-4,		0 },
348 
349 { L"12345678E9",	10,	12345678E9,		0 },
350 { L"+12345678E9",	11,	+12345678E9,		0 },
351 { L"-12345678E9",	11,	-12345678E9,		0 },
352 { L"     12345678E9",	15,	12345678E9,		0 },
353 { L"     +12345678E9",	16,	+12345678E9,		0 },
354 { L"     -12345678E9",	16,	-12345678E9,		0 },
355 
356 /* XXX: SunOS 5.8's wcstod(3) doesn't accept hex */
357 #if !defined(__SunOS__)
358 { L"0x1P+2",		6,	4,			0 },
359 { L"+0x1P+2",		7,	+4,			0 },
360 { L"-0x1P+2",		7,	-4,			0 },
361 { L"     0x1P+2",	11,	4,			0 },
362 { L"     +0x1P+2",	12,	+4,			0 },
363 { L"     -0x1P+2",	12,	-4,			0 },
364 
365 { L"0x1.0P+2",		8,	4,			0 },
366 { L"+0x1.0P+2",		9,	+4,			0 },
367 { L"-0x1.0P+2",		9,	-4,			0 },
368 { L"     0x1.0P+2",	13,	4,			0 },
369 { L"     +0x1.0P+2",	14,	+4,			0 },
370 { L"     -0x1.0P+2",	14,	-4,			0 },
371 
372 { L"0x1P-2",		6,	0.25,			0 },
373 { L"+0x1P-2",		7,	+0.25,			0 },
374 { L"-0x1P-2",		7,	-0.25,			0 },
375 { L"     0x1P-2",	11,	0.25,			0 },
376 { L"     +0x1P-2",	12,	+0.25,			0 },
377 { L"     -0x1P-2",	12,	-0.25,			0 },
378 
379 { L"0x1.0P-2",		8,	0.25,			0 },
380 { L"+0x1.0P-2",		9,	+0.25,			0 },
381 { L"-0x1.0P-2",		9,	-0.25,			0 },
382 { L"     0x1.0P-2",	13,	0.25,			0 },
383 { L"     +0x1.0P-2",	14,	+0.25,			0 },
384 { L"     -0x1.0P-2",	14,	-0.25,			0 },
385 #endif
386 
387 { NULL, 0, 0, 0 }
388 };
389 #endif /* !defined(__vax__) */
390 
391 ATF_TC(wcstod);
392 ATF_TC_HEAD(wcstod, tc)
393 {
394 	atf_tc_set_md_var(tc, "descr", "Checks wcstod(3)");
395 }
396 ATF_TC_BODY(wcstod, tc)
397 {
398 #if defined(__vax__)
399 #else
400 	struct test *t;
401 #endif
402 
403 #if !defined(__vax__)
404 	for (t = &tests[0]; t->wcs != NULL; ++t) {
405 		double d;
406 		size_t n;
407 		wchar_t *tail;
408 		char *buf;
409 
410 		/* we do not supported %ls nor %S yet. */
411 		n = wcstombs(NULL, t->wcs, 0);
412 		ATF_REQUIRE((buf = (void *)malloc(n + 1)) != NULL);
413 		(void)wcstombs(buf, t->wcs, n + 1);
414 		(void)printf("Checking wcstod(\"%s\", &tail):\n", buf);
415 		free(buf);
416 
417 		errno = 0;
418 		d = wcstod(t->wcs, &tail);
419 		(void)printf("[errno]\n");
420 		(void)printf("  got     : %s\n", strerror(errno));
421 		(void)printf("  expected: %s\n", strerror(t->err));
422 		ATF_REQUIRE_EQ(errno, t->err);
423 
424 		n = (size_t)(tail - t->wcs);
425 		(void)printf("[endptr - nptr]\n");
426 		(void)printf("  got     : %zd\n", n);
427 		(void)printf("  expected: %zd\n", t->len);
428 		ATF_REQUIRE_EQ(n, t->len);
429 
430 		(void)printf("[result]\n");
431 		(void)printf("  real:     %F\n", d);
432 		if (t->val == ALT_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_MINUS_HUGE_VAL) {
437 			(void)printf("  expected: %F\n", -HUGE_VAL);
438 			ATF_REQUIRE(isinf(d));
439 			ATF_REQUIRE_EQ(d, -HUGE_VAL);
440 		} else if (t->val == ALT_NAN) {
441 			(void)printf("  expected: %F\n", NAN);
442 			ATF_REQUIRE(isnan(d));
443 		} else {
444 			(void)printf("  expected: %F\n", t->val);
445 			ATF_REQUIRE_EQ(d, t->val);
446 		}
447 
448 		(void)printf("\n");
449 	}
450 #else /* !defined(__vax__) */
451 	atf_tc_skip("Test is unavailable on vax.");
452 #endif /* !defined(__vax__) */
453 }
454 
455 ATF_TP_ADD_TCS(tp)
456 {
457 	ATF_TP_ADD_TC(tp, wcstod);
458 
459 	return atf_no_error();
460 }
461