#include #include #include #include #include #include // max, min #include #include "../include/checked_result.hpp" #include "../include/interval.hpp" 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); std::cout << "x + x =" << add(x, x) << std::endl; std::cout << "x - x = " << subtract(x, x) << std::endl; return true; } 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; } 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; checked_result> r = add(t, u); std::cout << "r = " << r << std::endl; return true; } 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); } #include namespace test4 { using namespace boost::numeric; using max_t = std::intmax_t; 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 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) ); } template constexpr static const checked_result> r( const interval & t, const interval & u ){ if(std::numeric_limits::is_signed){ if(u.l > 0 || u.u < 0){ return divide(t, u); } else{ checked_result> lower = divide(t,r_lower(u)); if(! lower.no_exception()) return lower; checked_result> upper = divide(t,r_upper(u)); if(! upper.no_exception()) return upper; const interval< max_t> & il = lower; const interval< max_t> & iu = upper; return interval< max_t>( std::min(il.l, iu.l), std::max(il.u, iu.u) ); } } else{ // u is unsigned if(u.l > 0) return divide(t, u); else return divide(t, r_upper(u)) ; }; }; bool test1(){ using namespace boost::numeric; int status; std::cout << "test4::test1 int8_t / int8_t new range ignoring zero" << std::endl; const interval t; std::cout << abi::__cxa_demangle(typeid(t).name(),0,0,&status) << " t = " << t << std::endl; const interval u; std::cout << abi::__cxa_demangle(typeid(u).name(),0,0,&status) << " u = " << u << std::endl; const interval rx = r(t, u); std::cout << abi::__cxa_demangle(typeid(rx).name(),0,0,&status) << " rx = " << rx << std::endl; return true; } bool test2(){ using namespace boost::numeric; int status; std::cout << "test4::test2 int8_t / int64_t new range ignoring zero" << std::endl; const interval t; std::cout << abi::__cxa_demangle(typeid(t).name(),0,0,&status) << " t = " << t << std::endl; const interval u; std::cout << abi::__cxa_demangle(typeid(u).name(),0,0,&status) << " u = " << u << std::endl; const interval rx = r(t, u); std::cout << abi::__cxa_demangle(typeid(rx).name(),0,0,&status) << " rx = " << rx << std::endl; return true; } bool test3(){ using namespace boost::numeric; int status; std::cout << "test4::test3 int8_t / int64_t new range ignoring zer0" << std::endl; const interval t; std::cout << abi::__cxa_demangle(typeid(t).name(),0,0,&status) << " t = " << t << std::endl; const interval u; std::cout << abi::__cxa_demangle(typeid(u).name(),0,0,&status) << " u = " << u << std::endl; const checked_result> rx = r(t, u); std::cout << abi::__cxa_demangle(typeid(rx).name(),0,0,&status) << " rx = " << rx << std::endl; return true; } template bool test4(){ using namespace boost::numeric; int status; std::cout << "test4::test4 multiply" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(t_interval).name(),0,0,&status) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(u_interval).name(),0,0,&status) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const checked_result > r_interval = multiply(t_interval, u_interval); std::cout << abi::__cxa_demangle(typeid(r_interval).name(),0,0,&status) << " r_interval = " << r_interval << std::endl; return true; } template bool test5(){ using namespace boost::numeric; int status; std::cout << "test4::test4 divide_nz" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(t_interval).name(),0,0,&status) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(u_interval).name(),0,0,&status) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const checked_result > r_interval = divide_nz(t_interval, u_interval); std::cout << abi::__cxa_demangle(typeid(r_interval).name(),0,0,&status) << " r_interval = " << r_interval << std::endl; return true; } template bool test6(){ using namespace boost::numeric; int status; std::cout << "test4::test4 subtract" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(t_interval).name(),0,0,&status) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(u_interval).name(),0,0,&status) << " u_interval = " << u_interval << std::endl; using R = decltype(U() * T()); const checked_result > r_interval = subtract(t_interval, u_interval); std::cout << abi::__cxa_demangle(typeid(r_interval).name(),0,0,&status) << " r_interval = " << r_interval << std::endl; return true; } template bool test7(){ using namespace boost::numeric; int status; std::cout << "test4::test4 left shift" << std::endl; const interval t_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(t_interval).name(),0,0,&status) << " t_interval = " << t_interval << std::endl; const interval u_interval = { base_value(std::numeric_limits::min()), base_value(std::numeric_limits::max()) }; std::cout << abi::__cxa_demangle(typeid(u_interval).name(),0,0,&status) << " u_interval = " << u_interval << std::endl; using R = T; const checked_result > r_interval = left_shift(t_interval, u_interval); std::cout << abi::__cxa_demangle(typeid(r_interval).name(),0,0,&status) << " r_interval = " << r_interval << std::endl; return true; } template bool test(){ return test4() && test5() && test6() && test7() ; } } // test4 int main(){ bool rval = ( test1() && test2() && test3() && test5() && test5() && test5() && test5() && test5() && test5() && test5() && test5() && test4::test1() && test4::test2() && test4::test3() && 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; }