t_cos.c revision 272343
1272343Sngie/* $NetBSD: t_cos.c,v 1.4 2014/03/03 10:39:08 martin Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <atf-c.h> 33272343Sngie#include <math.h> 34272343Sngie 35272343Sngiestatic const struct { 36272343Sngie int angle; 37272343Sngie double x; 38272343Sngie double y; 39272343Sngie} angles[] = { 40272343Sngie { -180, -3.141592653589793, -1.0000000000000000 }, 41272343Sngie { -135, -2.356194490192345, -0.7071067811865476 }, 42272343Sngie { -90, -1.570796326794897, 0.0000000000000000 }, 43272343Sngie { -45, -0.785398163397448, 0.7071067811865476 }, 44272343Sngie { 0, 0.000000000000000, 1.0000000000000000 }, 45272343Sngie { 30, 0.523598775598299, 0.8660254037844386 }, 46272343Sngie { 45, 0.785398163397448, 0.7071067811865476 }, 47272343Sngie { 60, 1.047197551196598, 0.5000000000000000 }, 48272343Sngie { 90, 1.570796326794897, 0.0000000000000000 }, 49272343Sngie { 120, 2.094395102393195, -0.5000000000000000 }, 50272343Sngie { 135, 2.356194490192345, -0.7071067811865476 }, 51272343Sngie { 150, 2.617993877991494, -0.8660254037844386 }, 52272343Sngie { 180, 3.141592653589793, -1.0000000000000000 }, 53272343Sngie { 270, 4.712388980384690, 0.0000000000000000 }, 54272343Sngie { 360, 6.283185307179586, 1.0000000000000000 } 55272343Sngie}; 56272343Sngie 57272343Sngie/* 58272343Sngie * cos(3) 59272343Sngie */ 60272343SngieATF_TC(cos_angles); 61272343SngieATF_TC_HEAD(cos_angles, tc) 62272343Sngie{ 63272343Sngie atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 64272343Sngie} 65272343Sngie 66272343SngieATF_TC_BODY(cos_angles, tc) 67272343Sngie{ 68272343Sngie const double eps = 1.0e-15; 69272343Sngie size_t i; 70272343Sngie 71272343Sngie for (i = 0; i < __arraycount(angles); i++) { 72272343Sngie 73272343Sngie if (fabs(cos(angles[i].x) - angles[i].y) > eps) 74272343Sngie atf_tc_fail_nonfatal("cos(%d deg) != %0.01f", 75272343Sngie angles[i].angle, angles[i].y); 76272343Sngie } 77272343Sngie} 78272343Sngie 79272343SngieATF_TC(cos_nan); 80272343SngieATF_TC_HEAD(cos_nan, tc) 81272343Sngie{ 82272343Sngie atf_tc_set_md_var(tc, "descr", "Test cos(NaN) == NaN"); 83272343Sngie} 84272343Sngie 85272343SngieATF_TC_BODY(cos_nan, tc) 86272343Sngie{ 87272343Sngie const double x = 0.0L / 0.0L; 88272343Sngie 89272343Sngie ATF_CHECK(isnan(x) != 0); 90272343Sngie ATF_CHECK(isnan(cos(x)) != 0); 91272343Sngie} 92272343Sngie 93272343SngieATF_TC(cos_inf_neg); 94272343SngieATF_TC_HEAD(cos_inf_neg, tc) 95272343Sngie{ 96272343Sngie atf_tc_set_md_var(tc, "descr", "Test cos(-Inf) == NaN"); 97272343Sngie} 98272343Sngie 99272343SngieATF_TC_BODY(cos_inf_neg, tc) 100272343Sngie{ 101272343Sngie const double x = -1.0L / 0.0L; 102272343Sngie 103272343Sngie ATF_CHECK(isnan(cos(x)) != 0); 104272343Sngie} 105272343Sngie 106272343SngieATF_TC(cos_inf_pos); 107272343SngieATF_TC_HEAD(cos_inf_pos, tc) 108272343Sngie{ 109272343Sngie atf_tc_set_md_var(tc, "descr", "Test cos(+Inf) == NaN"); 110272343Sngie} 111272343Sngie 112272343SngieATF_TC_BODY(cos_inf_pos, tc) 113272343Sngie{ 114272343Sngie const double x = 1.0L / 0.0L; 115272343Sngie 116272343Sngie ATF_CHECK(isnan(cos(x)) != 0); 117272343Sngie} 118272343Sngie 119272343Sngie 120272343SngieATF_TC(cos_zero_neg); 121272343SngieATF_TC_HEAD(cos_zero_neg, tc) 122272343Sngie{ 123272343Sngie atf_tc_set_md_var(tc, "descr", "Test cos(-0.0) == 1.0"); 124272343Sngie} 125272343Sngie 126272343SngieATF_TC_BODY(cos_zero_neg, tc) 127272343Sngie{ 128272343Sngie const double x = -0.0L; 129272343Sngie 130272343Sngie ATF_CHECK(cos(x) == 1.0); 131272343Sngie} 132272343Sngie 133272343SngieATF_TC(cos_zero_pos); 134272343SngieATF_TC_HEAD(cos_zero_pos, tc) 135272343Sngie{ 136272343Sngie atf_tc_set_md_var(tc, "descr", "Test cos(+0.0) == 1.0"); 137272343Sngie} 138272343Sngie 139272343SngieATF_TC_BODY(cos_zero_pos, tc) 140272343Sngie{ 141272343Sngie const double x = 0.0L; 142272343Sngie 143272343Sngie ATF_CHECK(cos(x) == 1.0); 144272343Sngie} 145272343Sngie 146272343Sngie/* 147272343Sngie * cosf(3) 148272343Sngie */ 149272343SngieATF_TC(cosf_angles); 150272343SngieATF_TC_HEAD(cosf_angles, tc) 151272343Sngie{ 152272343Sngie atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 153272343Sngie} 154272343Sngie 155272343SngieATF_TC_BODY(cosf_angles, tc) 156272343Sngie{ 157272343Sngie const float eps = 1.0e-7; 158272343Sngie float x, y; 159272343Sngie size_t i; 160272343Sngie 161272343Sngie for (i = 0; i < __arraycount(angles); i++) { 162272343Sngie 163272343Sngie x = angles[i].x; 164272343Sngie y = angles[i].y; 165272343Sngie 166272343Sngie if (fabsf(cosf(x) - y) > eps) 167272343Sngie atf_tc_fail_nonfatal("cosf(%d deg) != %0.01f", 168272343Sngie angles[i].angle, angles[i].y); 169272343Sngie } 170272343Sngie} 171272343Sngie 172272343SngieATF_TC(cosf_nan); 173272343SngieATF_TC_HEAD(cosf_nan, tc) 174272343Sngie{ 175272343Sngie atf_tc_set_md_var(tc, "descr", "Test cosf(NaN) == NaN"); 176272343Sngie} 177272343Sngie 178272343SngieATF_TC_BODY(cosf_nan, tc) 179272343Sngie{ 180272343Sngie const float x = 0.0L / 0.0L; 181272343Sngie 182272343Sngie ATF_CHECK(isnan(x) != 0); 183272343Sngie ATF_CHECK(isnan(cosf(x)) != 0); 184272343Sngie} 185272343Sngie 186272343SngieATF_TC(cosf_inf_neg); 187272343SngieATF_TC_HEAD(cosf_inf_neg, tc) 188272343Sngie{ 189272343Sngie atf_tc_set_md_var(tc, "descr", "Test cosf(-Inf) == NaN"); 190272343Sngie} 191272343Sngie 192272343SngieATF_TC_BODY(cosf_inf_neg, tc) 193272343Sngie{ 194272343Sngie const float x = -1.0L / 0.0L; 195272343Sngie 196272343Sngie if (isnan(cosf(x)) == 0) { 197272343Sngie atf_tc_expect_fail("PR lib/45362"); 198272343Sngie atf_tc_fail("cosf(-Inf) != NaN"); 199272343Sngie } 200272343Sngie} 201272343Sngie 202272343SngieATF_TC(cosf_inf_pos); 203272343SngieATF_TC_HEAD(cosf_inf_pos, tc) 204272343Sngie{ 205272343Sngie atf_tc_set_md_var(tc, "descr", "Test cosf(+Inf) == NaN"); 206272343Sngie} 207272343Sngie 208272343SngieATF_TC_BODY(cosf_inf_pos, tc) 209272343Sngie{ 210272343Sngie const float x = 1.0L / 0.0L; 211272343Sngie 212272343Sngie if (isnan(cosf(x)) == 0) { 213272343Sngie atf_tc_expect_fail("PR lib/45362"); 214272343Sngie atf_tc_fail("cosf(+Inf) != NaN"); 215272343Sngie } 216272343Sngie} 217272343Sngie 218272343Sngie 219272343SngieATF_TC(cosf_zero_neg); 220272343SngieATF_TC_HEAD(cosf_zero_neg, tc) 221272343Sngie{ 222272343Sngie atf_tc_set_md_var(tc, "descr", "Test cosf(-0.0) == 1.0"); 223272343Sngie} 224272343Sngie 225272343SngieATF_TC_BODY(cosf_zero_neg, tc) 226272343Sngie{ 227272343Sngie const float x = -0.0L; 228272343Sngie 229272343Sngie ATF_CHECK(cosf(x) == 1.0); 230272343Sngie} 231272343Sngie 232272343SngieATF_TC(cosf_zero_pos); 233272343SngieATF_TC_HEAD(cosf_zero_pos, tc) 234272343Sngie{ 235272343Sngie atf_tc_set_md_var(tc, "descr", "Test cosf(+0.0) == 1.0"); 236272343Sngie} 237272343Sngie 238272343SngieATF_TC_BODY(cosf_zero_pos, tc) 239272343Sngie{ 240272343Sngie const float x = 0.0L; 241272343Sngie 242272343Sngie ATF_CHECK(cosf(x) == 1.0); 243272343Sngie} 244272343Sngie 245272343SngieATF_TP_ADD_TCS(tp) 246272343Sngie{ 247272343Sngie 248272343Sngie ATF_TP_ADD_TC(tp, cos_angles); 249272343Sngie ATF_TP_ADD_TC(tp, cos_nan); 250272343Sngie ATF_TP_ADD_TC(tp, cos_inf_neg); 251272343Sngie ATF_TP_ADD_TC(tp, cos_inf_pos); 252272343Sngie ATF_TP_ADD_TC(tp, cos_zero_neg); 253272343Sngie ATF_TP_ADD_TC(tp, cos_zero_pos); 254272343Sngie 255272343Sngie ATF_TP_ADD_TC(tp, cosf_angles); 256272343Sngie ATF_TP_ADD_TC(tp, cosf_nan); 257272343Sngie ATF_TP_ADD_TC(tp, cosf_inf_neg); 258272343Sngie ATF_TP_ADD_TC(tp, cosf_inf_pos); 259272343Sngie ATF_TP_ADD_TC(tp, cosf_zero_neg); 260272343Sngie ATF_TP_ADD_TC(tp, cosf_zero_pos); 261272343Sngie 262272343Sngie return atf_no_error(); 263272343Sngie} 264