tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libm / t_scalbn.c
blob4f15e44b7da3124b7987eebb740b0b99cbabea87
1 /* $NetBSD: t_scalbn.c,v 1.10 2013/05/24 11:47:13 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_scalbn.c,v 1.10 2013/05/24 11:47:13 martin Exp $");
34 #include <math.h>
35 #include <limits.h>
36 #include <float.h>
37 #include <errno.h>
39 #include <atf-c.h>
41 static const int exps[] = { 0, 1, -1, 100, -100 };
43 /* tests here do not require specific precision, so we just use double */
44 struct testcase {
45 int exp;
46 double inval;
47 double result;
48 int error;
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.
68 * scalbn(3)
70 ATF_TC(scalbn_val);
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);
80 size_t i;
81 double rv;
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,
87 tests[i].error);
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);
94 ATF_TC(scalbn_nan);
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)
102 #ifndef __vax__
103 const double x = 0.0L / 0.0L;
104 double y;
105 size_t i;
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);
113 #endif
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)
124 #ifndef __vax__
125 const double x = -1.0L / 0.0L;
126 size_t i;
128 for (i = 0; i < __arraycount(exps); i++)
129 ATF_CHECK(scalbn(x, exps[i]) == x);
130 #endif
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)
141 #ifndef __vax__
142 const double x = 1.0L / 0.0L;
143 size_t i;
145 for (i = 0; i < __arraycount(exps); i++)
146 ATF_CHECK(scalbn(x, exps[i]) == x);
147 #endif
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)
158 #ifndef __vax__
159 #if FLT_RADIX == 2
160 const double x = 2.91288191221812821;
161 double y;
162 size_t i;
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]));
170 #endif
171 #endif
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)
182 #ifndef __vax__
183 const double x = -0.0L;
184 double y;
185 size_t i;
187 ATF_REQUIRE(signbit(x) != 0);
189 for (i = 0; i < __arraycount(exps); i++) {
190 y = scalbn(x, exps[i]);
191 ATF_CHECK(x == y);
192 ATF_CHECK(signbit(y) != 0);
194 #endif
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)
205 #ifndef __vax__
206 const double x = 0.0L;
207 double y;
208 size_t i;
210 ATF_REQUIRE(signbit(x) == 0);
212 for (i = 0; i < __arraycount(exps); i++) {
213 y = scalbn(x, exps[i]);
214 ATF_CHECK(x == y);
215 ATF_CHECK(signbit(y) == 0);
217 #endif
221 * scalbnf(3)
223 ATF_TC(scalbnf_val);
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)
231 #ifndef __vax__
232 const struct testcase *tests = test_vals;
233 const size_t tcnt = __arraycount(test_vals);
234 size_t i;
235 double rv;
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,
241 tests[i].error);
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);
246 #endif
249 ATF_TC(scalbnf_nan);
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)
257 #ifndef __vax__
258 const float x = 0.0L / 0.0L;
259 float y;
260 size_t i;
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);
268 #endif
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)
279 #ifndef __vax__
280 const float x = -1.0L / 0.0L;
281 size_t i;
283 for (i = 0; i < __arraycount(exps); i++)
284 ATF_CHECK(scalbnf(x, exps[i]) == x);
285 #endif
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)
296 #ifndef __vax__
297 const float x = 1.0L / 0.0L;
298 size_t i;
300 for (i = 0; i < __arraycount(exps); i++)
301 ATF_CHECK(scalbnf(x, exps[i]) == x);
302 #endif
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)
313 #ifndef __vax__
314 #if FLT_RADIX == 2
315 const float x = 2.91288191221812821;
316 float y;
317 size_t i;
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]));
325 #endif
326 #endif
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)
337 #ifndef __vax__
338 const float x = -0.0L;
339 float y;
340 size_t i;
342 ATF_REQUIRE(signbit(x) != 0);
344 for (i = 0; i < __arraycount(exps); i++) {
345 y = scalbnf(x, exps[i]);
346 ATF_CHECK(x == y);
347 ATF_CHECK(signbit(y) != 0);
349 #endif
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)
360 #ifndef __vax__
361 const float x = 0.0L;
362 float y;
363 size_t i;
365 ATF_REQUIRE(signbit(x) == 0);
367 for (i = 0; i < __arraycount(exps); i++) {
368 y = scalbnf(x, exps[i]);
369 ATF_CHECK(x == y);
370 ATF_CHECK(signbit(y) == 0);
372 #endif
376 * scalbnl(3)
378 ATF_TC(scalbnl_val);
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");
388 #else
389 const struct testcase *tests = test_vals;
390 const size_t tcnt = __arraycount(test_vals);
391 size_t i;
392 long double rv;
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,
398 tests[i].error);
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);
403 #endif
406 ATF_TC(scalbnl_nan);
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)
414 #ifndef __vax__
415 #ifndef __HAVE_LONG_DOUBLE
416 atf_tc_skip("Requires long double support");
417 #else
418 const long double x = 0.0L / 0.0L;
419 long double y;
420 size_t i;
422 if (isnan(x) == 0) {
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);
431 #endif
432 #endif
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)
443 #ifndef __vax__
444 #ifndef __HAVE_LONG_DOUBLE
445 atf_tc_skip("Requires long double support");
446 #else
447 const long double x = -1.0L / 0.0L;
448 size_t i;
450 for (i = 0; i < __arraycount(exps); i++)
451 ATF_CHECK(scalbnl(x, exps[i]) == x);
452 #endif
453 #endif
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)
464 #ifndef __vax__
465 #ifndef __HAVE_LONG_DOUBLE
466 atf_tc_skip("Requires long double support");
467 #else
468 const long double x = 1.0L / 0.0L;
469 size_t i;
471 for (i = 0; i < __arraycount(exps); i++)
472 ATF_CHECK(scalbnl(x, exps[i]) == x);
473 #endif
474 #endif
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)
485 #ifndef __vax__
486 #ifndef __HAVE_LONG_DOUBLE
487 atf_tc_skip("Requires long double support");
488 #else
489 const long double x = -0.0L;
490 long double y;
491 size_t i;
493 ATF_REQUIRE(signbit(x) != 0);
495 for (i = 0; i < __arraycount(exps); i++) {
496 y = scalbnl(x, exps[i]);
497 ATF_CHECK(x == y);
498 ATF_CHECK(signbit(y) != 0);
500 #endif
501 #endif
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)
512 #ifndef __vax__
513 #ifndef __HAVE_LONG_DOUBLE
514 atf_tc_skip("Requires long double support");
515 #else
516 const long double x = 0.0L;
517 long double y;
518 size_t i;
520 ATF_REQUIRE(signbit(x) == 0);
522 for (i = 0; i < __arraycount(exps); i++) {
523 y = scalbnl(x, exps[i]);
524 ATF_CHECK(x == y);
525 ATF_CHECK(signbit(y) == 0);
527 #endif
528 #endif
531 ATF_TP_ADD_TCS(tp)
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();