test_multiply.hpp 4.6 KB

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