/* Test whether a basic cpowl 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 input2, long double complex lower, long double complex expected, long double complex upper, int flags)
{
	long double complex output = cpowl(input1, input2);
	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.) cpowl(%.4Lf + i*%.4Lf, %.4Lf + i*%.4Lf).real = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, creall(input1), cimagl(input1), creall(input2), cimagl(input2), 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.) cpowl(%.4Lf + i*%.4Lf, %.4Lf + i*%.4Lf).imag = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, creall(input1), cimagl(input1), creall(input2), cimagl(input2), 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(10.1, 4.2), CMPLXL(-0xd.3dba270767d576p+55L, 0xe.448251a4c3eb80bp+61L), CMPLXL(-0xd.3dba270767d575fp+55L, 0xe.448251a4c3eb80cp+61L), CMPLXL(-0xd.3dba270767d575ep+55L, 0xe.448251a4c3eb80dp+61L), 0);
	test(2, CMPLXL(0.0, 13.37), CMPLXL(10.1, 4.2), CMPLXL(-0x8.05875721aa2bd4p+21L, 0x9.9a4e36044904ccp+25L), CMPLXL(-0x8.05875721aa2bd3fp+21L, 0x9.9a4e36044904cc1p+25L), CMPLXL(-0x8.05875721aa2bd3ep+21L, 0x9.9a4e36044904cc2p+25L), 0);
	test(3, CMPLXL(90.01, 0.0), CMPLXL(10.1, 4.2), CMPLXL(0xb.db16bf799f3e5c3p+62L, 0x9.8384428e9f223bbp+58L), CMPLXL(0xb.db16bf799f3e5c4p+62L, 0x9.8384428e9f223bcp+58L), CMPLXL(0xb.db16bf799f3e5c5p+62L, 0x9.8384428e9f223bdp+58L), 0);
	test(4, CMPLXL(0.0, 0.0), CMPLXL(10.1, 4.2), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, -0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	test(5, CMPLXL(90.01, 13.37), CMPLXL(0.0, 4.2), CMPLXL(0x8.92beb961eafac49p-4L, 0xd.337485fc9f0f5aap-8L), CMPLXL(0x8.92beb961eafac4ap-4L, 0xd.337485fc9f0f5abp-8L), CMPLXL(0x8.92beb961eafac4bp-4L, 0xd.337485fc9f0f5acp-8L), 0);
	test(6, CMPLXL(0.0, 13.37), CMPLXL(0.0, 4.2), CMPLXL(-0x9.5c8768dd2e6f30dp-16L, -0xb.1cc141832ef8914p-13L), CMPLXL(-0x9.5c8768dd2e6f30cp-16L, -0xb.1cc141832ef8913p-13L), CMPLXL(-0x9.5c8768dd2e6f30bp-16L, -0xb.1cc141832ef8912p-13L), 0);
	test(7, CMPLXL(90.01, 0.0), CMPLXL(0.0, 4.2), CMPLXL(0xf.fadbb63b8c29a19p-4L, 0xc.d2b8dcff09be386p-8L), CMPLXL(0xf.fadbb63b8c29a1ap-4L, 0xc.d2b8dcff09be387p-8L), CMPLXL(0xf.fadbb63b8c29a1bp-4L, 0xc.d2b8dcff09be388p-8L), 0);
	test(8, CMPLXL(90.01, 13.37), CMPLXL(10.1, 0.0), CMPLXL(0x8.a033031730f0b17p+59L, 0xd.35bb2fa9da6611ep+62L), CMPLXL(0x8.a033031730f0b18p+59L, 0xd.35bb2fa9da6611fp+62L), CMPLXL(0x8.a033031730f0b19p+59L, 0xd.35bb2fa9da6612p+62L), 0);
	test(9, CMPLXL(0.0, 13.37), CMPLXL(10.1, 0.0), CMPLXL(-0xd.9981c078c9d5892p+34L, -0x8.9daa82574924218p+32L), CMPLXL(-0xd.9981c078c9d5891p+34L, -0x8.9daa82574924217p+32L), CMPLXL(-0xd.9981c078c9d589p+34L, -0x8.9daa82574924216p+32L), 0);
	test(10, CMPLXL(90.01, 0.0), CMPLXL(10.1, 0.0), CMPLXL(0xb.dee754071e7a36dp+62L, -0x0.000000000000001p-16385L), CMPLXL(0xb.dee754071e7a36ep+62L, 0x0.0p+0L), CMPLXL(0xb.dee754071e7a36fp+62L, 0x0.000000000000001p-16385L), 0);
	test(11, CMPLXL(0.0, 0.0), CMPLXL(10.1, 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(12, CMPLXL(90.01, 13.37), 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);
	test(13, CMPLXL(0.0, 13.37), 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);
	test(14, CMPLXL(90.01, 0.0), 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;
}
