1 /* $NetBSD: t_scalbn.c,v 1.11 2014/03/03 10:39:08 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_scalbn.c,v 1.11 2014/03/03 10:39:08 martin Exp $");
41 static const int exps[] = { 0, 1, -1, 100, -100 };
43 /* tests here do not require specific precision, so we just use double */
50 struct testcase test_vals[] = {
51 { 0, 1.00085, 1.00085, 0 },
52 { 0, 0.99755, 0.99755, 0 },
53 { 0, -1.00085, -1.00085, 0 },
54 { 0, -0.99755, -0.99755, 0 },
55 { 1, 1.00085, 2.0* 1.00085, 0 },
56 { 1, 0.99755, 2.0* 0.99755, 0 },
57 { 1, -1.00085, 2.0* -1.00085, 0 },
58 { 1, -0.99755, 2.0* -0.99755, 0 },
61 * We could add more corner test cases here, but we would have to
62 * add some ifdefs for the exact format and use a reliable
63 * generator program - bail for now and only do trivial stuff above.
71 ATF_TC_HEAD(scalbn_val, tc)
73 atf_tc_set_md_var(tc, "descr", "Test scalbn() for a few values");
76 ATF_TC_BODY(scalbn_val, tc)
78 const struct testcase *tests = test_vals;
79 const size_t tcnt = __arraycount(test_vals);
83 for (i = 0; i < tcnt; i++) {
87 rv = scalbn(tests[i].inval, tests[i].exp);
88 ATF_CHECK_EQ_MSG(errno, tests[i].error,
89 "test %zu: errno %d instead of %d", i, errno,
91 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*DBL_EPSILON,
92 "test %zu: return value %g instead of %g (difference %g)",
93 i, rv, tests[i].result, tests[i].result-rv);
98 ATF_TC_HEAD(scalbn_nan, tc)
100 atf_tc_set_md_var(tc, "descr", "Test scalbn(NaN, n) == NaN");
103 ATF_TC_BODY(scalbn_nan, tc)
105 const double x = 0.0L / 0.0L;
109 ATF_REQUIRE(isnan(x) != 0);
111 for (i = 0; i < __arraycount(exps); i++) {
112 y = scalbn(x, exps[i]);
113 ATF_CHECK(isnan(y) != 0);
117 ATF_TC(scalbn_inf_neg);
118 ATF_TC_HEAD(scalbn_inf_neg, tc)
120 atf_tc_set_md_var(tc, "descr", "Test scalbn(-Inf, n) == -Inf");
123 ATF_TC_BODY(scalbn_inf_neg, tc)
125 const double x = -1.0L / 0.0L;
128 for (i = 0; i < __arraycount(exps); i++)
129 ATF_CHECK(scalbn(x, exps[i]) == x);
132 ATF_TC(scalbn_inf_pos);
133 ATF_TC_HEAD(scalbn_inf_pos, tc)
135 atf_tc_set_md_var(tc, "descr", "Test scalbn(+Inf, n) == +Inf");
138 ATF_TC_BODY(scalbn_inf_pos, tc)
140 const double x = 1.0L / 0.0L;
143 for (i = 0; i < __arraycount(exps); i++)
144 ATF_CHECK(scalbn(x, exps[i]) == x);
147 ATF_TC(scalbn_ldexp);
148 ATF_TC_HEAD(scalbn_ldexp, tc)
150 atf_tc_set_md_var(tc, "descr", "Test scalbn(x, n) == ldexp(x, n)");
153 ATF_TC_BODY(scalbn_ldexp, tc)
156 const double x = 2.91288191221812821;
160 for (i = 0; i < __arraycount(exps); i++) {
161 y = scalbn(x, exps[i]);
162 ATF_CHECK_MSG(y == ldexp(x, exps[i]), "test %zu: exponent=%d, "
163 "y=%g, expected %g (diff: %g)", i, exps[i], y,
164 ldexp(x, exps[i]), y - ldexp(x, exps[i]));
169 ATF_TC(scalbn_zero_neg);
170 ATF_TC_HEAD(scalbn_zero_neg, tc)
172 atf_tc_set_md_var(tc, "descr", "Test scalbn(-0.0, n) == -0.0");
175 ATF_TC_BODY(scalbn_zero_neg, tc)
177 const double x = -0.0L;
181 ATF_REQUIRE(signbit(x) != 0);
183 for (i = 0; i < __arraycount(exps); i++) {
184 y = scalbn(x, exps[i]);
186 ATF_CHECK(signbit(y) != 0);
190 ATF_TC(scalbn_zero_pos);
191 ATF_TC_HEAD(scalbn_zero_pos, tc)
193 atf_tc_set_md_var(tc, "descr", "Test scalbn(+0.0, n) == +0.0");
196 ATF_TC_BODY(scalbn_zero_pos, tc)
198 const double x = 0.0L;
202 ATF_REQUIRE(signbit(x) == 0);
204 for (i = 0; i < __arraycount(exps); i++) {
205 y = scalbn(x, exps[i]);
207 ATF_CHECK(signbit(y) == 0);
215 ATF_TC_HEAD(scalbnf_val, tc)
217 atf_tc_set_md_var(tc, "descr", "Test scalbnf() for a few values");
220 ATF_TC_BODY(scalbnf_val, tc)
222 const struct testcase *tests = test_vals;
223 const size_t tcnt = __arraycount(test_vals);
227 for (i = 0; i < tcnt; i++) {
231 rv = scalbnf(tests[i].inval, tests[i].exp);
232 ATF_CHECK_EQ_MSG(errno, tests[i].error,
233 "test %zu: errno %d instead of %d", i, errno,
235 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*FLT_EPSILON,
236 "test %zu: return value %g instead of %g (difference %g)",
237 i, rv, tests[i].result, tests[i].result-rv);
242 ATF_TC_HEAD(scalbnf_nan, tc)
244 atf_tc_set_md_var(tc, "descr", "Test scalbnf(NaN, n) == NaN");
247 ATF_TC_BODY(scalbnf_nan, tc)
249 const float x = 0.0L / 0.0L;
253 ATF_REQUIRE(isnan(x) != 0);
255 for (i = 0; i < __arraycount(exps); i++) {
256 y = scalbnf(x, exps[i]);
257 ATF_CHECK(isnan(y) != 0);
261 ATF_TC(scalbnf_inf_neg);
262 ATF_TC_HEAD(scalbnf_inf_neg, tc)
264 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-Inf, n) == -Inf");
267 ATF_TC_BODY(scalbnf_inf_neg, tc)
269 const float x = -1.0L / 0.0L;
272 for (i = 0; i < __arraycount(exps); i++)
273 ATF_CHECK(scalbnf(x, exps[i]) == x);
276 ATF_TC(scalbnf_inf_pos);
277 ATF_TC_HEAD(scalbnf_inf_pos, tc)
279 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+Inf, n) == +Inf");
282 ATF_TC_BODY(scalbnf_inf_pos, tc)
284 const float x = 1.0L / 0.0L;
287 for (i = 0; i < __arraycount(exps); i++)
288 ATF_CHECK(scalbnf(x, exps[i]) == x);
291 ATF_TC(scalbnf_ldexpf);
292 ATF_TC_HEAD(scalbnf_ldexpf, tc)
294 atf_tc_set_md_var(tc, "descr", "Test scalbnf(x, n) == ldexpf(x, n)");
297 ATF_TC_BODY(scalbnf_ldexpf, tc)
300 const float x = 2.91288191221812821;
304 for (i = 0; i < __arraycount(exps); i++) {
305 y = scalbnf(x, exps[i]);
306 ATF_CHECK_MSG(y == ldexpf(x, exps[i]),
307 "test %zu: exponent=%d, y=%g ldexpf returns %g (diff: %g)",
308 i, exps[i], y, ldexpf(x, exps[i]), y-ldexpf(x, exps[i]));
313 ATF_TC(scalbnf_zero_neg);
314 ATF_TC_HEAD(scalbnf_zero_neg, tc)
316 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-0.0, n) == -0.0");
319 ATF_TC_BODY(scalbnf_zero_neg, tc)
321 const float x = -0.0L;
325 ATF_REQUIRE(signbit(x) != 0);
327 for (i = 0; i < __arraycount(exps); i++) {
328 y = scalbnf(x, exps[i]);
330 ATF_CHECK(signbit(y) != 0);
334 ATF_TC(scalbnf_zero_pos);
335 ATF_TC_HEAD(scalbnf_zero_pos, tc)
337 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+0.0, n) == +0.0");
340 ATF_TC_BODY(scalbnf_zero_pos, tc)
342 const float x = 0.0L;
346 ATF_REQUIRE(signbit(x) == 0);
348 for (i = 0; i < __arraycount(exps); i++) {
349 y = scalbnf(x, exps[i]);
351 ATF_CHECK(signbit(y) == 0);
359 ATF_TC_HEAD(scalbnl_val, tc)
361 atf_tc_set_md_var(tc, "descr", "Test scalbnl() for a few values");
364 ATF_TC_BODY(scalbnl_val, tc)
366 #ifndef __HAVE_LONG_DOUBLE
367 atf_tc_skip("Requires long double support");
369 const struct testcase *tests = test_vals;
370 const size_t tcnt = __arraycount(test_vals);
374 for (i = 0; i < tcnt; i++) {
378 rv = scalbnl(tests[i].inval, tests[i].exp);
379 ATF_CHECK_EQ_MSG(errno, tests[i].error,
380 "test %zu: errno %d instead of %d", i, errno,
382 ATF_CHECK_MSG(fabsl(rv-(long double)tests[i].result)<2.0*LDBL_EPSILON,
383 "test %zu: return value %Lg instead of %Lg (difference %Lg)",
384 i, rv, (long double)tests[i].result, (long double)tests[i].result-rv);
390 ATF_TC_HEAD(scalbnl_nan, tc)
392 atf_tc_set_md_var(tc, "descr", "Test scalbnl(NaN, n) == NaN");
395 ATF_TC_BODY(scalbnl_nan, tc)
397 #ifndef __HAVE_LONG_DOUBLE
398 atf_tc_skip("Requires long double support");
400 const long double x = 0.0L / 0.0L;
405 atf_tc_expect_fail("PR lib/45362");
406 atf_tc_fail("(0.0L / 0.0L) != NaN");
409 for (i = 0; i < __arraycount(exps); i++) {
410 y = scalbnl(x, exps[i]);
411 ATF_CHECK(isnan(y) != 0);
416 ATF_TC(scalbnl_inf_neg);
417 ATF_TC_HEAD(scalbnl_inf_neg, tc)
419 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-Inf, n) == -Inf");
422 ATF_TC_BODY(scalbnl_inf_neg, tc)
424 #ifndef __HAVE_LONG_DOUBLE
425 atf_tc_skip("Requires long double support");
427 const long double x = -1.0L / 0.0L;
430 for (i = 0; i < __arraycount(exps); i++)
431 ATF_CHECK(scalbnl(x, exps[i]) == x);
435 ATF_TC(scalbnl_inf_pos);
436 ATF_TC_HEAD(scalbnl_inf_pos, tc)
438 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+Inf, n) == +Inf");
441 ATF_TC_BODY(scalbnl_inf_pos, tc)
443 #ifndef __HAVE_LONG_DOUBLE
444 atf_tc_skip("Requires long double support");
446 const long double x = 1.0L / 0.0L;
449 for (i = 0; i < __arraycount(exps); i++)
450 ATF_CHECK(scalbnl(x, exps[i]) == x);
454 ATF_TC(scalbnl_zero_neg);
455 ATF_TC_HEAD(scalbnl_zero_neg, tc)
457 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-0.0, n) == -0.0");
460 ATF_TC_BODY(scalbnl_zero_neg, tc)
462 #ifndef __HAVE_LONG_DOUBLE
463 atf_tc_skip("Requires long double support");
465 const long double x = -0.0L;
469 ATF_REQUIRE(signbit(x) != 0);
471 for (i = 0; i < __arraycount(exps); i++) {
472 y = scalbnl(x, exps[i]);
474 ATF_CHECK(signbit(y) != 0);
479 ATF_TC(scalbnl_zero_pos);
480 ATF_TC_HEAD(scalbnl_zero_pos, tc)
482 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+0.0, n) == +0.0");
485 ATF_TC_BODY(scalbnl_zero_pos, tc)
487 #ifndef __HAVE_LONG_DOUBLE
488 atf_tc_skip("Requires long double support");
490 const long double x = 0.0L;
494 ATF_REQUIRE(signbit(x) == 0);
496 for (i = 0; i < __arraycount(exps); i++) {
497 y = scalbnl(x, exps[i]);
499 ATF_CHECK(signbit(y) == 0);
507 ATF_TP_ADD_TC(tp, scalbn_val);
508 ATF_TP_ADD_TC(tp, scalbn_nan);
509 ATF_TP_ADD_TC(tp, scalbn_inf_neg);
510 ATF_TP_ADD_TC(tp, scalbn_inf_pos);
511 ATF_TP_ADD_TC(tp, scalbn_ldexp);
512 ATF_TP_ADD_TC(tp, scalbn_zero_neg);
513 ATF_TP_ADD_TC(tp, scalbn_zero_pos);
515 ATF_TP_ADD_TC(tp, scalbnf_val);
516 ATF_TP_ADD_TC(tp, scalbnf_nan);
517 ATF_TP_ADD_TC(tp, scalbnf_inf_neg);
518 ATF_TP_ADD_TC(tp, scalbnf_inf_pos);
519 ATF_TP_ADD_TC(tp, scalbnf_ldexpf);
520 ATF_TP_ADD_TC(tp, scalbnf_zero_neg);
521 ATF_TP_ADD_TC(tp, scalbnf_zero_pos);
523 ATF_TP_ADD_TC(tp, scalbnl_val);
524 ATF_TP_ADD_TC(tp, scalbnl_nan);
525 ATF_TP_ADD_TC(tp, scalbnl_inf_neg);
526 ATF_TP_ADD_TC(tp, scalbnl_inf_pos);
527 /* ATF_TP_ADD_TC(tp, scalbnl_ldexp); */
528 ATF_TP_ADD_TC(tp, scalbnl_zero_neg);
529 ATF_TP_ADD_TC(tp, scalbnl_zero_pos);
531 return atf_no_error();