t_acos.c revision 1.6
1/* $NetBSD: t_acos.c,v 1.6 2014/03/02 22:40:45 dsl 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 32#include <atf-c.h> 33#include <math.h> 34#include <fenv.h> 35 36/* 37 * Check result of fn(arg) is correct within the bounds. 38 * Should be ok to do the checks using 'double' for 'float' functions. 39 */ 40#define T_LIBM_CHECK(subtest, fn, arg, expect, epsilon) do { \ 41 double r = fn(arg); \ 42 double e = fabs(r - expect); \ 43 if (e > epsilon) \ 44 atf_tc_fail_nonfatal( \ 45 "subtest %zu: " #fn "(%g) is %g not %g (error %g > %g), roundmode %x", \ 46 subtest, arg, r, expect, e, epsilon, fegetround()); \ 47 } while (0) 48 49/* Check that the result of fn(arg) is NaN */ 50#ifndef __vax__ 51#define T_LIBM_CHECK_NAN(subtest, fn, arg) do { \ 52 double r = fn(arg); \ 53 if (!isnan(r)) \ 54 atf_tc_fail_nonfatal("subtest %zu: " #fn "(%g) is %g not NaN", \ 55 subtest, arg, r); \ 56 } while (0) 57#else 58/* vax doesn't support NaN */ 59#define T_LIBM_CHECK_NAN(subtest, fn, arg) (void)(arg) 60#endif 61 62#define AFT_LIBM_TEST(name, description) \ 63ATF_TC(name); \ 64ATF_TC_HEAD(name, tc) { atf_tc_set_md_var(tc, "descr", description); } \ 65ATF_TC_BODY(name, tc) 66 67/* 68 * acos(3) and acosf(3) 69 */ 70 71AFT_LIBM_TEST(acos_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]") 72{ 73 static const double x[] = { 74 -1.000000001, 1.000000001, 75 -1.0000001, 1.0000001, 76 -1.1, 1.1, 77 0.0L / 0.0L, /* NAN */ 78 -1.0L / 0.0L, /* -Inf */ 79 +1.0L / 0.0L, /* +Inf */ 80 }; 81 size_t i; 82 83 for (i = 0; i < __arraycount(x); i++) { 84 T_LIBM_CHECK_NAN(i, acos, x[i]); 85 if (i < 2) 86 /* Values are too small for float */ 87 continue; 88 T_LIBM_CHECK_NAN(i, acosf, x[i]); 89 } 90} 91 92AFT_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values") 93{ 94 static const struct { 95 double x; 96 double y; 97 } values[] = { 98 { -1, M_PI, }, 99 { -0.99, 3.000053180265366, }, 100 { -0.5, 2.094395102393195, }, 101 { -0.1, 1.670963747956456, }, 102 { 0, M_PI / 2, }, 103 { 0.1, 1.470628905633337, }, 104 { 0.5, 1.047197551196598, }, 105 { 0.99, 0.141539473324427, }, 106 }; 107 size_t i; 108 109 /* 110 * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x). 111 * This means that acos(-1) is atan2(+0,-1), if the sign is wrong 112 * the value will be -M_PI (atan2(-0,-1)) not M_PI. 113 */ 114 115 for (i = 0; i < __arraycount(values); i++) { 116 T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15); 117 T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5); 118 } 119} 120 121AFT_LIBM_TEST(acos_one_pos, "Test acos(1.0) == +0.0") 122{ 123#ifndef __vax__ 124 const double y = acos(1.0); 125 126 if (fabs(y) > 0.0 || signbit(y) != 0) 127 atf_tc_fail_nonfatal("acos(1.0) != +0.0"); 128#endif 129} 130 131AFT_LIBM_TEST(acosf_one_pos, "Test acosf(1.0) == +0.0") 132{ 133#ifndef __vax__ 134 const float y = acosf(1.0); 135 136 if (fabsf(y) > 0.0 || signbit(y) != 0) 137 atf_tc_fail_nonfatal("acosf(1.0) != +0.0"); 138#endif 139} 140 141ATF_TP_ADD_TCS(tp) 142{ 143 144 ATF_TP_ADD_TC(tp, acos_nan); 145 ATF_TP_ADD_TC(tp, acos_inrange); 146 ATF_TP_ADD_TC(tp, acos_one_pos); 147 ATF_TP_ADD_TC(tp, acosf_one_pos); 148 149 return atf_no_error(); 150} 151