xref: /netbsd-src/tests/lib/libm/t_sqrt.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: t_sqrt.c,v 1.3 2012/02/13 05:09:01 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sqrt.c,v 1.3 2012/02/13 05:09:01 jruoho Exp $");
33 
34 #include <atf-c.h>
35 #include <math.h>
36 #include <stdio.h>
37 
38 /*
39  * sqrt(3)
40  */
41 ATF_TC(sqrt_nan);
42 ATF_TC_HEAD(sqrt_nan, tc)
43 {
44 	atf_tc_set_md_var(tc, "descr", "Test sqrt(NaN) == NaN");
45 }
46 
47 ATF_TC_BODY(sqrt_nan, tc)
48 {
49 #ifndef __vax__
50 	const double x = 0.0L / 0.0L;
51 
52 	ATF_CHECK(isnan(x) != 0);
53 	ATF_CHECK(isnan(sqrt(x)) != 0);
54 #endif
55 }
56 
57 ATF_TC(sqrt_pow);
58 ATF_TC_HEAD(sqrt_pow, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "Test sqrt(3) vs. pow(3)");
61 }
62 
63 ATF_TC_BODY(sqrt_pow, tc)
64 {
65 #ifndef __vax__
66 	const double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
67 	const double eps = 1.0e-40;
68 	double y, z;
69 	size_t i;
70 
71 	for (i = 0; i < __arraycount(x); i++) {
72 
73 		y = sqrt(x[i]);
74 		z = pow(x[i], 1.0 / 2.0);
75 
76 		if (fabs(y - z) > eps)
77 			atf_tc_fail_nonfatal("sqrt(%0.03f) != "
78 			    "pow(%0.03f, 1/2)\n", x[i], x[i]);
79 	}
80 #endif
81 }
82 
83 ATF_TC(sqrt_inf_neg);
84 ATF_TC_HEAD(sqrt_inf_neg, tc)
85 {
86 	atf_tc_set_md_var(tc, "descr", "Test sqrt(-Inf) == NaN");
87 }
88 
89 ATF_TC_BODY(sqrt_inf_neg, tc)
90 {
91 #ifndef __vax__
92 	const double x = -1.0L / 0.0L;
93 	double y = sqrt(x);
94 
95 	ATF_CHECK(isnan(y) != 0);
96 #endif
97 }
98 
99 ATF_TC(sqrt_inf_pos);
100 ATF_TC_HEAD(sqrt_inf_pos, tc)
101 {
102 	atf_tc_set_md_var(tc, "descr", "Test sqrt(+Inf) == +Inf");
103 }
104 
105 ATF_TC_BODY(sqrt_inf_pos, tc)
106 {
107 #ifndef __vax__
108 	const double x = 1.0L / 0.0L;
109 	double y = sqrt(x);
110 
111 	ATF_CHECK(isinf(y) != 0);
112 	ATF_CHECK(signbit(y) == 0);
113 #endif
114 }
115 
116 ATF_TC(sqrt_zero_neg);
117 ATF_TC_HEAD(sqrt_zero_neg, tc)
118 {
119 	atf_tc_set_md_var(tc, "descr", "Test sqrt(-0.0) == -0.0");
120 }
121 
122 ATF_TC_BODY(sqrt_zero_neg, tc)
123 {
124 #ifndef __vax__
125 	const double x = -0.0L;
126 	double y = sqrt(x);
127 
128 	if (fabs(y) > 0.0 || signbit(y) == 0)
129 		atf_tc_fail_nonfatal("sqrt(-0.0) != -0.0");
130 #endif
131 }
132 
133 ATF_TC(sqrt_zero_pos);
134 ATF_TC_HEAD(sqrt_zero_pos, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr", "Test sqrt(+0.0) == +0.0");
137 }
138 
139 ATF_TC_BODY(sqrt_zero_pos, tc)
140 {
141 #ifndef __vax__
142 	const double x = 0.0L;
143 	double y = sqrt(x);
144 
145 	if (fabs(y) > 0.0 || signbit(y) != 0)
146 		atf_tc_fail_nonfatal("sqrt(+0.0) != +0.0");
147 #endif
148 }
149 
150 /*
151  * sqrtf(3)
152  */
153 ATF_TC(sqrtf_nan);
154 ATF_TC_HEAD(sqrtf_nan, tc)
155 {
156 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(NaN) == NaN");
157 }
158 
159 ATF_TC_BODY(sqrtf_nan, tc)
160 {
161 #ifndef __vax__
162 	const float x = 0.0L / 0.0L;
163 
164 	ATF_CHECK(isnan(x) != 0);
165 	ATF_CHECK(isnan(sqrtf(x)) != 0);
166 #endif
167 }
168 
169 ATF_TC(sqrtf_powf);
170 ATF_TC_HEAD(sqrtf_powf, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(3) vs. powf(3)");
173 }
174 
175 ATF_TC_BODY(sqrtf_powf, tc)
176 {
177 #ifndef __vax__
178 	const float x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
179 	const float eps = 1.0e-30;
180 	volatile float y, z;
181 	size_t i;
182 
183 	for (i = 0; i < __arraycount(x); i++) {
184 
185 		y = sqrtf(x[i]);
186 		z = powf(x[i], 1.0 / 2.0);
187 
188 		if (fabsf(y - z) > eps)
189 			atf_tc_fail_nonfatal("sqrtf(%0.03f) != "
190 			    "powf(%0.03f, 1/2)\n", x[i], x[i]);
191 	}
192 #endif
193 }
194 
195 ATF_TC(sqrtf_inf_neg);
196 ATF_TC_HEAD(sqrtf_inf_neg, tc)
197 {
198 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(-Inf) == NaN");
199 }
200 
201 ATF_TC_BODY(sqrtf_inf_neg, tc)
202 {
203 #ifndef __vax__
204 	const float x = -1.0L / 0.0L;
205 	float y = sqrtf(x);
206 
207 	ATF_CHECK(isnan(y) != 0);
208 #endif
209 }
210 
211 ATF_TC(sqrtf_inf_pos);
212 ATF_TC_HEAD(sqrtf_inf_pos, tc)
213 {
214 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(+Inf) == +Inf");
215 }
216 
217 ATF_TC_BODY(sqrtf_inf_pos, tc)
218 {
219 #ifndef __vax__
220 	const float x = 1.0L / 0.0L;
221 	float y = sqrtf(x);
222 
223 	ATF_CHECK(isinf(y) != 0);
224 	ATF_CHECK(signbit(y) == 0);
225 #endif
226 }
227 
228 ATF_TC(sqrtf_zero_neg);
229 ATF_TC_HEAD(sqrtf_zero_neg, tc)
230 {
231 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(-0.0) == -0.0");
232 }
233 
234 ATF_TC_BODY(sqrtf_zero_neg, tc)
235 {
236 #ifndef __vax__
237 	const float x = -0.0L;
238 	float y = sqrtf(x);
239 
240 	if (fabsf(y) > 0.0 || signbit(y) == 0)
241 		atf_tc_fail_nonfatal("sqrtf(-0.0) != -0.0");
242 #endif
243 }
244 
245 ATF_TC(sqrtf_zero_pos);
246 ATF_TC_HEAD(sqrtf_zero_pos, tc)
247 {
248 	atf_tc_set_md_var(tc, "descr", "Test sqrtf(+0.0) == +0.0");
249 }
250 
251 ATF_TC_BODY(sqrtf_zero_pos, tc)
252 {
253 #ifndef __vax__
254 	const float x = 0.0L;
255 	float y = sqrtf(x);
256 
257 	if (fabsf(y) > 0.0 || signbit(y) != 0)
258 		atf_tc_fail_nonfatal("sqrtf(+0.0) != +0.0");
259 #endif
260 }
261 
262 ATF_TP_ADD_TCS(tp)
263 {
264 
265 	ATF_TP_ADD_TC(tp, sqrt_nan);
266 	ATF_TP_ADD_TC(tp, sqrt_pow);
267 	ATF_TP_ADD_TC(tp, sqrt_inf_neg);
268 	ATF_TP_ADD_TC(tp, sqrt_inf_pos);
269 	ATF_TP_ADD_TC(tp, sqrt_zero_neg);
270 	ATF_TP_ADD_TC(tp, sqrt_zero_pos);
271 
272 	ATF_TP_ADD_TC(tp, sqrtf_nan);
273 	ATF_TP_ADD_TC(tp, sqrtf_powf);
274 	ATF_TP_ADD_TC(tp, sqrtf_inf_neg);
275 	ATF_TP_ADD_TC(tp, sqrtf_inf_pos);
276 	ATF_TP_ADD_TC(tp, sqrtf_zero_neg);
277 	ATF_TP_ADD_TC(tp, sqrtf_zero_pos);
278 
279 	return atf_no_error();
280 }
281