test_checked_and.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 tyme checked_result<R> for some integer type R
  11. template<class T>
  12. bool test_checked_and(
  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 and operation "
  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 and operation "
  44. << std::hex << result << "(" << std::dec << result << ")"
  45. << " != "<< v1 << " & " << v2
  46. << std::endl;
  47. v1 & v2;
  48. return false;
  49. }
  50. #include "test_checked_and.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_and(
  59. signed_values<T>[i],
  60. signed_values<T>[j],
  61. signed_and_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_and(
  73. unsigned_values<T>[i],
  74. unsigned_values<T>[j],
  75. unsigned_and_results[i][j]
  76. );
  77. };
  78. };
  79. #include "check_symmetry.hpp"
  80. #include <boost/mp11/algorithm.hpp>
  81. int main(){
  82. using namespace boost::mp11;
  83. // sanity check on test matrix - should be symetrical
  84. check_symmetry(signed_and_results);
  85. check_symmetry(unsigned_and_results);
  86. bool rval = true;
  87. std::cout << "*** testing signed values\n";
  88. mp_for_each<
  89. mp_product<
  90. test_signed_pair,
  91. signed_test_types,
  92. signed_value_indices,
  93. signed_value_indices
  94. >
  95. >([&](auto I){
  96. rval &= I();
  97. });
  98. std::cout << "*** testing unsigned values\n";
  99. mp_for_each<
  100. mp_product<
  101. test_unsigned_pair,
  102. unsigned_test_types,
  103. unsigned_value_indices, unsigned_value_indices
  104. >
  105. >([&](auto I){
  106. rval &= I();
  107. });
  108. std::cout << (rval ? "success!" : "failure") << std::endl;
  109. return rval ? 0 : 1;
  110. }