/* Test whether a basic creal 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 lower, double expected, double upper, int flags)
{
	double output = creal(input1);
	if ( !(flags & MF_UNSPEC1) )
	{
		if ( !(isnan(expected) ? isnan(output) :
		       isfinite(expected) && expected != 0.0 ?
		       isfinite(output) && (output == expected || (lower < output && output < upper)) :
		       output == expected) )
		{
			if ( imprecise && isfinite(output) && isfinite(expected) )
				return;
			warnx("(%d.) creal(%.4f + i*%.4f) = %.14a, not %.14a, diff %.14a, ratio %.16g",
			     variant, creal(input1), cimag(input1), output, expected,
			     output - expected, output / expected);
			if ( !isfinite(output) || !isfinite(expected) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, CMPLX(90.01, 13.37), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(2, CMPLX(-12.34, 13.37), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(3, CMPLX(nan(""), 13.37), nan(""), nan(""), nan(""), 0);
	test(4, CMPLX(strtod("inf", NULL), 13.37), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(5, CMPLX(strtod("-inf", NULL), 13.37), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(6, CMPLX(0.0, 13.37), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(7, CMPLX(90.01, -12.34), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(8, CMPLX(-12.34, -12.34), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(9, CMPLX(nan(""), -12.34), nan(""), nan(""), nan(""), 0);
	test(10, CMPLX(strtod("inf", NULL), -12.34), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(11, CMPLX(strtod("-inf", NULL), -12.34), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(12, CMPLX(0.0, -12.34), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(13, CMPLX(90.01, nan("")), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(14, CMPLX(-12.34, nan("")), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(15, CMPLX(nan(""), nan("")), nan(""), nan(""), nan(""), 0);
	test(16, CMPLX(strtod("inf", NULL), nan("")), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(17, CMPLX(strtod("-inf", NULL), nan("")), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(18, CMPLX(0.0, nan("")), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(19, CMPLX(90.01, strtod("inf", NULL)), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(20, CMPLX(-12.34, strtod("inf", NULL)), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(21, CMPLX(nan(""), strtod("inf", NULL)), nan(""), nan(""), nan(""), 0);
	test(22, CMPLX(strtod("inf", NULL), strtod("inf", NULL)), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(23, CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(24, CMPLX(0.0, strtod("inf", NULL)), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(25, CMPLX(90.01, strtod("-inf", NULL)), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(26, CMPLX(-12.34, strtod("-inf", NULL)), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(27, CMPLX(nan(""), strtod("-inf", NULL)), nan(""), nan(""), nan(""), 0);
	test(28, CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(29, CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(30, CMPLX(0.0, strtod("-inf", NULL)), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(31, CMPLX(90.01, 0.0), 0x1.680a3d70a3d7p+6, 0x1.680a3d70a3d71p+6, 0x1.680a3d70a3d72p+6, 0);
	test(32, CMPLX(-12.34, 0.0), -0x1.8ae147ae147afp+3, -0x1.8ae147ae147aep+3, -0x1.8ae147ae147adp+3, 0);
	test(33, CMPLX(nan(""), 0.0), nan(""), nan(""), nan(""), 0);
	test(34, CMPLX(strtod("inf", NULL), 0.0), strtod("inf", NULL), strtod("inf", NULL), strtod("inf", NULL), 0);
	test(35, CMPLX(strtod("-inf", NULL), 0.0), strtod("-inf", NULL), strtod("-inf", NULL), strtod("-inf", NULL), 0);
	test(36, CMPLX(0.0, 0.0), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	return imprecise;
}
