/* Test whether a basic conj 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 = conj(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.) conj(%.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.) conj(%.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.680a3d70a3d7p+6, -0x1.abd70a3d70a3ep+3), CMPLX(0x1.680a3d70a3d71p+6, -0x1.abd70a3d70a3dp+3), CMPLX(0x1.680a3d70a3d72p+6, -0x1.abd70a3d70a3cp+3), 0);
	test(2, CMPLX(-12.34, 13.37), CMPLX(-0x1.8ae147ae147afp+3, -0x1.abd70a3d70a3ep+3), CMPLX(-0x1.8ae147ae147aep+3, -0x1.abd70a3d70a3dp+3), CMPLX(-0x1.8ae147ae147adp+3, -0x1.abd70a3d70a3cp+3), 0);
	test(3, CMPLX(nan(""), 13.37), CMPLX(nan(""), -0x1.abd70a3d70a3ep+3), CMPLX(nan(""), -0x1.abd70a3d70a3dp+3), CMPLX(nan(""), -0x1.abd70a3d70a3cp+3), 0);
	test(4, CMPLX(strtod("inf", NULL), 13.37), CMPLX(strtod("inf", NULL), -0x1.abd70a3d70a3ep+3), CMPLX(strtod("inf", NULL), -0x1.abd70a3d70a3dp+3), CMPLX(strtod("inf", NULL), -0x1.abd70a3d70a3cp+3), 0);
	test(5, CMPLX(strtod("-inf", NULL), 13.37), CMPLX(strtod("-inf", NULL), -0x1.abd70a3d70a3ep+3), CMPLX(strtod("-inf", NULL), -0x1.abd70a3d70a3dp+3), CMPLX(strtod("-inf", NULL), -0x1.abd70a3d70a3cp+3), 0);
	test(6, CMPLX(0.0, 13.37), CMPLX(-0x0.0000000000001p-1022, -0x1.abd70a3d70a3ep+3), CMPLX(0x0.0p+0, -0x1.abd70a3d70a3dp+3), CMPLX(0x0.0000000000001p-1022, -0x1.abd70a3d70a3cp+3), 0);
	test(7, CMPLX(90.01, -12.34), CMPLX(0x1.680a3d70a3d7p+6, 0x1.8ae147ae147adp+3), CMPLX(0x1.680a3d70a3d71p+6, 0x1.8ae147ae147aep+3), CMPLX(0x1.680a3d70a3d72p+6, 0x1.8ae147ae147afp+3), 0);
	test(8, CMPLX(-12.34, -12.34), CMPLX(-0x1.8ae147ae147afp+3, 0x1.8ae147ae147adp+3), CMPLX(-0x1.8ae147ae147aep+3, 0x1.8ae147ae147aep+3), CMPLX(-0x1.8ae147ae147adp+3, 0x1.8ae147ae147afp+3), 0);
	test(9, CMPLX(nan(""), -12.34), CMPLX(nan(""), 0x1.8ae147ae147adp+3), CMPLX(nan(""), 0x1.8ae147ae147aep+3), CMPLX(nan(""), 0x1.8ae147ae147afp+3), 0);
	test(10, CMPLX(strtod("inf", NULL), -12.34), CMPLX(strtod("inf", NULL), 0x1.8ae147ae147adp+3), CMPLX(strtod("inf", NULL), 0x1.8ae147ae147aep+3), CMPLX(strtod("inf", NULL), 0x1.8ae147ae147afp+3), 0);
	test(11, CMPLX(strtod("-inf", NULL), -12.34), CMPLX(strtod("-inf", NULL), 0x1.8ae147ae147adp+3), CMPLX(strtod("-inf", NULL), 0x1.8ae147ae147aep+3), CMPLX(strtod("-inf", NULL), 0x1.8ae147ae147afp+3), 0);
	test(12, CMPLX(0.0, -12.34), CMPLX(-0x0.0000000000001p-1022, 0x1.8ae147ae147adp+3), CMPLX(0x0.0p+0, 0x1.8ae147ae147aep+3), CMPLX(0x0.0000000000001p-1022, 0x1.8ae147ae147afp+3), 0);
	test(13, CMPLX(90.01, nan("")), CMPLX(0x1.680a3d70a3d7p+6, nan("")), CMPLX(0x1.680a3d70a3d71p+6, nan("")), CMPLX(0x1.680a3d70a3d72p+6, nan("")), 0);
	test(14, CMPLX(-12.34, nan("")), CMPLX(-0x1.8ae147ae147afp+3, nan("")), CMPLX(-0x1.8ae147ae147aep+3, nan("")), CMPLX(-0x1.8ae147ae147adp+3, 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(strtod("inf", NULL), nan("")), CMPLX(strtod("inf", NULL), nan("")), CMPLX(strtod("inf", NULL), nan("")), 0);
	test(17, CMPLX(strtod("-inf", NULL), nan("")), CMPLX(strtod("-inf", NULL), nan("")), CMPLX(strtod("-inf", NULL), nan("")), CMPLX(strtod("-inf", NULL), nan("")), 0);
	test(18, CMPLX(0.0, nan("")), CMPLX(-0x0.0000000000001p-1022, nan("")), CMPLX(0x0.0p+0, nan("")), CMPLX(0x0.0000000000001p-1022, nan("")), 0);
	test(19, CMPLX(90.01, strtod("inf", NULL)), CMPLX(0x1.680a3d70a3d7p+6, strtod("-inf", NULL)), CMPLX(0x1.680a3d70a3d71p+6, strtod("-inf", NULL)), CMPLX(0x1.680a3d70a3d72p+6, strtod("-inf", NULL)), 0);
	test(20, CMPLX(-12.34, strtod("inf", NULL)), CMPLX(-0x1.8ae147ae147afp+3, strtod("-inf", NULL)), CMPLX(-0x1.8ae147ae147aep+3, strtod("-inf", NULL)), CMPLX(-0x1.8ae147ae147adp+3, strtod("-inf", NULL)), 0);
	test(21, CMPLX(nan(""), strtod("inf", NULL)), CMPLX(nan(""), strtod("-inf", NULL)), CMPLX(nan(""), strtod("-inf", NULL)), CMPLX(nan(""), strtod("-inf", NULL)), 0);
	test(22, CMPLX(strtod("inf", NULL), strtod("inf", NULL)), CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), 0);
	test(23, CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), 0);
	test(24, CMPLX(0.0, strtod("inf", NULL)), CMPLX(-0x0.0000000000001p-1022, strtod("-inf", NULL)), CMPLX(0x0.0p+0, strtod("-inf", NULL)), CMPLX(0x0.0000000000001p-1022, strtod("-inf", NULL)), 0);
	test(25, CMPLX(90.01, strtod("-inf", NULL)), CMPLX(0x1.680a3d70a3d7p+6, strtod("inf", NULL)), CMPLX(0x1.680a3d70a3d71p+6, strtod("inf", NULL)), CMPLX(0x1.680a3d70a3d72p+6, strtod("inf", NULL)), 0);
	test(26, CMPLX(-12.34, strtod("-inf", NULL)), CMPLX(-0x1.8ae147ae147afp+3, strtod("inf", NULL)), CMPLX(-0x1.8ae147ae147aep+3, strtod("inf", NULL)), CMPLX(-0x1.8ae147ae147adp+3, strtod("inf", NULL)), 0);
	test(27, CMPLX(nan(""), strtod("-inf", NULL)), CMPLX(nan(""), strtod("inf", NULL)), CMPLX(nan(""), strtod("inf", NULL)), CMPLX(nan(""), strtod("inf", NULL)), 0);
	test(28, CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("inf", NULL), strtod("inf", NULL)), CMPLX(strtod("inf", NULL), strtod("inf", NULL)), CMPLX(strtod("inf", NULL), strtod("inf", NULL)), 0);
	test(29, CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), 0);
	test(30, CMPLX(0.0, strtod("-inf", NULL)), CMPLX(-0x0.0000000000001p-1022, strtod("inf", NULL)), CMPLX(0x0.0p+0, strtod("inf", NULL)), CMPLX(0x0.0000000000001p-1022, strtod("inf", NULL)), 0);
	test(31, CMPLX(90.01, 0.0), CMPLX(0x1.680a3d70a3d7p+6, -0x0.0000000000001p-1022), CMPLX(0x1.680a3d70a3d71p+6, -0x0.0p+0), CMPLX(0x1.680a3d70a3d72p+6, 0x0.0000000000001p-1022), 0);
	test(32, CMPLX(-12.34, 0.0), CMPLX(-0x1.8ae147ae147afp+3, -0x0.0000000000001p-1022), CMPLX(-0x1.8ae147ae147aep+3, -0x0.0p+0), CMPLX(-0x1.8ae147ae147adp+3, 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(strtod("inf", NULL), -0x0.0000000000001p-1022), CMPLX(strtod("inf", NULL), -0x0.0p+0), CMPLX(strtod("inf", NULL), 0x0.0000000000001p-1022), 0);
	test(35, CMPLX(strtod("-inf", NULL), 0.0), CMPLX(strtod("-inf", NULL), -0x0.0000000000001p-1022), CMPLX(strtod("-inf", NULL), -0x0.0p+0), CMPLX(strtod("-inf", NULL), 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;
}
