• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_TEST_XOR_HPP
2 #define BOOST_TEST_XOR_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 #include <iosfwd>
12 
13 #include <boost/safe_numerics/safe_integer.hpp>
14 #include <boost/safe_numerics/range_value.hpp>
15 
16 template<class T1, class T2>
test_xor(T1 v1,T2 v2,const char * av1,const char * av2,char expected_result)17 bool test_xor(
18     T1 v1,
19     T2 v2,
20     const char *av1,
21     const char *av2,
22     char expected_result
23 ){
24     std::cout << "testing"<< std::endl;
25     {
26         safe_t<T1> t1 = v1;
27         using result_type = decltype(t1 ^ v2);
28         std::cout << "safe<" << av1 << "> ^ " << av2 << " -> ";
29         static_assert(
30             boost::safe_numerics::is_safe<safe_t<T1> >::value,
31             "safe_t not safe!"
32         );
33         static_assert(
34             boost::safe_numerics::is_safe<result_type>::value,
35             "Expression failed to return safe type"
36         );
37 
38         try{
39             // use auto to avoid checking assignment.
40             auto result = t1 ^ v2;
41             std::cout << make_result_display(result);
42             if(expected_result == 'x'){
43                 std::cout
44                     << " ! = "<< av1 << " ^ " << av2
45                     << " failed to detect error in xor operation"
46                     << std::endl;
47                 t1 ^ v2;
48                 return false;
49             }
50             else
51             if(result != (v1 ^ v2)){
52                 std::cout
53                     << " ! = "<< av1 << " ^ " << av2
54                     << " incorrect result in xor operation"
55                     << std::endl;
56                 t1 ^ v2;
57                 boost::safe_numerics::safe_compare::equal(base_value(result), (v1 ^ v2));
58                 return false;
59             }
60             std::cout << std::endl;
61         }
62         catch(const std::exception &){
63             if(expected_result == '.'){
64                     std::cout
65                         << " == "<< av1 << " ^ " << av2
66                         << " erroneously detected error in xor operation"
67                         << std::endl;
68                 try{
69                     t1 ^ v2;
70                 }
71                 catch(const std::exception &){}
72                 return false;
73             }
74         }
75     }
76     {
77         safe_t<T2> t2 = v2;
78         using result_type = decltype(v1 ^ t2);
79         std::cout << av1 << " ^ " << "safe<" << av2 << "> -> ";
80         static_assert(
81             boost::safe_numerics::is_safe<safe_t<T2> >::value,
82             "safe_t not safe!"
83         );
84         static_assert(
85             boost::safe_numerics::is_safe<result_type>::value,
86             "Expression failed to return safe type"
87         );
88 
89         try{
90             // use auto to avoid checking assignment.
91             auto result = v1 ^ t2;
92             std::cout << make_result_display(result);
93             if(expected_result == 'x'){
94                 std::cout
95                     << " ! = "<< av1 << " ^ " << av2
96                     << " failed to detect error in and operation"
97                     << std::endl;
98                 v1 ^ t2;
99                 return false;
100             }
101             else
102             if(result != (v1 ^ v2)){
103                 std::cout
104                     << " ! = "<< av1 << " ^ " << av2
105                     << " incorrect result in xor operation"
106                     << std::endl;
107                 v1 ^ t2;
108                 return false;
109             }
110             std::cout << std::endl;
111         }
112         catch(const std::exception &){
113             if(expected_result == '.'){
114                 std::cout
115                     << " == "<< av1 << " ^ " << av2
116                     << " erroneously detected error in and operation"
117                     << std::endl;
118                 try{
119                     v1 ^ t2;
120                 }
121                 catch(const std::exception &){}
122                 return false;
123             }
124         }
125     }
126     {
127         safe_t<T1> t1 = v1;
128         safe_t<T2> t2 = v2;
129         using result_type = decltype(t1 ^ t2);
130         std::cout << "safe<" << av1 << "> ^ " << "safe<" << av2 << "> -> ";
131         static_assert(
132             boost::safe_numerics::is_safe<result_type>::value,
133             "Expression failed to return safe type"
134         );
135 
136         try{
137             // use auto to avoid checking assignment.
138             auto result = t1 ^ t2;
139             std::cout << make_result_display(result);
140             if(expected_result == 'x'){
141                 std::cout
142                     << " ! = "<< av1 << " ^ " << av2
143                     << " failed to detect error in and operation"
144                     << std::endl;
145                 t1 ^ t2;
146                 return false;
147             }
148             else
149             if(result != (v1 ^ v2)){
150                 std::cout
151                     << " ! = "<< av1 << " ^ " << av2
152                     << " incorrect result in xor operation"
153                     << std::endl;
154                 t1 ^ t2;
155                 return false;
156             }
157             std::cout << std::endl;
158         }
159         catch(const std::exception &){
160             if(expected_result == '.'){
161                 std::cout
162                     << " == "<< av1 << " ^ " << av2
163                     << " erroneously detected error in and operation"
164                     << std::endl;
165                 try{
166                     t1 ^ t2;
167                 }
168                 catch(const std::exception &){}
169                 return false;
170             }
171         }
172     }
173     return true; // correct result
174 }
175 
176 #endif // BOOST_TEST_XOR_HPP
177