test_assignment.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef BOOST_TEST_ASSIGNMENT_HPP
  2. #define BOOST_TEST_ASSIGNMENT_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_assignment(
  14. T1 t1,
  15. T2 t2,
  16. const char *at1,
  17. const char *at2,
  18. char expected_result
  19. ){
  20. std::cout << "testing " << std::endl;
  21. {
  22. std::cout << "safe<" << at1 << "> = " << at2 << std::endl;
  23. // std::cout << boost::core::demangle(typeid(t1).name()) << " = " << at2 << std::endl;
  24. safe_t<T2> s2(t2);
  25. static_assert(
  26. boost::safe_numerics::is_safe<safe_t<T1> >::value,
  27. "safe_t not safe!"
  28. );
  29. try{
  30. t1 = s2;
  31. if(expected_result == 'x'){
  32. std::cout
  33. << "failed to detect error in assignment "
  34. << boost::core::demangle(typeid(t1).name())
  35. << " = "
  36. << at2
  37. << std::endl;
  38. t1 = s2;
  39. return false;
  40. }
  41. }
  42. catch(const std::exception &){
  43. if(expected_result == '.'){
  44. std::cout
  45. << "erroneously detected error in assignment "
  46. << boost::core::demangle(typeid(t1).name())
  47. << " = "
  48. << at2
  49. << std::endl;
  50. try{
  51. t1 = s2;
  52. }
  53. catch(const std::exception &){}
  54. return false;
  55. }
  56. }
  57. }
  58. {
  59. safe_t<T1> s1(t1);
  60. safe_t<T2> s2(t2);
  61. std::cout
  62. << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
  63. << " = "
  64. << "safe<" << at2 << '>' << std::endl;
  65. try{
  66. s1 = s2;
  67. if(expected_result == 'x'){
  68. std::cout
  69. << "failed to detect error in assignment "
  70. << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
  71. << " = "
  72. << at2
  73. << std::endl;
  74. s1 = s2;
  75. return false;
  76. }
  77. }
  78. catch(const std::exception &){
  79. if(expected_result == '.'){
  80. std::cout
  81. << "erroneously detected error in assignment "
  82. << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
  83. << " = "
  84. << at2
  85. << std::endl;
  86. try{
  87. s1 = t2;
  88. }
  89. catch(const std::exception &){}
  90. return false;
  91. }
  92. }
  93. }
  94. return true; // correct result
  95. }
  96. #endif // BOOST_TEST_ASSIGNMENT_HPP