/* Test whether a basic catan invocation works. */
/* This test is generated by misc/genbasic.c. */

#include <complex.h>
#include <errno.h>
#include <fenv.h>
#include <math.h>

#include "../basic.h"

#pragma STDC FENV_ACCESS ON

#define MF_UNSPEC1 (1 << 0)
#define MF_UNSPEC2 (1 << 1)
#define MF_MAYERR (1 << 2)
#define MF_ANYSIGN1 (1 << 2)
#define MF_ANYSIGN2 (1 << 3)

// Soft fail on rounding errors and report only one.
int imprecise;

void test(int variant, double complex input1, double complex lower, double complex expected, double complex upper, int flags)
{
	double complex output = catan(input1);
	if ( !(flags & MF_UNSPEC1) )
	{
		double real = creal(output);
		real = (flags & MF_ANYSIGN1) && real < 0.0 ? -real : real;
		double lower_real = creal(lower);
		double expected_real = creal(expected);
		double upper_real = creal(upper);
		if ( !(isnan(expected_real) ? isnan(real) :
		       isfinite(expected_real) && expected_real != 0.0 ?
		       isfinite(real) && (real == expected_real || (lower_real < real && real < upper_real)) :
		       real == expected_real) )
		{
			if ( imprecise && isfinite(real) && isfinite(expected_real) )
				return;
			warnx("(%d.) catan(%.4f + i*%.4f).real = %.14a, not %.14a, diff %.14a, ratio %.16g",
			     variant, creal(input1), cimag(input1), real, expected_real,
			     real - expected_real, real / expected_real);
			if ( !isfinite(real) || !isfinite(expected_real) )
				exit(1);
			imprecise = 1;
		}
		double imag = cimag(output);
		imag = (flags & MF_ANYSIGN2) && imag < 0.0 ? -imag : imag;
		double lower_imag = cimag(lower);
		double expected_imag = cimag(expected);
		double upper_imag = cimag(upper);
		if ( !(isnan(expected_imag) ? isnan(imag) :
		       isfinite(expected_imag) && expected_imag != 0.0 ?
		       isfinite(imag) && (imag == expected_imag || (lower_imag < imag && imag < upper_imag)) :
		       imag == expected_imag) )
		{
			if ( imprecise && isfinite(imag) && isfinite(expected_imag) )
				return;
			warnx("(%d.) catan(%.4f + i*%.4f).imag = %.14a, not %.14a, diff %.14a, ratio %.16g",
			     variant, creal(input1), cimag(input1), imag, expected_imag,
			     imag - expected_imag, imag / expected_imag);
			if ( !isfinite(imag) || !isfinite(expected_imag) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, CMPLX(90.01, 13.37), CMPLX(0x1.8f575aef92719p+0, 0x1.a736fce697febp-10), CMPLX(0x1.8f575aef9271ap+0, 0x1.a736fce697fecp-10), CMPLX(0x1.8f575aef9271bp+0, 0x1.a736fce697fedp-10), 0);
	test(2, CMPLX(-12.34, 13.37), CMPLX(-0x1.8891da431d0c1p+0, 0x1.4a9599469002cp-5), CMPLX(-0x1.8891da431d0cp+0, 0x1.4a9599469002dp-5), CMPLX(-0x1.8891da431d0bfp+0, 0x1.4a9599469002ep-5), 0);
	test(3, CMPLX(nan(""), 13.37), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(4, CMPLX(strtod("inf", NULL), 13.37), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(5, CMPLX(strtod("-inf", NULL), 13.37), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(6, CMPLX(0.0, 13.37), CMPLX(0x1.921fb54442d17p+0, 0x1.32ee4367ded51p-4), CMPLX(0x1.921fb54442d18p+0, 0x1.32ee4367ded52p-4), CMPLX(0x1.921fb54442d19p+0, 0x1.32ee4367ded53p-4), 0);
	test(7, CMPLX(90.01, -12.34), CMPLX(0x1.8f5511fb8ea1cp+0, -0x1.87dd2c09cea1p-10), CMPLX(0x1.8f5511fb8ea1dp+0, -0x1.87dd2c09cea0fp-10), CMPLX(0x1.8f5511fb8ea1ep+0, -0x1.87dd2c09cea0ep-10), 0);
	test(8, CMPLX(-12.34, -12.34), CMPLX(-0x1.87bd60bff6ebap+0, -0x1.4b908e89d761ap-5), CMPLX(-0x1.87bd60bff6eb9p+0, -0x1.4b908e89d7619p-5), CMPLX(-0x1.87bd60bff6eb8p+0, -0x1.4b908e89d7618p-5), 0);
	test(9, CMPLX(nan(""), -12.34), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(10, CMPLX(strtod("inf", NULL), -12.34), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(11, CMPLX(strtod("-inf", NULL), -12.34), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(12, CMPLX(0.0, -12.34), CMPLX(0x1.921fb54442d17p+0, -0x1.4ca87d2c7ecep-4), CMPLX(0x1.921fb54442d18p+0, -0x1.4ca87d2c7ecdfp-4), CMPLX(0x1.921fb54442d19p+0, -0x1.4ca87d2c7ecdep-4), 0);
	test(13, CMPLX(90.01, nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(14, CMPLX(-12.34, nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(15, CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(16, CMPLX(strtod("inf", NULL), nan("")), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0 | MF_ANYSIGN2);
	test(17, CMPLX(strtod("-inf", NULL), nan("")), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0 | MF_ANYSIGN2);
	test(18, CMPLX(0.0, nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), CMPLX(nan(""), nan("")), 0);
	test(19, CMPLX(90.01, strtod("inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(20, CMPLX(-12.34, strtod("inf", NULL)), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(21, CMPLX(nan(""), strtod("inf", NULL)), CMPLX(nan(""), -0x0.0000000000001p-1022), CMPLX(nan(""), 0x0.0p+0), CMPLX(nan(""), 0x0.0000000000001p-1022), 0);
	test(22, CMPLX(strtod("inf", NULL), strtod("inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(23, CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(24, CMPLX(0.0, strtod("inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(25, CMPLX(90.01, strtod("-inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(26, CMPLX(-12.34, strtod("-inf", NULL)), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(27, CMPLX(nan(""), strtod("-inf", NULL)), CMPLX(nan(""), -0x0.0000000000001p-1022), CMPLX(nan(""), -0x0.0p+0), CMPLX(nan(""), 0x0.0000000000001p-1022), 0);
	test(28, CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(29, CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(30, CMPLX(0.0, strtod("-inf", NULL)), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, -0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(31, CMPLX(90.01, 0.0), CMPLX(0x1.8f47a42251a24p+0, -0x0.0000000000001p-1022), CMPLX(0x1.8f47a42251a25p+0, 0x0.0p+0), CMPLX(0x1.8f47a42251a26p+0, 0x0.0000000000001p-1022), 0);
	test(32, CMPLX(-12.34, 0.0), CMPLX(-0x1.7d6c6dd4a40cdp+0, -0x0.0000000000001p-1022), CMPLX(-0x1.7d6c6dd4a40ccp+0, 0x0.0p+0), CMPLX(-0x1.7d6c6dd4a40cbp+0, 0x0.0000000000001p-1022), 0);
	test(33, CMPLX(nan(""), 0.0), CMPLX(nan(""), -0x0.0000000000001p-1022), CMPLX(nan(""), 0x0.0p+0), CMPLX(nan(""), 0x0.0000000000001p-1022), 0);
	test(34, CMPLX(strtod("inf", NULL), 0.0), CMPLX(0x1.921fb54442d17p+0, -0x0.0000000000001p-1022), CMPLX(0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(0x1.921fb54442d19p+0, 0x0.0000000000001p-1022), 0);
	test(35, CMPLX(strtod("-inf", NULL), 0.0), CMPLX(-0x1.921fb54442d19p+0, -0x0.0000000000001p-1022), CMPLX(-0x1.921fb54442d18p+0, 0x0.0p+0), CMPLX(-0x1.921fb54442d17p+0, 0x0.0000000000001p-1022), 0);
	test(36, CMPLX(0.0, 0.0), CMPLX(-0x0.0000000000001p-1022, -0x0.0000000000001p-1022), CMPLX(0x0.0p+0, 0x0.0p+0), CMPLX(0x0.0000000000001p-1022, 0x0.0000000000001p-1022), 0);
	return imprecise;
}
