t_scalbn.c revision 1.8 1 /* $NetBSD: t_scalbn.c,v 1.8 2013/05/20 12:21:42 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
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.
18 *
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.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_scalbn.c,v 1.8 2013/05/20 12:21:42 martin Exp $");
33
34 #include <math.h>
35 #include <limits.h>
36 #include <float.h>
37 #include <errno.h>
38
39 #include <atf-c.h>
40
41 static const int exps[] = { 0, 1, -1, 100, -100 };
42
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;
49 };
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 },
59
60 /*
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.
64 */
65 };
66
67 /*
68 * scalbn(3)
69 */
70 ATF_TC(scalbn_val);
71 ATF_TC_HEAD(scalbn_val, tc)
72 {
73 atf_tc_set_md_var(tc, "descr", "Test scalbn() for a few values");
74 }
75
76 ATF_TC_BODY(scalbn_val, tc)
77 {
78 const struct testcase *tests = test_vals;
79 const size_t tcnt = __arraycount(test_vals);
80 size_t i;
81 double rv;
82
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);
91 }
92 }
93
94 ATF_TC(scalbn_nan);
95 ATF_TC_HEAD(scalbn_nan, tc)
96 {
97 atf_tc_set_md_var(tc, "descr", "Test scalbn(NaN, n) == NaN");
98 }
99
100 ATF_TC_BODY(scalbn_nan, tc)
101 {
102 #ifndef __vax__
103 const double x = 0.0L / 0.0L;
104 double y;
105 size_t i;
106
107 ATF_REQUIRE(isnan(x) != 0);
108
109 for (i = 0; i < __arraycount(exps); i++) {
110 y = scalbn(x, exps[i]);
111 ATF_CHECK(isnan(y) != 0);
112 }
113 #endif
114 }
115
116 ATF_TC(scalbn_inf_neg);
117 ATF_TC_HEAD(scalbn_inf_neg, tc)
118 {
119 atf_tc_set_md_var(tc, "descr", "Test scalbn(-Inf, n) == -Inf");
120 }
121
122 ATF_TC_BODY(scalbn_inf_neg, tc)
123 {
124 #ifndef __vax__
125 const double x = -1.0L / 0.0L;
126 size_t i;
127
128 for (i = 0; i < __arraycount(exps); i++)
129 ATF_CHECK(scalbn(x, exps[i]) == x);
130 #endif
131 }
132
133 ATF_TC(scalbn_inf_pos);
134 ATF_TC_HEAD(scalbn_inf_pos, tc)
135 {
136 atf_tc_set_md_var(tc, "descr", "Test scalbn(+Inf, n) == +Inf");
137 }
138
139 ATF_TC_BODY(scalbn_inf_pos, tc)
140 {
141 #ifndef __vax__
142 const double x = 1.0L / 0.0L;
143 size_t i;
144
145 for (i = 0; i < __arraycount(exps); i++)
146 ATF_CHECK(scalbn(x, exps[i]) == x);
147 #endif
148 }
149
150 ATF_TC(scalbn_ldexp);
151 ATF_TC_HEAD(scalbn_ldexp, tc)
152 {
153 atf_tc_set_md_var(tc, "descr", "Test scalbn(x, n) == ldexp(x, n)");
154 }
155
156 ATF_TC_BODY(scalbn_ldexp, tc)
157 {
158 #ifndef __vax__
159 #if FLT_RADIX == 2
160 const double x = 2.91288191221812821;
161 double y;
162 size_t i;
163
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]));
169 }
170 #endif
171 #endif
172 }
173
174 ATF_TC(scalbn_zero_neg);
175 ATF_TC_HEAD(scalbn_zero_neg, tc)
176 {
177 atf_tc_set_md_var(tc, "descr", "Test scalbn(-0.0, n) == -0.0");
178 }
179
180 ATF_TC_BODY(scalbn_zero_neg, tc)
181 {
182 #ifndef __vax__
183 const double x = -0.0L;
184 double y;
185 size_t i;
186
187 ATF_REQUIRE(signbit(x) != 0);
188
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);
193 }
194 #endif
195 }
196
197 ATF_TC(scalbn_zero_pos);
198 ATF_TC_HEAD(scalbn_zero_pos, tc)
199 {
200 atf_tc_set_md_var(tc, "descr", "Test scalbn(+0.0, n) == +0.0");
201 }
202
203 ATF_TC_BODY(scalbn_zero_pos, tc)
204 {
205 #ifndef __vax__
206 const double x = 0.0L;
207 double y;
208 size_t i;
209
210 ATF_REQUIRE(signbit(x) == 0);
211
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);
216 }
217 #endif
218 }
219
220 /*
221 * scalbnf(3)
222 */
223 ATF_TC(scalbnf_val);
224 ATF_TC_HEAD(scalbnf_val, tc)
225 {
226 atf_tc_set_md_var(tc, "descr", "Test scalbnf() for a few values");
227 }
228
229 ATF_TC_BODY(scalbnf_val, tc)
230 {
231 const struct testcase *tests = test_vals;
232 const size_t tcnt = __arraycount(test_vals);
233 size_t i;
234 double rv;
235
236 for (i = 0; i < tcnt; i++) {
237 rv = scalbnf(tests[i].inval, tests[i].exp);
238 ATF_CHECK_EQ_MSG(errno, tests[i].error,
239 "test %zu: errno %d instead of %d", i, errno,
240 tests[i].error);
241 ATF_CHECK_MSG(fabs(rv-tests[i].result)<2.0*FLT_EPSILON,
242 "test %zu: return value %g instead of %g (difference %g)",
243 i, rv, tests[i].result, tests[i].result-rv);
244 }
245 }
246
247 ATF_TC(scalbnf_nan);
248 ATF_TC_HEAD(scalbnf_nan, tc)
249 {
250 atf_tc_set_md_var(tc, "descr", "Test scalbnf(NaN, n) == NaN");
251 }
252
253 ATF_TC_BODY(scalbnf_nan, tc)
254 {
255 #ifndef __vax__
256 const float x = 0.0L / 0.0L;
257 float y;
258 size_t i;
259
260 ATF_REQUIRE(isnan(x) != 0);
261
262 for (i = 0; i < __arraycount(exps); i++) {
263 y = scalbnf(x, exps[i]);
264 ATF_CHECK(isnan(y) != 0);
265 }
266 #endif
267 }
268
269 ATF_TC(scalbnf_inf_neg);
270 ATF_TC_HEAD(scalbnf_inf_neg, tc)
271 {
272 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-Inf, n) == -Inf");
273 }
274
275 ATF_TC_BODY(scalbnf_inf_neg, tc)
276 {
277 #ifndef __vax__
278 const float x = -1.0L / 0.0L;
279 size_t i;
280
281 for (i = 0; i < __arraycount(exps); i++)
282 ATF_CHECK(scalbnf(x, exps[i]) == x);
283 #endif
284 }
285
286 ATF_TC(scalbnf_inf_pos);
287 ATF_TC_HEAD(scalbnf_inf_pos, tc)
288 {
289 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+Inf, n) == +Inf");
290 }
291
292 ATF_TC_BODY(scalbnf_inf_pos, tc)
293 {
294 #ifndef __vax__
295 const float x = 1.0L / 0.0L;
296 size_t i;
297
298 for (i = 0; i < __arraycount(exps); i++)
299 ATF_CHECK(scalbnf(x, exps[i]) == x);
300 #endif
301 }
302
303 ATF_TC(scalbnf_ldexpf);
304 ATF_TC_HEAD(scalbnf_ldexpf, tc)
305 {
306 atf_tc_set_md_var(tc, "descr", "Test scalbnf(x, n) == ldexpf(x, n)");
307 }
308
309 ATF_TC_BODY(scalbnf_ldexpf, tc)
310 {
311 #ifndef __vax__
312 #if FLT_RADIX == 2
313 const float x = 2.91288191221812821;
314 float y;
315 size_t i;
316
317 for (i = 0; i < __arraycount(exps); i++) {
318 y = scalbnf(x, exps[i]);
319 ATF_CHECK_MSG(y == ldexpf(x, exps[i]),
320 "test %zu: exponent=%d, y=%g ldexpf returns %g (diff: %g)",
321 i, exps[i], y, ldexpf(x, exps[i]), y-ldexpf(x, exps[i]));
322 }
323 #endif
324 #endif
325 }
326
327 ATF_TC(scalbnf_zero_neg);
328 ATF_TC_HEAD(scalbnf_zero_neg, tc)
329 {
330 atf_tc_set_md_var(tc, "descr", "Test scalbnf(-0.0, n) == -0.0");
331 }
332
333 ATF_TC_BODY(scalbnf_zero_neg, tc)
334 {
335 #ifndef __vax__
336 const float x = -0.0L;
337 float y;
338 size_t i;
339
340 ATF_REQUIRE(signbit(x) != 0);
341
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);
346 }
347 #endif
348 }
349
350 ATF_TC(scalbnf_zero_pos);
351 ATF_TC_HEAD(scalbnf_zero_pos, tc)
352 {
353 atf_tc_set_md_var(tc, "descr", "Test scalbnf(+0.0, n) == +0.0");
354 }
355
356 ATF_TC_BODY(scalbnf_zero_pos, tc)
357 {
358 #ifndef __vax__
359 const float x = 0.0L;
360 float y;
361 size_t i;
362
363 ATF_REQUIRE(signbit(x) == 0);
364
365 for (i = 0; i < __arraycount(exps); i++) {
366 y = scalbnf(x, exps[i]);
367 ATF_CHECK(x == y);
368 ATF_CHECK(signbit(y) == 0);
369 }
370 #endif
371 }
372
373 /*
374 * scalbnl(3)
375 */
376 ATF_TC(scalbnl_val);
377 ATF_TC_HEAD(scalbnl_val, tc)
378 {
379 atf_tc_set_md_var(tc, "descr", "Test scalbnl() for a few values");
380 }
381
382 ATF_TC_BODY(scalbnl_val, tc)
383 {
384 #ifndef __HAVE_LONG_DOUBLE
385 atf_tc_skip("Requires long double support");
386 #else
387 const struct testcase *tests = test_vals;
388 const size_t tcnt = __arraycount(test_vals);
389 size_t i;
390 long double rv;
391
392 for (i = 0; i < tcnt; i++) {
393 rv = scalbnl(tests[i].inval, tests[i].exp);
394 ATF_CHECK_EQ_MSG(errno, tests[i].error,
395 "test %zu: errno %d instead of %d", i, errno,
396 tests[i].error);
397 ATF_CHECK_MSG(fabsl(rv-(long double)tests[i].result)<2.0*LDBL_EPSILON,
398 "test %zu: return value %Lg instead of %Lg (difference %Lg)",
399 i, rv, (long double)tests[i].result, (long double)tests[i].result-rv);
400 }
401 #endif
402 }
403
404 ATF_TC(scalbnl_nan);
405 ATF_TC_HEAD(scalbnl_nan, tc)
406 {
407 atf_tc_set_md_var(tc, "descr", "Test scalbnl(NaN, n) == NaN");
408 }
409
410 ATF_TC_BODY(scalbnl_nan, tc)
411 {
412 #ifndef __vax__
413 #ifndef __HAVE_LONG_DOUBLE
414 atf_tc_skip("Requires long double support");
415 #else
416 const long double x = 0.0L / 0.0L;
417 long double y;
418 size_t i;
419
420 if (isnan(x) == 0) {
421 atf_tc_expect_fail("PR lib/45362");
422 atf_tc_fail("(0.0L / 0.0L) != NaN");
423 }
424
425 for (i = 0; i < __arraycount(exps); i++) {
426 y = scalbnl(x, exps[i]);
427 ATF_CHECK(isnan(y) != 0);
428 }
429 #endif
430 #endif
431 }
432
433 ATF_TC(scalbnl_inf_neg);
434 ATF_TC_HEAD(scalbnl_inf_neg, tc)
435 {
436 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-Inf, n) == -Inf");
437 }
438
439 ATF_TC_BODY(scalbnl_inf_neg, tc)
440 {
441 #ifndef __vax__
442 #ifndef __HAVE_LONG_DOUBLE
443 atf_tc_skip("Requires long double support");
444 #else
445 const long double x = -1.0L / 0.0L;
446 size_t i;
447
448 for (i = 0; i < __arraycount(exps); i++)
449 ATF_CHECK(scalbnl(x, exps[i]) == x);
450 #endif
451 #endif
452 }
453
454 ATF_TC(scalbnl_inf_pos);
455 ATF_TC_HEAD(scalbnl_inf_pos, tc)
456 {
457 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+Inf, n) == +Inf");
458 }
459
460 ATF_TC_BODY(scalbnl_inf_pos, tc)
461 {
462 #ifndef __vax__
463 #ifndef __HAVE_LONG_DOUBLE
464 atf_tc_skip("Requires long double support");
465 #else
466 const long double x = 1.0L / 0.0L;
467 size_t i;
468
469 for (i = 0; i < __arraycount(exps); i++)
470 ATF_CHECK(scalbnl(x, exps[i]) == x);
471 #endif
472 #endif
473 }
474
475 ATF_TC(scalbnl_zero_neg);
476 ATF_TC_HEAD(scalbnl_zero_neg, tc)
477 {
478 atf_tc_set_md_var(tc, "descr", "Test scalbnl(-0.0, n) == -0.0");
479 }
480
481 ATF_TC_BODY(scalbnl_zero_neg, tc)
482 {
483 #ifndef __vax__
484 #ifndef __HAVE_LONG_DOUBLE
485 atf_tc_skip("Requires long double support");
486 #else
487 const long double x = -0.0L;
488 long double y;
489 size_t i;
490
491 ATF_REQUIRE(signbit(x) != 0);
492
493 for (i = 0; i < __arraycount(exps); i++) {
494 y = scalbnl(x, exps[i]);
495 ATF_CHECK(x == y);
496 ATF_CHECK(signbit(y) != 0);
497 }
498 #endif
499 #endif
500 }
501
502 ATF_TC(scalbnl_zero_pos);
503 ATF_TC_HEAD(scalbnl_zero_pos, tc)
504 {
505 atf_tc_set_md_var(tc, "descr", "Test scalbnl(+0.0, n) == +0.0");
506 }
507
508 ATF_TC_BODY(scalbnl_zero_pos, tc)
509 {
510 #ifndef __vax__
511 #ifndef __HAVE_LONG_DOUBLE
512 atf_tc_skip("Requires long double support");
513 #else
514 const long double x = 0.0L;
515 long double y;
516 size_t i;
517
518 ATF_REQUIRE(signbit(x) == 0);
519
520 for (i = 0; i < __arraycount(exps); i++) {
521 y = scalbnl(x, exps[i]);
522 ATF_CHECK(x == y);
523 ATF_CHECK(signbit(y) == 0);
524 }
525 #endif
526 #endif
527 }
528
529 ATF_TP_ADD_TCS(tp)
530 {
531
532 ATF_TP_ADD_TC(tp, scalbn_val);
533 ATF_TP_ADD_TC(tp, scalbn_nan);
534 ATF_TP_ADD_TC(tp, scalbn_inf_neg);
535 ATF_TP_ADD_TC(tp, scalbn_inf_pos);
536 ATF_TP_ADD_TC(tp, scalbn_ldexp);
537 ATF_TP_ADD_TC(tp, scalbn_zero_neg);
538 ATF_TP_ADD_TC(tp, scalbn_zero_pos);
539
540 ATF_TP_ADD_TC(tp, scalbnf_val);
541 ATF_TP_ADD_TC(tp, scalbnf_nan);
542 ATF_TP_ADD_TC(tp, scalbnf_inf_neg);
543 ATF_TP_ADD_TC(tp, scalbnf_inf_pos);
544 ATF_TP_ADD_TC(tp, scalbnf_ldexpf);
545 ATF_TP_ADD_TC(tp, scalbnf_zero_neg);
546 ATF_TP_ADD_TC(tp, scalbnf_zero_pos);
547
548 ATF_TP_ADD_TC(tp, scalbnl_val);
549 ATF_TP_ADD_TC(tp, scalbnl_nan);
550 ATF_TP_ADD_TC(tp, scalbnl_inf_neg);
551 ATF_TP_ADD_TC(tp, scalbnl_inf_pos);
552 /* ATF_TP_ADD_TC(tp, scalbnl_ldexp); */
553 ATF_TP_ADD_TC(tp, scalbnl_zero_neg);
554 ATF_TP_ADD_TC(tp, scalbnl_zero_pos);
555
556 return atf_no_error();
557 }
558