/* Test whether a basic cimagl 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 lower, long double expected, long double upper, int flags)
{
	long double output = cimagl(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.) cimagl(%.4Lf + i*%.4Lf) = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, creall(input1), cimagl(input1), output, expected,
			     output - expected, output / expected);
			if ( !isfinite(output) || !isfinite(expected) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, CMPLXL(90.01, 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(2, CMPLXL(-12.34, 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(3, CMPLXL(nanl(""), 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(4, CMPLXL(strtold("inf", NULL), 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(5, CMPLXL(strtold("-inf", NULL), 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(6, CMPLXL(0.0, 13.37), 0xd.5eb851eb851e7ffp+0L, 0xd.5eb851eb851e8p+0L, 0xd.5eb851eb851e801p+0L, 0);
	test(7, CMPLXL(90.01, -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(8, CMPLXL(-12.34, -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(9, CMPLXL(nanl(""), -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(10, CMPLXL(strtold("inf", NULL), -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(11, CMPLXL(strtold("-inf", NULL), -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(12, CMPLXL(0.0, -12.34), -0xc.570a3d70a3d7001p+0L, -0xc.570a3d70a3d7p+0L, -0xc.570a3d70a3d6fffp+0L, 0);
	test(13, CMPLXL(90.01, nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(14, CMPLXL(-12.34, nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(15, CMPLXL(nanl(""), nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(16, CMPLXL(strtold("inf", NULL), nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(17, CMPLXL(strtold("-inf", NULL), nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(18, CMPLXL(0.0, nanl("")), nanl(""), nanl(""), nanl(""), 0);
	test(19, CMPLXL(90.01, strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(20, CMPLXL(-12.34, strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(21, CMPLXL(nanl(""), strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(22, CMPLXL(strtold("inf", NULL), strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(23, CMPLXL(strtold("-inf", NULL), strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(24, CMPLXL(0.0, strtold("inf", NULL)), strtold("inf", NULL), strtold("inf", NULL), strtold("inf", NULL), 0);
	test(25, CMPLXL(90.01, strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(26, CMPLXL(-12.34, strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(27, CMPLXL(nanl(""), strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(28, CMPLXL(strtold("inf", NULL), strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(29, CMPLXL(strtold("-inf", NULL), strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(30, CMPLXL(0.0, strtold("-inf", NULL)), strtold("-inf", NULL), strtold("-inf", NULL), strtold("-inf", NULL), 0);
	test(31, CMPLXL(90.01, 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	test(32, CMPLXL(-12.34, 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	test(33, CMPLXL(nanl(""), 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	test(34, CMPLXL(strtold("inf", NULL), 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	test(35, CMPLXL(strtold("-inf", NULL), 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	test(36, CMPLXL(0.0, 0.0), -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0);
	return imprecise;
}
