1 /* $NetBSD: t_scalbn.c,v 1.14 2017/01/13 21:09:12 agc 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.14 2017/01/13 21:09:12 agc 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++) {
85 rv = scalbn(tests[i].inval, tests[i].exp);
86 ATF_CHECK_EQ_MSG(errno, tests[i].error,
87 "test %zu: errno %d instead of %d", i, errno,
89 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*DBL_EPSILON,
90 "test %zu: return value %g instead of %g (difference %g)",
91 i, rv, tests[i].result, tests[i].result-rv);
96 ATF_TC_HEAD(scalbn_nan, tc)
98 atf_tc_set_md_var(tc, "descr", "Test scalbn(NaN, n) == NaN");
101 ATF_TC_BODY(scalbn_nan, tc)
103 const double x = 0.0L / 0.0L;
107 ATF_REQUIRE(isnan(x) != 0);
109 for (i = 0; i < __arraycount(exps); i++) {
110 y = scalbn(x, exps[i]);
111 ATF_CHECK(isnan(y) != 0);
115 ATF_TC(scalbn_inf_neg);
116 ATF_TC_HEAD(scalbn_inf_neg, tc)
118 atf_tc_set_md_var(tc, "descr", "Test scalbn(-Inf, n) == -Inf");
121 ATF_TC_BODY(scalbn_inf_neg, tc)
123 const double x = -1.0L / 0.0L;
126 for (i = 0; i < __arraycount(exps); i++)
127 ATF_CHECK(scalbn(x, exps[i]) == x);
130 ATF_TC(scalbn_inf_pos);
131 ATF_TC_HEAD(scalbn_inf_pos, tc)
133 atf_tc_set_md_var(tc, "descr", "Test scalbn(+Inf, n) == +Inf");
136 ATF_TC_BODY(scalbn_inf_pos, tc)
138 const double x = 1.0L / 0.0L;
141 for (i = 0; i < __arraycount(exps); i++)
142 ATF_CHECK(scalbn(x, exps[i]) == x);
145 ATF_TC(scalbn_ldexp);
146 ATF_TC_HEAD(scalbn_ldexp, tc)
148 atf_tc_set_md_var(tc, "descr", "Test scalbn(x, n) == ldexp(x, n)");
151 ATF_TC_BODY(scalbn_ldexp, tc)
154 const double x = 2.91288191221812821;
158 for (i = 0; i < __arraycount(exps); i++) {
159 y = scalbn(x, exps[i]);
160 ATF_CHECK_MSG(y == ldexp(x, exps[i]), "test %zu: exponent=%d, "
161 "y=%g, expected %g (diff: %g)", i, exps[i], y,
162 ldexp(x, exps[i]), y - ldexp(x, exps[i]));
167 ATF_TC(scalbn_zero_neg);
168 ATF_TC_HEAD(scalbn_zero_neg, tc)
170 atf_tc_set_md_var(tc, "descr", "Test scalbn(-0.0, n) == -0.0");
173 ATF_TC_BODY(scalbn_zero_neg, tc)
175 const double x = -0.0L;
179 ATF_REQUIRE(signbit(x) != 0);
181 for (i = 0; i < __arraycount(exps); i++) {
182 y = scalbn(x, exps[i]);
184 ATF_CHECK(signbit(y) != 0);
188 ATF_TC(scalbn_zero_pos);
189 ATF_TC_HEAD(scalbn_zero_pos, tc)
191 atf_tc_set_md_var(tc, "descr", "Test scalbn(+0.0, n) == +0.0");
194 ATF_TC_BODY(scalbn_zero_pos, tc)
196 const double x = 0.0L;
200 ATF_REQUIRE(signbit(x) == 0);
202 for (i = 0; i < __arraycount(exps); i++) {
203 y = scalbn(x, exps[i]);
205 ATF_CHECK(signbit(y) == 0);
213 ATF_TC_HEAD(scalbnf_val, tc)
215 atf_tc_set_md_var(tc, "descr", "Test scalbnf() for a few values");
218 ATF_TC_BODY(scalbnf_val, tc)
220 const struct testcase *tests = test_vals;
221 const size_t tcnt = __arraycount(test_vals);
225 for (i = 0; i < tcnt; i++) {
227 rv = scalbnf(tests[i].inval, tests[i].exp);
228 ATF_CHECK_EQ_MSG(errno, tests[i].error,
229 "test %zu: errno %d instead of %d", i, errno,
231 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*FLT_EPSILON,
232 "test %zu: return value %g instead of %g (difference %g)",
233 i, rv, tests[i].result, tests[i].result-rv);
238 ATF_TC_HEAD(scalbnf_nan, tc)
240 atf_tc_set_md_var(tc, "descr", "Test scalbnf(NaN, n) == NaN");
243 ATF_TC_BODY(scalbnf_nan, tc)
245 const float x = 0.0L / 0.0L;
249 ATF_REQUIRE(isnan(x) != 0);
251 for (i = 0; i < __arraycount(exps); i++) {
252 y = scalbnf(x, exps[i]);
253 ATF_CHECK(isnan(y) != 0);
257 ATF_TC(scalbnf_inf_neg);
258 ATF_TC_HEAD(scalbnf_inf_neg, tc)
260 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-Inf, n) == -Inf");
263 ATF_TC_BODY(scalbnf_inf_neg, tc)
265 const float x = -1.0L / 0.0L;
268 for (i = 0; i < __arraycount(exps); i++)
269 ATF_CHECK(scalbnf(x, exps[i]) == x);
272 ATF_TC(scalbnf_inf_pos);
273 ATF_TC_HEAD(scalbnf_inf_pos, tc)
275 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+Inf, n) == +Inf");
278 ATF_TC_BODY(scalbnf_inf_pos, tc)
280 const float x = 1.0L / 0.0L;
283 for (i = 0; i < __arraycount(exps); i++)
284 ATF_CHECK(scalbnf(x, exps[i]) == x);
287 ATF_TC(scalbnf_ldexpf);
288 ATF_TC_HEAD(scalbnf_ldexpf, tc)
290 atf_tc_set_md_var(tc, "descr", "Test scalbnf(x, n) == ldexpf(x, n)");
293 ATF_TC_BODY(scalbnf_ldexpf, tc)
296 const float x = 2.91288191221812821;
300 for (i = 0; i < __arraycount(exps); i++) {
301 y = scalbnf(x, exps[i]);
302 ATF_CHECK_MSG(y == ldexpf(x, exps[i]),
303 "test %zu: exponent=%d, y=%g ldexpf returns %g (diff: %g)",
304 i, exps[i], y, ldexpf(x, exps[i]), y-ldexpf(x, exps[i]));
309 ATF_TC(scalbnf_zero_neg);
310 ATF_TC_HEAD(scalbnf_zero_neg, tc)
312 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-0.0, n) == -0.0");
315 ATF_TC_BODY(scalbnf_zero_neg, tc)
317 const float x = -0.0L;
321 ATF_REQUIRE(signbit(x) != 0);
323 for (i = 0; i < __arraycount(exps); i++) {
324 y = scalbnf(x, exps[i]);
326 ATF_CHECK(signbit(y) != 0);
330 ATF_TC(scalbnf_zero_pos);
331 ATF_TC_HEAD(scalbnf_zero_pos, tc)
333 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+0.0, n) == +0.0");
336 ATF_TC_BODY(scalbnf_zero_pos, tc)
338 const float x = 0.0L;
342 ATF_REQUIRE(signbit(x) == 0);
344 for (i = 0; i < __arraycount(exps); i++) {
345 y = scalbnf(x, exps[i]);
347 ATF_CHECK(signbit(y) == 0);
355 ATF_TC_HEAD(scalbnl_val, tc)
357 atf_tc_set_md_var(tc, "descr", "Test scalbnl() for a few values");
360 ATF_TC_BODY(scalbnl_val, tc)
362 #ifndef __HAVE_LONG_DOUBLE
363 atf_tc_skip("Requires long double support");
365 const struct testcase *tests = test_vals;
366 const size_t tcnt = __arraycount(test_vals);
370 for (i = 0; i < tcnt; i++) {
372 rv = scalbnl(tests[i].inval, tests[i].exp);
373 ATF_CHECK_EQ_MSG(errno, tests[i].error,
374 "test %zu: errno %d instead of %d", i, errno,
376 ATF_CHECK_MSG(fabsl(rv-(long double)tests[i].result)<2.0*LDBL_EPSILON,
377 "test %zu: return value %Lg instead of %Lg (difference %Lg)",
378 i, rv, (long double)tests[i].result, (long double)tests[i].result-rv);
384 ATF_TC_HEAD(scalbnl_nan, tc)
386 atf_tc_set_md_var(tc, "descr", "Test scalbnl(NaN, n) == NaN");
389 ATF_TC_BODY(scalbnl_nan, tc)
391 #ifndef __HAVE_LONG_DOUBLE
392 atf_tc_skip("Requires long double support");
394 const long double x = 0.0L / 0.0L;
399 atf_tc_expect_fail("PR lib/45362");
400 atf_tc_fail("(0.0L / 0.0L) != NaN");
403 for (i = 0; i < __arraycount(exps); i++) {
404 y = scalbnl(x, exps[i]);
405 ATF_CHECK(isnan(y) != 0);
410 ATF_TC(scalbnl_inf_neg);
411 ATF_TC_HEAD(scalbnl_inf_neg, tc)
413 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-Inf, n) == -Inf");
416 ATF_TC_BODY(scalbnl_inf_neg, tc)
418 #ifndef __HAVE_LONG_DOUBLE
419 atf_tc_skip("Requires long double support");
421 const long double x = -1.0L / 0.0L;
424 for (i = 0; i < __arraycount(exps); i++)
425 ATF_CHECK(scalbnl(x, exps[i]) == x);
429 ATF_TC(scalbnl_inf_pos);
430 ATF_TC_HEAD(scalbnl_inf_pos, tc)
432 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+Inf, n) == +Inf");
435 ATF_TC_BODY(scalbnl_inf_pos, tc)
437 #ifndef __HAVE_LONG_DOUBLE
438 atf_tc_skip("Requires long double support");
440 const long double x = 1.0L / 0.0L;
443 for (i = 0; i < __arraycount(exps); i++)
444 ATF_CHECK(scalbnl(x, exps[i]) == x);
448 ATF_TC(scalbnl_zero_neg);
449 ATF_TC_HEAD(scalbnl_zero_neg, tc)
451 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-0.0, n) == -0.0");
454 ATF_TC_BODY(scalbnl_zero_neg, tc)
456 #ifndef __HAVE_LONG_DOUBLE
457 atf_tc_skip("Requires long double support");
459 const long double x = -0.0L;
463 ATF_REQUIRE(signbit(x) != 0);
465 for (i = 0; i < __arraycount(exps); i++) {
466 y = scalbnl(x, exps[i]);
468 ATF_CHECK(signbit(y) != 0);
473 ATF_TC(scalbnl_zero_pos);
474 ATF_TC_HEAD(scalbnl_zero_pos, tc)
476 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+0.0, n) == +0.0");
479 ATF_TC_BODY(scalbnl_zero_pos, tc)
481 #ifndef __HAVE_LONG_DOUBLE
482 atf_tc_skip("Requires long double support");
484 const long double x = 0.0L;
488 ATF_REQUIRE(signbit(x) == 0);
490 for (i = 0; i < __arraycount(exps); i++) {
491 y = scalbnl(x, exps[i]);
493 ATF_CHECK(signbit(y) == 0);
501 ATF_TP_ADD_TC(tp, scalbn_val);
502 ATF_TP_ADD_TC(tp, scalbn_nan);
503 ATF_TP_ADD_TC(tp, scalbn_inf_neg);
504 ATF_TP_ADD_TC(tp, scalbn_inf_pos);
505 ATF_TP_ADD_TC(tp, scalbn_ldexp);
506 ATF_TP_ADD_TC(tp, scalbn_zero_neg);
507 ATF_TP_ADD_TC(tp, scalbn_zero_pos);
509 ATF_TP_ADD_TC(tp, scalbnf_val);
510 ATF_TP_ADD_TC(tp, scalbnf_nan);
511 ATF_TP_ADD_TC(tp, scalbnf_inf_neg);
512 ATF_TP_ADD_TC(tp, scalbnf_inf_pos);
513 ATF_TP_ADD_TC(tp, scalbnf_ldexpf);
514 ATF_TP_ADD_TC(tp, scalbnf_zero_neg);
515 ATF_TP_ADD_TC(tp, scalbnf_zero_pos);
517 ATF_TP_ADD_TC(tp, scalbnl_val);
518 ATF_TP_ADD_TC(tp, scalbnl_nan);
519 ATF_TP_ADD_TC(tp, scalbnl_inf_neg);
520 ATF_TP_ADD_TC(tp, scalbnl_inf_pos);
521 /* ATF_TP_ADD_TC(tp, scalbnl_ldexp); */
522 ATF_TP_ADD_TC(tp, scalbnl_zero_neg);
523 ATF_TP_ADD_TC(tp, scalbnl_zero_pos);
525 return atf_no_error();