/* Test whether a basic cexpl 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, long double complex input1, long double complex lower, long double complex expected, long double complex upper, int flags)
{
	long double complex output = cexpl(input1);
	if ( !(flags & MF_UNSPEC1) )
	{
		long double real = creall(output);
		real = (flags & MF_ANYSIGN1) && real < 0.0 ? -real : real;
		long double lower_real = creall(lower);
		long double expected_real = creall(expected);
		long double upper_real = creall(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.) cexpl(%.4Lf + i*%.4Lf).real = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, creall(input1), cimagl(input1), real, expected_real,
			     real - expected_real, real / expected_real);
			if ( !isfinite(real) || !isfinite(expected_real) )
				exit(1);
			imprecise = 1;
		}
		long double imag = cimagl(output);
		imag = (flags & MF_ANYSIGN2) && imag < 0.0 ? -imag : imag;
		long double lower_imag = cimagl(lower);
		long double expected_imag = cimagl(expected);
		long double upper_imag = cimagl(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.) cexpl(%.4Lf + i*%.4Lf).imag = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, creall(input1), cimagl(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, CMPLXL(90.01, 13.37), CMPLXL(0xa.0eb53c3489a3f6fp+126L, 0xa.6e579bcc6c284c2p+126L), CMPLXL(0xa.0eb53c3489a3f7p+126L, 0xa.6e579bcc6c284c3p+126L), CMPLXL(0xa.0eb53c3489a3f71p+126L, 0xa.6e579bcc6c284c4p+126L), 0);
	test(2, CMPLXL(-12.34, 13.37), CMPLXL(0xc.bb522093227cb2dp-22L, 0xd.34625849f9c6db4p-22L), CMPLXL(0xc.bb522093227cb2ep-22L, 0xd.34625849f9c6db5p-22L), CMPLXL(0xc.bb522093227cb2fp-22L, 0xd.34625849f9c6db6p-22L), 0);
	test(3, CMPLXL(nanl(""), 13.37), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(4, CMPLXL(strtold("inf", NULL), 13.37), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), 0);
	test(5, CMPLXL(strtold("-inf", NULL), 13.37), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(6, CMPLXL(0.0, 13.37), CMPLXL(0xb.1b0718ac3f15cp-4L, 0xb.84a0de29eb10817p-4L), CMPLXL(0xb.1b0718ac3f15c01p-4L, 0xb.84a0de29eb10818p-4L), CMPLXL(0xb.1b0718ac3f15c02p-4L, 0xb.84a0de29eb10819p-4L), 0);
	test(7, CMPLXL(90.01, -12.34), CMPLXL(0xe.1eca344071632p+126L, 0xd.0235460a92c8f06p+124L), CMPLXL(0xe.1eca34407163201p+126L, 0xd.0235460a92c8f07p+124L), CMPLXL(0xe.1eca34407163202p+126L, 0xd.0235460a92c8f08p+124L), 0);
	test(8, CMPLXL(-12.34, -12.34), CMPLXL(0x8.effad760031f5cdp-21L, 0x8.3bda9b655cd01afp-23L), CMPLXL(0x8.effad760031f5cep-21L, 0x8.3bda9b655cd01bp-23L), CMPLXL(0x8.effad760031f5cfp-21L, 0x8.3bda9b655cd01b1p-23L), 0);
	test(9, CMPLXL(nanl(""), -12.34), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(10, CMPLXL(strtold("inf", NULL), -12.34), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), 0);
	test(11, CMPLXL(strtold("-inf", NULL), -12.34), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(12, CMPLXL(0.0, -12.34), CMPLXL(0xf.97801940f69bf73p-4L, 0xe.5d42e58f5a5c8abp-6L), CMPLXL(0xf.97801940f69bf74p-4L, 0xe.5d42e58f5a5c8acp-6L), CMPLXL(0xf.97801940f69bf75p-4L, 0xe.5d42e58f5a5c8adp-6L), 0);
	test(13, CMPLXL(90.01, nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(14, CMPLXL(-12.34, nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(15, CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(16, CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(17, CMPLXL(strtold("-inf", NULL), nanl("")), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(18, CMPLXL(0.0, nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(19, CMPLXL(90.01, strtold("inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(20, CMPLXL(-12.34, strtold("inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(21, CMPLXL(nanl(""), strtold("inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(22, CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(23, CMPLXL(strtold("-inf", NULL), strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(24, CMPLXL(0.0, strtold("inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(25, CMPLXL(90.01, strtold("-inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(26, CMPLXL(-12.34, strtold("-inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(27, CMPLXL(nanl(""), strtold("-inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(28, CMPLXL(strtold("inf", NULL), strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(29, CMPLXL(strtold("-inf", NULL), strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, -0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(30, CMPLXL(0.0, strtold("-inf", NULL)), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(31, CMPLXL(90.01, 0.0), CMPLXL(0xe.7d6d4f4fa1a3fddp+126L, -0x0.000000000000001p-16385L), CMPLXL(0xe.7d6d4f4fa1a3fdep+126L, 0x0.0p+0L), CMPLXL(0xe.7d6d4f4fa1a3fdfp+126L, 0x0.000000000000001p-16385L), 0);
	test(32, CMPLXL(-12.34, 0.0), CMPLXL(0x9.2be160e71b82096p-21L, -0x0.000000000000001p-16385L), CMPLXL(0x9.2be160e71b82097p-21L, 0x0.0p+0L), CMPLXL(0x9.2be160e71b82098p-21L, 0x0.000000000000001p-16385L), 0);
	test(33, CMPLXL(nanl(""), 0.0), CMPLXL(nanl(""), -0x0.000000000000001p-16385L), CMPLXL(nanl(""), 0x0.0p+0L), CMPLXL(nanl(""), 0x0.000000000000001p-16385L), 0);
	test(34, CMPLXL(strtold("inf", NULL), 0.0), CMPLXL(strtold("inf", NULL), -0x0.000000000000001p-16385L), CMPLXL(strtold("inf", NULL), 0x0.0p+0L), CMPLXL(strtold("inf", NULL), 0x0.000000000000001p-16385L), 0);
	test(35, CMPLXL(strtold("-inf", NULL), 0.0), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(36, CMPLXL(0.0, 0.0), CMPLXL(0xf.fffffffffffffffp-4L, -0x0.000000000000001p-16385L), CMPLXL(0x8.0p-3L, 0x0.0p+0L), CMPLXL(0x8.000000000000001p-3L, 0x0.000000000000001p-16385L), 0);
	return imprecise;
}
