test_add.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef BOOST_TEST_ADD_HPP
  2. #define BOOST_TEST_ADD_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_add(
  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. std::cout << "safe<" << av1 << "> + " << av2 << " -> ";
  23. static_assert(
  24. boost::safe_numerics::is_safe<safe_t<T1> >::value,
  25. "safe_t not safe!"
  26. );
  27. try{
  28. // use auto to avoid checking assignment.
  29. auto result = t1 + v2;
  30. static_assert(
  31. boost::safe_numerics::is_safe<decltype(result)>::value,
  32. "Expression failed to return safe type"
  33. );
  34. std::cout << make_result_display(result);
  35. if(expected_result == 'x'){
  36. std::cout
  37. << " ! = "<< av1 << " + " << av2
  38. << " failed to detect error in addition "
  39. << 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
  48. << " == "<< av1 << " + " << av2
  49. << " erroneously detected error in addition "
  50. << std::endl;
  51. try{
  52. t1 + v2;
  53. }
  54. catch(const std::exception &){}
  55. return false;
  56. }
  57. std::cout << std::endl;
  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 addition "
  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 addition "
  91. << std::endl;
  92. try{
  93. v1 + t2;
  94. }
  95. catch(const std::exception &){}
  96. return false;
  97. }
  98. std::cout << std::endl;
  99. }
  100. }
  101. {
  102. safe_t<T1> t1 = v1;
  103. safe_t<T2> t2 = v2;
  104. using result_type = decltype(t1 + t2);
  105. std::cout << "safe<" << av1 << "> + " << "safe<" << av2 << "> -> ";
  106. static_assert(
  107. boost::safe_numerics::is_safe<result_type>::value,
  108. "Expression failed to return safe type"
  109. );
  110. try{
  111. // use auto to avoid checking assignment.
  112. auto result = t1 + t2;
  113. std::cout << make_result_display(result);
  114. if(expected_result == 'x'){
  115. std::cout
  116. << " ! = "<< av1 << " + " << av2
  117. << " failed to detect error in addition "
  118. << std::endl;
  119. t1 + t2;
  120. return false;
  121. }
  122. std::cout << std::endl;
  123. }
  124. catch(const std::exception &){
  125. if(expected_result == '.'){
  126. std::cout
  127. << " == "<< av1 << " + " << av2
  128. << " erroneously detected error in addition "
  129. << std::endl;
  130. try{
  131. t1 + t2;
  132. }
  133. catch(const std::exception &){}
  134. return false;
  135. }
  136. std::cout << std::endl;
  137. }
  138. }
  139. return true; // correct result
  140. }
  141. #endif // BOOST_TEST_DIVIDE