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