test_checked_multiply.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_multiply(
  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 "
  21. << v1 << " * " << v2 << " -> " << result
  22. << std::endl;
  23. switch(expected_result){
  24. case '0':
  25. case '.':
  26. if(result.exception()){
  27. std::cout
  28. << "erroneously detected error in multiplication "
  29. << std::endl;
  30. v1 * v2;
  31. return false;
  32. }
  33. if(expected_result == '0'
  34. && result != T(0)
  35. ){
  36. std::cout
  37. << "failed to get expected zero result "
  38. << std::endl;
  39. v1 * v2;
  40. return false;
  41. }
  42. return true;
  43. case '-':
  44. if(safe_numerics_error::negative_overflow_error == result.m_e)
  45. return true;
  46. break;
  47. case '+':
  48. if(safe_numerics_error::positive_overflow_error == result.m_e)
  49. return true;
  50. break;
  51. case '!':
  52. if(safe_numerics_error::range_error == result.m_e)
  53. return true;
  54. break;
  55. }
  56. std::cout
  57. << "failed to detect error in multiplication "
  58. << std::hex << result << "(" << std::dec << result << ")"
  59. << " != "<< v1 << " * " << v2
  60. << std::endl;
  61. v1 * v2;
  62. return false;
  63. }
  64. #include "test_checked_multiply.hpp"
  65. template<typename T, typename First, typename Second>
  66. struct test_signed_pair {
  67. bool operator()() const {
  68. std::size_t i = First();
  69. std::size_t j = Second();
  70. std::cout << std::dec << i << ',' << j << ','
  71. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  72. return test_checked_multiply(
  73. signed_values<T>[i],
  74. signed_values<T>[j],
  75. signed_multiplication_results[i][j]
  76. );
  77. };
  78. };
  79. template<typename T, typename First, typename Second>
  80. struct test_unsigned_pair {
  81. bool operator()() const {
  82. std::size_t i = First();
  83. std::size_t j = Second();
  84. std::cout << std::dec << i << ',' << j << ','
  85. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  86. return test_checked_multiply(
  87. unsigned_values<T>[i],
  88. unsigned_values<T>[j],
  89. unsigned_multiplication_results[i][j]
  90. );
  91. };
  92. };
  93. #include "check_symmetry.hpp"
  94. #include <boost/mp11/algorithm.hpp>
  95. int main(int , char *[]){
  96. // sanity check on test matrix - should be symetrical
  97. check_symmetry(signed_multiplication_results);
  98. check_symmetry(unsigned_multiplication_results);
  99. using namespace boost::mp11;
  100. bool rval = true;
  101. mp_for_each<
  102. mp_product<
  103. test_signed_pair,
  104. signed_test_types,
  105. signed_value_indices,
  106. signed_value_indices
  107. >
  108. >([&](auto I){
  109. rval &= I();
  110. });
  111. std::cout << "*** testing unsigned values\n";
  112. mp_for_each<
  113. mp_product<
  114. test_unsigned_pair,
  115. unsigned_test_types,
  116. unsigned_value_indices, unsigned_value_indices
  117. >
  118. >([&](auto I){
  119. rval &= I();
  120. });
  121. std::cout << (rval ? "success!" : "failure") << std::endl;
  122. return rval ? 0 : 1;
  123. }