/* Test whether a basic ctanl 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 = ctanl(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.) ctanl(%.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.) ctanl(%.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(-0x8.b72e875c3a43cd3p-41L, 0x8.0000000018f8b8ep-3L), CMPLXL(-0x8.b72e875c3a43cd2p-41L, 0x8.0000000018f8b8fp-3L), CMPLXL(-0x8.b72e875c3a43cd1p-41L, 0x8.0000000018f8b9p-3L), 0);
	test(2, CMPLXL(-12.34, 13.37), CMPLXL(0x9.6113be446110e88p-42L, 0xf.ffffffffb2dff0ep-4L), CMPLXL(0x9.6113be446110e89p-42L, 0xf.ffffffffb2dff0fp-4L), CMPLXL(0x9.6113be446110e8ap-42L, 0xf.ffffffffb2dff1p-4L), 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(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(5, CMPLXL(strtold("-inf", NULL), 13.37), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(6, CMPLXL(0.0, 13.37), CMPLXL(-0x0.000000000000001p-16385L, 0xf.ffffffffaa3be4cp-4L), CMPLXL(0x0.0p+0L, 0xf.ffffffffaa3be4dp-4L), CMPLXL(0x0.000000000000001p-16385L, 0xf.ffffffffaa3be4ep-4L), 0);
	test(7, CMPLXL(90.01, -12.34), CMPLXL(-0x8.8c391c0f25b542ap-38L, -0x8.00000000c3ed1b4p-3L), CMPLXL(-0x8.8c391c0f25b5429p-38L, -0x8.00000000c3ed1b3p-3L), CMPLXL(-0x8.8c391c0f25b5428p-38L, -0x8.00000000c3ed1b2p-3L), 0);
	test(8, CMPLXL(-12.34, -12.34), CMPLXL(0x9.32d8ea6151c6178p-39L, -0xf.fffffffda2e0b5ep-4L), CMPLXL(0x9.32d8ea6151c6179p-39L, -0xf.fffffffda2e0b5dp-4L), CMPLXL(0x9.32d8ea6151c617ap-39L, -0xf.fffffffda2e0b5cp-4L), 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(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(11, CMPLXL(strtold("-inf", NULL), -12.34), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(12, CMPLXL(0.0, -12.34), CMPLXL(-0x0.000000000000001p-16385L, -0xf.fffffffd5f150dap-4L), CMPLXL(0x0.0p+0L, -0xf.fffffffd5f150d9p-4L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffd5f150d8p-4L), 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(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(17, CMPLXL(strtold("-inf", NULL), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 0);
	test(18, CMPLXL(0.0, nanl("")), CMPLXL(-0x0.000000000000001p-16385L, nanl("")), CMPLXL(0x0.0p+0L, nanl("")), CMPLXL(0x0.000000000000001p-16385L, nanl("")), 0);
	test(19, CMPLXL(90.01, strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(-0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(20, CMPLXL(-12.34, strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(21, CMPLXL(nanl(""), strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(22, CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(23, CMPLXL(strtold("-inf", NULL), strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(-0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(24, CMPLXL(0.0, strtold("inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, 0xf.fffffffffffffffp-4L), CMPLXL(0x0.0p+0L, 0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, 0x8.000000000000001p-3L), 0);
	test(25, CMPLXL(90.01, strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(-0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(26, CMPLXL(-12.34, strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(27, CMPLXL(nanl(""), strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(28, CMPLXL(strtold("inf", NULL), strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(29, CMPLXL(strtold("-inf", NULL), strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(-0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(30, CMPLXL(0.0, strtold("-inf", NULL)), CMPLXL(-0x0.000000000000001p-16385L, -0x8.000000000000001p-3L), CMPLXL(0x0.0p+0L, -0x8.0p-3L), CMPLXL(0x0.000000000000001p-16385L, -0xf.fffffffffffffffp-4L), 0);
	test(31, CMPLXL(90.01, 0.0), CMPLXL(-0xf.9227c33dfdfda27p-3L, -0x0.000000000000001p-16385L), CMPLXL(-0xf.9227c33dfdfda26p-3L, 0x0.0p+0L), CMPLXL(-0xf.9227c33dfdfda25p-3L, 0x0.000000000000001p-16385L), 0);
	test(32, CMPLXL(-12.34, 0.0), CMPLXL(0xe.bd88b31d321f789p-6L, -0x0.000000000000001p-16385L), CMPLXL(0xe.bd88b31d321f78ap-6L, 0x0.0p+0L), CMPLXL(0xe.bd88b31d321f78bp-6L, 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(nanl(""), -0x0.000000000000001p-16385L), CMPLXL(nanl(""), 0x0.0p+0L), CMPLXL(nanl(""), 0x0.000000000000001p-16385L), 0);
	test(35, CMPLXL(strtold("-inf", NULL), 0.0), CMPLXL(nanl(""), -0x0.000000000000001p-16385L), CMPLXL(nanl(""), 0x0.0p+0L), CMPLXL(nanl(""), 0x0.000000000000001p-16385L), 0);
	test(36, CMPLXL(0.0, 0.0), CMPLXL(-0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L), CMPLXL(0x0.0p+0L, 0x0.0p+0L), CMPLXL(0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L), 0);
	return imprecise;
}
