test_auto.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <iostream>
  2. #include <cassert>
  3. #include <boost/core/demangle.hpp>
  4. #include <boost/safe_numerics/safe_integer.hpp>
  5. #include <boost/safe_numerics/safe_integer_range.hpp>
  6. #include <boost/safe_numerics/automatic.hpp>
  7. #include <boost/safe_numerics/utility.hpp>
  8. int test_log(){
  9. using namespace boost::safe_numerics::utility;
  10. assert(ilog2(127u) == 6);
  11. assert(ilog2(128u) == 7);
  12. assert(ilog2(129u) == 7);
  13. assert(ilog2(255u) == 7);
  14. assert(ilog2(256u) == 8);
  15. assert(ilog2(127) == 6);
  16. assert(ilog2(128) == 7);
  17. assert(ilog2(129) == 7);
  18. assert(ilog2(255) == 7);
  19. assert(ilog2(256) == 8);
  20. return 0;
  21. }
  22. template<class T>
  23. void print_argument_type(const T & t){
  24. const std::type_info & ti = typeid(T);
  25. std::cout
  26. << boost::core::demangle(ti.name()) << ' ' << t << std::endl;
  27. }
  28. template<typename T, typename U>
  29. int test_auto(const T & t, const U & u){
  30. using namespace boost::safe_numerics;
  31. try{
  32. safe<T, automatic>(t) + u;
  33. }
  34. catch(const std::exception &){
  35. safe<T, automatic>(t) + u;
  36. }
  37. try{
  38. t + safe<U, automatic>(u);
  39. }
  40. catch(const std::exception &){
  41. t + safe<U, automatic>(u);
  42. }
  43. return 0;
  44. }
  45. int test_auto_result(){
  46. using namespace boost::safe_numerics;
  47. automatic::addition_result<
  48. safe<std::int8_t, automatic>,
  49. safe<std::uint16_t, automatic>
  50. >::type r1;
  51. print_argument_type(r1);
  52. automatic::addition_result<
  53. safe_signed_range<-3, 8, automatic>,
  54. safe_signed_range<-4, 9, automatic>
  55. >::type r2;
  56. print_argument_type(r2);
  57. return 0;
  58. }
  59. int test_compare_result(){
  60. using namespace boost::safe_numerics;
  61. automatic::comparison_result<
  62. safe<std::int16_t, automatic>,
  63. safe<std::int16_t, automatic>
  64. >::type r1;
  65. print_argument_type(r1);
  66. return 0;
  67. }
  68. int main(){
  69. using namespace boost::safe_numerics;
  70. test_log();
  71. test_auto<std::int8_t, std::int8_t>(1, -128);
  72. test_auto_result();
  73. test_compare_result();
  74. return 0;
  75. }