libcruft-util/test/range.cpp

55 lines
3.1 KiB
C++
Raw Permalink Normal View History

#include <cruft/util/range.hpp>
2016-01-19 18:31:49 +11:00
#include <cruft/util/tap.hpp>
2015-04-13 16:45:56 +10:00
2011-05-23 17:18:52 +10:00
#include <cstdlib>
#include <limits>
int
2016-01-19 18:31:49 +11:00
main (int, char **)
{
cruft::TAP::logger tap;
2016-01-19 18:31:49 +11:00
2012-06-15 16:38:10 +10:00
// Check some simple cases close to the edges of a unit range. Tests float rounding.
tap.expect ( cruft::range<double>::unit ().contains ( 0.0), "floating unit contains 0");
tap.expect ( cruft::range<double>::unit ().contains ( 1.0), "floating unit contains 1");
tap.expect ( cruft::range<double>::unit ().contains (std::numeric_limits<double>::min ()), "floating unit contains min");
tap.expect (!cruft::range<double>::unit ().contains (-0.00001), "doesn't contain fractionally low value");
tap.expect (!cruft::range<double>::unit ().contains ( 1.00001), "doesn't contain fractionally high value");
2011-05-23 17:18:52 +10:00
2012-06-15 16:38:10 +10:00
// Check edge cases of unit with integer values
tap.expect ( cruft::range<uint16_t>::unit ().contains (0), "unsigned unit contains 0");
tap.expect ( cruft::range<uint16_t>::unit ().contains (1), "unsigned unit contains 1");
tap.expect (!cruft::range<uint16_t>::unit ().contains (2), "unsigned unit doesn't contain 2");
tap.expect (!cruft::range<uint16_t>::unit ().contains (std::numeric_limits <uint16_t>::max ()), "unsigned unit doesn't contain max");
2011-05-23 17:18:52 +10:00
// Check the inclusivity of unlimited with special floating values
tap.expect ( cruft::range<double>::unlimited ().contains (0.0), "floating unlimited contains 0");
tap.expect ( cruft::range<double>::unlimited ().contains (+std::numeric_limits<double>::infinity ()), "floating unlimited contains +INF");
tap.expect ( cruft::range<double>::unlimited ().contains (-std::numeric_limits<double>::infinity ()), "floating unlimited contains -INF");
tap.expect (!cruft::range<double>::unlimited ().contains ( std::numeric_limits<double>::quiet_NaN ()), "floating unlimited doesn't contain NAN");
2011-05-23 17:18:52 +10:00
// Check the inclusivity of unlimited with some large numbers
tap.expect ( cruft::range<uint16_t>::unlimited ().contains (std::numeric_limits<uint16_t>::min()), "floating unlimited contains min");
tap.expect ( cruft::range<uint16_t>::unlimited ().contains (std::numeric_limits<uint16_t>::max()), "floating unlimited contains max");
// Check inclusivity of max
tap.expect (!cruft::range<double>::max ().contains ( std::numeric_limits<double>::infinity ()), "floating max contains +INF");
tap.expect (!cruft::range<double>::max ().contains (-std::numeric_limits<double>::infinity ()), "floating max contains -INF");
2012-06-15 16:38:10 +10:00
tap.expect ( cruft::range<uint16_t>::max ().contains (std::numeric_limits<uint16_t>::min()), "unsigned max contains min");
tap.expect ( cruft::range<uint16_t>::max ().contains (std::numeric_limits<uint16_t>::max()), "unsigned max contains max");
2012-06-15 16:38:10 +10:00
// Check that expansion via NaN is a noop
{
cruft::range<double> initial_nan (std::numeric_limits<double>::quiet_NaN (),
2016-01-19 18:31:49 +11:00
std::numeric_limits<double>::quiet_NaN ());
initial_nan.expand (1.0);
2016-01-19 18:31:49 +11:00
tap.expect_eq (initial_nan.lo, 1.0, "NAN expansion noop for lo");
tap.expect_eq (initial_nan.hi, 1.0, "NAN expansion noop for hi");
}
2016-01-19 18:31:49 +11:00
return tap.status ();
2011-05-23 17:18:52 +10:00
}