Files
safe_numerics/test/test_compare.cpp
Robert Ramey da574497e5 working
test add
test cast
test compare
test conversion
test subtract
test multiply
2014-04-14 14:41:48 -07:00

160 lines
4.9 KiB
C++

// Copyright (c) 2014 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <cassert>
#include "../include/safe_integer.hpp"
#include "../include/safe_compare.hpp"
// we could have used decltype and auto for C++11 but we've decided
// to use boost/typeof to be compatible with older compilers
#include <boost/typeof/typeof.hpp>
template<class T1, class T2>
bool test_compare_detail(
T1 v1,
T2 v2,
char expected_result
){
if('=' == expected_result){
if(! boost::numeric::safe_compare::equal(v1, v2))
return false;
if(boost::numeric::safe_compare::less_than(v1, v2))
return false;
if(boost::numeric::safe_compare::greater_than(v1, v2))
return false;
}
if('<' == expected_result){
if(! boost::numeric::safe_compare::less_than(v1, v2))
return false;
if(boost::numeric::safe_compare::equal(v1, v2))
return false;
if(boost::numeric::safe_compare::greater_than(v1, v2))
return false;
}
if('>' == expected_result){
if(! boost::numeric::safe_compare::greater_than(v1, v2))
return false;
if(boost::numeric::safe_compare::less_than(v1, v2))
return false;
if(boost::numeric::safe_compare::equal(v1, v2))
return false;
}
return true;
}
template<class T1, class T2>
bool test_compare(
T1 v1,
T2 v2,
const char *av1,
const char *av2,
char expected_result
){
std::cout
<< "testing "
<< av1 << ' ' << expected_result << ' ' << av2
<< std::endl;
if(! test_compare_detail(v1, v2,expected_result)){
std::cout
<< "error "
<< av1 << ' ' << expected_result << ' ' << av2
<< std::endl;
return false;
}
boost::numeric::safe<T1> t1 = v1;
if(!test_compare_detail(t1, v2, expected_result)){
std::cout
<< "error "
<< "safe(" << av1 << ')' << ' ' << expected_result << ' ' << av2
<< std::endl;
return false;
}
return true; // correct result
}
#include "test.hpp"
#include "test_values.hpp"
const char *test_compare_result[VALUE_ARRAY_SIZE] = {
// 0 0 0 0
// 01234567012345670123456701234567
// 01234567890123456789012345678901
/* 0*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/* 1*/ ">=>>><>>><>>><>>>=<<><<<><<<><<<",
/* 2*/ "<<=<<<><<<><<<><<<<<<<<<<<<<<<<<",
/* 3*/ "<<>=<<>=<<>=<<>=<<<<<<<<<<<<<<<<",
/* 4*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/* 5*/ ">>>>>=>>><>>><>>>>>>>=<<><<<><<<",
/* 6*/ "<<<<<<=<<<><<<><<<<<<<<<<<<<<<<<",
/* 7*/ "<<>=<<>=<<>=<<>=<<<<<<<<<<<<<<<<",
/* 8*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/* 9*/ ">>>>>>>>>=>>><>>>>>>>>>>>=<<><<<",
/*10*/ "<<<<<<<<<<=<<<><<<<<<<<<<<<<<<<<",
/*11*/ "<<>=<<>=<<>=<<>=<<<<<<<<<<<<<<<<",
/*12*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/*13*/ ">>>>>>>>>>>>>=>>>>>>>>>>>>>>>=<<",
/*14*/ "<<<<<<<<<<<<<<=<<<<<<<<<<<<<<<<<",
/*15*/ "<<>=<<>=<<>=<<>=<<<<<<<<<<<<<<<<",
// 0 0 0 0
// 01234567012345670123456701234567
// 01234567890123456789012345678901
/*16*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/*17*/ ">=>>><>>><>>><>>>=<<><<<><<<><<<",
/*18*/ ">>>>><>>><>>><>>>>=<><<<><<<><<<",
/*19*/ ">>>>><>>><>>><>>>>>=><<<><<<><<<",
/*20*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/*21*/ ">>>>>=>>><>>><>>>>>>>=<<><<<><<<",
/*22*/ ">>>>>>>>><>>><>>>>>>>>=<><<<><<<",
/*23*/ ">>>>>>>>><>>><>>>>>>>>>=><<<><<<",
/*24*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/*25*/ ">>>>>>>>>=>>><>>>>>>>>>>>=<<><<<",
/*26*/ ">>>>>>>>>>>>><>>>>>>>>>>>>=<><<<",
/*27*/ ">>>>>>>>>>>>><>>>>>>>>>>>>>=><<<",
/*28*/ "=<>>=<>>=<>>=<>>=<<<=<<<=<<<=<<<",
/*29*/ ">>>>>>>>>>>>>=>>>>>>>>>>>>>>>=<<",
/*30*/ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>=<",
/*31*/ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>="
};
#define TEST_IMPL(v1, v2, result) \
rval &= test_compare( \
v1, \
v2, \
BOOST_PP_STRINGIZE(v1), \
BOOST_PP_STRINGIZE(v2), \
result \
);
/**/
#define TESTX(value_index1, value_index2) \
(std::cout << value_index1 << ',' << value_index2 << ','); \
TEST_IMPL( \
BOOST_PP_ARRAY_ELEM(value_index1, VALUES), \
BOOST_PP_ARRAY_ELEM(value_index2, VALUES), \
test_compare_result[value_index1][value_index2] \
)
/**/
#define COUNT sizeof(test_addition_result)
int main(int argc, char * argv[]){
// sanity check on test matrix - should be symetrical
for(int i = 0; i < VALUE_ARRAY_SIZE; ++i)
for(int j = i + 1; j < VALUE_ARRAY_SIZE; ++j)
;//assert(test_compare_result[i][j] == test_compare_result[j][i]);
bool rval = true;
TEST_EACH_VALUE_PAIR
return ! rval ;
}