xref: /freebsd/contrib/netbsd-tests/lib/libm/t_asin.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /* $NetBSD: t_asin.c,v 1.3 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 	double x;
37 	double y;
38 } values[] = {
39 	{ -1.0, -M_PI / 2, },
40 	{ -0.9, -1.119769514998634, },
41 	{ -0.5, -M_PI / 6, },
42 	{ -0.1, -0.1001674211615598, },
43 	{  0.1,  0.1001674211615598, },
44 	{  0.5,  M_PI / 6, },
45 	{  0.9,  1.119769514998634, },
46 	{  1.0,  M_PI / 2, },
47 };
48 
49 /*
50  * asin(3)
51  */
52 ATF_TC(asin_nan);
53 ATF_TC_HEAD(asin_nan, tc)
54 {
55 	atf_tc_set_md_var(tc, "descr", "Test asin(NaN) == NaN");
56 }
57 
58 ATF_TC_BODY(asin_nan, tc)
59 {
60 	const double x = 0.0L / 0.0L;
61 
62 	if (isnan(asin(x)) == 0)
63 		atf_tc_fail_nonfatal("asin(NaN) != NaN");
64 }
65 
66 ATF_TC(asin_inf_neg);
67 ATF_TC_HEAD(asin_inf_neg, tc)
68 {
69 	atf_tc_set_md_var(tc, "descr", "Test asin(-Inf) == NaN");
70 }
71 
72 ATF_TC_BODY(asin_inf_neg, tc)
73 {
74 	const double x = -1.0L / 0.0L;
75 
76 	if (isnan(asin(x)) == 0)
77 		atf_tc_fail_nonfatal("asin(-Inf) != NaN");
78 }
79 
80 ATF_TC(asin_inf_pos);
81 ATF_TC_HEAD(asin_inf_pos, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr", "Test asin(+Inf) == NaN");
84 }
85 
86 ATF_TC_BODY(asin_inf_pos, tc)
87 {
88 	const double x = 1.0L / 0.0L;
89 
90 	if (isnan(asin(x)) == 0)
91 		atf_tc_fail_nonfatal("asin(+Inf) != NaN");
92 }
93 
94 ATF_TC(asin_range);
95 ATF_TC_HEAD(asin_range, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr", "Test asin(x) == NaN, x < -1, x > 1");
98 }
99 
100 ATF_TC_BODY(asin_range, tc)
101 {
102 	const double x[] = { -1.1, -1.000000001, 1.1, 1.000000001 };
103 	size_t i;
104 
105 	for (i = 0; i < __arraycount(x); i++) {
106 
107 		if (isnan(asin(x[i])) == 0)
108 			atf_tc_fail_nonfatal("asin(%f) != NaN", x[i]);
109 	}
110 }
111 
112 ATF_TC(asin_inrange);
113 ATF_TC_HEAD(asin_inrange, tc)
114 {
115 	atf_tc_set_md_var(tc, "descr", "Test asin(x) for some values");
116 }
117 
118 ATF_TC_BODY(asin_inrange, tc)
119 {
120 	const double eps = 1.0e-15;
121 	double y;
122 	size_t i;
123 
124 	for (i = 0; i < __arraycount(values); i++) {
125 		y = asin(values[i].x);
126 		if (fabs(y - values[i].y) > eps)
127 			atf_tc_fail_nonfatal("asin(%g) != %g",
128 				values[i].x, values[i].y);
129 	}
130 }
131 
132 ATF_TC(asin_zero_neg);
133 ATF_TC_HEAD(asin_zero_neg, tc)
134 {
135 	atf_tc_set_md_var(tc, "descr", "Test asin(-0.0) == -0.0");
136 }
137 
138 ATF_TC_BODY(asin_zero_neg, tc)
139 {
140 	const double x = -0.0L;
141 	double y = asin(x);
142 
143 	if (fabs(y) > 0.0 || signbit(y) == 0)
144 		atf_tc_fail_nonfatal("asin(-0.0) != -0.0");
145 }
146 
147 ATF_TC(asin_zero_pos);
148 ATF_TC_HEAD(asin_zero_pos, tc)
149 {
150 	atf_tc_set_md_var(tc, "descr", "Test asin(+0.0) == +0.0");
151 }
152 
153 ATF_TC_BODY(asin_zero_pos, tc)
154 {
155 	const double x = 0.0L;
156 	double y = asin(x);
157 
158 	if (fabs(y) > 0.0 || signbit(y) != 0)
159 		atf_tc_fail_nonfatal("asin(+0.0) != +0.0");
160 }
161 
162 /*
163  * asinf(3)
164  */
165 ATF_TC(asinf_nan);
166 ATF_TC_HEAD(asinf_nan, tc)
167 {
168 	atf_tc_set_md_var(tc, "descr", "Test asinf(NaN) == NaN");
169 }
170 
171 ATF_TC_BODY(asinf_nan, tc)
172 {
173 	const float x = 0.0L / 0.0L;
174 
175 	if (isnan(asinf(x)) == 0)
176 		atf_tc_fail_nonfatal("asinf(NaN) != NaN");
177 }
178 
179 ATF_TC(asinf_inf_neg);
180 ATF_TC_HEAD(asinf_inf_neg, tc)
181 {
182 	atf_tc_set_md_var(tc, "descr", "Test asinf(-Inf) == NaN");
183 }
184 
185 ATF_TC_BODY(asinf_inf_neg, tc)
186 {
187 	const float x = -1.0L / 0.0L;
188 
189 	if (isnan(asinf(x)) == 0)
190 		atf_tc_fail_nonfatal("asinf(-Inf) != NaN");
191 }
192 
193 ATF_TC(asinf_inf_pos);
194 ATF_TC_HEAD(asinf_inf_pos, tc)
195 {
196 	atf_tc_set_md_var(tc, "descr", "Test asinf(+Inf) == NaN");
197 }
198 
199 ATF_TC_BODY(asinf_inf_pos, tc)
200 {
201 	const float x = 1.0L / 0.0L;
202 
203 	if (isnan(asinf(x)) == 0)
204 		atf_tc_fail_nonfatal("asinf(+Inf) != NaN");
205 }
206 
207 ATF_TC(asinf_range);
208 ATF_TC_HEAD(asinf_range, tc)
209 {
210 	atf_tc_set_md_var(tc, "descr", "Test asinf(x) == NaN, x < -1, x > 1");
211 }
212 
213 ATF_TC_BODY(asinf_range, tc)
214 {
215 	const float x[] = { -1.1, -1.0000001, 1.1, 1.0000001 };
216 	size_t i;
217 
218 	for (i = 0; i < __arraycount(x); i++) {
219 
220 		if (isnan(asinf(x[i])) == 0)
221 			atf_tc_fail_nonfatal("asinf(%f) != NaN", x[i]);
222 	}
223 }
224 
225 ATF_TC(asinf_inrange);
226 ATF_TC_HEAD(asinf_inrange, tc)
227 {
228 	atf_tc_set_md_var(tc, "descr", "Test asinf(x) for some values");
229 }
230 
231 ATF_TC_BODY(asinf_inrange, tc)
232 {
233 	const float eps = 1.0e-6;
234 	float x;
235 	float y;
236 	size_t i;
237 
238 	for (i = 0; i < __arraycount(values); i++) {
239 		x = values[i].x;
240 		y = values[i].y;
241 		if (fabs(asinf(x) - y) > eps)
242 			atf_tc_fail_nonfatal("asinf(%g) != %g", x, y);
243 	}
244 }
245 
246 ATF_TC(asinf_zero_neg);
247 ATF_TC_HEAD(asinf_zero_neg, tc)
248 {
249 	atf_tc_set_md_var(tc, "descr", "Test asinf(-0.0) == -0.0");
250 }
251 
252 ATF_TC_BODY(asinf_zero_neg, tc)
253 {
254 	const float x = -0.0L;
255 	float y = asinf(x);
256 
257 	if (fabsf(y) > 0.0 || signbit(y) == 0)
258 		atf_tc_fail_nonfatal("asinf(-0.0) != -0.0");
259 }
260 
261 ATF_TC(asinf_zero_pos);
262 ATF_TC_HEAD(asinf_zero_pos, tc)
263 {
264 	atf_tc_set_md_var(tc, "descr", "Test asinf(+0.0) == +0.0");
265 }
266 
267 ATF_TC_BODY(asinf_zero_pos, tc)
268 {
269 	const float x = 0.0L;
270 	float y = asinf(x);
271 
272 	if (fabsf(y) > 0.0 || signbit(y) != 0)
273 		atf_tc_fail_nonfatal("asinf(+0.0) != +0.0");
274 }
275 
276 ATF_TP_ADD_TCS(tp)
277 {
278 
279 	ATF_TP_ADD_TC(tp, asin_nan);
280 	ATF_TP_ADD_TC(tp, asin_inf_neg);
281 	ATF_TP_ADD_TC(tp, asin_inf_pos);
282 	ATF_TP_ADD_TC(tp, asin_range);
283 	ATF_TP_ADD_TC(tp, asin_inrange);
284 	ATF_TP_ADD_TC(tp, asin_zero_neg);
285 	ATF_TP_ADD_TC(tp, asin_zero_pos);
286 
287 	ATF_TP_ADD_TC(tp, asinf_nan);
288 	ATF_TP_ADD_TC(tp, asinf_inf_neg);
289 	ATF_TP_ADD_TC(tp, asinf_inf_pos);
290 	ATF_TP_ADD_TC(tp, asinf_range);
291 	ATF_TP_ADD_TC(tp, asinf_inrange);
292 	ATF_TP_ADD_TC(tp, asinf_zero_neg);
293 	ATF_TP_ADD_TC(tp, asinf_zero_pos);
294 
295 	return atf_no_error();
296 }
297