libcruft-util/test/range.cpp

55 lines
3.0 KiB
C++
Raw Normal View History

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