/* Test whether a basic atan2 invocation works. */
/* This test is generated by misc/genbasic.c. */

#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)

// Soft fail on rounding errors and report only one.
int imprecise;

static const char* fperrname(int excepts)
{
	switch ( excepts )
	{
	case 0: return "FE_NONE";
	case FE_INVALID: return "FE_INVALID";
	case FE_DIVBYZERO: return "FE_DIVBYZERO";
	case FE_OVERFLOW: return "FE_OVERFLOW";
	case FE_UNDERFLOW: return "FE_UNDERFLOW";
	default: return "FE_MULTIPLE";
	}
}

void test(int variant, double input1, double input2, int errnum, int fperr, double lower, double expected, double upper, int flags)
{
	errno = 0;
	if ( feclearexcept(FE_ALL_EXCEPT) )
		errx(1, "feclearexcept");
	double output = atan2(input1, input2);
	if ( errnum == 0 && errno )
		err(1, "(%d.) atan2(%.4f, %.4f) failed",
                    variant, input1, input2);
	if ( (math_errhandling & MATH_ERRNO) && errnum && errno != errnum )
		errx(1, "(%d.) atan2(%.4f, %.4f) did not %s",
                     variant, input1, input2, strerrno(errnum));
	int excepts = fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
	if ( fperr == 0 && excepts )
		errx(1, "(%d.) atan2(%.4f, %.4f) %s",
                     variant, input1, input2, fperrname(excepts));
	if ( (math_errhandling & MATH_ERREXCEPT) && fperr != 0 &&
	     excepts != fperr && !((flags & MF_MAYERR) && !excepts) )
		errx(1, "(%d.) atan2(%.4f, %.4f) did not %s",
                     variant, input1, input2, fperrname(fperr));
	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.) atan2(%.4f, %.4f) = %.14a, not %.14a, diff %.14a, ratio %.16g",
			     variant, input1, input2, output, expected,
			     output - expected, output / expected);
			if ( !isfinite(output) || !isfinite(expected) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, 90.01, 13.37, 0, 0, 0x1.6c5fb6e0732dep+0, 0x1.6c5fb6e0732dfp+0, 0x1.6c5fb6e0732ep+0, 0);
	test(2, -12.34, 13.37, 0, 0, -0x1.7d9f7a4a298c7p-1, -0x1.7d9f7a4a298c6p-1, -0x1.7d9f7a4a298c5p-1, 0);
	test(3, nan(""), 13.37, 0, 0, nan(""), nan(""), nan(""), 0);
	test(4, strtod("inf", NULL), 13.37, 0, 0, 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(5, strtod("-inf", NULL), 13.37, 0, 0, -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(6, 0.0, 13.37, 0, 0, -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(7, 90.01, -12.34, 0, 0, 0x1.b500c286491e5p+0, 0x1.b500c286491e6p+0, 0x1.b500c286491e7p+0, 0);
	test(8, -12.34, -12.34, 0, 0, -0x1.2d97c7f3321d3p+1, -0x1.2d97c7f3321d2p+1, -0x1.2d97c7f3321d1p+1, 0);
	test(9, nan(""), -12.34, 0, 0, nan(""), nan(""), nan(""), 0);
	test(10, strtod("inf", NULL), -12.34, 0, 0, 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(11, strtod("-inf", NULL), -12.34, 0, 0, -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(12, 0.0, -12.34, 0, 0, 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(13, 90.01, nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(14, -12.34, nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(15, nan(""), nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(16, strtod("inf", NULL), nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(17, strtod("-inf", NULL), nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(18, 0.0, nan(""), 0, 0, nan(""), nan(""), nan(""), 0);
	test(19, 90.01, strtod("inf", NULL), 0, 0, -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(20, -12.34, strtod("inf", NULL), 0, 0, -0x0.0000000000001p-1022, -0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(21, nan(""), strtod("inf", NULL), 0, 0, nan(""), nan(""), nan(""), 0);
	test(22, strtod("inf", NULL), strtod("inf", NULL), 0, 0, 0x1.921fb54442d17p-1, 0x1.921fb54442d18p-1, 0x1.921fb54442d19p-1, 0);
	test(23, strtod("-inf", NULL), strtod("inf", NULL), 0, 0, -0x1.921fb54442d19p-1, -0x1.921fb54442d18p-1, -0x1.921fb54442d17p-1, 0);
	test(24, 0.0, strtod("inf", NULL), 0, 0, -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	test(25, 90.01, strtod("-inf", NULL), 0, 0, 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(26, -12.34, strtod("-inf", NULL), 0, 0, -0x1.921fb54442d19p+1, -0x1.921fb54442d18p+1, -0x1.921fb54442d17p+1, 0);
	test(27, nan(""), strtod("-inf", NULL), 0, 0, nan(""), nan(""), nan(""), 0);
	test(28, strtod("inf", NULL), strtod("-inf", NULL), 0, 0, 0x1.2d97c7f3321d1p+1, 0x1.2d97c7f3321d2p+1, 0x1.2d97c7f3321d3p+1, 0);
	test(29, strtod("-inf", NULL), strtod("-inf", NULL), 0, 0, -0x1.2d97c7f3321d3p+1, -0x1.2d97c7f3321d2p+1, -0x1.2d97c7f3321d1p+1, 0);
	test(30, 0.0, strtod("-inf", NULL), 0, 0, 0x1.921fb54442d17p+1, 0x1.921fb54442d18p+1, 0x1.921fb54442d19p+1, 0);
	test(31, 90.01, 0.0, 0, 0, 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(32, -12.34, 0.0, 0, 0, -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(33, nan(""), 0.0, 0, 0, nan(""), nan(""), nan(""), 0);
	test(34, strtod("inf", NULL), 0.0, 0, 0, 0x1.921fb54442d17p+0, 0x1.921fb54442d18p+0, 0x1.921fb54442d19p+0, 0);
	test(35, strtod("-inf", NULL), 0.0, 0, 0, -0x1.921fb54442d19p+0, -0x1.921fb54442d18p+0, -0x1.921fb54442d17p+0, 0);
	test(36, 0.0, 0.0, 0, 0, -0x0.0000000000001p-1022, 0x0.0p+0, 0x0.0000000000001p-1022, 0);
	return imprecise;
}
