xref: /netbsd-src/tests/lib/libc/stdlib/t_strtod.c (revision cb861154c176d3dcc8ff846f449e3c16a5f5edb5)
1 /*	$NetBSD: t_strtod.c,v 1.7 2011/04/12 02:56:20 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
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 /* Public domain, Otto Moerbeek <otto@drijf.net>, 2006. */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_strtod.c,v 1.7 2011/04/12 02:56:20 jruoho Exp $");
36 
37 #include <errno.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <atf-c.h>
44 #include <atf-c/config.h>
45 
46 ATF_TC(strtod_basic);
47 ATF_TC_HEAD(strtod_basic, tc)
48 {
49 	atf_tc_set_md_var(tc, "descr", "A basic test of strtod(3)");
50 }
51 
52 ATF_TC_BODY(strtod_basic, tc)
53 {
54 	char buf[512];
55 	size_t i, n;
56 	double d;
57 
58 	n = 1024 * 1000;
59 
60 	for (i = 1; i < n; i = i + 1024) {
61 
62 		(void)snprintf(buf, sizeof(buf), "%zu.%zu", i, i + 1);
63 
64 		errno = 0;
65 		d = strtod(buf, NULL);
66 
67 		ATF_REQUIRE(d > 0.0);
68 		ATF_REQUIRE(errno == 0);
69 	}
70 }
71 
72 ATF_TC(strtod_hex);
73 ATF_TC_HEAD(strtod_hex, tc)
74 {
75 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with hexadecimals");
76 }
77 
78 ATF_TC_BODY(strtod_hex, tc)
79 {
80 	const char *str;
81 	char *end;
82 	double d;
83 
84 	str = "-0x0";
85 	d = strtod(str, &end);	/* -0.0 */
86 
87 	ATF_REQUIRE(end == str + 4);
88 	ATF_REQUIRE(signbit(d) != 0);
89 	ATF_REQUIRE(fabs(d) < 1.0e-40);
90 
91 	str = "-0x";
92 	d = strtod(str, &end);	/* -0.0 */
93 
94 	ATF_REQUIRE(end == str + 2);
95 	ATF_REQUIRE(signbit(d) != 0);
96 	ATF_REQUIRE(fabs(d) < 1.0e-40);
97 }
98 
99 ATF_TC(strtod_inf);
100 ATF_TC_HEAD(strtod_inf, tc)
101 {
102 	atf_tc_set_md_var(tc, "descr", "A strtod(3) with INF");
103 }
104 
105 ATF_TC_BODY(strtod_inf, tc)
106 {
107 #ifndef __vax__
108 
109 	long double ld;
110 	double d;
111 	float f;
112 
113 	/*
114 	 * See old PR lib/33262.
115 	 */
116 	d = strtod("INF", NULL);
117 	ATF_REQUIRE(isinf(d) != 0);
118 
119 	f = strtof("INF", NULL);
120 	ATF_REQUIRE(isinf(f) != 0);
121 
122 	ld = strtold("INF", NULL);
123 	ATF_REQUIRE(isinf(ld) != 0);
124 #endif
125 }
126 
127 ATF_TC(strtod_underflow);
128 ATF_TC_HEAD(strtod_underflow, tc)
129 {
130 	atf_tc_set_md_var(tc, "descr", "Test underflow in strtod(3)");
131 }
132 
133 ATF_TC_BODY(strtod_underflow, tc)
134 {
135 
136 	const char *tmp =
137 	    "0.0000000000000000000000000000000000000000000000000000"
138 	    "000000000000000000000000000000000000000000000000000000"
139 	    "000000000000000000000000000000000000000000000000000000"
140 	    "000000000000000000000000000000000000000000000000000000"
141 	    "000000000000000000000000000000000000000000000000000000"
142 	    "000000000000000000000000000000000000000000000000000000"
143 	    "000000000000000000000000000000000000000000000000000000"
144 	    "000000000000000002";
145 
146 	double d;
147 
148 	errno = 0;
149 	d = strtod(tmp, NULL);
150 
151 	if (errno != ERANGE)
152 		atf_tc_fail("strtod(3) did not detect underflow");
153 }
154 
155 ATF_TP_ADD_TCS(tp)
156 {
157 
158 	ATF_TP_ADD_TC(tp, strtod_basic);
159 	ATF_TP_ADD_TC(tp, strtod_hex);
160 	ATF_TP_ADD_TC(tp, strtod_inf);
161 	ATF_TP_ADD_TC(tp, strtod_underflow);
162 
163 	return atf_no_error();
164 }
165