]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/test/test_subtract.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / safe_numerics / test / test_subtract.hpp
1 #ifndef BOOST_TEST_SUBTRACT_HPP
2 #define BOOST_TEST_SUBTRACT_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_subtract(
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 subtraction "
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 subtraction "
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 subtraction "
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 subtraction "
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 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 subtraction "
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 subtraction "
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_SUBTRACT