test_checked_add.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2012 Robert Ramey
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <boost/core/demangle.hpp>
  8. #include <boost/safe_numerics/checked_result_operations.hpp>
  9. #include <boost/safe_numerics/checked_integer.hpp>
  10. // note: T should be of type checked_result<R> for some integer type R
  11. template<class T>
  12. bool test_checked_add(
  13. T v1,
  14. T v2,
  15. char expected_result
  16. ){
  17. using namespace boost::safe_numerics;
  18. const T result = v1 + v2;
  19. std::cout
  20. << "testing " << boost::core::demangle(typeid(T).name()) << ' '
  21. << v1 << " + " << v2 << " -> " << result
  22. << std::endl;
  23. switch(expected_result){
  24. case '.':
  25. if(result.exception()){
  26. std::cout
  27. << "erroneously detected error in addition "
  28. << std::endl;
  29. v1 + v2;
  30. return false;
  31. }
  32. return true;
  33. case '-':
  34. if(safe_numerics_error::negative_overflow_error == result.m_e)
  35. return true;
  36. break;
  37. case '+':
  38. if(safe_numerics_error::positive_overflow_error == result.m_e)
  39. return true;
  40. break;
  41. case '!':
  42. if(safe_numerics_error::range_error == result.m_e)
  43. return true;
  44. break;
  45. }
  46. std::cout
  47. << "failed to detect error in addition "
  48. << std::hex << result << "(" << std::dec << result << ")"
  49. << " != "<< v1 << " + " << v2
  50. << std::endl;
  51. v1 + v2;
  52. return false;
  53. }
  54. #include "test_checked_add.hpp"
  55. template<typename T, typename First, typename Second>
  56. struct test_signed_pair {
  57. bool operator()() const {
  58. std::size_t i = First();
  59. std::size_t j = Second();
  60. std::cout << std::dec << i << ',' << j << ','
  61. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  62. return test_checked_add(
  63. signed_values<T>[i],
  64. signed_values<T>[j],
  65. signed_addition_results[i][j]
  66. );
  67. };
  68. };
  69. template<typename T, typename First, typename Second>
  70. struct test_unsigned_pair {
  71. bool operator()() const {
  72. std::size_t i = First();
  73. std::size_t j = Second();
  74. std::cout << std::dec << i << ',' << j << ','
  75. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  76. return test_checked_add(
  77. unsigned_values<T>[i],
  78. unsigned_values<T>[j],
  79. unsigned_addition_results[i][j]
  80. );
  81. };
  82. };
  83. #include "check_symmetry.hpp"
  84. #include <boost/mp11/algorithm.hpp>
  85. int main(){
  86. // sanity check on test matrix - should be symetrical
  87. check_symmetry(signed_addition_results);
  88. check_symmetry(unsigned_addition_results);
  89. using namespace boost::mp11;
  90. bool rval = true;
  91. mp_for_each<
  92. mp_product<
  93. test_signed_pair,
  94. signed_test_types,
  95. signed_value_indices,
  96. signed_value_indices
  97. >
  98. >([&](auto I){
  99. rval &= I();
  100. });
  101. mp_for_each<
  102. mp_product<
  103. test_unsigned_pair,
  104. unsigned_test_types,
  105. unsigned_value_indices, unsigned_value_indices
  106. >
  107. >([&](auto I){
  108. rval &= I();
  109. });
  110. std::cout << (rval ? "success!" : "failure") << std::endl;
  111. return rval ? 0 : 1;
  112. }