test_checked_subtract.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 tyme checked_result<R> for some integer type R
  11. template<class T>
  12. bool test_checked_subtract(
  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. if(result.exception()){
  25. std::cout
  26. << "erroneously detected error in subtraction "
  27. << std::endl;
  28. v1 - v2;
  29. return false;
  30. }
  31. return true;
  32. case '-':
  33. if(safe_numerics_error::negative_overflow_error == result.m_e)
  34. return true;
  35. case '+':
  36. if(safe_numerics_error::positive_overflow_error == result.m_e)
  37. return true;
  38. case '!':
  39. if(safe_numerics_error::range_error == result.m_e)
  40. return true;
  41. }
  42. std::cout
  43. << "failed to detect error in ition "
  44. << std::hex << result << "(" << std::dec << result << ")"
  45. << " != "<< v1 << " - " << v2
  46. << std::endl;
  47. v1 - v2;
  48. return false;
  49. }
  50. #include "test_checked_subtract.hpp"
  51. template<typename T, typename First, typename Second>
  52. struct test_signed_pair {
  53. bool operator()() const {
  54. std::size_t i = First();
  55. std::size_t j = Second();
  56. std::cout << std::dec << i << ',' << j << ','
  57. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  58. return test_checked_subtract(
  59. signed_values<T>[i],
  60. signed_values<T>[j],
  61. signed_subtraction_results[i][j]
  62. );
  63. };
  64. };
  65. template<typename T, typename First, typename Second>
  66. struct test_unsigned_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_subtract(
  73. unsigned_values<T>[i],
  74. unsigned_values<T>[j],
  75. unsigned_subtraction_results[i][j]
  76. );
  77. };
  78. };
  79. #include <boost/mp11/algorithm.hpp>
  80. int main(){
  81. using namespace boost::mp11;
  82. bool rval = true;
  83. std::cout << "*** testing signed values\n";
  84. mp_for_each<
  85. mp_product<
  86. test_signed_pair,
  87. signed_test_types,
  88. signed_value_indices,
  89. signed_value_indices
  90. >
  91. >([&](auto I){
  92. rval &= I();
  93. });
  94. std::cout << "*** testing unsigned values\n";
  95. mp_for_each<
  96. mp_product<
  97. test_unsigned_pair,
  98. unsigned_test_types,
  99. unsigned_value_indices, unsigned_value_indices
  100. >
  101. >([&](auto I){
  102. rval &= I();
  103. });
  104. std::cout << (rval ? "success!" : "failure") << std::endl;
  105. return rval ? 0 : 1;
  106. }