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