fixed tests for checked_xor, checked_or, checked_add, and checked_and

made all checking of symmetries of test matrices occur at compile time via constexpr.
added -Wextra to warning switches.
This commit is contained in:
Robert Ramey
2019-12-25 14:37:37 -08:00
parent d692eddd3d
commit 35bedab3f6
29 changed files with 188 additions and 98 deletions
+10 -4
View File
@@ -25,12 +25,18 @@ set(CMAKE_CXX_EXTENSIONS OFF)
message(STATUS "compiler is ${CMAKE_CXX_COMPILER_ID}" )
if( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
add_definitions( -ftemplate-depth=300 )
add_definitions( -ftemplate-depth=255 )
add_compile_options(-Wnon-virtual-dtor -ansi -Wcast-align -Wchar-subscripts -Wall -Wextra -W -Wshadow -Wsign-compare )
elseif( CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
add_definitions( /wd4996 )
add_definitions( /wd4996 /wd4068 )
# warning level 4 and all warnings as errors
add_compile_options(/W4 /WX)
elseif( CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" )
add_definitions( -ftemplate-depth=300 )
add_compile_options(-Wnon-virtual-dtor -ansi -Wcast-align -Wchar-subscripts -Wall -W -Wshadow -Wsign-compare )
add_definitions( -ftemplate-depth=255 )
add_compile_options(-Wnon-virtual-dtor -ansi -Wcast-align -Wchar-subscripts -Wall -Wextra -W -Wshadow -Wsign-compare )
elseif( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
add_definitions( -ftemplate-depth=255 )
add_compile_options(-Wnon-virtual-dtor -ansi -Wcast-align -Wchar-subscripts -Wall -Wextra -W -Wshadow -Wsign-compare )
endif()
#
View File
+1 -1
View File
@@ -124,7 +124,7 @@ namespace ilog2_detail {
constexpr static unsigned int ilog2(const boost::uint_t<8>::exact & t){
#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
const char LogTable256[256] = {
static_cast<const char>(-1), 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
static_cast<char>(-1), 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
};
+2 -2
View File
@@ -1,10 +1,10 @@
// given an array of values of a particular type
template<typename T, unsigned int N>
constexpr void check_symmetry(const T (&value)[N]) {
constexpr bool check_symmetry(const T (&value)[N]) {
using namespace boost::safe_numerics;
// for each pair of values p1, p2 (100)
for(unsigned int i = 0; i < N; i++)
for(unsigned int j = 0; j < N; j++)
assert(value[i][j] == value[j][i]);
return true;
}
+4 -2
View File
@@ -53,8 +53,10 @@ struct test {
#include "check_symmetry.hpp"
int main(){
// sanity check on test matrix - should be symetrical
check_symmetry(test_addition_automatic_result);
static_assert(
check_symmetry(test_addition_automatic_result),
"sanity check on test matrix - should be symmetrical"
);
//TEST_EACH_VALUE_PAIR
test<test_values> rval(true);
+4 -2
View File
@@ -36,8 +36,10 @@ struct test_pair {
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(test_addition_automatic_result);
static_assert(
check_symmetry(test_addition_automatic_result),
"sanity check on test matrix - should be symmetrical"
);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
+4 -2
View File
@@ -52,8 +52,10 @@ struct test {
#include "check_symmetry.hpp"
int main(){
// sanity check on test matrix - should be symetrical
check_symmetry(test_addition_native_result);
static_assert(
check_symmetry(test_addition_native_result),
"sanity check on test matrix - should be symmetrical"
);
test<test_values> rval(true);
+4 -2
View File
@@ -37,8 +37,10 @@ struct test_pair {
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(test_addition_native_result);
static_assert(
check_symmetry(test_addition_native_result),
"sanity check on test matrix - should be symmetrical"
);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
+8 -3
View File
@@ -93,9 +93,14 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
// sanity check on test matrix - should be symetrical
check_symmetry(signed_addition_results);
check_symmetry(unsigned_addition_results);
static_assert(
check_symmetry(signed_addition_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_addition_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
bool rval = true;
+9 -3
View File
@@ -60,10 +60,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
using namespace boost::mp11;
check_symmetry(signed_addition_results);
check_symmetry(unsigned_addition_results);
static_assert(
check_symmetry(signed_addition_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_addition_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
static_assert(
mp_all_of<
mp_product<
+12 -4
View File
@@ -36,12 +36,15 @@ bool test_checked_and(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "failed to detect error in and operation "
@@ -89,11 +92,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(signed_and_results);
check_symmetry(unsigned_and_results);
static_assert(
check_symmetry(signed_and_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_and_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
bool rval = true;
std::cout << "*** testing signed values\n";
+9 -2
View File
@@ -60,9 +60,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
static_assert(
check_symmetry(signed_and_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_and_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
check_symmetry(signed_and_results);
check_symmetry(unsigned_and_results);
static_assert(
mp_all_of<
+3
View File
@@ -46,12 +46,15 @@ bool test_checked_divide(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "*** failed to detect error in division "
+3
View File
@@ -46,12 +46,15 @@ bool test_checked_modulus(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "failed to detect error in modulus "
+8 -3
View File
@@ -103,9 +103,14 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(int , char *[]){
// sanity check on test matrix - should be symetrical
check_symmetry(signed_multiplication_results);
check_symmetry(unsigned_multiplication_results);
static_assert(
check_symmetry(signed_multiplication_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_multiplication_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
bool rval = true;
+8 -2
View File
@@ -68,9 +68,15 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
static_assert(
check_symmetry(signed_multiplication_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_multiplication_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
check_symmetry(signed_multiplication_results);
check_symmetry(unsigned_multiplication_results);
static_assert(
mp_all_of<
+12 -4
View File
@@ -36,12 +36,15 @@ bool test_checked_or(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "failed to detect error in bitwise or "
@@ -89,11 +92,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(signed_or_results);
check_symmetry(unsigned_or_results);
static_assert(
check_symmetry(signed_or_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_or_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
bool rval = true;
std::cout << "*** testing signed values\n";
+12 -12
View File
@@ -21,24 +21,24 @@ constexpr const char * const signed_or_results[] = {
// 012345678
/* 0*/ "!!!!!!!!!",
/* 1*/ "!!!!!!!!!",
/* 2*/ "!!++++++!",
/* 3*/ "!!+.....-",
/* 4*/ "!!+.....-",
/* 5*/ "!!+.....-",
/* 6*/ "!!+.....-",
/* 7*/ "!!+.....-",
/* 8*/ "!!!------",
/* 2*/ "!!!!!!!!!",
/* 3*/ "!!!.....!",
/* 4*/ "!!!.....!",
/* 5*/ "!!!.....!",
/* 6*/ "!!!.....!",
/* 7*/ "!!!.....!",
/* 8*/ "!!!!!!!!!",
};
constexpr const char * const unsigned_or_results[] = {
// 0123456
/* 0*/ "!!!!!!!",
/* 1*/ "!!!!!!!",
/* 2*/ "!!+++++",
/* 3*/ "!!+...-",
/* 4*/ "!!+...-",
/* 5*/ "!!+...-",
/* 6*/ "!!+----",
/* 2*/ "!!!!!!!",
/* 3*/ "!!!...!",
/* 4*/ "!!!...!",
/* 5*/ "!!!...!",
/* 6*/ "!!!!!!!",
};
#endif // BOOST_SAFE_NUMERICS_TEST_CHECKED_OR_HPP
+8 -3
View File
@@ -67,9 +67,14 @@ struct test_unsigned_pair {
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(signed_or_results);
check_symmetry(unsigned_or_results);
static_assert(
check_symmetry(signed_or_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_or_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
mp_all_of<
+4 -1
View File
@@ -36,15 +36,18 @@ bool test_checked_subtract(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "failed to detect error in ition "
<< "failed to detect error in subtraction "
<< std::hex << result << "(" << std::dec << result << ")"
<< " != "<< v1 << " - " << v2
<< std::endl;
+11 -11
View File
@@ -21,24 +21,24 @@ constexpr const char * signed_subtraction_results[] = {
// 012345678
/* 0*/ "!!!!!!!!!",
/* 1*/ "!!!!!!!!!",
/* 2*/ "!!+++++++",
/* 3*/ "!!-...++-",
/* 4*/ "---....++",
/* 5*/ "---....++",
/* 6*/ "---.....+",
/* 7*/ "!!---...-",
/* 8*/ "!!-------",
/* 2*/ "!!!++++++",
/* 3*/ "!!-...+++",
/* 4*/ "!!-....++",
/* 5*/ "!!-....++",
/* 6*/ "!!-.....+",
/* 7*/ "!!---...+",
/* 8*/ "!!------!",
};
constexpr const char * unsigned_subtraction_results[] = {
// 0123456
/* 0*/ "!!!!!!!",
/* 1*/ "!!!!!!!",
/* 2*/ "!!+++++",
/* 2*/ "!!!++++",
/* 3*/ "!!-...+",
/* 4*/ "----..+",
/* 5*/ "-----.+",
/* 6*/ "------+",
/* 4*/ "!!--..+",
/* 5*/ "!!---.+",
/* 6*/ "!!----!",
};
#endif // BOOST_SAFE_NUMERICS_TEST_CHECKED_AND_HPP
+12 -4
View File
@@ -36,12 +36,15 @@ bool test_checked_xor(
case '-':
if(safe_numerics_error::negative_overflow_error == result.m_e)
return true;
break;
case '+':
if(safe_numerics_error::positive_overflow_error == result.m_e)
return true;
break;
case '!':
if(safe_numerics_error::range_error == result.m_e)
return true;
break;
}
std::cout
<< "failed to detect error in exclusive or "
@@ -89,11 +92,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(signed_xor_results);
check_symmetry(unsigned_xor_results);
static_assert(
check_symmetry(signed_xor_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_xor_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
bool rval = true;
std::cout << "*** testing signed values\n";
+12 -12
View File
@@ -21,24 +21,24 @@ constexpr const char * const signed_xor_results[] = {
// 012345678
/* 0*/ "!!!!!!!!!",
/* 1*/ "!!!!!!!!!",
/* 2*/ "!!++++++!",
/* 3*/ "!!+.....-",
/* 4*/ "!!+.....-",
/* 5*/ "!!+.....-",
/* 6*/ "!!+.....-",
/* 7*/ "!!+.....-",
/* 8*/ "!!!-----!",
/* 2*/ "!!!!!!!!!",
/* 3*/ "!!!.....!",
/* 4*/ "!!!.....!",
/* 5*/ "!!!.....!",
/* 6*/ "!!!.....!",
/* 7*/ "!!!.....!",
/* 8*/ "!!!!!!!!!",
};
constexpr const char * const unsigned_xor_results[] = {
// 0123456
/* 0*/ "!!!!!!!",
/* 1*/ "!!!!!!!",
/* 2*/ "!!+++++",
/* 3*/ "!!+...-",
/* 4*/ "!!+...-",
/* 5*/ "!!+...-",
/* 6*/ "!!+----",
/* 2*/ "!!!!!!!",
/* 3*/ "!!!...!",
/* 4*/ "!!!...!",
/* 5*/ "!!!...!",
/* 6*/ "!!!!!!!",
};
#endif // BOOST_SAFE_NUMERICS_TEST_CHECKED_XOR_HPP
+9 -2
View File
@@ -68,9 +68,16 @@ struct test_unsigned_pair {
#include <boost/mp11/algorithm.hpp>
int main(){
static_assert(
check_symmetry(signed_xor_results),
"sanity check on test matrix - should be symmetrical"
);
static_assert(
check_symmetry(unsigned_xor_results),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
check_symmetry(signed_xor_results);
check_symmetry(unsigned_xor_results);
static_assert(
mp_all_of<
+5 -4
View File
@@ -52,10 +52,11 @@ struct test {
#include "check_symmetry.hpp"
int main(){
// sanity check on test matrix - should be symetrical
check_symmetry(test_multiplication_automatic_result);
//TEST_EACH_VALUE_PAIR
static_assert(
check_symmetry(test_multiplication_automatic_result),
"sanity check on test matrix - should be symmetrical"
);
test<test_values> rval(true);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
+5 -4
View File
@@ -34,11 +34,12 @@ struct test_pair {
#include "check_symmetry.hpp"
int main(){
static_assert(
check_symmetry(test_multiplication_automatic_result),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(test_multiplication_automatic_result);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
static_assert(
+5 -3
View File
@@ -51,9 +51,11 @@ struct test {
#include "check_symmetry.hpp"
int main(){
// sanity check on test matrix - should be symetrical
check_symmetry(test_multiplication_native_result);
//TEST_EACH_VALUE_PAIR
static_assert(
check_symmetry(test_multiplication_native_result),
"sanity check on test matrix - should be symmetrical"
);
test<test_values> rval(true);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
+4 -4
View File
@@ -34,11 +34,11 @@ struct test_pair {
#include "check_symmetry.hpp"
int main(){
static_assert(
check_symmetry(test_multiplication_native_result),
"sanity check on test matrix - should be symmetrical"
);
using namespace boost::mp11;
// sanity check on test matrix - should be symetrical
check_symmetry(test_multiplication_native_result);
using value_indices = mp_iota_c<mp_size<test_values>::value>;
static_assert(
-2
View File
@@ -31,11 +31,9 @@ struct test_pair {
#include <boost/mp11/list.hpp>
#include <boost/mp11/algorithm.hpp>
#include "check_symmetry.hpp"
int main(){
using namespace boost::mp11;
using value_indices = mp_iota_c<mp_size<test_values>::value>;
static_assert(