test_checked_or_constexpr.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <boost/safe_numerics/checked_result.hpp>
  7. #include <boost/safe_numerics/checked_result_operations.hpp>
  8. #include <boost/safe_numerics/checked_integer.hpp>
  9. // note: T should be of tyme checked_result<R> for some integer type R
  10. template<class T>
  11. constexpr bool test_checked_or(
  12. T v1,
  13. T v2,
  14. char expected_result
  15. ){
  16. using namespace boost::safe_numerics;
  17. const T result = v1 | v2;
  18. switch(expected_result){
  19. case '.':
  20. if(result.exception()){
  21. return false;
  22. }
  23. return true;
  24. case '-':
  25. if(safe_numerics_error::negative_overflow_error == result.m_e)
  26. return true;
  27. case '+':
  28. if(safe_numerics_error::positive_overflow_error == result.m_e)
  29. return true;
  30. case '!':
  31. if(safe_numerics_error::range_error == result.m_e)
  32. return true;
  33. }
  34. return false;
  35. }
  36. #include "test_checked_or.hpp"
  37. template<typename T, typename First, typename Second>
  38. struct test_signed_pair {
  39. static const std::size_t i = First();
  40. static const std::size_t j = Second();
  41. // note: is constexpr really required here? compilers disagree!
  42. constexpr static const bool value = test_checked_or(
  43. signed_values<T>[i],
  44. signed_values<T>[j],
  45. signed_or_results[i][j]
  46. );
  47. };
  48. template<typename T, typename First, typename Second>
  49. struct test_unsigned_pair {
  50. static const std::size_t i = First();
  51. static const std::size_t j = Second();
  52. // note: is constexpr really required here? compilers disagree!
  53. constexpr static const bool value = test_checked_or(
  54. unsigned_values<T>[i],
  55. unsigned_values<T>[j],
  56. unsigned_or_results[i][j]
  57. );
  58. };
  59. #include "check_symmetry.hpp"
  60. #include <boost/mp11/algorithm.hpp>
  61. int main(){
  62. using namespace boost::mp11;
  63. // sanity check on test matrix - should be symetrical
  64. check_symmetry(signed_or_results);
  65. check_symmetry(unsigned_or_results);
  66. static_assert(
  67. mp_all_of<
  68. mp_product<
  69. test_signed_pair,
  70. signed_test_types,
  71. signed_value_indices, signed_value_indices
  72. >,
  73. mp_to_bool
  74. >(),
  75. "all values for all signed types correctly or'ed"
  76. );
  77. static_assert(
  78. mp_all_of<
  79. mp_product<
  80. test_unsigned_pair,
  81. unsigned_test_types,
  82. unsigned_value_indices, unsigned_value_indices
  83. >,
  84. mp_to_bool
  85. >(),
  86. "all values for all unsigned types correctly or'ed"
  87. );
  88. return 0;
  89. }