#include #include #include #include #include // max, min #include #include #include #include "../include/checked_result.hpp" #include "../include/interval.hpp" // test simple interval addition bool test1(){ using namespace boost::numeric; std::cout << "test1" << std::endl; interval x = {-64, 63}; std::cout << "x = " << x << std::endl; interval y(-128, 126); std::cout << "y = " << y << std::endl; assert(static_cast>(add(x,x)) == y); if(add(x,x) != y) return false; std::cout << "x + x =" << add(x, x) << std::endl; std::cout << "x - x = " << subtract(x, x) << std::endl; return true; } // test simple interval equality bool test2(){ using namespace boost::numeric; std::cout << "test2" << std::endl; boost::numeric::interval x = {-64, 63}; std::cout << "x = " << x << std::endl; std::cout << std::boolalpha << "(x == x) = " << (x == x) << std::endl; return true; } // test erroneous addition bool test3(){ using namespace boost::numeric; std::cout << "test3" << std::endl; interval t; std::cout << "t = " << t << std::endl; interval u; std::cout << "u = " << u << std::endl; using max_t = unsigned long long; interval> r = add(t, u); std::cout << "r = " << r << std::endl; return true; } // test simple interval addition bool test6(){ using namespace boost::numeric; std::cout << "test6" << std::endl; interval x; std::cout << "x = " << x << std::endl; interval y(8, 8); std::cout << "y = " << y << std::endl; assert(static_cast>(add(x,x)) == y); if(add(x,x) != y) return false; std::cout << "x + x =" << add(x, x) << std::endl; std::cout << "x - x = " << subtract(x, x) << std::endl; return true; } // test simple interval inclusion template bool test5(){ using namespace boost::numeric; std::cout << "test5" << std::endl; interval t; std::cout << "t = " << t << std::endl; interval u; std::cout << "u = " << u << std::endl; if(t.includes(u)) std::cout << "t includes u\n"; if(u.includes(t)) std::cout << "u includes t\n"; if(!t.includes(u) && ! u.includes(t)) std::cout << "neither interval includes the other\n"; return ExpectedResult == t.includes(u); } namespace test4 { using namespace boost::numeric; using max_t = std::intmax_t; // utilities // figure the lower portion of range which includes zero template constexpr static const interval r_lower(const interval & t){ static_assert( std::is_literal_type< interval >::value, "interval is not literal type" ); return interval( t.l, std::min(Tx(-1), t.u) ); } // figure the upper portion of range which includes zero template constexpr static const interval r_upper(const interval & t){ static_assert( std::is_literal_type< interval >::value, "interval is not literal type" ); return interval( std::max(Tx(1), t.l), t.u ); } template bool divide_result(){ using namespace boost::numeric; std::cout << "divide result" << std::endl; const interval t; std::cout << boost::core::demangle(typeid(t).name()) << " t = " << t << std::endl; const interval u; std::cout << boost::core::demangle(typeid(u).name()) << " u = " << u << std::endl; const interval> rx = divide(t, u); // r(t, u); std::cout << boost::core::demangle(typeid(rx).name()) << " rx = " << rx << std::endl; return true; } template bool test_multiply(){ using namespace boost::numeric; std::cout << "test4::multiply" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const interval> r_interval = multiply(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_divide(){ using namespace boost::numeric; std::cout << "test4::test4 divide" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const interval> r_interval = divide(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_add( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 add" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(U() + T()); const interval> r_interval = add(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_add(){ return test_add(interval(), interval()); } template bool test_subtract( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 subtract" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const interval> r_interval = subtract(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_subtract(){ return test_subtract(interval(), interval()); } template bool test_left_shift( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 left shift" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(T() << U()); const interval> r_interval = left_shift(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_left_shift(){ return test_left_shift(interval(), interval()); } template bool test_right_shift( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 right shift" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(T() << U()); const interval> r_interval = right_shift(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_right_shift(){ return test_right_shift(interval(), interval()); } template bool test_mod( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 mod" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(T() % U()); const interval> r_interval = modulus(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_mod(){ return test_mod(interval(), interval()); } template bool test(){ return test_multiply() && test_divide() && test_add() && test_subtract() && test_left_shift() && test_right_shift() && test_mod() ; } template bool test_bitwise_or( const interval & t_interval, const interval & u_interval ){ using namespace boost::numeric; std::cout << "test4::test4 bitwise or" << std::endl; std::cout << boost::core::demangle(typeid(t_interval).name()) << " t_interval = " << t_interval << std::endl; std::cout << boost::core::demangle(typeid(u_interval).name()) << " u_interval = " << u_interval << std::endl; using R = decltype(T() | U()); const interval> r_interval = bitwise_or(t_interval, u_interval); std::cout << boost::core::demangle(typeid(r_interval).name()) << " r_interval = " << r_interval << std::endl; return true; } template bool test_bitwise_or(){ return test_bitwise_or(interval(), interval()); } } // test4 int main(){ using namespace boost::numeric; bool rval = ( test1() && test2() && test3() && test4::test_mod() && test4::test_add() && test4::test_bitwise_or() && test4::test_bitwise_or() && test4::test_bitwise_or() && test4::test_right_shift() && test4::test_mod() && test4::test_left_shift() && test4::test_left_shift() && test4::test_left_shift( interval(), interval(8, 8) ) && test4::test_right_shift() && test4::test_right_shift() && test5() && test5() && test5() && test5() && test5() && test5() && test5() && test5() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::divide_result() && test4::test() && test4::test() && test4::test() && test4::test() && test4::test() && test4::test() && test4::test() ); std::cout << (rval ? "success!" : "failure") << std::endl; return rval ? EXIT_SUCCESS : EXIT_FAILURE; }