1 /* $NetBSD: t_atan.c,v 1.6 2012/03/11 06:36:05 jruoho 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-40; 68 69 if (strcmp(atf_config_get("atf_arch"), "i386") == 0 && 70 system("cpuctl identify 0 | grep -q QEMU") != 0) 71 atf_tc_expect_fail("PR port-i386/46108"); 72 73 if (fabs(atan(x) + M_PI_2) > eps) 74 atf_tc_fail_nonfatal("atan(-Inf) != -pi/2"); 75 #endif 76 } 77 78 ATF_TC(atan_inf_pos); 79 ATF_TC_HEAD(atan_inf_pos, tc) 80 { 81 atf_tc_set_md_var(tc, "descr", "Test atan(+Inf) == pi/2"); 82 } 83 84 ATF_TC_BODY(atan_inf_pos, tc) 85 { 86 #ifndef __vax__ 87 const double x = +1.0L / 0.0L; 88 const double eps = 1.0e-40; 89 90 if (strcmp(atf_config_get("atf_arch"), "i386") == 0 && 91 system("cpuctl identify 0 | grep -q QEMU") != 0) 92 atf_tc_expect_fail("PR port-i386/46108"); 93 94 if (fabs(atan(x) - M_PI_2) > eps) 95 atf_tc_fail_nonfatal("atan(+Inf) != pi/2"); 96 #endif 97 } 98 99 ATF_TC(atan_tan); 100 ATF_TC_HEAD(atan_tan, tc) 101 { 102 atf_tc_set_md_var(tc, "descr", "Test atan(tan(x)) == x"); 103 } 104 105 ATF_TC_BODY(atan_tan, tc) 106 { 107 #ifndef __vax__ 108 const double x[] = { 0.0, 1.0, M_PI / 2, M_PI / 3, M_PI / 6 }; 109 const double eps = 1.0e-40; 110 double y; 111 size_t i; 112 113 if (strcmp(atf_config_get("atf_arch"), "i386") == 0 && 114 system("cpuctl identify 0 | grep -q QEMU") != 0) 115 atf_tc_expect_fail("PR port-i386/46108"); 116 117 for (i = 0; i < __arraycount(x); i++) { 118 119 y = atan(tan(x[i])); 120 121 if (fabs(y - x[i]) > eps) 122 atf_tc_fail_nonfatal("atan(tan(%0.03f)) != %0.03f", 123 x[i], x[i]); 124 } 125 #endif 126 } 127 128 ATF_TC(atan_zero_neg); 129 ATF_TC_HEAD(atan_zero_neg, tc) 130 { 131 atf_tc_set_md_var(tc, "descr", "Test atan(-0.0) == -0.0"); 132 } 133 134 ATF_TC_BODY(atan_zero_neg, tc) 135 { 136 #ifndef __vax__ 137 const double x = -0.0L; 138 double y = atan(x); 139 140 if (fabs(y) > 0.0 || signbit(y) == 0) 141 atf_tc_fail_nonfatal("atan(-0.0) != -0.0"); 142 #endif 143 } 144 145 ATF_TC(atan_zero_pos); 146 ATF_TC_HEAD(atan_zero_pos, tc) 147 { 148 atf_tc_set_md_var(tc, "descr", "Test atan(+0.0) == +0.0"); 149 } 150 151 ATF_TC_BODY(atan_zero_pos, tc) 152 { 153 #ifndef __vax__ 154 const double x = 0.0L; 155 double y = atan(x); 156 157 if (fabs(y) > 0.0 || signbit(y) != 0) 158 atf_tc_fail_nonfatal("atan(+0.0) != +0.0"); 159 #endif 160 } 161 162 /* 163 * atanf(3) 164 */ 165 ATF_TC(atanf_nan); 166 ATF_TC_HEAD(atanf_nan, tc) 167 { 168 atf_tc_set_md_var(tc, "descr", "Test atanf(NaN) == NaN"); 169 } 170 171 ATF_TC_BODY(atanf_nan, tc) 172 { 173 #ifndef __vax__ 174 const float x = 0.0L / 0.0L; 175 176 if (isnan(atanf(x)) == 0) 177 atf_tc_fail_nonfatal("atanf(NaN) != NaN"); 178 #endif 179 } 180 181 ATF_TC(atanf_inf_neg); 182 ATF_TC_HEAD(atanf_inf_neg, tc) 183 { 184 atf_tc_set_md_var(tc, "descr", "Test atanf(-Inf) == -pi/2"); 185 } 186 187 ATF_TC_BODY(atanf_inf_neg, tc) 188 { 189 #ifndef __vax__ 190 const float x = -1.0L / 0.0L; 191 const float eps = 1.0e-7; 192 193 if (fabsf(atanf(x) + M_PI_2) > eps) 194 atf_tc_fail_nonfatal("atanf(-Inf) != -pi/2"); 195 #endif 196 } 197 198 ATF_TC(atanf_inf_pos); 199 ATF_TC_HEAD(atanf_inf_pos, tc) 200 { 201 atf_tc_set_md_var(tc, "descr", "Test atanf(+Inf) == pi/2"); 202 } 203 204 ATF_TC_BODY(atanf_inf_pos, tc) 205 { 206 #ifndef __vax__ 207 const float x = +1.0L / 0.0L; 208 const float eps = 1.0e-7; 209 210 if (fabsf(atanf(x) - M_PI_2) > eps) 211 atf_tc_fail_nonfatal("atanf(+Inf) != pi/2"); 212 #endif 213 } 214 215 ATF_TC(atanf_tanf); 216 ATF_TC_HEAD(atanf_tanf, tc) 217 { 218 atf_tc_set_md_var(tc, "descr", "Test atanf(tanf(x)) == x"); 219 } 220 221 ATF_TC_BODY(atanf_tanf, tc) 222 { 223 #ifndef __vax__ 224 const float x[] = { 0.0, 1.0, M_PI / 3, M_PI / 6 }; 225 const float eps = 1.0e-7; 226 float y; 227 size_t i; 228 229 for (i = 0; i < __arraycount(x); i++) { 230 231 y = atanf(tanf(x[i])); 232 233 if (fabsf(y - x[i]) > eps) 234 atf_tc_fail_nonfatal("atanf(tanf(%0.03f)) != %0.03f", 235 x[i], x[i]); 236 } 237 #endif 238 } 239 240 ATF_TC(atanf_zero_neg); 241 ATF_TC_HEAD(atanf_zero_neg, tc) 242 { 243 atf_tc_set_md_var(tc, "descr", "Test atanf(-0.0) == -0.0"); 244 } 245 246 ATF_TC_BODY(atanf_zero_neg, tc) 247 { 248 #ifndef __vax__ 249 const float x = -0.0L; 250 float y = atanf(x); 251 252 if (fabsf(y) > 0.0 || signbit(y) == 0) 253 atf_tc_fail_nonfatal("atanf(-0.0) != -0.0"); 254 #endif 255 } 256 257 ATF_TC(atanf_zero_pos); 258 ATF_TC_HEAD(atanf_zero_pos, tc) 259 { 260 atf_tc_set_md_var(tc, "descr", "Test atanf(+0.0) == +0.0"); 261 } 262 263 ATF_TC_BODY(atanf_zero_pos, tc) 264 { 265 #ifndef __vax__ 266 const float x = 0.0L; 267 float y = atanf(x); 268 269 if (fabsf(y) > 0.0 || signbit(y) != 0) 270 atf_tc_fail_nonfatal("atanf(+0.0) != +0.0"); 271 #endif 272 } 273 274 ATF_TP_ADD_TCS(tp) 275 { 276 277 ATF_TP_ADD_TC(tp, atan_nan); 278 ATF_TP_ADD_TC(tp, atan_inf_neg); 279 ATF_TP_ADD_TC(tp, atan_inf_pos); 280 ATF_TP_ADD_TC(tp, atan_tan); 281 ATF_TP_ADD_TC(tp, atan_zero_neg); 282 ATF_TP_ADD_TC(tp, atan_zero_pos); 283 284 ATF_TP_ADD_TC(tp, atanf_nan); 285 ATF_TP_ADD_TC(tp, atanf_inf_neg); 286 ATF_TP_ADD_TC(tp, atanf_inf_pos); 287 ATF_TP_ADD_TC(tp, atanf_tanf); 288 ATF_TP_ADD_TC(tp, atanf_zero_neg); 289 ATF_TP_ADD_TC(tp, atanf_zero_pos); 290 291 return atf_no_error(); 292 } 293