xref: /openbsd-src/regress/lib/libm/msun/invctrig_test.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: invctrig_test.c,v 1.1 2021/10/22 18:00:22 mbuhl Exp $	*/
2 /*-
3  * Copyright (c) 2008-2013 David Schultz <das@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "macros.h"
29 
30 /*
31  * Tests for casin[h](), cacos[h](), and catan[h]().
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <complex.h>
39 #include <fenv.h>
40 #include <float.h>
41 #include <math.h>
42 #include <stdio.h>
43 
44 #include "test-utils.h"
45 
46 #pragma	STDC FENV_ACCESS	ON
47 #pragma	STDC CX_LIMITED_RANGE	OFF
48 
49 /*
50  * Test that a function returns the correct value and sets the
51  * exception flags correctly. The exceptmask specifies which
52  * exceptions we should check. We need to be lenient for several
53  * reasons, but mainly because on some architectures it's impossible
54  * to raise FE_OVERFLOW without raising FE_INEXACT.
55  *
56  * These are macros instead of functions so that assert provides more
57  * meaningful error messages.
58  *
59  * XXX The volatile here is to avoid gcc's bogus constant folding and work
60  *     around the lack of support for the FENV_ACCESS pragma.
61  */
62 #define	test_p(func, z, result, exceptmask, excepts, checksign)	do {	\
63 	volatile long double complex _d = z;				\
64 	debug("  testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func,	\
65 	    creall(_d), cimagl(_d), creall(result), cimagl(result));	\
66 	ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));		\
67 	CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign));		\
68 	CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)",	\
69 	    #func, #z);							\
70 } while (0)
71 
72 /*
73  * Test within a given tolerance.  The tolerance indicates relative error
74  * in ulps.
75  */
76 #define	test_p_tol(func, z, result, tol)			do {	\
77 	debug("  testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func,	\
78 	    creall(z), cimagl(z), creall(result), cimagl(result));	\
79 	CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), CS_BOTH);	\
80 } while (0)
81 
82 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
83 #define	test(func, z, result, exceptmask, excepts, checksign)	do {	\
84 	test_p(func, z, result, exceptmask, excepts, checksign);	\
85 	test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
86 } while (0)
87 #define	test_tol(func, z, result, tol)				do {	\
88 	test_p_tol(func, z, result, tol);				\
89 	test_p_tol(func, conjl(z), conjl(result), tol);			\
90 } while (0)
91 
92 /* Test the given function in all precisions. */
93 #define	testall(func, x, result, exceptmask, excepts, checksign) do {	\
94 	test(func, x, result, exceptmask, excepts, checksign);		\
95 	test(func##f, x, result, exceptmask, excepts, checksign);	\
96 } while (0)
97 #define	testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
98 	testall(func, x, result, exceptmask, excepts, checksign);	\
99 	testall(func, -(x), -result, exceptmask, excepts, checksign);	\
100 } while (0)
101 #define	testall_even(func, x, result, exceptmask, excepts, checksign) do { \
102 	testall(func, x, result, exceptmask, excepts, checksign);	\
103 	testall(func, -(x), result, exceptmask, excepts, checksign);	\
104 } while (0)
105 
106 /*
107  * Test the given function in all precisions, within a given tolerance.
108  * The tolerance is specified in ulps.
109  */
110 #define	testall_tol(func, x, result, tol)	       		   do { \
111 	test_tol(func, x, result, (tol) * DBL_ULP());			\
112 	test_tol(func##f, x, result, (tol) * FLT_ULP());		\
113 } while (0)
114 #define	testall_odd_tol(func, x, result, tol)	       		   do { \
115 	testall_tol(func, x, result, tol);				\
116 	testall_tol(func, -(x), -result, tol);				\
117 } while (0)
118 #define	testall_even_tol(func, x, result, tol)	       		   do { \
119 	testall_tol(func, x, result, tol);				\
120 	testall_tol(func, -(x), result, tol);				\
121 } while (0)
122 
123 static const long double
124 pi = 3.14159265358979323846264338327950280L,
125 c3pi = 9.42477796076937971538793014983850839L;
126 
127 
128 /* Tests for 0 */
129 ATF_TC_WITHOUT_HEAD(zero);
130 ATF_TC_BODY(zero, tc)
131 {
132 	long double complex zero = CMPLXL(0.0, 0.0);
133 
134 	testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
135 	testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
136 	testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
137 	testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
138 
139 	testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
140 	testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
141 
142 	testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
143 	testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
144 }
145 
146 /*
147  * Tests for NaN inputs.
148  */
149 ATF_TC_WITHOUT_HEAD(nan);
150 ATF_TC_BODY(nan, tc)
151 {
152 	long double complex nan_nan = CMPLXL(NAN, NAN);
153 	long double complex z;
154 
155 	/*
156 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
157 	 * NaN,NaN	NaN,NaN	    NaN,NaN	NaN,NaN	    NaN,NaN
158 	 * finite,NaN	NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
159 	 * NaN,finite   NaN,NaN*    NaN,NaN*	NaN,NaN*    NaN,NaN*
160 	 * NaN,Inf	Inf,NaN     NaN,-Inf	?Inf,NaN    ?0,pi/2
161 	 * +-Inf,NaN	Inf,NaN     NaN,?Inf	+-Inf,NaN   +-0,NaN
162 	 * +-0,NaN	NaN,NaN*    pi/2,NaN	NaN,NaN*    +-0,NaN
163 	 * NaN,0	NaN,NaN*    NaN,NaN*	NaN,0	    NaN,NaN*
164 	 *
165 	 *  * = raise invalid
166 	 */
167 	z = nan_nan;
168 	testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
169 	testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
170 	testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
171 	testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
172 	testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
173 	testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
174 
175 	z = CMPLXL(0.5, NAN);
176 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
177 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
178 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
179 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
180 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
181 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
182 
183 	z = CMPLXL(NAN, 0.5);
184 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
185 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
186 	testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
187 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
188 	testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
189 	testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
190 
191 	z = CMPLXL(NAN, INFINITY);
192 	testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
193 	testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
194 	testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
195 	testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
196 	testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
197 	testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
198 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
199 
200 	z = CMPLXL(INFINITY, NAN);
201 	testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
202 		     CS_REAL);
203 	testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
204 	testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
205 		    CS_REAL);
206 	testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
207 	testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
208 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
209 
210 	z = CMPLXL(0.0, NAN);
211         /* XXX We allow a spurious inexact exception here. */
212 	testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
213 	testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
214 	testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
215 	testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
216 	testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
217 	testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
218 
219 	z = CMPLXL(NAN, 0.0);
220 	testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
221 	testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
222 	testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
223 	testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
224 	testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
225 	testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
226 }
227 
228 ATF_TC_WITHOUT_HEAD(inf);
229 ATF_TC_BODY(inf, tc)
230 {
231 	long double complex z;
232 
233 	/*
234 	 * IN		CACOSH	    CACOS	CASINH	    CATANH
235 	 * Inf,Inf	Inf,pi/4    pi/4,-Inf	Inf,pi/4    0,pi/2
236 	 * -Inf,Inf	Inf,3pi/4   3pi/4,-Inf	---	    ---
237 	 * Inf,finite	Inf,0	    0,-Inf	Inf,0	    0,pi/2
238 	 * -Inf,finite	Inf,pi      pi,-Inf	---	    ---
239 	 * finite,Inf	Inf,pi/2    pi/2,-Inf	Inf,pi/2    0,pi/2
240 	 */
241 	z = CMPLXL(INFINITY, INFINITY);
242 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
243 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
244 	testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
245 	testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
246 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
247 	testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
248 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
249 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
250 
251 	z = CMPLXL(INFINITY, 0.5);
252 	/* XXX We allow a spurious inexact exception here. */
253 	testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
254 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
255 	testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
256 	testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
257 	testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
258 	testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
259 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
260 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
261 
262 	z = CMPLXL(0.5, INFINITY);
263 	testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
264 	testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
265 	testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
266 	testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
267 	testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
268 	/* XXX We allow a spurious inexact exception here. */
269 	testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
270 	testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
271 	testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
272 }
273 
274 /* Tests along the real and imaginary axes. */
275 ATF_TC_WITHOUT_HEAD(axes);
276 ATF_TC_BODY(axes, tc)
277 {
278 	static const long double nums[] = {
279 		-2, -1, -0.5, 0.5, 1, 2
280 	};
281 	long double complex z;
282 	unsigned i;
283 
284 	for (i = 0; i < nitems(nums); i++) {
285 		/* Real axis */
286 		z = CMPLXL(nums[i], 0.0);
287 		if (fabsl(nums[i]) <= 1) {
288 			testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
289 			testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
290 			testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
291 			testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
292 		} else {
293 			testall_tol(cacosh, z,
294 				    CMPLXL(acosh(fabsl(nums[i])),
295 					   (nums[i] < 0) ? pi : 0), 1);
296 			testall_tol(cacos, z,
297 				    CMPLXL((nums[i] < 0) ? pi : 0,
298 					   -acosh(fabsl(nums[i]))), 1);
299 			testall_tol(casin, z,
300 				    CMPLXL(copysign(pi / 2, nums[i]),
301 					   acosh(fabsl(nums[i]))), 1);
302 			testall_tol(catanh, z,
303 				    CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
304 		}
305 		testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
306 		testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
307 
308 		/* TODO: Test the imaginary axis. */
309 	}
310 }
311 
312 ATF_TC_WITHOUT_HEAD(small);
313 ATF_TC_BODY(small, tc)
314 {
315 	/*
316 	 * z =  0.75 + i 0.25
317 	 *     acos(z) = Pi/4 - i ln(2)/2
318 	 *     asin(z) = Pi/4 + i ln(2)/2
319 	 *     atan(z) = atan(4)/2 + i ln(17/9)/4
320 	 */
321 	complex long double z;
322 	complex long double acos_z;
323 	complex long double asin_z;
324 	complex long double atan_z;
325 
326 	z = CMPLXL(0.75L, 0.25L);
327 	acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L);
328 	asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L);
329 	atan_z = CMPLXL(0.66290883183401623252961960521423782L,
330 			 0.15899719167999917436476103600701878L);
331 
332 	testall_tol(cacos, z, acos_z, 2);
333 	testall_odd_tol(casin, z, asin_z, 2);
334 	testall_odd_tol(catan, z, atan_z, 2);
335 }
336 
337 /* Test inputs that might cause overflow in a sloppy implementation. */
338 ATF_TC_WITHOUT_HEAD(large);
339 ATF_TC_BODY(large, tc)
340 {
341 	/* TODO: Write these tests */
342 }
343 
344 ATF_TP_ADD_TCS(tp)
345 {
346 	ATF_TP_ADD_TC(tp, zero);
347 	ATF_TP_ADD_TC(tp, nan);
348 	ATF_TP_ADD_TC(tp, inf);
349 	ATF_TP_ADD_TC(tp, axes);
350 	ATF_TP_ADD_TC(tp, small);
351 	ATF_TP_ADD_TC(tp, large);
352 
353 	return (atf_no_error());
354 }
355