1 #ifndef BOOST_TEST_AND_HPP
2 #define BOOST_TEST_AND_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_and(T1 v1,T2 v2,const char * av1,const char * av2,char expected_result)16 bool test_and(
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 using result_type = decltype(t1 & v2);
27 std::cout << "safe<" << av1 << "> & " << av2 << " -> ";
28 static_assert(
29 boost::safe_numerics::is_safe<safe_t<T1> >::value,
30 "safe_t not safe!"
31 );
32 static_assert(
33 boost::safe_numerics::is_safe<result_type>::value,
34 "Expression failed to return safe type"
35 );
36 try{
37 // use auto to avoid checking assignment.
38 auto result = t1 & v2;
39 std::cout << make_result_display(result);
40 if(expected_result == 'x'){
41 std::cout
42 << " ! = "<< av1 << " & " << av2
43 << " failed to detect error in and operation "
44 << std::endl;
45 t1 & v2;
46 return false;
47 }
48 std::cout << std::endl;
49 }
50 catch(const std::exception &){
51 if(expected_result == '.'){
52 std::cout
53 << " == "<< av1 << " & " << av2
54 << " erroneously detected error in and operation "
55 << std::endl;
56 try{
57 t1 & v2;
58 }
59 catch(const std::exception &){}
60 return false;
61 }
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
77 try{
78 // use auto to avoid checking assignment.
79 auto result = v1 & t2;
80 std::cout << make_result_display(result);
81 if(expected_result == 'x'){
82 std::cout
83 << " ! = "<< av1 << " & " << av2
84 << " failed to detect error in and operation "
85 << std::endl;
86 v1 & t2;
87 return false;
88 }
89 std::cout << std::endl;
90 }
91 catch(const std::exception &){
92 if(expected_result == '.'){
93 std::cout
94 << " == "<< av1 << " & " << av2
95 << " erroneously detected error in and operation "
96 << std::endl;
97 try{
98 v1 & t2;
99 }
100 catch(const std::exception &){}
101 return false;
102 }
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 and operation"
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 and operation"
134 << std::endl;
135 try{
136 t1 & t2;
137 }
138 catch(const std::exception &){}
139 return false;
140 }
141 }
142 }
143 return true; // correct result
144 }
145
146 #endif // BOOST_TEST_AND_HPP
147