added tests for subtract and multiply automatic

This commit is contained in:
Robert Ramey
2015-08-02 11:30:14 -07:00
parent 79d480790a
commit 194d76a42e
7 changed files with 677 additions and 80 deletions

View File

@@ -6,7 +6,7 @@
#include <iostream>
#include <exception>
#include <type_traits> // is_same
#include <cxxabi.h>
#include "../include/safe_integer.hpp"
@@ -20,137 +20,159 @@ bool test_multiply(
const char *av2,
char expected_result
){
std::cout
<< "testing "
<< av1 << " * " << av2
<< std::endl;
using namespace boost::numeric;
auto unsafe_result = v1 + v2;
{
boost::numeric::safe<T1> t1 = v1;
// presuming native policy
boost::numeric::safe<decltype(v1 * v2)> result;
std::cout << "testing safe<" << av1 << "> * " << av2 << " -> ";
static_assert(boost::numeric::is_safe<safe<T1> >::value, "safe not safe!");
safe<T1> t1 = v1;
using result_type = decltype(t1 * v2);
result_type result;
static_assert(
boost::numeric::is_safe<result_type>::value,
"Expression failed to return safe type"
);
try{
result = t1 * v2;
static_assert(
boost::numeric::is_safe<decltype(t1 * v2)>::value,
"Expression failed to return safe type"
);
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == 'x'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "failed to detect error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " ! = "<< av1 << " * " << av2
<< "*** failed to detect error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
t1 * v2;
}
catch(std::exception){}
assert(result != unsafe_result);
return false;
}
}
catch(std::exception){
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == '.'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "erroneously detected error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " == "<< av1 << " * " << av2
<< "*** erroneously detected error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
t1 * v2;
}
catch(std::exception){}
assert(result == unsafe_result);
return false;
}
}
}
{
boost::numeric::safe<T2> t2 = v2;
// presuming native policy
boost::numeric::safe<decltype(v1 * v2)> result;
std::cout << "testing " << av1 << " * " << "safe<"<< av2 << "> -> ";
static_assert(boost::numeric::is_safe<safe<T2> >::value, "safe not safe!");
safe<T2> t2 = v2;
using result_type = decltype(v1 * t2);
result_type result;
static_assert(
boost::numeric::is_safe<result_type>::value,
"Expression failed to return safe type"
);
try{
result = v1 * t2;
static_assert(
boost::numeric::is_safe<decltype(v1 * t2)>::value,
"Expression failed to return safe type"
);
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == 'x'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "failed to detect error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " ! = "<< av1 << " * " << av2
<< "*** failed to detect error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
v1 * t2;
}
catch(std::exception){}
assert(result != unsafe_result);
return false;
}
}
catch(std::exception){
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == '.'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "erroneously detected error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " == "<< av1 << " * " << av2
<< "*** erroneously detected error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
v1 * t2;
}
catch(std::exception){}
assert(result == unsafe_result);
return false;
}
}
}
{
boost::numeric::safe<T1> t1 = v1;
boost::numeric::safe<T2> t2 = v2;
std::cout << "testing safe<" << av1 << "> * safe<" << av2 << "> -> ";
safe<T1> t1 = v1;
safe<T2> t2 = v2;
// presuming native policy
boost::numeric::safe<decltype(v1 * v2)> result;
using result_type = decltype(t1 * t2);
result_type result;
static_assert(
std::is_same<
boost::numeric::safe<decltype(v1 * v2)>,
decltype(t1 * t2)
>::value,
"unexpected result type"
boost::numeric::is_safe<result_type>::value,
"Expression failed to return safe type"
);
try{
result = t1 * t2;
static_assert(
boost::numeric::is_safe<decltype(t1 * t2)>::value,
"Expression failed to return safe type"
);
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == 'x'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "failed to detect error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " ! = "<< av1 << " * " << av2
<< "*** failed to detect error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
t1 * t2;
}
catch(std::exception){}
assert(result != unsafe_result);
return false;
}
}
catch(std::exception){
std::cout << std::hex << result << "(" << std::dec << result << ")"
<< std::endl;
if(expected_result == '.'){
const std::type_info & ti = typeid(result);
int status;
std::cout
<< "erroneously detected error in multiplication "
<< std::hex << result << "(" << std::dec << result << ")"
<< " == "<< av1 << " * " << av2
<< "*** erroneously detected error in multiplication "
<< abi::__cxa_demangle(ti.name(),0,0,&status) << '\n'
<< std::endl;
try{
t1 * t2;
}
catch(std::exception){}
assert(result == unsafe_result);
return false;
}
}