/* Test whether a basic carg 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, double complex input1, double lower, double expected, double upper, int flags)
{
	double output = carg(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.) carg(%.4f + i*%.4f) = %.14a, not %.14a, diff %.14a, ratio %.16g",
			     variant, creal(input1), cimag(input1), output, expected,
			     output - expected, output / expected);
			if ( !isfinite(output) || !isfinite(expected) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, CMPLX(90.01, 13.37), 0x1.2dfff31e7d1c9p-3, 0x1.2dfff31e7d1cap-3, 0x1.2dfff31e7d1cbp-3, 0);
	test(2, CMPLX(-12.34, 13.37), 0x1.2877b934abcbdp+1, 0x1.2877b934abcbep+1, 0x1.2877b934abcbfp+1, 0);
	test(3, CMPLX(nan(""), 13.37), nan(""), nan(""), nan(""), 0);
	test(4, CMPLX(strtod("inf", NULL), 13.37), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(5, CMPLX(strtod("-inf", NULL), 13.37), 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(6, CMPLX(0.0, 13.37), 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(7, CMPLX(90.01, -12.34), -0x1.17086a103266ep-3, -0x1.17086a103266dp-3, -0x1.17086a103266cp-3, 0);
	test(8, CMPLX(-12.34, -12.34), -0x1.2d97c7f3321d3p+1, -0x1.2d97c7f3321d2p+1, -0x1.2d97c7f3321d1p+1, 0);
	test(9, CMPLX(nan(""), -12.34), nan(""), nan(""), nan(""), 0);
	test(10, CMPLX(strtod("inf", NULL), -12.34), -0x0.0000000000001p-1022, -0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(11, CMPLX(strtod("-inf", NULL), -12.34), -0x1.921fb54442d19p+1, -0x1.921fb54442d18p+1, -0x1.921fb54442d17p+1, 0);
	test(12, CMPLX(0.0, -12.34), -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(13, CMPLX(90.01, nan("")), nan(""), nan(""), nan(""), 0);
	test(14, CMPLX(-12.34, nan("")), nan(""), nan(""), nan(""), 0);
	test(15, CMPLX(nan(""), nan("")), nan(""), nan(""), nan(""), 0);
	test(16, CMPLX(strtod("inf", NULL), nan("")), nan(""), nan(""), nan(""), 0);
	test(17, CMPLX(strtod("-inf", NULL), nan("")), nan(""), nan(""), nan(""), 0);
	test(18, CMPLX(0.0, nan("")), nan(""), nan(""), nan(""), 0);
	test(19, CMPLX(90.01, strtod("inf", NULL)), 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(20, CMPLX(-12.34, strtod("inf", NULL)), 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(21, CMPLX(nan(""), strtod("inf", NULL)), nan(""), nan(""), nan(""), 0);
	test(22, CMPLX(strtod("inf", NULL), strtod("inf", NULL)), 0x1.921fb54442d17p-1, 0x1.921fb54442d18p-1, 0x1.921fb54442d19p-1, 0);
	test(23, CMPLX(strtod("-inf", NULL), strtod("inf", NULL)), 0x1.2d97c7f3321d1p+1, 0x1.2d97c7f3321d2p+1, 0x1.2d97c7f3321d3p+1, 0);
	test(24, CMPLX(0.0, strtod("inf", NULL)), 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(25, CMPLX(90.01, strtod("-inf", NULL)), -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(26, CMPLX(-12.34, strtod("-inf", NULL)), -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(27, CMPLX(nan(""), strtod("-inf", NULL)), nan(""), nan(""), nan(""), 0);
	test(28, CMPLX(strtod("inf", NULL), strtod("-inf", NULL)), -0x1.921fb54442d19p-1, -0x1.921fb54442d18p-1, -0x1.921fb54442d17p-1, 0);
	test(29, CMPLX(strtod("-inf", NULL), strtod("-inf", NULL)), -0x1.2d97c7f3321d3p+1, -0x1.2d97c7f3321d2p+1, -0x1.2d97c7f3321d1p+1, 0);
	test(30, CMPLX(0.0, strtod("-inf", NULL)), -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(31, CMPLX(90.01, 0.0), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(32, CMPLX(-12.34, 0.0), 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(33, CMPLX(nan(""), 0.0), nan(""), nan(""), nan(""), 0);
	test(34, CMPLX(strtod("inf", NULL), 0.0), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(35, CMPLX(strtod("-inf", NULL), 0.0), 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(36, CMPLX(0.0, 0.0), -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	return imprecise;
}
