1 /* $NetBSD: t_scalbn.c,v 1.10 2013/05/24 11:47:13 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.10 2013/05/24 11:47:13 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
++) {
84 rv
= scalbn(tests
[i
].inval
, tests
[i
].exp
);
85 ATF_CHECK_EQ_MSG(errno
, tests
[i
].error
,
86 "test %zu: errno %d instead of %d", i
, errno
,
88 ATF_CHECK_MSG(fabs(rv
-tests
[i
].result
)<2.0*DBL_EPSILON
,
89 "test %zu: return value %g instead of %g (difference %g)",
90 i
, rv
, tests
[i
].result
, tests
[i
].result
-rv
);
95 ATF_TC_HEAD(scalbn_nan
, tc
)
97 atf_tc_set_md_var(tc
, "descr", "Test scalbn(NaN, n) == NaN");
100 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);
116 ATF_TC(scalbn_inf_neg
);
117 ATF_TC_HEAD(scalbn_inf_neg
, tc
)
119 atf_tc_set_md_var(tc
, "descr", "Test scalbn(-Inf, n) == -Inf");
122 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
);
133 ATF_TC(scalbn_inf_pos
);
134 ATF_TC_HEAD(scalbn_inf_pos
, tc
)
136 atf_tc_set_md_var(tc
, "descr", "Test scalbn(+Inf, n) == +Inf");
139 ATF_TC_BODY(scalbn_inf_pos
, tc
)
142 const double x
= 1.0L / 0.0L;
145 for (i
= 0; i
< __arraycount(exps
); i
++)
146 ATF_CHECK(scalbn(x
, exps
[i
]) == x
);
150 ATF_TC(scalbn_ldexp
);
151 ATF_TC_HEAD(scalbn_ldexp
, tc
)
153 atf_tc_set_md_var(tc
, "descr", "Test scalbn(x, n) == ldexp(x, n)");
156 ATF_TC_BODY(scalbn_ldexp
, tc
)
160 const double x
= 2.91288191221812821;
164 for (i
= 0; i
< __arraycount(exps
); i
++) {
165 y
= scalbn(x
, exps
[i
]);
166 ATF_CHECK_MSG(y
== ldexp(x
, exps
[i
]), "test %zu: exponent=%d, "
167 "y=%g, expected %g (diff: %g)", i
, exps
[i
], y
,
168 ldexp(x
, exps
[i
]), y
- ldexp(x
, exps
[i
]));
174 ATF_TC(scalbn_zero_neg
);
175 ATF_TC_HEAD(scalbn_zero_neg
, tc
)
177 atf_tc_set_md_var(tc
, "descr", "Test scalbn(-0.0, n) == -0.0");
180 ATF_TC_BODY(scalbn_zero_neg
, tc
)
183 const double x
= -0.0L;
187 ATF_REQUIRE(signbit(x
) != 0);
189 for (i
= 0; i
< __arraycount(exps
); i
++) {
190 y
= scalbn(x
, exps
[i
]);
192 ATF_CHECK(signbit(y
) != 0);
197 ATF_TC(scalbn_zero_pos
);
198 ATF_TC_HEAD(scalbn_zero_pos
, tc
)
200 atf_tc_set_md_var(tc
, "descr", "Test scalbn(+0.0, n) == +0.0");
203 ATF_TC_BODY(scalbn_zero_pos
, tc
)
206 const double x
= 0.0L;
210 ATF_REQUIRE(signbit(x
) == 0);
212 for (i
= 0; i
< __arraycount(exps
); i
++) {
213 y
= scalbn(x
, exps
[i
]);
215 ATF_CHECK(signbit(y
) == 0);
224 ATF_TC_HEAD(scalbnf_val
, tc
)
226 atf_tc_set_md_var(tc
, "descr", "Test scalbnf() for a few values");
229 ATF_TC_BODY(scalbnf_val
, tc
)
232 const struct testcase
*tests
= test_vals
;
233 const size_t tcnt
= __arraycount(test_vals
);
237 for (i
= 0; i
< tcnt
; i
++) {
238 rv
= scalbnf(tests
[i
].inval
, tests
[i
].exp
);
239 ATF_CHECK_EQ_MSG(errno
, tests
[i
].error
,
240 "test %zu: errno %d instead of %d", i
, errno
,
242 ATF_CHECK_MSG(fabs(rv
-tests
[i
].result
)<2.0*FLT_EPSILON
,
243 "test %zu: return value %g instead of %g (difference %g)",
244 i
, rv
, tests
[i
].result
, tests
[i
].result
-rv
);
250 ATF_TC_HEAD(scalbnf_nan
, tc
)
252 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(NaN, n) == NaN");
255 ATF_TC_BODY(scalbnf_nan
, tc
)
258 const float x
= 0.0L / 0.0L;
262 ATF_REQUIRE(isnan(x
) != 0);
264 for (i
= 0; i
< __arraycount(exps
); i
++) {
265 y
= scalbnf(x
, exps
[i
]);
266 ATF_CHECK(isnan(y
) != 0);
271 ATF_TC(scalbnf_inf_neg
);
272 ATF_TC_HEAD(scalbnf_inf_neg
, tc
)
274 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(-Inf, n) == -Inf");
277 ATF_TC_BODY(scalbnf_inf_neg
, 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
);
288 ATF_TC(scalbnf_inf_pos
);
289 ATF_TC_HEAD(scalbnf_inf_pos
, tc
)
291 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(+Inf, n) == +Inf");
294 ATF_TC_BODY(scalbnf_inf_pos
, tc
)
297 const float x
= 1.0L / 0.0L;
300 for (i
= 0; i
< __arraycount(exps
); i
++)
301 ATF_CHECK(scalbnf(x
, exps
[i
]) == x
);
305 ATF_TC(scalbnf_ldexpf
);
306 ATF_TC_HEAD(scalbnf_ldexpf
, tc
)
308 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(x, n) == ldexpf(x, n)");
311 ATF_TC_BODY(scalbnf_ldexpf
, tc
)
315 const float x
= 2.91288191221812821;
319 for (i
= 0; i
< __arraycount(exps
); i
++) {
320 y
= scalbnf(x
, exps
[i
]);
321 ATF_CHECK_MSG(y
== ldexpf(x
, exps
[i
]),
322 "test %zu: exponent=%d, y=%g ldexpf returns %g (diff: %g)",
323 i
, exps
[i
], y
, ldexpf(x
, exps
[i
]), y
-ldexpf(x
, exps
[i
]));
329 ATF_TC(scalbnf_zero_neg
);
330 ATF_TC_HEAD(scalbnf_zero_neg
, tc
)
332 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(-0.0, n) == -0.0");
335 ATF_TC_BODY(scalbnf_zero_neg
, 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);
352 ATF_TC(scalbnf_zero_pos
);
353 ATF_TC_HEAD(scalbnf_zero_pos
, tc
)
355 atf_tc_set_md_var(tc
, "descr", "Test scalbnf(+0.0, n) == +0.0");
358 ATF_TC_BODY(scalbnf_zero_pos
, tc
)
361 const float x
= 0.0L;
365 ATF_REQUIRE(signbit(x
) == 0);
367 for (i
= 0; i
< __arraycount(exps
); i
++) {
368 y
= scalbnf(x
, exps
[i
]);
370 ATF_CHECK(signbit(y
) == 0);
379 ATF_TC_HEAD(scalbnl_val
, tc
)
381 atf_tc_set_md_var(tc
, "descr", "Test scalbnl() for a few values");
384 ATF_TC_BODY(scalbnl_val
, tc
)
386 #ifndef __HAVE_LONG_DOUBLE
387 atf_tc_skip("Requires long double support");
389 const struct testcase
*tests
= test_vals
;
390 const size_t tcnt
= __arraycount(test_vals
);
394 for (i
= 0; i
< tcnt
; i
++) {
395 rv
= scalbnl(tests
[i
].inval
, tests
[i
].exp
);
396 ATF_CHECK_EQ_MSG(errno
, tests
[i
].error
,
397 "test %zu: errno %d instead of %d", i
, errno
,
399 ATF_CHECK_MSG(fabsl(rv
-(long double)tests
[i
].result
)<2.0*LDBL_EPSILON
,
400 "test %zu: return value %Lg instead of %Lg (difference %Lg)",
401 i
, rv
, (long double)tests
[i
].result
, (long double)tests
[i
].result
-rv
);
407 ATF_TC_HEAD(scalbnl_nan
, tc
)
409 atf_tc_set_md_var(tc
, "descr", "Test scalbnl(NaN, n) == NaN");
412 ATF_TC_BODY(scalbnl_nan
, tc
)
415 #ifndef __HAVE_LONG_DOUBLE
416 atf_tc_skip("Requires long double support");
418 const long double x
= 0.0L / 0.0L;
423 atf_tc_expect_fail("PR lib/45362");
424 atf_tc_fail("(0.0L / 0.0L) != NaN");
427 for (i
= 0; i
< __arraycount(exps
); i
++) {
428 y
= scalbnl(x
, exps
[i
]);
429 ATF_CHECK(isnan(y
) != 0);
435 ATF_TC(scalbnl_inf_neg
);
436 ATF_TC_HEAD(scalbnl_inf_neg
, tc
)
438 atf_tc_set_md_var(tc
, "descr", "Test scalbnl(-Inf, n) == -Inf");
441 ATF_TC_BODY(scalbnl_inf_neg
, tc
)
444 #ifndef __HAVE_LONG_DOUBLE
445 atf_tc_skip("Requires long double support");
447 const long double x
= -1.0L / 0.0L;
450 for (i
= 0; i
< __arraycount(exps
); i
++)
451 ATF_CHECK(scalbnl(x
, exps
[i
]) == x
);
456 ATF_TC(scalbnl_inf_pos
);
457 ATF_TC_HEAD(scalbnl_inf_pos
, tc
)
459 atf_tc_set_md_var(tc
, "descr", "Test scalbnl(+Inf, n) == +Inf");
462 ATF_TC_BODY(scalbnl_inf_pos
, tc
)
465 #ifndef __HAVE_LONG_DOUBLE
466 atf_tc_skip("Requires long double support");
468 const long double x
= 1.0L / 0.0L;
471 for (i
= 0; i
< __arraycount(exps
); i
++)
472 ATF_CHECK(scalbnl(x
, exps
[i
]) == x
);
477 ATF_TC(scalbnl_zero_neg
);
478 ATF_TC_HEAD(scalbnl_zero_neg
, tc
)
480 atf_tc_set_md_var(tc
, "descr", "Test scalbnl(-0.0, n) == -0.0");
483 ATF_TC_BODY(scalbnl_zero_neg
, tc
)
486 #ifndef __HAVE_LONG_DOUBLE
487 atf_tc_skip("Requires long double support");
489 const long double x
= -0.0L;
493 ATF_REQUIRE(signbit(x
) != 0);
495 for (i
= 0; i
< __arraycount(exps
); i
++) {
496 y
= scalbnl(x
, exps
[i
]);
498 ATF_CHECK(signbit(y
) != 0);
504 ATF_TC(scalbnl_zero_pos
);
505 ATF_TC_HEAD(scalbnl_zero_pos
, tc
)
507 atf_tc_set_md_var(tc
, "descr", "Test scalbnl(+0.0, n) == +0.0");
510 ATF_TC_BODY(scalbnl_zero_pos
, tc
)
513 #ifndef __HAVE_LONG_DOUBLE
514 atf_tc_skip("Requires long double support");
516 const long double x
= 0.0L;
520 ATF_REQUIRE(signbit(x
) == 0);
522 for (i
= 0; i
< __arraycount(exps
); i
++) {
523 y
= scalbnl(x
, exps
[i
]);
525 ATF_CHECK(signbit(y
) == 0);
534 ATF_TP_ADD_TC(tp
, scalbn_val
);
535 ATF_TP_ADD_TC(tp
, scalbn_nan
);
536 ATF_TP_ADD_TC(tp
, scalbn_inf_neg
);
537 ATF_TP_ADD_TC(tp
, scalbn_inf_pos
);
538 ATF_TP_ADD_TC(tp
, scalbn_ldexp
);
539 ATF_TP_ADD_TC(tp
, scalbn_zero_neg
);
540 ATF_TP_ADD_TC(tp
, scalbn_zero_pos
);
542 ATF_TP_ADD_TC(tp
, scalbnf_val
);
543 ATF_TP_ADD_TC(tp
, scalbnf_nan
);
544 ATF_TP_ADD_TC(tp
, scalbnf_inf_neg
);
545 ATF_TP_ADD_TC(tp
, scalbnf_inf_pos
);
546 ATF_TP_ADD_TC(tp
, scalbnf_ldexpf
);
547 ATF_TP_ADD_TC(tp
, scalbnf_zero_neg
);
548 ATF_TP_ADD_TC(tp
, scalbnf_zero_pos
);
550 ATF_TP_ADD_TC(tp
, scalbnl_val
);
551 ATF_TP_ADD_TC(tp
, scalbnl_nan
);
552 ATF_TP_ADD_TC(tp
, scalbnl_inf_neg
);
553 ATF_TP_ADD_TC(tp
, scalbnl_inf_pos
);
554 /* ATF_TP_ADD_TC(tp, scalbnl_ldexp); */
555 ATF_TP_ADD_TC(tp
, scalbnl_zero_neg
);
556 ATF_TP_ADD_TC(tp
, scalbnl_zero_pos
);
558 return atf_no_error();