/* Test whether a basic cacoshl 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 = cacoshl(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.) cacoshl(%.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.) cacoshl(%.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.686c459f9ac542dp-1L, 0x9.7024686ab81b73fp-6L), CMPLXL(0xa.686c459f9ac542ep-1L, 0x9.7024686ab81b74p-6L), CMPLXL(0xa.686c459f9ac542fp-1L, 0x9.7024686ab81b741p-6L), 0);
	test(2, CMPLXL(-12.34, 13.37), CMPLXL(0xe.60948fc95e5b8aep-2L, 0x9.42f87bd8074fde4p-2L), CMPLXL(0xe.60948fc95e5b8afp-2L, 0x9.42f87bd8074fde5p-2L), CMPLXL(0xe.60948fc95e5b8bp-2L, 0x9.42f87bd8074fde6p-2L), 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), -0x0.000000000000001p-16385L), CMPLXL(strtold("inf", NULL), 0x0.0p+0L), CMPLXL(strtold("inf", NULL), 0x0.000000000000001p-16385L), 0);
	test(5, CMPLXL(strtold("-inf", NULL), 13.37), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c234p-2L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-2L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-2L), 0);
	test(6, CMPLXL(0.0, 13.37), CMPLXL(0xd.26752188978df9dp-2L, 0xc.90fdaa22168c234p-3L), CMPLXL(0xd.26752188978df9ep-2L, 0xc.90fdaa22168c235p-3L), CMPLXL(0xd.26752188978df9fp-2L, 0xc.90fdaa22168c236p-3L), 0);
	test(7, CMPLXL(90.01, -12.34), CMPLXL(0xa.679a5075b72ab24p-1L, -0x8.b86581e8aa0b7ebp-6L), CMPLXL(0xa.679a5075b72ab25p-1L, -0x8.b86581e8aa0b7eap-6L), CMPLXL(0xa.679a5075b72ab26p-1L, -0x8.b86581e8aa0b7e9p-6L), 0);
	test(8, CMPLXL(-12.34, -12.34), CMPLXL(0xe.35d45558a6e657p-2L, -0x9.6be70f659bfbf5ap-2L), CMPLXL(0xe.35d45558a6e6571p-2L, -0x9.6be70f659bfbf59p-2L), CMPLXL(0xe.35d45558a6e6572p-2L, -0x9.6be70f659bfbf58p-2L), 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), -0x0.000000000000001p-16385L), CMPLXL(strtold("inf", NULL), -0x0.0p+0L), CMPLXL(strtold("inf", NULL), 0x0.000000000000001p-16385L), 0);
	test(11, CMPLXL(strtold("-inf", NULL), -12.34), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c236p-2L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c235p-2L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c234p-2L), 0);
	test(12, CMPLXL(0.0, -12.34), CMPLXL(0xc.d49d33e487b0299p-2L, -0xc.90fdaa22168c236p-3L), CMPLXL(0xc.d49d33e487b029ap-2L, -0xc.90fdaa22168c235p-3L), CMPLXL(0xc.d49d33e487b029bp-2L, -0xc.90fdaa22168c234p-3L), 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(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(18, CMPLXL(0.0, nanl("")), CMPLXL(nanl(""), 0xc.90fdaa22168c234p-3L), CMPLXL(nanl(""), 0xc.90fdaa22168c235p-3L), CMPLXL(nanl(""), 0xc.90fdaa22168c236p-3L), 0 | MF_ANYSIGN2);
	test(19, CMPLXL(90.01, strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c234p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-3L), 0);
	test(20, CMPLXL(-12.34, strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c234p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-3L), 0);
	test(21, CMPLXL(nanl(""), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(22, CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c234p-4L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-4L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-4L), 0);
	test(23, CMPLXL(strtold("-inf", NULL), strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), 0x9.6cbe3f9990e91a7p-2L), CMPLXL(strtold("inf", NULL), 0x9.6cbe3f9990e91a8p-2L), CMPLXL(strtold("inf", NULL), 0x9.6cbe3f9990e91a9p-2L), 0);
	test(24, CMPLXL(0.0, strtold("inf", NULL)), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c234p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-3L), 0);
	test(25, CMPLXL(90.01, strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c236p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c234p-3L), 0);
	test(26, CMPLXL(-12.34, strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c236p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c234p-3L), 0);
	test(27, CMPLXL(nanl(""), strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), CMPLXL(strtold("inf", NULL), nanl("")), 0);
	test(28, CMPLXL(strtold("inf", NULL), strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c236p-4L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c235p-4L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c234p-4L), 0);
	test(29, CMPLXL(strtold("-inf", NULL), strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), -0x9.6cbe3f9990e91a9p-2L), CMPLXL(strtold("inf", NULL), -0x9.6cbe3f9990e91a8p-2L), CMPLXL(strtold("inf", NULL), -0x9.6cbe3f9990e91a7p-2L), 0);
	test(30, CMPLXL(0.0, strtold("-inf", NULL)), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c236p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c235p-3L), CMPLXL(strtold("inf", NULL), -0xc.90fdaa22168c234p-3L), 0);
	test(31, CMPLXL(90.01, 0.0), CMPLXL(0xa.62d5c225948a71cp-1L, -0x0.000000000000001p-16385L), CMPLXL(0xa.62d5c225948a71dp-1L, 0x0.0p+0L), CMPLXL(0xa.62d5c225948a71ep-1L, 0x0.000000000000001p-16385L), 0);
	test(32, CMPLXL(-12.34, 0.0), CMPLXL(0xc.d140709b29a4316p-2L, 0xc.90fdaa22168c234p-2L), CMPLXL(0xc.d140709b29a4317p-2L, 0xc.90fdaa22168c235p-2L), CMPLXL(0xc.d140709b29a4318p-2L, 0xc.90fdaa22168c236p-2L), 0);
	test(33, CMPLXL(nanl(""), 0.0), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), CMPLXL(nanl(""), nanl("")), 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(strtold("inf", NULL), 0xc.90fdaa22168c234p-2L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c235p-2L), CMPLXL(strtold("inf", NULL), 0xc.90fdaa22168c236p-2L), 0);
	test(36, CMPLXL(0.0, 0.0), CMPLXL(-0x0.000000000000001p-16385L, 0xc.90fdaa22168c234p-3L), CMPLXL(0x0.0p+0L, 0xc.90fdaa22168c235p-3L), CMPLXL(0x0.000000000000001p-16385L, 0xc.90fdaa22168c236p-3L), 0);
	return imprecise;
}
