• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_TEST_ADD_HPP
2 #define BOOST_TEST_ADD_HPP
3 
4 //  Copyright (c) 2015 Robert Ramey
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <iostream>
11 
12 #include <boost/safe_numerics/safe_integer.hpp>
13 #include <boost/safe_numerics/range_value.hpp>
14 
15 template<class T1, class T2>
test_add(T1 v1,T2 v2,const char * av1,const char * av2,char expected_result)16 bool test_add(
17     T1 v1,
18     T2 v2,
19     const char *av1,
20     const char *av2,
21     char expected_result
22 ){
23     std::cout << "testing"<< std::endl;
24     {
25         safe_t<T1> t1 = v1;
26         std::cout << "safe<" << av1 << "> + " << av2 << " -> ";
27         static_assert(
28             boost::safe_numerics::is_safe<safe_t<T1> >::value,
29             "safe_t not safe!"
30         );
31         try{
32             // use auto to avoid checking assignment.
33             auto result = t1 + v2;
34             static_assert(
35                 boost::safe_numerics::is_safe<decltype(result)>::value,
36                 "Expression failed to return safe type"
37             );
38             std::cout << make_result_display(result);
39             if(expected_result == 'x'){
40                 std::cout
41                     << " ! = "<< av1 << " + " << av2
42                     << " failed to detect error in addition "
43                     << std::endl;
44                 t1 + v2;
45                 return false;
46             }
47             std::cout << std::endl;
48         }
49         catch(const std::exception &){
50             if(expected_result == '.'){
51                 std::cout
52                     << " == "<< av1 << " + " << av2
53                     << " erroneously detected error in addition "
54                     << std::endl;
55                 try{
56                     t1 + v2;
57                 }
58                 catch(const std::exception &){}
59                 return false;
60             }
61             std::cout << std::endl;
62         }
63     }
64     {
65         safe_t<T2> t2 = v2;
66         using result_type = decltype(v1 + t2);
67         std::cout << av1 << " + " << "safe<" << av2 << "> -> ";
68         static_assert(
69             boost::safe_numerics::is_safe<safe_t<T2> >::value,
70             "safe_t not safe!"
71         );
72         static_assert(
73             boost::safe_numerics::is_safe<result_type>::value,
74             "Expression failed to return safe type"
75         );
76         try{
77             // use auto to avoid checking assignment.
78             auto result = v1 + t2;
79             std::cout << make_result_display(result);
80             if(expected_result == 'x'){
81                 std::cout
82                     << " ! = "<< av1 << " + " << av2
83                     << " failed to detect error in addition "
84                     << std::endl;
85                 v1 + t2;
86                 return false;
87             }
88             std::cout << std::endl;
89         }
90         catch(const std::exception &){
91             if(expected_result == '.'){
92                 std::cout
93                     << " == "<< av1 << " + " << av2
94                     << " erroneously detected error in addition "
95                     << std::endl;
96                 try{
97                     v1 + t2;
98                 }
99                 catch(const std::exception &){}
100                 return false;
101             }
102             std::cout << std::endl;
103         }
104     }
105     {
106         safe_t<T1> t1 = v1;
107         safe_t<T2> t2 = v2;
108         using result_type = decltype(t1 + t2);
109         std::cout << "safe<" << av1 << "> + " << "safe<" << av2 << "> -> ";
110         static_assert(
111             boost::safe_numerics::is_safe<result_type>::value,
112             "Expression failed to return safe type"
113         );
114 
115         try{
116             // use auto to avoid checking assignment.
117             auto result = t1 + t2;
118             std::cout << make_result_display(result);
119             if(expected_result == 'x'){
120                 std::cout
121                     << " ! = "<< av1 << " + " << av2
122                     << " failed to detect error in addition "
123                     << std::endl;
124                 t1 + t2;
125                 return false;
126             }
127             std::cout << std::endl;
128         }
129         catch(const std::exception &){
130             if(expected_result == '.'){
131                 std::cout
132                     << " == "<< av1 << " + " << av2
133                     << " erroneously detected error in addition "
134                     << std::endl;
135                 try{
136                     t1 + t2;
137                 }
138                 catch(const std::exception &){}
139                 return false;
140             }
141             std::cout << std::endl;
142         }
143     }
144     return true; // correct result
145 }
146 
147 #endif // BOOST_TEST_DIVIDE
148