test_and.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef BOOST_TEST_AND_HPP
  2. #define BOOST_TEST_AND_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. template<class T1, class T2>
  12. bool test_and(
  13. T1 v1,
  14. T2 v2,
  15. const char *av1,
  16. const char *av2,
  17. char expected_result
  18. ){
  19. std::cout << "testing"<< std::endl;
  20. {
  21. safe_t<T1> t1 = v1;
  22. using result_type = decltype(t1 & v2);
  23. std::cout << "safe<" << av1 << "> & " << av2 << " -> ";
  24. static_assert(
  25. boost::safe_numerics::is_safe<safe_t<T1> >::value,
  26. "safe_t not safe!"
  27. );
  28. static_assert(
  29. boost::safe_numerics::is_safe<result_type>::value,
  30. "Expression failed to return safe type"
  31. );
  32. try{
  33. // use auto to avoid checking assignment.
  34. auto result = t1 & v2;
  35. std::cout << make_result_display(result);
  36. if(expected_result == 'x'){
  37. std::cout
  38. << " ! = "<< av1 << " & " << av2
  39. << " failed to detect error in and operation "
  40. << std::endl;
  41. t1 & v2;
  42. return false;
  43. }
  44. std::cout << std::endl;
  45. }
  46. catch(const std::exception &){
  47. if(expected_result == '.'){
  48. std::cout
  49. << " == "<< av1 << " & " << av2
  50. << " erroneously detected error in and operation "
  51. << std::endl;
  52. try{
  53. t1 & v2;
  54. }
  55. catch(const std::exception &){}
  56. return false;
  57. }
  58. }
  59. }
  60. {
  61. safe_t<T2> t2 = v2;
  62. using result_type = decltype(v1 & t2);
  63. std::cout << av1 << " & " << "safe<" << av2 << "> -> ";
  64. static_assert(
  65. boost::safe_numerics::is_safe<safe_t<T2> >::value,
  66. "safe_t not safe!"
  67. );
  68. static_assert(
  69. boost::safe_numerics::is_safe<result_type>::value,
  70. "Expression failed to return safe type"
  71. );
  72. try{
  73. // use auto to avoid checking assignment.
  74. auto result = v1 & t2;
  75. std::cout << make_result_display(result);
  76. if(expected_result == 'x'){
  77. std::cout
  78. << " ! = "<< av1 << " & " << av2
  79. << " failed to detect error in and operation "
  80. << std::endl;
  81. v1 & t2;
  82. return false;
  83. }
  84. std::cout << std::endl;
  85. }
  86. catch(const std::exception &){
  87. if(expected_result == '.'){
  88. std::cout
  89. << " == "<< av1 << " & " << av2
  90. << " erroneously detected error in and operation "
  91. << std::endl;
  92. try{
  93. v1 & t2;
  94. }
  95. catch(const std::exception &){}
  96. return false;
  97. }
  98. }
  99. }
  100. {
  101. safe_t<T1> t1 = v1;
  102. safe_t<T2> t2 = v2;
  103. using result_type = decltype(t1 & t2);
  104. std::cout << "safe<" << av1 << "> & " << "safe<" << av2 << "> -> ";
  105. static_assert(
  106. boost::safe_numerics::is_safe<result_type>::value,
  107. "Expression failed to return safe type"
  108. );
  109. try{
  110. // use auto to avoid checking assignment.
  111. auto result = t1 & t2;
  112. std::cout << make_result_display(result);
  113. if(expected_result == 'x'){
  114. std::cout
  115. << " != "<< av1 << " & " << av2
  116. << " failed to detect error in and operation"
  117. << std::endl;
  118. t1 & t2;
  119. return false;
  120. }
  121. std::cout << std::endl;
  122. }
  123. catch(const std::exception &){
  124. if(expected_result == '.'){
  125. std::cout
  126. << " == "<< av1 << " & " << av2
  127. << " erroneously detected error in and operation"
  128. << std::endl;
  129. try{
  130. t1 & t2;
  131. }
  132. catch(const std::exception &){}
  133. return false;
  134. }
  135. }
  136. }
  137. return true; // correct result
  138. }
  139. #endif // BOOST_TEST_AND_HPP