test_less_than.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2015 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. #ifndef BOOST_TEST_LESS_THAN_HPP
  7. #define BOOST_TEST_LESS_THAN_HPP
  8. #include <iostream>
  9. #include <boost/safe_numerics/safe_integer.hpp>
  10. #include <boost/safe_numerics/range_value.hpp>
  11. // works for both GCC and clang
  12. #pragma GCC diagnostic push
  13. #pragma GCC diagnostic ignored "-Wunused-value"
  14. template<class T1, class T2>
  15. bool test_less_than(
  16. T1 v1,
  17. T2 v2,
  18. const char *av1,
  19. const char *av2,
  20. char expected_result
  21. ){
  22. using namespace boost::safe_numerics;
  23. std::cout << "testing"<< std::boolalpha << std::endl;
  24. {
  25. safe_t<T1> t1 = v1;
  26. std::cout << "safe<" << av1 << "> < " << av2 << " -> ";
  27. static_assert(
  28. boost::safe_numerics::is_safe<safe_t<T1> >::value,
  29. "safe_t not safe!"
  30. );
  31. try{
  32. // use auto to avoid checking assignment.
  33. auto result = t1 < v2;
  34. std::cout << make_result_display(result);
  35. if(expected_result == 'x'){
  36. std::cout
  37. << " ! = "<< av1 << " < " << av2
  38. << " failed to detect error in less than"
  39. << std::endl;
  40. t1 < v2;
  41. return false;
  42. }
  43. if(result != (expected_result == '<')){
  44. std::cout
  45. << " ! = "<< av1 << " < " << av2
  46. << " produced the wrong answer"
  47. << std::endl;
  48. t1 < v2;
  49. return false;
  50. }
  51. std::cout << std::endl;
  52. }
  53. catch(const std::exception &){
  54. if(expected_result != 'x'){
  55. std::cout
  56. << " == "<< av1 << " < " << av2
  57. << " erroneously detected error in less than"
  58. << std::endl;
  59. try{
  60. t1 < v2;
  61. }
  62. catch(const std::exception &){}
  63. return false;
  64. }
  65. std::cout << std::endl;
  66. }
  67. }
  68. {
  69. safe_t<T2> t2 = v2;
  70. std::cout << av1 << " < " << "safe<" << av2 << "> -> ";
  71. static_assert(
  72. boost::safe_numerics::is_safe<safe_t<T2> >::value,
  73. "safe_t not safe!"
  74. );
  75. try{
  76. // use auto to avoid checking assignment.
  77. auto result = v1 < t2;
  78. std::cout << make_result_display(result);
  79. if(expected_result == 'x'){
  80. std::cout
  81. << " ! = "<< av1 << " < " << av2
  82. << " failed to detect error in less than"
  83. << std::endl;
  84. v1 < t2;
  85. return false;
  86. }
  87. if(result != (expected_result == '<')){
  88. std::cout
  89. << " ! = "<< av1 << " < " << av2
  90. << " produced the wrong answer"
  91. << std::endl;
  92. v1 < t2;
  93. return false;
  94. }
  95. std::cout << std::endl;
  96. }
  97. catch(const std::exception &){
  98. if(expected_result != 'x'){
  99. std::cout
  100. << " == "<< av1 << " < " << av2
  101. << " erroneously detected error in less than"
  102. << std::endl;
  103. try{
  104. v1 < t2;
  105. }
  106. catch(const std::exception &){}
  107. return false;
  108. }
  109. std::cout << std::endl;
  110. }
  111. }
  112. {
  113. safe_t<T1> t1 = v1;
  114. safe_t<T2> t2 = v2;
  115. std::cout << "safe<" << av1 << "> < " << "safe<" << av2 << "> -> ";
  116. try{
  117. // use auto to avoid checking assignment.
  118. auto result = t1 < t2;
  119. std::cout << make_result_display(result);
  120. if(expected_result == 'x'){
  121. std::cout
  122. << " ! = "<< av1 << " < " << av2
  123. << " failed to detect error in less than"
  124. << std::endl;
  125. t1 < t2;
  126. return false;
  127. }
  128. if(result != (expected_result == '<')){
  129. std::cout
  130. << " ! = "<< av1 << " < " << av2
  131. << " produced the wrong answer"
  132. << std::endl;
  133. t1 < t2;
  134. return false;
  135. }
  136. std::cout << std::endl;
  137. }
  138. catch(const std::exception &){
  139. if(expected_result == '.'){
  140. std::cout
  141. << " == "<< av1 << " < " << av2
  142. << " erroneously detected error in less than"
  143. << std::endl;
  144. try{
  145. t1 < t2;
  146. }
  147. catch(const std::exception &){}
  148. return false;
  149. }
  150. std::cout << std::endl;
  151. }
  152. }
  153. return true; // correct result
  154. }
  155. #pragma GCC diagnostic pop
  156. #endif // BOOST_TEST_SUBTRACT