test_checked_right_shift.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.hpp>
  9. #include <boost/safe_numerics/checked_result_operations.hpp>
  10. #include <boost/safe_numerics/checked_integer.hpp>
  11. // note: T should be of tyme checked_result<R> for some integer type R
  12. template<class T>
  13. bool test_checked_right_shift(
  14. T v1,
  15. T v2,
  16. char expected_result
  17. ){
  18. using namespace boost::safe_numerics;
  19. const T result = v1 >> v2;
  20. std::cout
  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 right shift "
  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. case 'n': // n negative_shift
  46. if(safe_numerics_error::negative_shift == result.m_e)
  47. return true;
  48. break;
  49. case 's': // s negative_value_shift
  50. if(safe_numerics_error::negative_value_shift == result.m_e)
  51. return true;
  52. break;
  53. case 'l': // l shift_too_large
  54. if(safe_numerics_error::shift_too_large == result.m_e)
  55. return true;
  56. break;
  57. default:
  58. assert(false);
  59. }
  60. std::cout
  61. << "failed to detect error in right shift "
  62. << std::hex << result << "(" << std::dec << result << ")"
  63. << " != "<< v1 << " >> " << v2
  64. << std::endl;
  65. v1 >> v2;
  66. return false;
  67. }
  68. #include "test_checked_right_shift.hpp"
  69. template<typename T, typename First, typename Second>
  70. struct test_signed_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_right_shift(
  77. signed_values<T>[i],
  78. signed_values<T>[j],
  79. signed_right_shift_results[i][j]
  80. );
  81. };
  82. };
  83. template<typename T, typename First, typename Second>
  84. struct test_unsigned_pair {
  85. bool operator()() const {
  86. std::size_t i = First();
  87. std::size_t j = Second();
  88. std::cout << std::dec << i << ',' << j << ','
  89. << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
  90. return test_checked_right_shift(
  91. unsigned_values<T>[i],
  92. unsigned_values<T>[j],
  93. unsigned_right_shift_results[i][j]
  94. );
  95. };
  96. };
  97. #include <boost/mp11/algorithm.hpp>
  98. int main(){
  99. using namespace boost::mp11;
  100. bool rval = true;
  101. std::cout << "*** testing signed values\n";
  102. mp_for_each<
  103. mp_product<
  104. test_signed_pair,
  105. signed_test_types,
  106. signed_value_indices,
  107. signed_value_indices
  108. >
  109. >([&](auto I){
  110. rval &= I();
  111. });
  112. std::cout << "*** testing unsigned values\n";
  113. mp_for_each<
  114. mp_product<
  115. test_unsigned_pair,
  116. unsigned_test_types,
  117. unsigned_value_indices, unsigned_value_indices
  118. >
  119. >([&](auto I){
  120. rval &= I();
  121. });
  122. std::cout << (rval ? "success!" : "failure") << std::endl;
  123. return rval ? 0 : 1;
  124. }