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