/* Test whether a basic modfl 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, long double input1, int errnum, int fperr, long double lower, long double lower_integral, long double expected, long double expected_integral, long double upper, long double upper_integral, int flags)
{
	errno = 0;
	if ( feclearexcept(FE_ALL_EXCEPT) )
		errx(1, "feclearexcept");
	long double integral;
	long double output = modfl(input1, &integral);
	if ( errnum == 0 && errno )
		err(1, "(%d.) modfl(%.4Lf) failed",
                    variant, input1);
	if ( (math_errhandling & MATH_ERRNO) && errnum && errno != errnum )
		errx(1, "(%d.) modfl(%.4Lf) did not %s",
                     variant, input1, strerrno(errnum));
	int excepts = fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
	if ( fperr == 0 && excepts )
		errx(1, "(%d.) modfl(%.4Lf) %s",
                     variant, input1, fperrname(excepts));
	if ( (math_errhandling & MATH_ERREXCEPT) && fperr != 0 &&
	     excepts != fperr && !((flags & MF_MAYERR) && !excepts) )
		errx(1, "(%d.) modfl(%.4Lf) did not %s",
                     variant, input1, 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.) modfl(%.4Lf) = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, input1, output, expected,
			     output - expected, output / expected);
			if ( !isfinite(output) || !isfinite(expected) )
				exit(1);
			imprecise = 1;
		}
	}
	if ( !(flags & MF_UNSPEC2) )
	{
		if ( !(isnan(expected_integral) ? isnan(integral) :
		       isfinite(expected_integral) && expected_integral != 0.0 ?
		       isfinite(integral) && (integral == expected_integral || (lower_integral < integral && integral < upper_integral)) :
		       integral == expected_integral) )
		{
			if ( imprecise && isfinite(integral) && isfinite(expected_integral) )
				return;
			warnx("(%d.) modfl(%.4Lf).integral = %.17La, not %.17La, diff %.17La, ratio %.20Lg",
			     variant, input1, integral, expected_integral,
			     integral - expected_integral, integral / expected_integral);
			if ( !isfinite(integral) || !isfinite(expected_integral) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, 90.01, 0, 0, 0xa.3d70a3d70ffffffp-10L, 0xb.3ffffffffffffffp+3L, 0xa.3d70a3d71p-10L, 0xb.4p+3L, 0xa.3d70a3d71000001p-10L, 0xb.400000000000001p+3L, 0);
	test(2, -12.34, 0, 0, -0xa.e147ae147ae0001p-5L, -0xc.000000000000001p+0L, -0xa.e147ae147aep-5L, -0xc.0p+0L, -0xa.e147ae147adffffp-5L, -0xb.fffffffffffffffp+0L, 0);
	test(3, nanl(""), 0, 0, nanl(""), nanl(""), nanl(""), nanl(""), nanl(""), nanl(""), 0);
	test(4, strtold("inf", NULL), 0, 0, -0x0.000000000000001p-16385L, strtold("inf", NULL), 0x0.0p+0L, strtold("inf", NULL), 0x0.000000000000001p-16385L, strtold("inf", NULL), 0);
	test(5, strtold("-inf", NULL), 0, 0, -0x0.000000000000001p-16385L, strtold("-inf", NULL), -0x0.0p+0L, strtold("-inf", NULL), 0x0.000000000000001p-16385L, strtold("-inf", NULL), 0);
	test(6, 0.0, 0, 0, -0x0.000000000000001p-16385L, -0x0.000000000000001p-16385L, 0x0.0p+0L, 0x0.0p+0L, 0x0.000000000000001p-16385L, 0x0.000000000000001p-16385L, 0);
	return imprecise;
}
