1 /* $NetBSD: t_sqrt.c,v 1.5 2013/11/22 17:19:14 martin Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.5 2013/11/22 17:19:14 martin Exp $");
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
)
51 const double x
= 0.0L / 0.0L;
53 ATF_CHECK(isnan(x
) != 0);
54 ATF_CHECK(isnan(sqrt(x
)) != 0);
59 ATF_TC_HEAD(sqrt_pow
, tc
)
61 atf_tc_set_md_var(tc
, "descr", "Test sqrt(3) vs. pow(3)");
64 ATF_TC_BODY(sqrt_pow
, tc
)
67 const double x
[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
68 const double eps
= 1.0e-40;
72 for (i
= 0; i
< __arraycount(x
); i
++) {
75 z
= pow(x
[i
], 1.0 / 2.0);
77 if (fabs(y
- z
) > eps
)
78 atf_tc_fail_nonfatal("sqrt(%0.03f) != "
79 "pow(%0.03f, 1/2)\n", x
[i
], x
[i
]);
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
)
93 const double x
= -1.0L / 0.0L;
96 ATF_CHECK(isnan(y
) != 0);
100 ATF_TC(sqrt_inf_pos
);
101 ATF_TC_HEAD(sqrt_inf_pos
, tc
)
103 atf_tc_set_md_var(tc
, "descr", "Test sqrt(+Inf) == +Inf");
106 ATF_TC_BODY(sqrt_inf_pos
, tc
)
109 const double x
= 1.0L / 0.0L;
112 ATF_CHECK(isinf(y
) != 0);
113 ATF_CHECK(signbit(y
) == 0);
117 ATF_TC(sqrt_zero_neg
);
118 ATF_TC_HEAD(sqrt_zero_neg
, tc
)
120 atf_tc_set_md_var(tc
, "descr", "Test sqrt(-0.0) == -0.0");
123 ATF_TC_BODY(sqrt_zero_neg
, tc
)
126 const double x
= -0.0L;
129 if (fabs(y
) > 0.0 || signbit(y
) == 0)
130 atf_tc_fail_nonfatal("sqrt(-0.0) != -0.0");
134 ATF_TC(sqrt_zero_pos
);
135 ATF_TC_HEAD(sqrt_zero_pos
, tc
)
137 atf_tc_set_md_var(tc
, "descr", "Test sqrt(+0.0) == +0.0");
140 ATF_TC_BODY(sqrt_zero_pos
, tc
)
143 const double x
= 0.0L;
146 if (fabs(y
) > 0.0 || signbit(y
) != 0)
147 atf_tc_fail_nonfatal("sqrt(+0.0) != +0.0");
155 ATF_TC_HEAD(sqrtf_nan
, tc
)
157 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(NaN) == NaN");
160 ATF_TC_BODY(sqrtf_nan
, tc
)
163 const float x
= 0.0L / 0.0L;
165 ATF_CHECK(isnan(x
) != 0);
166 ATF_CHECK(isnan(sqrtf(x
)) != 0);
171 ATF_TC_HEAD(sqrtf_powf
, tc
)
173 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(3) vs. powf(3)");
176 ATF_TC_BODY(sqrtf_powf
, tc
)
179 const float x
[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
180 const float eps
= 1.0e-30;
184 for (i
= 0; i
< __arraycount(x
); i
++) {
187 z
= powf(x
[i
], 1.0 / 2.0);
189 if (fabsf(y
- z
) > eps
)
190 atf_tc_fail_nonfatal("sqrtf(%0.03f) != "
191 "powf(%0.03f, 1/2)\n", x
[i
], x
[i
]);
196 ATF_TC(sqrtf_inf_neg
);
197 ATF_TC_HEAD(sqrtf_inf_neg
, tc
)
199 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(-Inf) == NaN");
202 ATF_TC_BODY(sqrtf_inf_neg
, tc
)
205 const float x
= -1.0L / 0.0L;
208 ATF_CHECK(isnan(y
) != 0);
212 ATF_TC(sqrtf_inf_pos
);
213 ATF_TC_HEAD(sqrtf_inf_pos
, tc
)
215 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(+Inf) == +Inf");
218 ATF_TC_BODY(sqrtf_inf_pos
, tc
)
221 const float x
= 1.0L / 0.0L;
224 ATF_CHECK(isinf(y
) != 0);
225 ATF_CHECK(signbit(y
) == 0);
229 ATF_TC(sqrtf_zero_neg
);
230 ATF_TC_HEAD(sqrtf_zero_neg
, tc
)
232 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(-0.0) == -0.0");
235 ATF_TC_BODY(sqrtf_zero_neg
, tc
)
238 const float x
= -0.0L;
241 if (fabsf(y
) > 0.0 || signbit(y
) == 0)
242 atf_tc_fail_nonfatal("sqrtf(-0.0) != -0.0");
246 ATF_TC(sqrtf_zero_pos
);
247 ATF_TC_HEAD(sqrtf_zero_pos
, tc
)
249 atf_tc_set_md_var(tc
, "descr", "Test sqrtf(+0.0) == +0.0");
252 ATF_TC_BODY(sqrtf_zero_pos
, tc
)
255 const float x
= 0.0L;
258 if (fabsf(y
) > 0.0 || signbit(y
) != 0)
259 atf_tc_fail_nonfatal("sqrtf(+0.0) != +0.0");
267 ATF_TC_HEAD(sqrtl_nan
, tc
)
269 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(NaN) == NaN");
272 ATF_TC_BODY(sqrtl_nan
, tc
)
275 const long double x
= 0.0L / 0.0L;
277 ATF_CHECK(isnan(x
) != 0);
278 ATF_CHECK(isnan(sqrtl(x
)) != 0);
283 ATF_TC_HEAD(sqrtl_powl
, tc
)
285 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(3) vs. powl(3)");
288 ATF_TC_BODY(sqrtl_powl
, tc
)
291 const long double x
[] = { 0.0, 0.005, 1.0, 99.0, 123.123, 9999.9999 };
292 const long double eps
= 5.0*DBL_EPSILON
; /* XXX powl == pow for now */
293 volatile long double y
, z
;
296 for (i
= 0; i
< __arraycount(x
); i
++) {
299 z
= powl(x
[i
], 1.0 / 2.0);
301 if (fabsl(y
- z
) > eps
)
302 atf_tc_fail_nonfatal("sqrtl(%0.03Lf) != "
303 "powl(%0.03Lf, 1/2)\n", x
[i
], x
[i
]);
308 ATF_TC(sqrtl_inf_neg
);
309 ATF_TC_HEAD(sqrtl_inf_neg
, tc
)
311 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(-Inf) == NaN");
314 ATF_TC_BODY(sqrtl_inf_neg
, tc
)
317 const long double x
= -1.0L / 0.0L;
318 long double y
= sqrtl(x
);
320 ATF_CHECK(isnan(y
) != 0);
324 ATF_TC(sqrtl_inf_pos
);
325 ATF_TC_HEAD(sqrtl_inf_pos
, tc
)
327 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(+Inf) == +Inf");
330 ATF_TC_BODY(sqrtl_inf_pos
, tc
)
333 const long double x
= 1.0L / 0.0L;
334 long double y
= sqrtl(x
);
336 ATF_CHECK(isinf(y
) != 0);
337 ATF_CHECK(signbit(y
) == 0);
341 ATF_TC(sqrtl_zero_neg
);
342 ATF_TC_HEAD(sqrtl_zero_neg
, tc
)
344 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(-0.0) == -0.0");
347 ATF_TC_BODY(sqrtl_zero_neg
, tc
)
350 const long double x
= -0.0L;
351 long double y
= sqrtl(x
);
353 if (fabsl(y
) > 0.0 || signbit(y
) == 0)
354 atf_tc_fail_nonfatal("sqrtl(-0.0) != -0.0");
358 ATF_TC(sqrtl_zero_pos
);
359 ATF_TC_HEAD(sqrtl_zero_pos
, tc
)
361 atf_tc_set_md_var(tc
, "descr", "Test sqrtl(+0.0) == +0.0");
364 ATF_TC_BODY(sqrtl_zero_pos
, tc
)
367 const long double x
= 0.0L;
368 long double y
= sqrtl(x
);
370 if (fabsl(y
) > 0.0 || signbit(y
) != 0)
371 atf_tc_fail_nonfatal("sqrtl(+0.0) != +0.0");
378 ATF_TP_ADD_TC(tp
, sqrt_nan
);
379 ATF_TP_ADD_TC(tp
, sqrt_pow
);
380 ATF_TP_ADD_TC(tp
, sqrt_inf_neg
);
381 ATF_TP_ADD_TC(tp
, sqrt_inf_pos
);
382 ATF_TP_ADD_TC(tp
, sqrt_zero_neg
);
383 ATF_TP_ADD_TC(tp
, sqrt_zero_pos
);
385 ATF_TP_ADD_TC(tp
, sqrtf_nan
);
386 ATF_TP_ADD_TC(tp
, sqrtf_powf
);
387 ATF_TP_ADD_TC(tp
, sqrtf_inf_neg
);
388 ATF_TP_ADD_TC(tp
, sqrtf_inf_pos
);
389 ATF_TP_ADD_TC(tp
, sqrtf_zero_neg
);
390 ATF_TP_ADD_TC(tp
, sqrtf_zero_pos
);
392 ATF_TP_ADD_TC(tp
, sqrtl_nan
);
393 ATF_TP_ADD_TC(tp
, sqrtl_powl
);
394 ATF_TP_ADD_TC(tp
, sqrtl_inf_neg
);
395 ATF_TP_ADD_TC(tp
, sqrtl_inf_pos
);
396 ATF_TP_ADD_TC(tp
, sqrtl_zero_neg
);
397 ATF_TP_ADD_TC(tp
, sqrtl_zero_pos
);
399 return atf_no_error();