#include <sys/param.h>
#include <complex.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include "test-utils.h"
#pragma STDC FENV_ACCESS ON
#pragma STDC CX_LIMITED_RANGE OFF
#define test_p(func, z, result, exceptmask, excepts, checksign) do { \
volatile long double complex _d = z; \
debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \
creall(_d), cimagl(_d), creall(result), cimagl(result)); \
ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
CHECK_CFPEQUAL_CS((func)(_d), (result), (checksign)); \
CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
#func, #z); \
} while (0)
#define test_p_tol(func, z, result, tol) do { \
debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \
creall(z), cimagl(z), creall(result), cimagl(result)); \
CHECK_CFPEQUAL_TOL((func)(z), (result), (tol), CS_BOTH); \
} while (0)
#define test(func, z, result, exceptmask, excepts, checksign) do { \
test_p(func, z, result, exceptmask, excepts, checksign); \
test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
} while (0)
#define test_tol(func, z, result, tol) do { \
test_p_tol(func, z, result, tol); \
test_p_tol(func, conjl(z), conjl(result), tol); \
} while (0)
#define testall(func, x, result, exceptmask, excepts, checksign) do { \
test(func, x, result, exceptmask, excepts, checksign); \
test(func##f, x, result, exceptmask, excepts, checksign); \
} while (0)
#define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
testall(func, x, result, exceptmask, excepts, checksign); \
testall(func, -(x), -result, exceptmask, excepts, checksign); \
} while (0)
#define testall_even(func, x, result, exceptmask, excepts, checksign) do { \
testall(func, x, result, exceptmask, excepts, checksign); \
testall(func, -(x), result, exceptmask, excepts, checksign); \
} while (0)
#define testall_tol(func, x, result, tol) do { \
test_tol(func, x, result, (tol) * DBL_ULP()); \
test_tol(func##f, x, result, (tol) * FLT_ULP()); \
} while (0)
#define testall_odd_tol(func, x, result, tol) do { \
testall_tol(func, x, result, tol); \
testall_tol(func, -(x), -result, tol); \
} while (0)
#define testall_even_tol(func, x, result, tol) do { \
testall_tol(func, x, result, tol); \
testall_tol(func, -(x), result, tol); \
} while (0)
static const long double
pi = 3.14159265358979323846264338327950280L,
c3pi = 9.42477796076937971538793014983850839L;
ATF_TC_WITHOUT_HEAD(zero);
ATF_TC_BODY(zero, tc)
{
#if defined(__riscv)
atf_tc_expect_death("https://bugs.freebsd.org/290099");
#endif
long double complex zero = CMPLXL(0.0, 0.0);
testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
}
ATF_TC_WITHOUT_HEAD(nan);
ATF_TC_BODY(nan, tc)
{
#if defined(__riscv)
atf_tc_expect_death("https://bugs.freebsd.org/290099");
#endif
long double complex nan_nan = CMPLXL(NAN, NAN);
long double complex z;
z = nan_nan;
testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
z = CMPLXL(0.5, NAN);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, 0.5);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, INFINITY);
testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
z = CMPLXL(INFINITY, NAN);
testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
CS_REAL);
testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
CS_REAL);
testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
z = CMPLXL(0.0, NAN);
testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, 0.0);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
}
ATF_TC_WITHOUT_HEAD(inf);
ATF_TC_BODY(inf, tc)
{
#if defined(__riscv)
atf_tc_expect_death("https://bugs.freebsd.org/290099");
#endif
long double complex z;
z = CMPLXL(INFINITY, INFINITY);
testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
z = CMPLXL(INFINITY, 0.5);
testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
z = CMPLXL(0.5, INFINITY);
testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
}
ATF_TC_WITHOUT_HEAD(axes);
ATF_TC_BODY(axes, tc)
{
#if defined(__riscv)
atf_tc_expect_death("https://bugs.freebsd.org/290099");
#endif
static const long double nums[] = {
-2, -1, -0.5, 0.5, 1, 2
};
long double complex z;
unsigned i;
for (i = 0; i < nitems(nums); i++) {
z = CMPLXL(nums[i], 0.0);
if (fabsl(nums[i]) <= 1) {
testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
} else {
testall_tol(cacosh, z,
CMPLXL(acosh(fabsl(nums[i])),
(nums[i] < 0) ? pi : 0), 1);
testall_tol(cacos, z,
CMPLXL((nums[i] < 0) ? pi : 0,
-acosh(fabsl(nums[i]))), 1);
testall_tol(casin, z,
CMPLXL(copysign(pi / 2, nums[i]),
acosh(fabsl(nums[i]))), 1);
testall_tol(catanh, z,
CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
}
testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
}
}
ATF_TC_WITHOUT_HEAD(small);
ATF_TC_BODY(small, tc)
{
#if defined(__riscv)
atf_tc_expect_death("https://bugs.freebsd.org/290099");
#endif
complex long double z;
complex long double acos_z;
complex long double asin_z;
complex long double atan_z;
z = CMPLXL(0.75L, 0.25L);
acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L);
asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L);
atan_z = CMPLXL(0.66290883183401623252961960521423782L,
0.15899719167999917436476103600701878L);
testall_tol(cacos, z, acos_z, 2);
testall_odd_tol(casin, z, asin_z, 2);
testall_odd_tol(catan, z, atan_z, 2);
}
ATF_TC_WITHOUT_HEAD(large);
ATF_TC_BODY(large, tc)
{
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, zero);
ATF_TP_ADD_TC(tp, nan);
ATF_TP_ADD_TC(tp, inf);
ATF_TP_ADD_TC(tp, axes);
ATF_TP_ADD_TC(tp, small);
ATF_TP_ADD_TC(tp, large);
return (atf_no_error());
}