etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libm / t_sqrt.c
blob1d551ec3101d8b9a18cbbcff9c9047d4c1ee9137
1 /* $NetBSD: t_sqrt.c,v 1.7 2014/03/12 21:40:07 martin Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sqrt.c,v 1.7 2014/03/12 21:40:07 martin Exp $");
34 #include <atf-c.h>
35 #include <math.h>
36 #include <float.h>
37 #include <stdio.h>
40 * sqrt(3)
42 ATF_TC(sqrt_nan);
43 ATF_TC_HEAD(sqrt_nan, tc)
45 atf_tc_set_md_var(tc, "descr", "Test sqrt(NaN) == NaN");
48 ATF_TC_BODY(sqrt_nan, tc)
50 const double x = 0.0L / 0.0L;
52 ATF_CHECK(isnan(x) != 0);
53 ATF_CHECK(isnan(sqrt(x)) != 0);
56 ATF_TC(sqrt_pow);
57 ATF_TC_HEAD(sqrt_pow, tc)
59 atf_tc_set_md_var(tc, "descr", "Test sqrt(3) vs. pow(3)");
62 ATF_TC_BODY(sqrt_pow, tc)
64 const double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
65 #if __DBL_MIN_10_EXP__ <= -40
66 const double eps = 1.0e-40;
67 #else
68 const double eps = __DBL_MIN__*4.0;
69 #endif
70 double y, z;
71 size_t i;
73 for (i = 0; i < __arraycount(x); i++) {
75 y = sqrt(x[i]);
76 z = pow(x[i], 1.0 / 2.0);
78 if (fabs(y - z) > eps)
79 atf_tc_fail_nonfatal("sqrt(%0.03f) != "
80 "pow(%0.03f, 1/2)\n", x[i], x[i]);
84 ATF_TC(sqrt_inf_neg);
85 ATF_TC_HEAD(sqrt_inf_neg, tc)
87 atf_tc_set_md_var(tc, "descr", "Test sqrt(-Inf) == NaN");
90 ATF_TC_BODY(sqrt_inf_neg, tc)
92 const double x = -1.0L / 0.0L;
93 double y = sqrt(x);
95 ATF_CHECK(isnan(y) != 0);
98 ATF_TC(sqrt_inf_pos);
99 ATF_TC_HEAD(sqrt_inf_pos, tc)
101 atf_tc_set_md_var(tc, "descr", "Test sqrt(+Inf) == +Inf");
104 ATF_TC_BODY(sqrt_inf_pos, tc)
106 const double x = 1.0L / 0.0L;
107 double y = sqrt(x);
109 ATF_CHECK(isinf(y) != 0);
110 ATF_CHECK(signbit(y) == 0);
113 ATF_TC(sqrt_zero_neg);
114 ATF_TC_HEAD(sqrt_zero_neg, tc)
116 atf_tc_set_md_var(tc, "descr", "Test sqrt(-0.0) == -0.0");
119 ATF_TC_BODY(sqrt_zero_neg, tc)
121 const double x = -0.0L;
122 double y = sqrt(x);
124 if (fabs(y) > 0.0 || signbit(y) == 0)
125 atf_tc_fail_nonfatal("sqrt(-0.0) != -0.0");
128 ATF_TC(sqrt_zero_pos);
129 ATF_TC_HEAD(sqrt_zero_pos, tc)
131 atf_tc_set_md_var(tc, "descr", "Test sqrt(+0.0) == +0.0");
134 ATF_TC_BODY(sqrt_zero_pos, tc)
136 const double x = 0.0L;
137 double y = sqrt(x);
139 if (fabs(y) > 0.0 || signbit(y) != 0)
140 atf_tc_fail_nonfatal("sqrt(+0.0) != +0.0");
144 * sqrtf(3)
146 ATF_TC(sqrtf_nan);
147 ATF_TC_HEAD(sqrtf_nan, tc)
149 atf_tc_set_md_var(tc, "descr", "Test sqrtf(NaN) == NaN");
152 ATF_TC_BODY(sqrtf_nan, tc)
154 const float x = 0.0L / 0.0L;
156 ATF_CHECK(isnan(x) != 0);
157 ATF_CHECK(isnan(sqrtf(x)) != 0);
160 ATF_TC(sqrtf_powf);
161 ATF_TC_HEAD(sqrtf_powf, tc)
163 atf_tc_set_md_var(tc, "descr", "Test sqrtf(3) vs. powf(3)");
166 ATF_TC_BODY(sqrtf_powf, tc)
168 const float x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
169 const float eps = 1.0e-30;
170 volatile float y, z;
171 size_t i;
173 for (i = 0; i < __arraycount(x); i++) {
175 y = sqrtf(x[i]);
176 z = powf(x[i], 1.0 / 2.0);
178 if (fabsf(y - z) > eps)
179 atf_tc_fail_nonfatal("sqrtf(%0.03f) != "
180 "powf(%0.03f, 1/2)\n", x[i], x[i]);
184 ATF_TC(sqrtf_inf_neg);
185 ATF_TC_HEAD(sqrtf_inf_neg, tc)
187 atf_tc_set_md_var(tc, "descr", "Test sqrtf(-Inf) == NaN");
190 ATF_TC_BODY(sqrtf_inf_neg, tc)
192 const float x = -1.0L / 0.0L;
193 float y = sqrtf(x);
195 ATF_CHECK(isnan(y) != 0);
198 ATF_TC(sqrtf_inf_pos);
199 ATF_TC_HEAD(sqrtf_inf_pos, tc)
201 atf_tc_set_md_var(tc, "descr", "Test sqrtf(+Inf) == +Inf");
204 ATF_TC_BODY(sqrtf_inf_pos, tc)
206 const float x = 1.0L / 0.0L;
207 float y = sqrtf(x);
209 ATF_CHECK(isinf(y) != 0);
210 ATF_CHECK(signbit(y) == 0);
213 ATF_TC(sqrtf_zero_neg);
214 ATF_TC_HEAD(sqrtf_zero_neg, tc)
216 atf_tc_set_md_var(tc, "descr", "Test sqrtf(-0.0) == -0.0");
219 ATF_TC_BODY(sqrtf_zero_neg, tc)
221 const float x = -0.0L;
222 float y = sqrtf(x);
224 if (fabsf(y) > 0.0 || signbit(y) == 0)
225 atf_tc_fail_nonfatal("sqrtf(-0.0) != -0.0");
228 ATF_TC(sqrtf_zero_pos);
229 ATF_TC_HEAD(sqrtf_zero_pos, tc)
231 atf_tc_set_md_var(tc, "descr", "Test sqrtf(+0.0) == +0.0");
234 ATF_TC_BODY(sqrtf_zero_pos, tc)
236 const float x = 0.0L;
237 float y = sqrtf(x);
239 if (fabsf(y) > 0.0 || signbit(y) != 0)
240 atf_tc_fail_nonfatal("sqrtf(+0.0) != +0.0");
244 * sqrtl(3)
246 ATF_TC(sqrtl_nan);
247 ATF_TC_HEAD(sqrtl_nan, tc)
249 atf_tc_set_md_var(tc, "descr", "Test sqrtl(NaN) == NaN");
252 ATF_TC_BODY(sqrtl_nan, tc)
254 const long double x = 0.0L / 0.0L;
256 ATF_CHECK(isnan(x) != 0);
257 ATF_CHECK(isnan(sqrtl(x)) != 0);
260 ATF_TC(sqrtl_powl);
261 ATF_TC_HEAD(sqrtl_powl, tc)
263 atf_tc_set_md_var(tc, "descr", "Test sqrtl(3) vs. powl(3)");
266 ATF_TC_BODY(sqrtl_powl, tc)
268 const long double x[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
269 const long double eps = 5.0*DBL_EPSILON; /* XXX powl == pow for now */
270 volatile long double y, z;
271 size_t i;
273 for (i = 0; i < __arraycount(x); i++) {
275 y = sqrtl(x[i]);
276 z = powl(x[i], 1.0 / 2.0);
278 if (fabsl(y - z) > eps)
279 atf_tc_fail_nonfatal("sqrtl(%0.03Lf) != "
280 "powl(%0.03Lf, 1/2)\n", x[i], x[i]);
284 ATF_TC(sqrtl_inf_neg);
285 ATF_TC_HEAD(sqrtl_inf_neg, tc)
287 atf_tc_set_md_var(tc, "descr", "Test sqrtl(-Inf) == NaN");
290 ATF_TC_BODY(sqrtl_inf_neg, tc)
292 const long double x = -1.0L / 0.0L;
293 long double y = sqrtl(x);
295 ATF_CHECK(isnan(y) != 0);
298 ATF_TC(sqrtl_inf_pos);
299 ATF_TC_HEAD(sqrtl_inf_pos, tc)
301 atf_tc_set_md_var(tc, "descr", "Test sqrtl(+Inf) == +Inf");
304 ATF_TC_BODY(sqrtl_inf_pos, tc)
306 const long double x = 1.0L / 0.0L;
307 long double y = sqrtl(x);
309 ATF_CHECK(isinf(y) != 0);
310 ATF_CHECK(signbit(y) == 0);
313 ATF_TC(sqrtl_zero_neg);
314 ATF_TC_HEAD(sqrtl_zero_neg, tc)
316 atf_tc_set_md_var(tc, "descr", "Test sqrtl(-0.0) == -0.0");
319 ATF_TC_BODY(sqrtl_zero_neg, tc)
321 const long double x = -0.0L;
322 long double y = sqrtl(x);
324 if (fabsl(y) > 0.0 || signbit(y) == 0)
325 atf_tc_fail_nonfatal("sqrtl(-0.0) != -0.0");
328 ATF_TC(sqrtl_zero_pos);
329 ATF_TC_HEAD(sqrtl_zero_pos, tc)
331 atf_tc_set_md_var(tc, "descr", "Test sqrtl(+0.0) == +0.0");
334 ATF_TC_BODY(sqrtl_zero_pos, tc)
336 const long double x = 0.0L;
337 long double y = sqrtl(x);
339 if (fabsl(y) > 0.0 || signbit(y) != 0)
340 atf_tc_fail_nonfatal("sqrtl(+0.0) != +0.0");
343 ATF_TP_ADD_TCS(tp)
346 ATF_TP_ADD_TC(tp, sqrt_nan);
347 ATF_TP_ADD_TC(tp, sqrt_pow);
348 ATF_TP_ADD_TC(tp, sqrt_inf_neg);
349 ATF_TP_ADD_TC(tp, sqrt_inf_pos);
350 ATF_TP_ADD_TC(tp, sqrt_zero_neg);
351 ATF_TP_ADD_TC(tp, sqrt_zero_pos);
353 ATF_TP_ADD_TC(tp, sqrtf_nan);
354 ATF_TP_ADD_TC(tp, sqrtf_powf);
355 ATF_TP_ADD_TC(tp, sqrtf_inf_neg);
356 ATF_TP_ADD_TC(tp, sqrtf_inf_pos);
357 ATF_TP_ADD_TC(tp, sqrtf_zero_neg);
358 ATF_TP_ADD_TC(tp, sqrtf_zero_pos);
360 ATF_TP_ADD_TC(tp, sqrtl_nan);
361 ATF_TP_ADD_TC(tp, sqrtl_powl);
362 ATF_TP_ADD_TC(tp, sqrtl_inf_neg);
363 ATF_TP_ADD_TC(tp, sqrtl_inf_pos);
364 ATF_TP_ADD_TC(tp, sqrtl_zero_neg);
365 ATF_TP_ADD_TC(tp, sqrtl_zero_pos);
367 return atf_no_error();