Home | History | Annotate | Line # | Download | only in libm
t_cos.c revision 1.1
      1 /* $NetBSD: t_cos.c,v 1.1 2011/09/14 05:18:19 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 <math.h>
     34 
     35 static const struct {
     36 	int		angle;
     37 	double		x;
     38 	double		y;
     39 } angles[] = {
     40 	{ -180, -3.141592653589793, -1.0000000000000000 },
     41 	{ -135, -2.356194490192345, -0.7071067811865476 },
     42 	{  -90, -1.570796326794897,  0.0000000000000000 },
     43 	{  -45, -0.785398163397448,  0.7071067811865476 },
     44 	{    0,  0.000000000000000,  1.0000000000000000 },
     45 	{   30,  0.523598775598299,  0.8660254037844386 },
     46 	{   45,  0.785398163397448,  0.7071067811865476 },
     47 	{   60,  1.047197551196598,  0.5000000000000000 },
     48 	{   90,  1.570796326794897,  0.0000000000000000 },
     49 	{  120,  2.094395102393195, -0.5000000000000000 },
     50 	{  135,  2.356194490192345, -0.7071067811865476 },
     51 	{  150,  2.617993877991494, -0.8660254037844386 },
     52 	{  180,  3.141592653589793, -1.0000000000000000 },
     53 	{  270,  4.712388980384690,  0.0000000000000000 },
     54 	{  360,  6.283185307179586,  1.0000000000000000 }
     55 };
     56 
     57 /*
     58  * cos(3)
     59  */
     60 ATF_TC(cos_angles);
     61 ATF_TC_HEAD(cos_angles, tc)
     62 {
     63 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
     64 }
     65 
     66 ATF_TC_BODY(cos_angles, tc)
     67 {
     68 	const double eps = 1.0e-15;
     69 	size_t i;
     70 
     71 	for (i = 0; i < __arraycount(angles); i++) {
     72 
     73 		if (fabs(cos(angles[i].x) - angles[i].y) > eps)
     74 			atf_tc_fail_nonfatal("cos(%d deg) != %0.01f",
     75 			    angles[i].angle, angles[i].y);
     76 	}
     77 }
     78 
     79 ATF_TC(cos_nan);
     80 ATF_TC_HEAD(cos_nan, tc)
     81 {
     82 	atf_tc_set_md_var(tc, "descr", "Test cos(NaN) == NaN");
     83 }
     84 
     85 ATF_TC_BODY(cos_nan, tc)
     86 {
     87 #ifndef __vax__
     88 	const double x = 0.0L / 0.0L;
     89 
     90 	ATF_CHECK(isnan(x) != 0);
     91 	ATF_CHECK(isnan(cos(x)) != 0);
     92 #endif
     93 }
     94 
     95 ATF_TC(cos_inf_neg);
     96 ATF_TC_HEAD(cos_inf_neg, tc)
     97 {
     98 	atf_tc_set_md_var(tc, "descr", "Test cos(-Inf) == NaN");
     99 }
    100 
    101 ATF_TC_BODY(cos_inf_neg, tc)
    102 {
    103 #ifndef __vax__
    104 	const double x = -1.0L / 0.0L;
    105 
    106 	ATF_CHECK(isnan(cos(x)) != 0);
    107 #endif
    108 }
    109 
    110 ATF_TC(cos_inf_pos);
    111 ATF_TC_HEAD(cos_inf_pos, tc)
    112 {
    113 	atf_tc_set_md_var(tc, "descr", "Test cos(+Inf) == NaN");
    114 }
    115 
    116 ATF_TC_BODY(cos_inf_pos, tc)
    117 {
    118 #ifndef __vax__
    119 	const double x = 1.0L / 0.0L;
    120 
    121 	ATF_CHECK(isnan(cos(x)) != 0);
    122 #endif
    123 }
    124 
    125 
    126 ATF_TC(cos_zero_neg);
    127 ATF_TC_HEAD(cos_zero_neg, tc)
    128 {
    129 	atf_tc_set_md_var(tc, "descr", "Test cos(-0.0) == 1.0");
    130 }
    131 
    132 ATF_TC_BODY(cos_zero_neg, tc)
    133 {
    134 #ifndef __vax__
    135 	const double x = -0.0L;
    136 
    137 	ATF_CHECK(cos(x) == 1.0);
    138 #endif
    139 }
    140 
    141 ATF_TC(cos_zero_pos);
    142 ATF_TC_HEAD(cos_zero_pos, tc)
    143 {
    144 	atf_tc_set_md_var(tc, "descr", "Test cos(+0.0) == 1.0");
    145 }
    146 
    147 ATF_TC_BODY(cos_zero_pos, tc)
    148 {
    149 #ifndef __vax__
    150 	const double x = 0.0L;
    151 
    152 	ATF_CHECK(cos(x) == 1.0);
    153 #endif
    154 }
    155 
    156 /*
    157  * cosf(3)
    158  */
    159 ATF_TC(cosf_angles);
    160 ATF_TC_HEAD(cosf_angles, tc)
    161 {
    162 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
    163 }
    164 
    165 ATF_TC_BODY(cosf_angles, tc)
    166 {
    167 	const float eps = 1.0e-7;
    168 	float x, y;
    169 	size_t i;
    170 
    171 	for (i = 0; i < __arraycount(angles); i++) {
    172 
    173 		x = angles[i].x;
    174 		y = angles[i].y;
    175 
    176 		if (fabs(cosf(x) - y) > eps)
    177 			atf_tc_fail_nonfatal("cosf(%d deg) != %0.01f",
    178 			    angles[i].angle, angles[i].y);
    179 	}
    180 }
    181 
    182 ATF_TC(cosf_nan);
    183 ATF_TC_HEAD(cosf_nan, tc)
    184 {
    185 	atf_tc_set_md_var(tc, "descr", "Test cosf(NaN) == NaN");
    186 }
    187 
    188 ATF_TC_BODY(cosf_nan, tc)
    189 {
    190 #ifndef __vax__
    191 	const float x = 0.0L / 0.0L;
    192 
    193 	ATF_CHECK(isnan(x) != 0);
    194 	ATF_CHECK(isnan(cosf(x)) != 0);
    195 #endif
    196 }
    197 
    198 ATF_TC(cosf_inf_neg);
    199 ATF_TC_HEAD(cosf_inf_neg, tc)
    200 {
    201 	atf_tc_set_md_var(tc, "descr", "Test cosf(-Inf) == NaN");
    202 }
    203 
    204 ATF_TC_BODY(cosf_inf_neg, tc)
    205 {
    206 #ifndef __vax__
    207 	const float x = -1.0L / 0.0L;
    208 
    209 	ATF_CHECK(isnan(cosf(x)) != 0);
    210 #endif
    211 }
    212 
    213 ATF_TC(cosf_inf_pos);
    214 ATF_TC_HEAD(cosf_inf_pos, tc)
    215 {
    216 	atf_tc_set_md_var(tc, "descr", "Test cosf(+Inf) == NaN");
    217 }
    218 
    219 ATF_TC_BODY(cosf_inf_pos, tc)
    220 {
    221 #ifndef __vax__
    222 	const float x = 1.0L / 0.0L;
    223 
    224 	ATF_CHECK(isnan(cosf(x)) != 0);
    225 #endif
    226 }
    227 
    228 
    229 ATF_TC(cosf_zero_neg);
    230 ATF_TC_HEAD(cosf_zero_neg, tc)
    231 {
    232 	atf_tc_set_md_var(tc, "descr", "Test cosf(-0.0) == 1.0");
    233 }
    234 
    235 ATF_TC_BODY(cosf_zero_neg, tc)
    236 {
    237 #ifndef __vax__
    238 	const float x = -0.0L;
    239 
    240 	ATF_CHECK(cosf(x) == 1.0);
    241 #endif
    242 }
    243 
    244 ATF_TC(cosf_zero_pos);
    245 ATF_TC_HEAD(cosf_zero_pos, tc)
    246 {
    247 	atf_tc_set_md_var(tc, "descr", "Test cosf(+0.0) == 1.0");
    248 }
    249 
    250 ATF_TC_BODY(cosf_zero_pos, tc)
    251 {
    252 #ifndef __vax__
    253 	const float x = 0.0L;
    254 
    255 	ATF_CHECK(cosf(x) == 1.0);
    256 #endif
    257 }
    258 
    259 ATF_TP_ADD_TCS(tp)
    260 {
    261 
    262 	ATF_TP_ADD_TC(tp, cos_angles);
    263 	ATF_TP_ADD_TC(tp, cos_nan);
    264 	ATF_TP_ADD_TC(tp, cos_inf_neg);
    265 	ATF_TP_ADD_TC(tp, cos_inf_pos);
    266 	ATF_TP_ADD_TC(tp, cos_zero_neg);
    267 	ATF_TP_ADD_TC(tp, cos_zero_pos);
    268 
    269 	ATF_TP_ADD_TC(tp, cosf_angles);
    270 	ATF_TP_ADD_TC(tp, cosf_nan);
    271 	ATF_TP_ADD_TC(tp, cosf_inf_neg);
    272 	ATF_TP_ADD_TC(tp, cosf_inf_pos);
    273 	ATF_TP_ADD_TC(tp, cosf_zero_neg);
    274 	ATF_TP_ADD_TC(tp, cosf_zero_pos);
    275 
    276 	return atf_no_error();
    277 }
    278