Changeset View
Changeset View
Standalone View
Standalone View
lib/msun/tests/nearbyint_test.c
Show All 30 Lines | ||||||||||||
* - adapt tests for rint(3) | * - adapt tests for rint(3) | |||||||||||
* - tests for harder values (more mantissa bits than float) | * - tests for harder values (more mantissa bits than float) | |||||||||||
*/ | */ | |||||||||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | |||||||||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | |||||||||||
#include <sys/param.h> | #include <sys/param.h> | |||||||||||
#include <assert.h> | ||||||||||||
#include <fenv.h> | #include <fenv.h> | |||||||||||
#include <math.h> | #include <math.h> | |||||||||||
#include <stdio.h> | #include <stdio.h> | |||||||||||
#include "test-utils.h" | #include "test-utils.h" | |||||||||||
static int testnum; | ||||||||||||
static const int rmodes[] = { | static const int rmodes[] = { | |||||||||||
FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO, | FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO, | |||||||||||
}; | }; | |||||||||||
/* Make sure we're testing the library, not some broken compiler built-ins. */ | /* Make sure we're testing the library, not some broken compiler built-ins. */ | |||||||||||
static double (*libnearbyint)(double) = nearbyint; | static double (*libnearbyint)(double) = nearbyint; | |||||||||||
static float (*libnearbyintf)(float) = nearbyintf; | static float (*libnearbyintf)(float) = nearbyintf; | |||||||||||
static long double (*libnearbyintl)(long double) = nearbyintl; | static long double (*libnearbyintl)(long double) = nearbyintl; | |||||||||||
Show All 34 Lines | ||||||||||||
static void | static void | |||||||||||
test_nearby(int testindex) | test_nearby(int testindex) | |||||||||||
{ | { | |||||||||||
float in, out; | float in, out; | |||||||||||
unsigned i; | unsigned i; | |||||||||||
for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { | for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { | |||||||||||
fesetround(rmodes[i]); | ATF_REQUIRE_EQ(0, fesetround(rmodes[i])); | |||||||||||
feclearexcept(ALL_STD_EXCEPT); | ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); | |||||||||||
in = tests[testindex].in; | in = tests[testindex].in; | |||||||||||
out = get_output(testindex, i, 0); | out = get_output(testindex, i, 0); | |||||||||||
assert(fpequal(out, libnearbyintf(in))); | ATF_CHECK(fpequal(out, libnearbyintf(in))); | |||||||||||
assert(fpequal(out, nearbyint(in))); | ATF_CHECK(fpequal(out, nearbyint(in))); | |||||||||||
assert(fpequal(out, nearbyintl(in))); | ATF_CHECK(fpequal(out, nearbyintl(in))); | |||||||||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | |||||||||||
in = -tests[testindex].in; | in = -tests[testindex].in; | |||||||||||
out = get_output(testindex, i, 1); | out = get_output(testindex, i, 1); | |||||||||||
assert(fpequal(out, nearbyintf(in))); | ATF_CHECK(fpequal(out, nearbyintf(in))); | |||||||||||
assert(fpequal(out, nearbyint(in))); | ATF_CHECK(fpequal(out, nearbyint(in))); | |||||||||||
assert(fpequal(out, nearbyintl(in))); | ATF_CHECK(fpequal(out, nearbyintl(in))); | |||||||||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | |||||||||||
} | } | |||||||||||
printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in); | ||||||||||||
} | } | |||||||||||
ngie: TAP printf can be deleted. | ||||||||||||
static void | static void | |||||||||||
test_modf(int testindex) | test_modf(int testindex) | |||||||||||
{ | { | |||||||||||
float in, out; | float in, out; | |||||||||||
float ipartf, ipart_expected; | float ipartf, ipart_expected; | |||||||||||
double ipart; | double ipart; | |||||||||||
long double ipartl; | long double ipartl; | |||||||||||
unsigned i; | unsigned i; | |||||||||||
for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { | for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) { | |||||||||||
fesetround(rmodes[i]); | ATF_REQUIRE_EQ(0, fesetround(rmodes[i])); | |||||||||||
feclearexcept(ALL_STD_EXCEPT); | ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT)); | |||||||||||
in = tests[testindex].in; | in = tests[testindex].in; | |||||||||||
ipart_expected = tests[testindex].out[1]; | ipart_expected = tests[testindex].out[1]; | |||||||||||
out = copysignf( | out = copysignf( | |||||||||||
isinf(ipart_expected) ? 0.0 : in - ipart_expected, in); | isinf(ipart_expected) ? 0.0 : in - ipart_expected, in); | |||||||||||
ipartl = ipart = ipartf = 42.0; | ipartl = ipart = ipartf = 42.0; | |||||||||||
assert(fpequal(out, modff(in, &ipartf))); | ATF_CHECK(fpequal(out, modff(in, &ipartf))); | |||||||||||
assert(fpequal(ipart_expected, ipartf)); | ATF_CHECK(fpequal(ipart_expected, ipartf)); | |||||||||||
assert(fpequal(out, modf(in, &ipart))); | ATF_CHECK(fpequal(out, modf(in, &ipart))); | |||||||||||
assert(fpequal(ipart_expected, ipart)); | ATF_CHECK(fpequal(ipart_expected, ipart)); | |||||||||||
assert(fpequal(out, modfl(in, &ipartl))); | ATF_CHECK(fpequal(out, modfl(in, &ipartl))); | |||||||||||
assert(fpequal(ipart_expected, ipartl)); | ATF_CHECK(fpequal(ipart_expected, ipartl)); | |||||||||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | |||||||||||
in = -in; | in = -in; | |||||||||||
ipart_expected = -ipart_expected; | ipart_expected = -ipart_expected; | |||||||||||
out = -out; | out = -out; | |||||||||||
ipartl = ipart = ipartf = 42.0; | ipartl = ipart = ipartf = 42.0; | |||||||||||
assert(fpequal(out, modff(in, &ipartf))); | ATF_CHECK(fpequal(out, modff(in, &ipartf))); | |||||||||||
assert(fpequal(ipart_expected, ipartf)); | ATF_CHECK(fpequal(ipart_expected, ipartf)); | |||||||||||
assert(fpequal(out, modf(in, &ipart))); | ATF_CHECK(fpequal(out, modf(in, &ipart))); | |||||||||||
assert(fpequal(ipart_expected, ipart)); | ATF_CHECK(fpequal(ipart_expected, ipart)); | |||||||||||
assert(fpequal(out, modfl(in, &ipartl))); | ATF_CHECK(fpequal(out, modfl(in, &ipartl))); | |||||||||||
assert(fpequal(ipart_expected, ipartl)); | ATF_CHECK(fpequal(ipart_expected, ipartl)); | |||||||||||
assert(fetestexcept(ALL_STD_EXCEPT) == 0); | CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); | |||||||||||
} | } | |||||||||||
printf("ok %d\t\t# modf(+%g)\n", testnum++, in); | ||||||||||||
} | } | |||||||||||
Done Inline Actions
TAP printf can be deleted. ngie: TAP printf can be deleted. | ||||||||||||
int | ATF_TC_WITHOUT_HEAD(nearbyint); | |||||||||||
main(void) | ATF_TC_BODY(nearbyint, tc) | |||||||||||
{ | { | |||||||||||
unsigned i; | unsigned i; | |||||||||||
printf("1..%zu\n", (size_t)(nitems(tests) * 2)); | ||||||||||||
testnum = 1; | ||||||||||||
for (i = 0; i < nitems(tests); i++) { | for (i = 0; i < nitems(tests); i++) { | |||||||||||
test_nearby(i); | test_nearby(i); | |||||||||||
test_modf(i); | test_modf(i); | |||||||||||
} | } | |||||||||||
} | ||||||||||||
return (0); | ATF_TP_ADD_TCS(tp) | |||||||||||
{ | ||||||||||||
ATF_TP_ADD_TC(tp, nearbyint); | ||||||||||||
return (atf_no_error()); | ||||||||||||
} | } | |||||||||||
Done Inline Actions
newline ngie: newline |
TAP printf can be deleted.