test_equal.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef BOOST_TEST_LESS_THAN_HPP
  2. #define BOOST_TEST_LESS_THAN_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/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_equal(
  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 equals"
  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 equals"
  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 equals "
  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 equals"
  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. auto result = (t1 == t2);
  118. std::cout << make_result_display(result);
  119. if(expected_result == 'x'){
  120. std::cout
  121. << " ! = "<< av1 << " == " << av2
  122. << " failed to detect error in equals"
  123. << std::endl;
  124. t1 == t2;
  125. return false;
  126. }
  127. if(result != (expected_result == '=')){
  128. std::cout
  129. << " ! = "<< av1 << " == " << av2
  130. << " produced the wrong answer"
  131. << std::endl;
  132. t1 == t2;
  133. return false;
  134. }
  135. std::cout << std::endl;
  136. }
  137. catch(const std::exception &){
  138. if(expected_result == '.'){
  139. std::cout
  140. << " == "<< av1 << " == " << av2
  141. << " erroneously detected error in equals"
  142. << std::endl;
  143. try{
  144. t1 == t2;
  145. }
  146. catch(const std::exception &){}
  147. return false;
  148. }
  149. std::cout << std::endl;
  150. }
  151. }
  152. return true; // correct result
  153. }
  154. #pragma GCC diagnostic pop
  155. #endif // BOOST_TEST_SUBTRACT