1 /* $NetBSD: t_acos.c,v 1.4 2013/04/09 12:11:04 isaki 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 35 static const struct { 36 double x; 37 double y; 38 } values[] = { 39 { -1, M_PI, }, 40 { -0.99, 3.000053180265366, }, 41 { -0.5, 2.094395102393195, }, 42 { -0.1, 1.670963747956456, }, 43 { 0, M_PI / 2, }, 44 { 0.1, 1.470628905633337, }, 45 { 0.5, 1.047197551196598, }, 46 { 0.99, 0.141539473324427, }, 47 }; 48 49 /* 50 * acos(3) 51 */ 52 ATF_TC(acos_nan); 53 ATF_TC_HEAD(acos_nan, tc) 54 { 55 atf_tc_set_md_var(tc, "descr", "Test acos(NaN) == NaN"); 56 } 57 58 ATF_TC_BODY(acos_nan, tc) 59 { 60 #ifndef __vax__ 61 const double x = 0.0L / 0.0L; 62 63 if (isnan(acos(x)) == 0) 64 atf_tc_fail_nonfatal("acos(NaN) != NaN"); 65 #endif 66 } 67 68 ATF_TC(acos_inf_neg); 69 ATF_TC_HEAD(acos_inf_neg, tc) 70 { 71 atf_tc_set_md_var(tc, "descr", "Test acos(-Inf) == NaN"); 72 } 73 74 ATF_TC_BODY(acos_inf_neg, tc) 75 { 76 #ifndef __vax__ 77 const double x = -1.0L / 0.0L; 78 79 if (isnan(acos(x)) == 0) 80 atf_tc_fail_nonfatal("acos(-Inf) != NaN"); 81 #endif 82 } 83 84 ATF_TC(acos_inf_pos); 85 ATF_TC_HEAD(acos_inf_pos, tc) 86 { 87 atf_tc_set_md_var(tc, "descr", "Test acos(+Inf) == NaN"); 88 } 89 90 ATF_TC_BODY(acos_inf_pos, tc) 91 { 92 #ifndef __vax__ 93 const double x = 1.0L / 0.0L; 94 95 if (isnan(acos(x)) == 0) 96 atf_tc_fail_nonfatal("acos(+Inf) != NaN"); 97 #endif 98 } 99 100 ATF_TC(acos_one_pos); 101 ATF_TC_HEAD(acos_one_pos, tc) 102 { 103 atf_tc_set_md_var(tc, "descr", "Test acos(1.0) == +0.0"); 104 } 105 106 ATF_TC_BODY(acos_one_pos, tc) 107 { 108 #ifndef __vax__ 109 const double y = acos(1.0); 110 111 if (fabs(y) > 0.0 || signbit(y) != 0) 112 atf_tc_fail_nonfatal("acos(1.0) != +0.0"); 113 #endif 114 } 115 116 ATF_TC(acos_range); 117 ATF_TC_HEAD(acos_range, tc) 118 { 119 atf_tc_set_md_var(tc, "descr", "Test acos(x) == NaN, x < -1, x > 1"); 120 } 121 122 ATF_TC_BODY(acos_range, tc) 123 { 124 #ifndef __vax__ 125 const double x[] = { -1.1, -1.000000001, 1.1, 1.000000001 }; 126 size_t i; 127 128 for (i = 0; i < __arraycount(x); i++) { 129 130 if (isnan(acos(x[i])) == 0) 131 atf_tc_fail_nonfatal("acos(%f) != NaN", x[i]); 132 } 133 #endif 134 } 135 136 ATF_TC(acos_inrange); 137 ATF_TC_HEAD(acos_inrange, tc) 138 { 139 atf_tc_set_md_var(tc, "descr", "Test acos(x) for some values"); 140 } 141 142 ATF_TC_BODY(acos_inrange, tc) 143 { 144 #ifndef __vax__ 145 const double eps = 1.0e-15; 146 double x; 147 double y; 148 size_t i; 149 150 for (i = 0; i < __arraycount(values); i++) { 151 x = values[i].x; 152 y = values[i].y; 153 if (fabs(acos(x) - y) > eps) 154 atf_tc_fail_nonfatal("acos(%g) != %g", x, y); 155 } 156 #endif 157 } 158 159 /* 160 * acosf(3) 161 */ 162 ATF_TC(acosf_nan); 163 ATF_TC_HEAD(acosf_nan, tc) 164 { 165 atf_tc_set_md_var(tc, "descr", "Test acosf(NaN) == NaN"); 166 } 167 168 ATF_TC_BODY(acosf_nan, tc) 169 { 170 #ifndef __vax__ 171 const float x = 0.0L / 0.0L; 172 173 if (isnan(acosf(x)) == 0) 174 atf_tc_fail_nonfatal("acosf(NaN) != NaN"); 175 #endif 176 } 177 178 ATF_TC(acosf_inf_neg); 179 ATF_TC_HEAD(acosf_inf_neg, tc) 180 { 181 atf_tc_set_md_var(tc, "descr", "Test acosf(-Inf) == NaN"); 182 } 183 184 ATF_TC_BODY(acosf_inf_neg, tc) 185 { 186 #ifndef __vax__ 187 const float x = -1.0L / 0.0L; 188 189 if (isnan(acosf(x)) == 0) 190 atf_tc_fail_nonfatal("acosf(-Inf) != NaN"); 191 #endif 192 } 193 194 ATF_TC(acosf_inf_pos); 195 ATF_TC_HEAD(acosf_inf_pos, tc) 196 { 197 atf_tc_set_md_var(tc, "descr", "Test acosf(+Inf) == NaN"); 198 } 199 200 ATF_TC_BODY(acosf_inf_pos, tc) 201 { 202 #ifndef __vax__ 203 const float x = 1.0L / 0.0L; 204 205 if (isnan(acosf(x)) == 0) 206 atf_tc_fail_nonfatal("acosf(+Inf) != NaN"); 207 #endif 208 } 209 210 ATF_TC(acosf_one_pos); 211 ATF_TC_HEAD(acosf_one_pos, tc) 212 { 213 atf_tc_set_md_var(tc, "descr", "Test acosf(1.0) == +0.0"); 214 } 215 216 ATF_TC_BODY(acosf_one_pos, tc) 217 { 218 #ifndef __vax__ 219 const float y = acosf(1.0); 220 221 if (fabsf(y) > 0.0 || signbit(y) != 0) 222 atf_tc_fail_nonfatal("acosf(1.0) != +0.0"); 223 #endif 224 } 225 226 ATF_TC(acosf_range); 227 ATF_TC_HEAD(acosf_range, tc) 228 { 229 atf_tc_set_md_var(tc, "descr", "Test acosf(x) == NaN, x < -1, x > 1"); 230 } 231 232 ATF_TC_BODY(acosf_range, tc) 233 { 234 #ifndef __vax__ 235 const float x[] = { -1.1, -1.0000001, 1.1, 1.0000001 }; 236 size_t i; 237 238 for (i = 0; i < __arraycount(x); i++) { 239 240 if (isnan(acosf(x[i])) == 0) 241 atf_tc_fail_nonfatal("acosf(%f) != NaN", x[i]); 242 } 243 #endif 244 } 245 246 ATF_TC(acosf_inrange); 247 ATF_TC_HEAD(acosf_inrange, tc) 248 { 249 atf_tc_set_md_var(tc, "descr", "Test acosf(x) for some values"); 250 } 251 252 ATF_TC_BODY(acosf_inrange, tc) 253 { 254 #ifndef __vax__ 255 const float eps = 1.0e-5; 256 float x; 257 float y; 258 size_t i; 259 260 for (i = 0; i < __arraycount(values); i++) { 261 x = values[i].x; 262 y = values[i].y; 263 if (fabsf(acosf(x) - y) > eps) 264 atf_tc_fail_nonfatal("acosf(%g) != %g", x, y); 265 } 266 #endif 267 } 268 269 ATF_TP_ADD_TCS(tp) 270 { 271 272 ATF_TP_ADD_TC(tp, acos_nan); 273 ATF_TP_ADD_TC(tp, acos_inf_neg); 274 ATF_TP_ADD_TC(tp, acos_inf_pos); 275 ATF_TP_ADD_TC(tp, acos_one_pos); 276 ATF_TP_ADD_TC(tp, acos_range); 277 ATF_TP_ADD_TC(tp, acos_inrange); 278 279 ATF_TP_ADD_TC(tp, acosf_nan); 280 ATF_TP_ADD_TC(tp, acosf_inf_neg); 281 ATF_TP_ADD_TC(tp, acosf_inf_pos); 282 ATF_TP_ADD_TC(tp, acosf_one_pos); 283 ATF_TP_ADD_TC(tp, acosf_range); 284 ATF_TP_ADD_TC(tp, acosf_inrange); 285 286 return atf_no_error(); 287 } 288