etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libm / t_scalbn.c
blob586c2c3bc621878ac4c92306cee3760f17af1da7
1 /* $NetBSD: t_scalbn.c,v 1.11 2014/03/03 10:39:08 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.11 2014/03/03 10:39:08 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 const double x = 0.0L / 0.0L;
103 double y;
104 size_t i;
106 ATF_REQUIRE(isnan(x) != 0);
108 for (i = 0; i < __arraycount(exps); i++) {
109 y = scalbn(x, exps[i]);
110 ATF_CHECK(isnan(y) != 0);
114 ATF_TC(scalbn_inf_neg);
115 ATF_TC_HEAD(scalbn_inf_neg, tc)
117 atf_tc_set_md_var(tc, "descr", "Test scalbn(-Inf, n) == -Inf");
120 ATF_TC_BODY(scalbn_inf_neg, tc)
122 const double x = -1.0L / 0.0L;
123 size_t i;
125 for (i = 0; i < __arraycount(exps); i++)
126 ATF_CHECK(scalbn(x, exps[i]) == x);
129 ATF_TC(scalbn_inf_pos);
130 ATF_TC_HEAD(scalbn_inf_pos, tc)
132 atf_tc_set_md_var(tc, "descr", "Test scalbn(+Inf, n) == +Inf");
135 ATF_TC_BODY(scalbn_inf_pos, tc)
137 const double x = 1.0L / 0.0L;
138 size_t i;
140 for (i = 0; i < __arraycount(exps); i++)
141 ATF_CHECK(scalbn(x, exps[i]) == x);
144 ATF_TC(scalbn_ldexp);
145 ATF_TC_HEAD(scalbn_ldexp, tc)
147 atf_tc_set_md_var(tc, "descr", "Test scalbn(x, n) == ldexp(x, n)");
150 ATF_TC_BODY(scalbn_ldexp, tc)
152 #if FLT_RADIX == 2
153 const double x = 2.91288191221812821;
154 double y;
155 size_t i;
157 for (i = 0; i < __arraycount(exps); i++) {
158 y = scalbn(x, exps[i]);
159 ATF_CHECK_MSG(y == ldexp(x, exps[i]), "test %zu: exponent=%d, "
160 "y=%g, expected %g (diff: %g)", i, exps[i], y,
161 ldexp(x, exps[i]), y - ldexp(x, exps[i]));
163 #endif
166 ATF_TC(scalbn_zero_neg);
167 ATF_TC_HEAD(scalbn_zero_neg, tc)
169 atf_tc_set_md_var(tc, "descr", "Test scalbn(-0.0, n) == -0.0");
172 ATF_TC_BODY(scalbn_zero_neg, tc)
174 const double x = -0.0L;
175 double y;
176 size_t i;
178 ATF_REQUIRE(signbit(x) != 0);
180 for (i = 0; i < __arraycount(exps); i++) {
181 y = scalbn(x, exps[i]);
182 ATF_CHECK(x == y);
183 ATF_CHECK(signbit(y) != 0);
187 ATF_TC(scalbn_zero_pos);
188 ATF_TC_HEAD(scalbn_zero_pos, tc)
190 atf_tc_set_md_var(tc, "descr", "Test scalbn(+0.0, n) == +0.0");
193 ATF_TC_BODY(scalbn_zero_pos, tc)
195 const double x = 0.0L;
196 double y;
197 size_t i;
199 ATF_REQUIRE(signbit(x) == 0);
201 for (i = 0; i < __arraycount(exps); i++) {
202 y = scalbn(x, exps[i]);
203 ATF_CHECK(x == y);
204 ATF_CHECK(signbit(y) == 0);
209 * scalbnf(3)
211 ATF_TC(scalbnf_val);
212 ATF_TC_HEAD(scalbnf_val, tc)
214 atf_tc_set_md_var(tc, "descr", "Test scalbnf() for a few values");
217 ATF_TC_BODY(scalbnf_val, tc)
219 const struct testcase *tests = test_vals;
220 const size_t tcnt = __arraycount(test_vals);
221 size_t i;
222 double rv;
224 for (i = 0; i < tcnt; i++) {
225 rv = scalbnf(tests[i].inval, tests[i].exp);
226 ATF_CHECK_EQ_MSG(errno, tests[i].error,
227 "test %zu: errno %d instead of %d", i, errno,
228 tests[i].error);
229 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*FLT_EPSILON,
230 "test %zu: return value %g instead of %g (difference %g)",
231 i, rv, tests[i].result, tests[i].result-rv);
235 ATF_TC(scalbnf_nan);
236 ATF_TC_HEAD(scalbnf_nan, tc)
238 atf_tc_set_md_var(tc, "descr", "Test scalbnf(NaN, n) == NaN");
241 ATF_TC_BODY(scalbnf_nan, tc)
243 const float x = 0.0L / 0.0L;
244 float y;
245 size_t i;
247 ATF_REQUIRE(isnan(x) != 0);
249 for (i = 0; i < __arraycount(exps); i++) {
250 y = scalbnf(x, exps[i]);
251 ATF_CHECK(isnan(y) != 0);
255 ATF_TC(scalbnf_inf_neg);
256 ATF_TC_HEAD(scalbnf_inf_neg, tc)
258 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-Inf, n) == -Inf");
261 ATF_TC_BODY(scalbnf_inf_neg, tc)
263 const float x = -1.0L / 0.0L;
264 size_t i;
266 for (i = 0; i < __arraycount(exps); i++)
267 ATF_CHECK(scalbnf(x, exps[i]) == x);
270 ATF_TC(scalbnf_inf_pos);
271 ATF_TC_HEAD(scalbnf_inf_pos, tc)
273 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+Inf, n) == +Inf");
276 ATF_TC_BODY(scalbnf_inf_pos, tc)
278 const float x = 1.0L / 0.0L;
279 size_t i;
281 for (i = 0; i < __arraycount(exps); i++)
282 ATF_CHECK(scalbnf(x, exps[i]) == x);
285 ATF_TC(scalbnf_ldexpf);
286 ATF_TC_HEAD(scalbnf_ldexpf, tc)
288 atf_tc_set_md_var(tc, "descr", "Test scalbnf(x, n) == ldexpf(x, n)");
291 ATF_TC_BODY(scalbnf_ldexpf, tc)
293 #if FLT_RADIX == 2
294 const float x = 2.91288191221812821;
295 float y;
296 size_t i;
298 for (i = 0; i < __arraycount(exps); i++) {
299 y = scalbnf(x, exps[i]);
300 ATF_CHECK_MSG(y == ldexpf(x, exps[i]),
301 "test %zu: exponent=%d, y=%g ldexpf returns %g (diff: %g)",
302 i, exps[i], y, ldexpf(x, exps[i]), y-ldexpf(x, exps[i]));
304 #endif
307 ATF_TC(scalbnf_zero_neg);
308 ATF_TC_HEAD(scalbnf_zero_neg, tc)
310 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-0.0, n) == -0.0");
313 ATF_TC_BODY(scalbnf_zero_neg, tc)
315 const float x = -0.0L;
316 float y;
317 size_t i;
319 ATF_REQUIRE(signbit(x) != 0);
321 for (i = 0; i < __arraycount(exps); i++) {
322 y = scalbnf(x, exps[i]);
323 ATF_CHECK(x == y);
324 ATF_CHECK(signbit(y) != 0);
328 ATF_TC(scalbnf_zero_pos);
329 ATF_TC_HEAD(scalbnf_zero_pos, tc)
331 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+0.0, n) == +0.0");
334 ATF_TC_BODY(scalbnf_zero_pos, tc)
336 const float x = 0.0L;
337 float y;
338 size_t i;
340 ATF_REQUIRE(signbit(x) == 0);
342 for (i = 0; i < __arraycount(exps); i++) {
343 y = scalbnf(x, exps[i]);
344 ATF_CHECK(x == y);
345 ATF_CHECK(signbit(y) == 0);
350 * scalbnl(3)
352 ATF_TC(scalbnl_val);
353 ATF_TC_HEAD(scalbnl_val, tc)
355 atf_tc_set_md_var(tc, "descr", "Test scalbnl() for a few values");
358 ATF_TC_BODY(scalbnl_val, tc)
360 #ifndef __HAVE_LONG_DOUBLE
361 atf_tc_skip("Requires long double support");
362 #else
363 const struct testcase *tests = test_vals;
364 const size_t tcnt = __arraycount(test_vals);
365 size_t i;
366 long double rv;
368 for (i = 0; i < tcnt; i++) {
369 rv = scalbnl(tests[i].inval, tests[i].exp);
370 ATF_CHECK_EQ_MSG(errno, tests[i].error,
371 "test %zu: errno %d instead of %d", i, errno,
372 tests[i].error);
373 ATF_CHECK_MSG(fabsl(rv-(long double)tests[i].result)<2.0*LDBL_EPSILON,
374 "test %zu: return value %Lg instead of %Lg (difference %Lg)",
375 i, rv, (long double)tests[i].result, (long double)tests[i].result-rv);
377 #endif
380 ATF_TC(scalbnl_nan);
381 ATF_TC_HEAD(scalbnl_nan, tc)
383 atf_tc_set_md_var(tc, "descr", "Test scalbnl(NaN, n) == NaN");
386 ATF_TC_BODY(scalbnl_nan, tc)
388 #ifndef __HAVE_LONG_DOUBLE
389 atf_tc_skip("Requires long double support");
390 #else
391 const long double x = 0.0L / 0.0L;
392 long double y;
393 size_t i;
395 if (isnan(x) == 0) {
396 atf_tc_expect_fail("PR lib/45362");
397 atf_tc_fail("(0.0L / 0.0L) != NaN");
400 for (i = 0; i < __arraycount(exps); i++) {
401 y = scalbnl(x, exps[i]);
402 ATF_CHECK(isnan(y) != 0);
404 #endif
407 ATF_TC(scalbnl_inf_neg);
408 ATF_TC_HEAD(scalbnl_inf_neg, tc)
410 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-Inf, n) == -Inf");
413 ATF_TC_BODY(scalbnl_inf_neg, tc)
415 #ifndef __HAVE_LONG_DOUBLE
416 atf_tc_skip("Requires long double support");
417 #else
418 const long double x = -1.0L / 0.0L;
419 size_t i;
421 for (i = 0; i < __arraycount(exps); i++)
422 ATF_CHECK(scalbnl(x, exps[i]) == x);
423 #endif
426 ATF_TC(scalbnl_inf_pos);
427 ATF_TC_HEAD(scalbnl_inf_pos, tc)
429 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+Inf, n) == +Inf");
432 ATF_TC_BODY(scalbnl_inf_pos, tc)
434 #ifndef __HAVE_LONG_DOUBLE
435 atf_tc_skip("Requires long double support");
436 #else
437 const long double x = 1.0L / 0.0L;
438 size_t i;
440 for (i = 0; i < __arraycount(exps); i++)
441 ATF_CHECK(scalbnl(x, exps[i]) == x);
442 #endif
445 ATF_TC(scalbnl_zero_neg);
446 ATF_TC_HEAD(scalbnl_zero_neg, tc)
448 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-0.0, n) == -0.0");
451 ATF_TC_BODY(scalbnl_zero_neg, tc)
453 #ifndef __HAVE_LONG_DOUBLE
454 atf_tc_skip("Requires long double support");
455 #else
456 const long double x = -0.0L;
457 long double y;
458 size_t i;
460 ATF_REQUIRE(signbit(x) != 0);
462 for (i = 0; i < __arraycount(exps); i++) {
463 y = scalbnl(x, exps[i]);
464 ATF_CHECK(x == y);
465 ATF_CHECK(signbit(y) != 0);
467 #endif
470 ATF_TC(scalbnl_zero_pos);
471 ATF_TC_HEAD(scalbnl_zero_pos, tc)
473 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+0.0, n) == +0.0");
476 ATF_TC_BODY(scalbnl_zero_pos, tc)
478 #ifndef __HAVE_LONG_DOUBLE
479 atf_tc_skip("Requires long double support");
480 #else
481 const long double x = 0.0L;
482 long double y;
483 size_t i;
485 ATF_REQUIRE(signbit(x) == 0);
487 for (i = 0; i < __arraycount(exps); i++) {
488 y = scalbnl(x, exps[i]);
489 ATF_CHECK(x == y);
490 ATF_CHECK(signbit(y) == 0);
492 #endif
495 ATF_TP_ADD_TCS(tp)
498 ATF_TP_ADD_TC(tp, scalbn_val);
499 ATF_TP_ADD_TC(tp, scalbn_nan);
500 ATF_TP_ADD_TC(tp, scalbn_inf_neg);
501 ATF_TP_ADD_TC(tp, scalbn_inf_pos);
502 ATF_TP_ADD_TC(tp, scalbn_ldexp);
503 ATF_TP_ADD_TC(tp, scalbn_zero_neg);
504 ATF_TP_ADD_TC(tp, scalbn_zero_pos);
506 ATF_TP_ADD_TC(tp, scalbnf_val);
507 ATF_TP_ADD_TC(tp, scalbnf_nan);
508 ATF_TP_ADD_TC(tp, scalbnf_inf_neg);
509 ATF_TP_ADD_TC(tp, scalbnf_inf_pos);
510 ATF_TP_ADD_TC(tp, scalbnf_ldexpf);
511 ATF_TP_ADD_TC(tp, scalbnf_zero_neg);
512 ATF_TP_ADD_TC(tp, scalbnf_zero_pos);
514 ATF_TP_ADD_TC(tp, scalbnl_val);
515 ATF_TP_ADD_TC(tp, scalbnl_nan);
516 ATF_TP_ADD_TC(tp, scalbnl_inf_neg);
517 ATF_TP_ADD_TC(tp, scalbnl_inf_pos);
518 /* ATF_TP_ADD_TC(tp, scalbnl_ldexp); */
519 ATF_TP_ADD_TC(tp, scalbnl_zero_neg);
520 ATF_TP_ADD_TC(tp, scalbnl_zero_pos);
522 return atf_no_error();