test_divide.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef BOOST_TEST_DIVIDE_HPP
  2. #define BOOST_TEST_DIVIDE_HPP
  3. // Copyright (c) 2015 Robert Ramey
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <iostream>
  9. #include <boost/core/demangle.hpp>
  10. #include <boost/safe_numerics/safe_integer.hpp>
  11. #include <boost/safe_numerics/range_value.hpp>
  12. template<class T1, class T2>
  13. bool test_divide(
  14. T1 v1,
  15. T2 v2,
  16. const char *av1,
  17. const char *av2,
  18. char expected_result
  19. ){
  20. {
  21. safe_t<T1> t1 = v1;
  22. using result_type = decltype(t1 / v2);
  23. std::cout
  24. << "safe<" << av1 << "> / " << av2 << " -> "
  25. << boost::core::demangle(typeid(result_type).name()) << '\n';
  26. static_assert(
  27. boost::safe_numerics::is_safe<safe_t<T1> >::value,
  28. "safe_t not safe!"
  29. );
  30. static_assert(
  31. boost::safe_numerics::is_safe<result_type>::value,
  32. "Expression failed to return safe type"
  33. );
  34. try{
  35. // use auto to avoid checking assignment.
  36. auto result = t1 / v2;
  37. std::cout << make_result_display(result);
  38. if(expected_result == 'x'){
  39. std::cout << " *** failed to detect error in division" << std::endl;
  40. t1 / v2;
  41. return false;
  42. }
  43. std::cout << std::endl;
  44. }
  45. catch(const std::exception &){
  46. if(expected_result == '.'){
  47. std::cout << " *** erroneously detected error in division" << std::endl;
  48. try{
  49. t1 / v2;
  50. }
  51. catch(const std::exception &){}
  52. return false;
  53. }
  54. }
  55. }
  56. {
  57. safe_t<T2> t2 = v2;
  58. using result_type = decltype(v1 / t2);
  59. std::cout
  60. << "safe<" << av1 << "> / " << av2 << " -> "
  61. << boost::core::demangle(typeid(result_type).name()) << '\n';
  62. static_assert(
  63. boost::safe_numerics::is_safe<safe_t<T2> >::value,
  64. "safe_t not safe!"
  65. );
  66. static_assert(
  67. boost::safe_numerics::is_safe<result_type>::value,
  68. "Expression failed to return safe type"
  69. );
  70. try{
  71. // use auto to avoid checking assignment.
  72. auto result = v1 / t2;
  73. std::cout << make_result_display(result);
  74. if(expected_result == 'x'){
  75. std::cout << " *** failed to detect error in division " << std::endl;
  76. v1 / t2;
  77. return false;
  78. }
  79. std::cout << std::endl;
  80. }
  81. catch(const std::exception &){
  82. if(expected_result == '.'){
  83. std::cout << "*** erroneously detected error in division " << std::endl;
  84. try{
  85. v1 / t2;
  86. }
  87. catch(const std::exception &){}
  88. // assert(result == unsafe_result);
  89. return false;
  90. }
  91. }
  92. }
  93. {
  94. safe_t<T1> t1 = v1;
  95. safe_t<T2> t2 = v2;
  96. using result_type = decltype(t1 / t2);
  97. std::cout
  98. << "testing safe<" << av1 << "> / safe<" << av2 << "> -> "
  99. << boost::core::demangle(typeid(result_type).name()) << '\n';
  100. static_assert(
  101. boost::safe_numerics::is_safe<result_type>::value,
  102. "Expression failed to return safe type"
  103. );
  104. try{
  105. // use auto to avoid checking assignment.
  106. auto result = t1 / t2;
  107. std::cout << make_result_display(result);
  108. if(expected_result == 'x'){
  109. std::cout << " *** failed to detect error in division" << std::endl;
  110. t1 / t2;
  111. return false;
  112. }
  113. std::cout << std::endl;
  114. }
  115. catch(const std::exception &){
  116. if(expected_result == '.'){
  117. std::cout << " *** erroneously detected error in division \\" << std::endl;
  118. try{
  119. t1 / t2;
  120. }
  121. catch(const std::exception &){}
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. #endif // BOOST_TEST_DIVIDE