/* Test whether a basic llroundl 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 long expected, int flags)
{
	errno = 0;
	if ( feclearexcept(FE_ALL_EXCEPT) )
		errx(1, "feclearexcept");
	long long output = llroundl(input1);
	if ( errnum == 0 && errno )
		err(1, "(%d.) llroundl(%.4Lf) failed",
                    variant, input1);
	if ( (math_errhandling & MATH_ERRNO) && errnum && errno != errnum )
		errx(1, "(%d.) llroundl(%.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.) llroundl(%.4Lf) %s",
                     variant, input1, fperrname(excepts));
	if ( (math_errhandling & MATH_ERREXCEPT) && fperr != 0 &&
	     excepts != fperr && !((flags & MF_MAYERR) && !excepts) )
		errx(1, "(%d.) llroundl(%.4Lf) did not %s",
                     variant, input1, fperrname(fperr));
	if ( !(flags & MF_UNSPEC1) )
	{
		if ( output != expected )
			errx(1, "(%d.) llroundl(%.4Lf) = %lld, not %lld",
			     variant, input1, output, expected);
	}
}

int main(void)
{
	test(1, 90.01, 0, 0, 90LL, 0);
	test(2, -12.34, 0, 0, -12LL, 0);
	test(3, nanl(""), EDOM, FE_INVALID, 0, 0 | MF_UNSPEC1);
	test(4, strtold("inf", NULL), EDOM, FE_INVALID, 0, 0 | MF_UNSPEC1);
	test(5, strtold("-inf", NULL), EDOM, FE_INVALID, 0, 0 | MF_UNSPEC1);
	test(6, 0.0, 0, 0, 0LL, 0);
	return imprecise;
}
