test_checked_less_than_constexpr.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2018 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_less_than(
  12. T v1,
  13. T v2,
  14. char expected_result
  15. ){
  16. using namespace boost::safe_numerics;
  17. const boost::logic::tribool result = v1 < v2;
  18. switch(expected_result){
  19. case '<':
  20. if(result)
  21. return true;
  22. break;
  23. case '>':
  24. case '=':
  25. if(!result)
  26. return true;
  27. break;
  28. case '!':
  29. if(indeterminate(result))
  30. return true;
  31. break;
  32. }
  33. return false;
  34. }
  35. #include "test_checked_comparison.hpp"
  36. template<typename T, typename First, typename Second>
  37. struct test_signed_pair {
  38. static const std::size_t i = First();
  39. static const std::size_t j = Second();
  40. // note: is constexpr really required here? compilers disagree!
  41. constexpr static const bool value = test_checked_less_than(
  42. signed_values<T>[i],
  43. signed_values<T>[j],
  44. signed_comparison_results[i][j]
  45. );
  46. };
  47. template<typename T, typename First, typename Second>
  48. struct test_unsigned_pair {
  49. static const std::size_t i = First();
  50. static const std::size_t j = Second();
  51. // note: is constexpr really required here? compilers disagree!
  52. constexpr static const bool value = test_checked_less_than(
  53. unsigned_values<T>[i],
  54. unsigned_values<T>[j],
  55. unsigned_comparison_results[i][j]
  56. );
  57. };
  58. #include <boost/mp11/algorithm.hpp>
  59. int main(){
  60. using namespace boost::mp11;
  61. static_assert(
  62. mp_all_of<
  63. mp_product<
  64. test_signed_pair,
  65. signed_test_types,
  66. signed_value_indices, signed_value_indices
  67. >,
  68. mp_to_bool
  69. >(),
  70. "all values for all signed types correctly compared"
  71. );
  72. static_assert(
  73. mp_all_of<
  74. mp_product<
  75. test_unsigned_pair,
  76. unsigned_test_types,
  77. unsigned_value_indices, unsigned_value_indices
  78. >,
  79. mp_to_bool
  80. >(),
  81. "all values for all unsigned types correctly compared"
  82. );
  83. return 0;
  84. }