xref: /freebsd/contrib/netbsd-tests/lib/libm/t_tan.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /* $NetBSD: t_tan.c,v 1.5 2014/03/03 10:39:08 martin 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,  0.0000000000000000 },
41 	{ -135, -2.356194490192345,  1.0000000000000000 },
42 	{  -45, -0.785398163397448, -1.0000000000000000 },
43 	{    0,  0.000000000000000,  0.0000000000000000 },
44 	{   30,  0.523598775598299,  0.5773502691896258 },
45 	{   45,  0.785398163397448,  1.0000000000000000 },
46 	{   60,  1.047197551196598,  1.7320508075688773 },
47 	{  120,  2.094395102393195, -1.7320508075688773 },
48 	{  135,  2.356194490192345, -1.0000000000000000 },
49 	{  150,  2.617993877991494, -0.5773502691896258 },
50 	{  180,  3.141592653589793,  0.0000000000000000 },
51 	{  360,  6.283185307179586,  0.0000000000000000 }
52 };
53 
54 /*
55  * tan(3)
56  */
57 ATF_TC(tan_angles);
58 ATF_TC_HEAD(tan_angles, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
61 }
62 
63 ATF_TC_BODY(tan_angles, tc)
64 {
65 	const double eps = 1.0e-14;
66 	size_t i;
67 
68 	for (i = 0; i < __arraycount(angles); i++) {
69 
70 		if (fabs(tan(angles[i].x) - angles[i].y) > eps)
71 			atf_tc_fail_nonfatal("tan(%d deg) != %0.01f",
72 			    angles[i].angle, angles[i].y);
73 	}
74 }
75 
76 ATF_TC(tan_nan);
77 ATF_TC_HEAD(tan_nan, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr", "Test tan(NaN) == NaN");
80 }
81 
82 ATF_TC_BODY(tan_nan, tc)
83 {
84 	const double x = 0.0L / 0.0L;
85 
86 	ATF_CHECK(isnan(x) != 0);
87 	ATF_CHECK(isnan(tan(x)) != 0);
88 }
89 
90 ATF_TC(tan_inf_neg);
91 ATF_TC_HEAD(tan_inf_neg, tc)
92 {
93 	atf_tc_set_md_var(tc, "descr", "Test tan(-Inf) == NaN");
94 }
95 
96 ATF_TC_BODY(tan_inf_neg, tc)
97 {
98 	const double x = -1.0L / 0.0L;
99 
100 	ATF_CHECK(isnan(tan(x)) != 0);
101 }
102 
103 ATF_TC(tan_inf_pos);
104 ATF_TC_HEAD(tan_inf_pos, tc)
105 {
106 	atf_tc_set_md_var(tc, "descr", "Test tan(+Inf) == NaN");
107 }
108 
109 ATF_TC_BODY(tan_inf_pos, tc)
110 {
111 	const double x = 1.0L / 0.0L;
112 
113 	ATF_CHECK(isnan(tan(x)) != 0);
114 }
115 
116 
117 ATF_TC(tan_zero_neg);
118 ATF_TC_HEAD(tan_zero_neg, tc)
119 {
120 	atf_tc_set_md_var(tc, "descr", "Test tan(-0.0) == -0.0");
121 }
122 
123 ATF_TC_BODY(tan_zero_neg, tc)
124 {
125 	const double x = -0.0L;
126 
127 	ATF_CHECK(tan(x) == x);
128 }
129 
130 ATF_TC(tan_zero_pos);
131 ATF_TC_HEAD(tan_zero_pos, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "Test tan(+0.0) == +0.0");
134 }
135 
136 ATF_TC_BODY(tan_zero_pos, tc)
137 {
138 	const double x = 0.0L;
139 
140 	ATF_CHECK(tan(x) == x);
141 }
142 
143 /*
144  * tanf(3)
145  */
146 ATF_TC(tanf_angles);
147 ATF_TC_HEAD(tanf_angles, tc)
148 {
149 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
150 }
151 
152 ATF_TC_BODY(tanf_angles, tc)
153 {
154 	const float eps = 1.0e-6;
155 	float x, y;
156 	size_t i;
157 
158 	for (i = 0; i < __arraycount(angles); i++) {
159 
160 		x = angles[i].x;
161 		y = angles[i].y;
162 
163 		if (fabsf(tanf(x) - y) > eps)
164 			atf_tc_fail_nonfatal("tanf(%d deg) != %0.01f",
165 			    angles[i].angle, angles[i].y);
166 	}
167 }
168 
169 ATF_TC(tanf_nan);
170 ATF_TC_HEAD(tanf_nan, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr", "Test tanf(NaN) == NaN");
173 }
174 
175 ATF_TC_BODY(tanf_nan, tc)
176 {
177 	const float x = 0.0L / 0.0L;
178 
179 	ATF_CHECK(isnan(x) != 0);
180 	ATF_CHECK(isnan(tanf(x)) != 0);
181 }
182 
183 ATF_TC(tanf_inf_neg);
184 ATF_TC_HEAD(tanf_inf_neg, tc)
185 {
186 	atf_tc_set_md_var(tc, "descr", "Test tanf(-Inf) == NaN");
187 }
188 
189 ATF_TC_BODY(tanf_inf_neg, tc)
190 {
191 	const float x = -1.0L / 0.0L;
192 
193 	if (isnan(tanf(x)) == 0) {
194 		atf_tc_expect_fail("PR lib/45362");
195 		atf_tc_fail("tanf(-Inf) != NaN");
196 	}
197 }
198 
199 ATF_TC(tanf_inf_pos);
200 ATF_TC_HEAD(tanf_inf_pos, tc)
201 {
202 	atf_tc_set_md_var(tc, "descr", "Test tanf(+Inf) == NaN");
203 }
204 
205 ATF_TC_BODY(tanf_inf_pos, tc)
206 {
207 	const float x = 1.0L / 0.0L;
208 
209 	if (isnan(tanf(x)) == 0) {
210 		atf_tc_expect_fail("PR lib/45362");
211 		atf_tc_fail("tanf(+Inf) != NaN");
212 	}
213 }
214 
215 
216 ATF_TC(tanf_zero_neg);
217 ATF_TC_HEAD(tanf_zero_neg, tc)
218 {
219 	atf_tc_set_md_var(tc, "descr", "Test tanf(-0.0) == -0.0");
220 }
221 
222 ATF_TC_BODY(tanf_zero_neg, tc)
223 {
224 	const float x = -0.0L;
225 
226 	ATF_CHECK(tanf(x) == x);
227 }
228 
229 ATF_TC(tanf_zero_pos);
230 ATF_TC_HEAD(tanf_zero_pos, tc)
231 {
232 	atf_tc_set_md_var(tc, "descr", "Test tanf(+0.0) == +0.0");
233 }
234 
235 ATF_TC_BODY(tanf_zero_pos, tc)
236 {
237 	const float x = 0.0L;
238 
239 	ATF_CHECK(tanf(x) == x);
240 }
241 
242 ATF_TP_ADD_TCS(tp)
243 {
244 
245 	ATF_TP_ADD_TC(tp, tan_angles);
246 	ATF_TP_ADD_TC(tp, tan_nan);
247 	ATF_TP_ADD_TC(tp, tan_inf_neg);
248 	ATF_TP_ADD_TC(tp, tan_inf_pos);
249 	ATF_TP_ADD_TC(tp, tan_zero_neg);
250 	ATF_TP_ADD_TC(tp, tan_zero_pos);
251 
252 	ATF_TP_ADD_TC(tp, tanf_angles);
253 	ATF_TP_ADD_TC(tp, tanf_nan);
254 	ATF_TP_ADD_TC(tp, tanf_inf_neg);
255 	ATF_TP_ADD_TC(tp, tanf_inf_pos);
256 	ATF_TP_ADD_TC(tp, tanf_zero_neg);
257 	ATF_TP_ADD_TC(tp, tanf_zero_pos);
258 
259 	return atf_no_error();
260 }
261