test0.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2012 Robert Ramey
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <boost/safe_numerics/safe_integer_range.hpp>
  8. #include <boost/safe_numerics/safe_integer.hpp>
  9. bool test1(){
  10. std::cout << "test1" << std::endl;
  11. boost::safe_numerics::safe_signed_range<-64, 63> x, y, z;
  12. x = 1;
  13. y = 2;
  14. z = 3;
  15. z = x + y;
  16. z = x - y;
  17. try{
  18. short int yi, zi;
  19. yi = y;
  20. zi = x + yi;
  21. }
  22. catch(const std::exception & e){
  23. // none of the above should trap. Mark failure if they do
  24. std::cout << e.what() << std::endl;
  25. return false;
  26. }
  27. return true;
  28. }
  29. bool test2(){
  30. std::cout << "test2" << std::endl;
  31. boost::safe_numerics::safe_unsigned_range<0, 64> x, y, z;
  32. x = 1;
  33. y = 2;
  34. z = 3;
  35. bool success = false;
  36. try{
  37. z = x - y; // should trap here
  38. }
  39. catch(const std::exception & e){
  40. success = true;
  41. }
  42. if(success == false)
  43. return false;
  44. try{
  45. int yi = y;
  46. z = x + yi; // should trap here
  47. }
  48. catch(const std::exception & e){
  49. // none of the above should trap. Mark failure if they do
  50. std::cout << e.what() << std::endl;
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool test3(){
  56. using namespace boost::safe_numerics;
  57. std::cout << "test3" << std::endl;
  58. safe<int> x, y, z;
  59. x = 1;
  60. y = 2;
  61. z = 3;
  62. try{
  63. z = x + y;
  64. z = x - y;
  65. int yi, zi;
  66. zi = x + yi;
  67. z = x + yi;
  68. }
  69. catch(const std::exception & e){
  70. // none of the above should trap. Mark failure if they do
  71. std::cout << e.what() << std::endl;
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool test4(){
  77. std::cout << "test4" << std::endl;
  78. boost::safe_numerics::safe<unsigned int> x, y, z;
  79. x = 1;
  80. y = 2;
  81. z = 3;
  82. z = x + y;
  83. bool success = false;
  84. try{
  85. z = x - y; // should trap here
  86. }
  87. catch(const std::exception & e){
  88. success = true;
  89. }
  90. if(success == false)
  91. return false;
  92. unsigned int yi, zi;
  93. zi = x;
  94. zi = x + yi;
  95. z = x + yi;
  96. zi = x + y;
  97. return true;
  98. }
  99. #include <cstdint>
  100. bool test5(){
  101. std::cout << "test5" << std::endl;
  102. boost::safe_numerics::safe<boost::uint64_t> x, y, z;
  103. x = 1;
  104. y = 2;
  105. z = 3;
  106. z = x + y;
  107. bool success = false;
  108. try{
  109. z = x - y; // should trap here
  110. }
  111. catch(const std::exception & e){
  112. success = true;
  113. }
  114. if(success == false)
  115. return false;
  116. boost::uint64_t yi, zi;
  117. zi = x;
  118. zi = x + yi;
  119. z = x + yi;
  120. zi = x + y;
  121. return true;
  122. }
  123. int main(int, char *[]){
  124. bool rval = (
  125. test1() &&
  126. test2() &&
  127. test3() &&
  128. test4() &&
  129. test5()
  130. );
  131. std::cout << (rval ? "success!" : "failure") << std::endl;
  132. return rval ? EXIT_SUCCESS : EXIT_FAILURE;
  133. }