| 1 | #include <iostream> |
| 2 | #include <cassert> |
| 3 | #include <limits> |
| 4 | |
| 5 | #include <boost/safe_numerics/utility.hpp> |
| 6 | #include <boost/safe_numerics/safe_integer_range.hpp> |
| 7 | |
| 8 | template<typename T> |
| 9 | void display_log(T Max){ |
| 10 | std::cout |
| 11 | << "ilog2(" << Max << ") = " |
| 12 | << boost::safe_numerics::utility::ilog2(Max) << std::endl; |
| 13 | } |
| 14 | |
| 15 | bool test_significant_bits(){ |
| 16 | using namespace boost::safe_numerics; |
| 17 | assert(utility::significant_bits(127u) == 7); // 7 bits |
| 18 | assert(utility::significant_bits(127u) == 7); // 7 bits |
| 19 | assert(utility::significant_bits(128u) == 8); // 8 bits |
| 20 | assert(utility::significant_bits(129u) == 8); // 8 bits |
| 21 | assert(utility::significant_bits(255u) == 8); // 8 bits |
| 22 | assert(utility::significant_bits(256u) == 9); // 9 bits |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | bool test1(){ |
| 27 | using namespace boost::safe_numerics; |
| 28 | using t2 = safe_unsigned_range<0u, 1000u>; |
| 29 | static_assert( |
| 30 | std::numeric_limits<t2>::is_signed == false, |
| 31 | "this range should be unsigned" |
| 32 | ); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | #include <boost/safe_numerics/automatic.hpp> |
| 37 | |
| 38 | template < |
| 39 | std::intmax_t Min, |
| 40 | std::intmax_t Max |
| 41 | > |
| 42 | using safe_t = boost::safe_numerics::safe_signed_range< |
| 43 | Min, |
| 44 | Max, |
| 45 | boost::safe_numerics::automatic, |
| 46 | boost::safe_numerics::default_exception_policy |
| 47 | >; |
| 48 | |
| 49 | bool test2(){ |
| 50 | std::cout << "test1" << std::endl; |
| 51 | try{ |
| 52 | const safe_t<-64, 63> x(1); |
| 53 | safe_t<-64, 63> y; |
| 54 | y = 2; |
| 55 | std::cout << "x = " << x << std::endl; |
| 56 | std::cout << "y = " << y << std::endl; |
| 57 | auto z = x + y; |
| 58 | std::cout << "x + y = [" |
| 59 | << std::numeric_limits<decltype(z)>::min() << "," |
| 60 | << std::numeric_limits<decltype(z)>::max() << "] = " |
| 61 | << z << std::endl; |
| 62 | |
| 63 | auto z2 = x - y; |
| 64 | std::cout << "x - y = [" |
| 65 | << std::numeric_limits<decltype(z2)>::min() << "," |
| 66 | << std::numeric_limits<decltype(z2)>::max() << "] = " |
| 67 | << z2 << std::endl; |
| 68 | |
| 69 | short int yi, zi; |
| 70 | yi = y; |
| 71 | zi = x + yi; |
| 72 | } |
| 73 | catch(const std::exception & e){ |
| 74 | // none of the above should trap. Mark failure if they do |
| 75 | std::cout << e.what() << std::endl; |
| 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | int main(){ |
| 82 | //using namespace boost::safe_numerics; |
| 83 | //safe_signed_literal2<100> one_hundred; |
| 84 | //one_hundred = 200; |
| 85 | |
| 86 | bool rval = |
| 87 | test_significant_bits() && |
| 88 | test1() && |
| 89 | test2() /* && |
| 90 | test3() && |
| 91 | test4() |
| 92 | */ |
| 93 | ; |
| 94 | std::cout << (rval ? "success!" : "failure" ) << std::endl; |
| 95 | return rval ? EXIT_SUCCESS : EXIT_FAILURE; |
| 96 | } |
| 97 | |