test_checked_less_than.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/logic/tribool_io.hpp>
  8. #include <boost/core/demangle.hpp>
  9. #include <boost/safe_numerics/checked_result_operations.hpp>
  10. #include <boost/safe_numerics/checked_integer.hpp>
  11. // works for both GCC and clang
  12. #pragma GCC diagnostic push
  13. #pragma GCC diagnostic ignored "-Wunused-value"
  14. // note: T should be of type checked_result<R> for some integer type R
  15. template<class T>
  16. bool test_checked_less_than(
  17. T v1,
  18. T v2,
  19. char expected_result
  20. ){
  21. using namespace boost::safe_numerics;
  22. const boost::logic::tribool result = v1 < v2;
  23. std::cout
  24. << std::boolalpha << v1 << " < " << v2 << " -> " << result
  25. << std::endl;
  26. switch(expected_result){
  27. case '<':
  28. if(result)
  29. return true;
  30. break;
  31. case '>':
  32. case '=':
  33. if(!result)
  34. return true;
  35. break;
  36. case '!':
  37. if(indeterminate(result))
  38. return true;
  39. break;
  40. }
  41. std::cout
  42. << "failed to detect error in addition "
  43. << std::hex << result << "(" << std::dec << result << ")"
  44. << " != "<< v1 << " < " << v2
  45. << std::endl;
  46. v1 < v2;
  47. return false;
  48. }
  49. #pragma GCC diagnostic pop
  50. #include "test_checked_comparison.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_less_than(
  59. signed_values<T>[i],
  60. signed_values<T>[j],
  61. signed_comparison_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_less_than(
  73. unsigned_values<T>[i],
  74. unsigned_values<T>[j],
  75. unsigned_comparison_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. mp_for_each<
  84. mp_product<
  85. test_signed_pair,
  86. signed_test_types,
  87. signed_value_indices,
  88. signed_value_indices
  89. >
  90. >([&](auto I){
  91. rval &= I();
  92. });
  93. mp_for_each<
  94. mp_product<
  95. test_unsigned_pair,
  96. unsigned_test_types,
  97. unsigned_value_indices, unsigned_value_indices
  98. >
  99. >([&](auto I){
  100. rval &= I();
  101. });
  102. std::cout << (rval ? "success!" : "failure") << std::endl;
  103. return rval ? 0 : 1;
  104. }