Library: investigate what could be tested with floating-point
The floating-point variant turns out to be difficult to test, since it is never ''actually exact'' -- due to the nature of floating-points. We can not expect reasonable behaviour at the domain boundaries, even while in some cases the computation works (and in others it goes infinite and is sanitised to `minVal`). The limit for rejection is itself rather a heuristic; thus it is difficult to show "wrong" behaviour beyond the limit, while the limit itself is typically already so large that it is hard to handle in test code -- at least when expecting visible results.
This commit is contained in:
parent
078aa48879
commit
0c69db19d7
2 changed files with 101 additions and 3 deletions
|
|
@ -216,7 +216,7 @@ namespace util {
|
|||
offset += PERIOD;
|
||||
if (not (offset < PERIOD)) // can happen due to ε or non-finite values
|
||||
offset = 0;
|
||||
F res = minVal + offset; // could be rounded to maxVal
|
||||
F res = minVal + offset; // could be rounded to maxVal (or become infinite)
|
||||
return res < maxVal? res : minVal;
|
||||
}
|
||||
|
||||
|
|
@ -245,6 +245,7 @@ namespace util {
|
|||
limit_cyclicWrap (F period)
|
||||
{
|
||||
REQUIRE (std::isfinite (period));
|
||||
REQUIRE (F(0) < period);
|
||||
F limit = period / std::numeric_limits<F>::epsilon();
|
||||
if (not std::isfinite (limit))
|
||||
limit = std::numeric_limits<F>::max();
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ using ::Test;
|
|||
//using util::isnil;
|
||||
//using util::_Fmt;
|
||||
using util::isLimited;
|
||||
|
||||
using std::floating_point;
|
||||
|
||||
namespace util {
|
||||
namespace test {
|
||||
|
|
@ -42,6 +42,9 @@ namespace test {
|
|||
template<typename NUM>
|
||||
constexpr NUM _MIN = std::numeric_limits<NUM>::lowest();
|
||||
|
||||
template<floating_point FLO>
|
||||
constexpr FLO _EPS = std::numeric_limits<FLO>::epsilon();
|
||||
|
||||
} // (End) Test helpers
|
||||
|
||||
|
||||
|
|
@ -75,6 +78,8 @@ namespace test {
|
|||
verifyIntegerTypes<uint32_t>();
|
||||
verifyIntegerTypes<int64_t> ();
|
||||
verifyIntegerTypes<uint64_t>();
|
||||
|
||||
verifyFloatTypes();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -105,7 +110,7 @@ namespace test {
|
|||
const I LOWER(-100); // note: expected to wrap for unsigned types
|
||||
const I UPPER(LOWER+5);
|
||||
|
||||
auto cyc = [&](I val){ return cyclicWrap (I(val), LOWER,UPPER); };
|
||||
auto cyc = [&](I val){ return cyclicWrap (val, LOWER,UPPER); };
|
||||
|
||||
CHECK (LOWER == cyc(LOWER+0) );
|
||||
CHECK (LOWER+1 == cyc(LOWER+1) );
|
||||
|
|
@ -134,6 +139,98 @@ namespace test {
|
|||
|
||||
CHECK (LOWER + offset == cyc(_MIN<I>) );
|
||||
}
|
||||
|
||||
void
|
||||
verifyFloatTypes()
|
||||
{
|
||||
double maxDom = _MAX<double>;
|
||||
double super = _MAX<double> - _MIN<double>;
|
||||
SHOW_EXPR(maxDom)
|
||||
SHOW_EXPR(super)
|
||||
SHOW_EXPR(_EPS<double>)
|
||||
SHOW_EXPR(limit_cyclicWrap<double>(1))
|
||||
SHOW_EXPR(limit_cyclicWrap<double>(1024))
|
||||
SHOW_EXPR(limit_cyclicWrap<double>(1.0/1024))
|
||||
SHOW_EXPR(limit_cyclicWrap<double>(_EPS<double>))
|
||||
SHOW_EXPR(limit_cyclicWrap<double>(maxDom))
|
||||
|
||||
double limit5 = limit_cyclicWrap<double>(5);
|
||||
double atLimit = limit5 * (1-_EPS<double>);
|
||||
double stepLim = std::floor (limit5 / 5) * 5;
|
||||
SHOW_EXPR(limit5*_EPS<double>)
|
||||
SHOW_EXPR(limit5)
|
||||
SHOW_EXPR(stepLim)
|
||||
SHOW_EXPR(atLimit)
|
||||
|
||||
SHOW_EXPR(cyclicWrap (10.0 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (limit5 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (atLimit ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim-5 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+1 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+2 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+3 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+4 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+5 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+6 ,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+10,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+20,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+30,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+40,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+50,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+100,0.0, 5.0))
|
||||
SHOW_EXPR(cyclicWrap (stepLim+101,0.0, 5.0))
|
||||
|
||||
SHOW_EXPL(cyclicWrap (limit5 ,10.0, 15.0))
|
||||
SHOW_EXPL(cyclicWrap (limit5+1 ,10.0, 15.0))
|
||||
double epsStep = _MAX<double>*_EPS<double>;
|
||||
double toxicMin = _MIN<double> + 2*epsStep;
|
||||
double toxicMax = epsStep;
|
||||
double toxicPeriod = toxicMax - toxicMin;
|
||||
SHOW_EXPR(epsStep)
|
||||
SHOW_EXPR(toxicMin)
|
||||
SHOW_EXPR(toxicMax)
|
||||
SHOW_EXPR(toxicPeriod)
|
||||
SHOW_EXPR(cyclicWrap<double>(0, toxicMin,toxicMax) )
|
||||
SHOW_EXPR(cyclicWrap<double>(1, toxicMin,toxicMax) )
|
||||
SHOW_EXPR(cyclicWrap<double>(1*epsStep, toxicMin,toxicMax) )
|
||||
SHOW_EXPR(cyclicWrap<double>(2*epsStep, toxicMin,toxicMax) )
|
||||
SHOW_EXPR(cyclicWrap<double>(3*epsStep, toxicMin,toxicMax) )
|
||||
SHOW_EXPR(cyclicWrap<double>(0, 1, _MAX<double>) )
|
||||
SHOW_EXPR(cyclicWrap<double>(1, 1, _MAX<double>) )
|
||||
SHOW_EXPR(cyclicWrap<double>(2, 1, _MAX<double>) )
|
||||
SHOW_EXPR(cyclicWrap<double>(-1,1, _MAX<double>) )
|
||||
SHOW_EXPR(cyclicWrap<double>(-epsStep,1, _MAX<double>) )
|
||||
SHOW_EXPR(1 == cyclicWrap<double>(1, 1, _MAX<double>))
|
||||
SHOW_EXPR(2 == cyclicWrap<double>(2, 1, _MAX<double>))
|
||||
SHOW_EXPR(1 == cyclicWrap<double>(-1,1, _MAX<double>) )
|
||||
double wrapped = cyclicWrap (-epsStep,double(1), _MAX<double>);
|
||||
SHOW_EXPR(1 < wrapped)
|
||||
SHOW_EXPR(wrapped < _MAX<double>)
|
||||
|
||||
auto cyc = [&](double val){ return cyclicWrap (val, double(-10),double(-5)); };
|
||||
|
||||
CHECK (almostEqual (double(-10), cyc(0)));
|
||||
CHECK (almostEqual (double( -9), cyc(1)));
|
||||
CHECK (almostEqual (double( -8), cyc(2)));
|
||||
CHECK (almostEqual (double( -7), cyc(3)));
|
||||
CHECK (almostEqual (double( -6), cyc(4)));
|
||||
CHECK (almostEqual (double(-10), cyc(5)));
|
||||
CHECK (almostEqual (double( -9), cyc(6)));
|
||||
CHECK (almostEqual (double( -8), cyc(7)));
|
||||
CHECK (almostEqual (double( -7), cyc(8)));
|
||||
CHECK (almostEqual (double( -6), cyc(9)));
|
||||
CHECK (almostEqual (double(-10), cyc(105)));
|
||||
|
||||
CHECK (almostEqual (double(5)/2, limit5 * _EPS<double>));
|
||||
CHECK (limit5 + 1 == limit5);
|
||||
|
||||
CHECK (1 == cyclicWrap<double>( 1, 1,_MAX<double>) );
|
||||
CHECK (2 == cyclicWrap<double>( 2, 1,_MAX<double>) );
|
||||
CHECK (1 == cyclicWrap<double>(-1, 1,_MAX<double>) );
|
||||
CHECK ( 1 < wrapped );
|
||||
CHECK (wrapped < _MAX<double>);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue