test_checked_divide.cpp 3.4 KB

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