Changeset View
Changeset View
Standalone View
Standalone View
lib/msun/tests/lrint_test.c
Show All 25 Lines | |||||
/* | /* | ||||
* Test for lrint(), lrintf(), llrint(), and llrintf(). | * Test for lrint(), lrintf(), llrint(), and llrintf(). | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <assert.h> | |||||
#include <fenv.h> | #include <fenv.h> | ||||
#include <limits.h> | #include <limits.h> | ||||
#include <math.h> | #include <math.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#ifdef __i386__ | #ifdef __i386__ | ||||
#include <ieeefp.h> | #include <ieeefp.h> | ||||
#endif | #endif | ||||
#include "test-utils.h" | #include "test-utils.h" | ||||
/* | /* | ||||
* XXX The volatile here is to avoid gcc's bogus constant folding and work | * XXX The volatile here is to avoid gcc's bogus constant folding and work | ||||
* around the lack of support for the FENV_ACCESS pragma. | * around the lack of support for the FENV_ACCESS pragma. | ||||
*/ | */ | ||||
#define test(func, x, result, excepts) do { \ | #define test(func, x, result, excepts) do { \ | ||||
volatile double _d = x; \ | volatile double _d = x; \ | ||||
assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ | ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0); \ | ||||
assert((func)(_d) == (result) || fetestexcept(FE_INVALID)); \ | ATF_CHECK((func)(_d) == (result) || fetestexcept(FE_INVALID)); \ | ||||
assert((fetestexcept(FE_ALL_EXCEPT) & ALL_STD_EXCEPT) \ | CHECK_FP_EXCEPTIONS_MSG(excepts, FE_ALL_EXCEPT & ALL_STD_EXCEPT,\ | ||||
== (excepts)); \ | "for %s(%s)", #func, #x); \ | ||||
} while (0) | } while (0) | ||||
#define testall(x, result, excepts) do { \ | #define testall(x, result, excepts) do { \ | ||||
test(lrint, x, result, excepts); \ | test(lrint, x, result, excepts); \ | ||||
test(lrintf, x, result, excepts); \ | test(lrintf, x, result, excepts); \ | ||||
test(lrintl, x, result, excepts); \ | test(lrintl, x, result, excepts); \ | ||||
test(llrint, x, result, excepts); \ | test(llrint, x, result, excepts); \ | ||||
test(llrintf, x, result, excepts); \ | test(llrintf, x, result, excepts); \ | ||||
test(llrintl, x, result, excepts); \ | test(llrintl, x, result, excepts); \ | ||||
} while (0) | } while (0) | ||||
#define IGNORE 0 | #define IGNORE 0 | ||||
#pragma STDC FENV_ACCESS ON | #pragma STDC FENV_ACCESS ON | ||||
static void | static void | ||||
run_tests(void) | run_tests(void) | ||||
{ | { | ||||
ATF_REQUIRE_EQ(0, fesetround(FE_DOWNWARD)); | |||||
assert(fesetround(FE_DOWNWARD) == 0); | |||||
testall(0.75, 0, FE_INEXACT); | testall(0.75, 0, FE_INEXACT); | ||||
testall(-0.5, -1, FE_INEXACT); | testall(-0.5, -1, FE_INEXACT); | ||||
assert(fesetround(FE_TONEAREST) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST)); | ||||
testall(0.0, 0, 0); | testall(0.0, 0, 0); | ||||
testall(0.25, 0, FE_INEXACT); | testall(0.25, 0, FE_INEXACT); | ||||
testall(0.5, 0, FE_INEXACT); | testall(0.5, 0, FE_INEXACT); | ||||
testall(-2.5, -2, FE_INEXACT); | testall(-2.5, -2, FE_INEXACT); | ||||
testall(1.0, 1, 0); | testall(1.0, 1, 0); | ||||
testall(0x12345000p0, 0x12345000, 0); | testall(0x12345000p0, 0x12345000, 0); | ||||
testall(0x1234.fp0, 0x1235, FE_INEXACT); | testall(0x1234.fp0, 0x1235, FE_INEXACT); | ||||
testall(INFINITY, IGNORE, FE_INVALID); | testall(INFINITY, IGNORE, FE_INVALID); | ||||
testall(NAN, IGNORE, FE_INVALID); | testall(NAN, IGNORE, FE_INVALID); | ||||
#if (LONG_MAX == 0x7fffffffl) | #if (LONG_MAX == 0x7fffffffl) | ||||
assert(fesetround(FE_UPWARD) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_UPWARD)); | ||||
test(lrint, 0x7fffffff.8p0, IGNORE, FE_INVALID); | test(lrint, 0x7fffffff.8p0, IGNORE, FE_INVALID); | ||||
test(lrint, -0x80000000.4p0, -0x80000000l, FE_INEXACT); | test(lrint, -0x80000000.4p0, (long)-0x80000000l, FE_INEXACT); | ||||
assert(fesetround(FE_DOWNWARD) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_DOWNWARD)); | ||||
test(lrint, -0x80000000.8p0, IGNORE, FE_INVALID); | test(lrint, -0x80000000.8p0, IGNORE, FE_INVALID); | ||||
test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); | test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); | ||||
test(lrint, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT); | test(lrint, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT); | ||||
test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); | test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); | ||||
test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); | test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); | ||||
assert(fesetround(FE_TOWARDZERO) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_TOWARDZERO)); | ||||
test(lrint, 0x7fffffff.8p0, 0x7fffffffl, FE_INEXACT); | test(lrint, 0x7fffffff.8p0, 0x7fffffffl, FE_INEXACT); | ||||
test(lrint, -0x80000000.8p0, -0x80000000l, FE_INEXACT); | test(lrint, -0x80000000.8p0, -0x80000000l, FE_INEXACT); | ||||
test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); | test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID); | ||||
test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); | test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID); | ||||
test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); | test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0); | ||||
#elif (LONG_MAX == 0x7fffffffffffffffll) | #elif (LONG_MAX == 0x7fffffffffffffffll) | ||||
assert(fesetround(FE_TONEAREST) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST)); | ||||
test(lrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); | test(lrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); | ||||
test(lrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); | test(lrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); | ||||
test(lrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0); | test(lrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0); | ||||
test(lrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0); | test(lrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0); | ||||
test(lrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); | test(lrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); | ||||
test(lrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); | test(lrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); | ||||
test(lrint, -0x8000000000000000.0p0, -0x8000000000000000l, 0); | test(lrint, -0x8000000000000000.0p0, (long long)-0x8000000000000000ul, 0); | ||||
test(lrintf, -0x8000000000000000.0p0f, -0x8000000000000000l, 0); | test(lrintf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ul, 0); | ||||
#else | #else | ||||
#error "Unsupported long size" | #error "Unsupported long size" | ||||
#endif | #endif | ||||
#if (LLONG_MAX == 0x7fffffffffffffffLL) | #if (LLONG_MAX == 0x7fffffffffffffffLL) | ||||
assert(fesetround(FE_TONEAREST) == 0); | ATF_REQUIRE_EQ(0, fesetround(FE_TONEAREST)); | ||||
test(llrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); | test(llrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID); | ||||
test(llrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); | test(llrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID); | ||||
test(llrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0); | test(llrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0); | ||||
test(llrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0); | test(llrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0); | ||||
test(llrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); | test(llrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID); | ||||
test(llrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); | test(llrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID); | ||||
test(llrint, -0x8000000000000000.0p0, -0x8000000000000000ll, 0); | test(llrint, -0x8000000000000000.0p0, (long long)-0x8000000000000000ull, 0); | ||||
test(llrintf, -0x8000000000000000.0p0f, -0x8000000000000000ll, 0); | test(llrintf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ull, 0); | ||||
#else | #else | ||||
#error "Unsupported long long size" | #error "Unsupported long long size" | ||||
#endif | #endif | ||||
} | } | ||||
int | ATF_TC_WITHOUT_HEAD(lrint); | ||||
main(void) | ATF_TC_BODY(lrint, tc) | ||||
{ | { | ||||
printf("1..1\n"); | |||||
run_tests(); | run_tests(); | ||||
#ifdef __i386__ | #ifdef __i386__ | ||||
fpsetprec(FP_PE); | fpsetprec(FP_PE); | ||||
run_tests(); | run_tests(); | ||||
#endif | #endif | ||||
} | |||||
printf("ok 1 - lrint\n"); | ATF_TP_ADD_TCS(tp) | ||||
{ | |||||
return (0); | ATF_TP_ADD_TC(tp, lrint); | ||||
return (atf_no_error()); | |||||
ngie: newline | |||||
} | } |
newline