]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/test/test_or.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / safe_numerics / test / test_or.hpp
1 #ifndef BOOST_TEST_OR_HPP
2 #define BOOST_TEST_OR_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>
16 bool test_or(
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
37 try{
38 // use auto to avoid checking assignment.
39 auto result = t1 | v2;
40 std::cout << make_result_display(result);
41 if(expected_result == 'x'){
42 std::cout
43 << " ! = "<< av1 << " | " << av2
44 << " failed to detect error in bitwise or"
45 << std::endl;
46 t1 | v2;
47 return false;
48 }
49 std::cout << std::endl;
50 }
51 catch(const std::exception &){
52 if(expected_result == '.'){
53 std::cout
54 << " != "<< av1 << " | " << av2
55 << " erroneously detected error in bitwise or"
56 << std::endl;
57 try{
58 t1 | v2;
59 }
60 catch(const std::exception &){}
61 return false;
62 }
63 }
64 }
65 {
66 safe_t<T2> t2 = v2;
67 using result_type = decltype(v1 | t2);
68 std::cout << av1 << " | " << "safe<" << av2 << "> -> ";
69 static_assert(
70 boost::safe_numerics::is_safe<safe_t<T2> >::value,
71 "safe_t not safe!"
72 );
73 static_assert(
74 boost::safe_numerics::is_safe<result_type>::value,
75 "Expression failed to return safe type"
76 );
77
78 try{
79 // use auto to avoid checking assignment.
80 auto result = v1 | t2;
81 std::cout << make_result_display(result);
82 if(expected_result == 'x'){
83 std::cout
84 << " ! = "<< av1 << " | " << av2
85 << " failed to detect error in bitwise or"
86 << std::endl;
87 v1 | t2;
88 return false;
89 }
90 std::cout << std::endl;
91 }
92 catch(const std::exception &){
93 if(expected_result == '.'){
94 std::cout
95 << " == "<< av1 << " | " << av2
96 << " erroneously detected error in bitwise or"
97 << std::endl;
98 try{
99 v1 | t2;
100 }
101 catch(const std::exception &){}
102 return false;
103 }
104 }
105 }
106 {
107 safe_t<T1> t1 = v1;
108 safe_t<T2> t2 = v2;
109 using result_type = decltype(t1 | t2);
110 std::cout << "safe<" << av1 << "> | " << "safe<" << av2 << "> -> ";
111 static_assert(
112 boost::safe_numerics::is_safe<result_type>::value,
113 "Expression failed to return safe type"
114 );
115
116 try{
117 // use auto to avoid checking assignment.
118 auto result = t1 | t2;
119 std::cout << make_result_display(result);
120 if(expected_result == 'x'){
121 std::cout
122 << " ! = "<< av1 << " | " << av2
123 << " failed to detect error in bitwise or"
124 << std::endl;
125 t1 | t2;
126 return false;
127 }
128 std::cout << std::endl;
129 }
130 catch(const std::exception &){
131 if(expected_result == '.'){
132 std::cout
133 << " == "<< av1 << " | " << av2
134 << " erroneously detected error in bitwise or"
135 << std::endl;
136 try{
137 t1 | t2;
138 }
139 catch(const std::exception &){}
140 return false;
141 }
142 }
143 }
144 return true; // correct result
145 }
146
147 #endif // BOOST_TEST_DIVIDE