]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/test/test_construction.cpp
1 // Copyright (c) 2012 Robert Ramey
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/safe_numerics/safe_compare.hpp>
12 #include <boost/safe_numerics/safe_integer.hpp>
14 template<class T2
, class T1
>
15 bool test_construction(T1 t1
, const char *t2_name
, const char *t1_name
){
16 using namespace boost::safe_numerics
;
18 << "testing constructions to " << t2_name
<< " from " << t1_name
21 /* (1) test construction of safe<T1> from T1 type */
24 // should always arrive here!
26 catch(const std::exception
&){
27 // should never, ever arrive here
29 << "erroneously detected error in construction "
30 << "safe<" << t1_name
<< "> (" << t1_name
<< ")"
33 safe
<T1
> s1(t1
); // try again for debugging
35 catch(const std::exception
&){}
40 /* (2) test construction of safe<T2> from T1 type */
44 if(! safe_compare::equal(t2
, t1
)){
46 << "failed to detect error in construction "
47 << "safe<" << t2_name
<< "> (" << t1_name
<< ")"
53 catch(const std::exception
&){
54 if(safe_compare::equal(t2
, t1
)){
56 << "erroneously detected error in construction "
57 << "safe<" << t2_name
<< "> (" << t1_name
<< ")"
60 safe
<T2
> sx2(t1
); // try again for debugging
62 catch(const std::exception
&){}
68 /* (3) test construction of safe<T1> from safe<T1> type */
74 << "copy constructor altered value "
75 << "safe<" << t1_name
<< "> (safe<" << t1_name
<< ">(" << t1
<< "))"
81 catch(const std::exception
&){
82 // should never arrive here
84 << "erroneously detected error in construction "
85 << "safe<" << t1_name
<< "> (safe<" << t1_name
<< ">(" << t1
<< "))"
90 catch(const std::exception
&){}
95 /* (4) test construction of safe<T2> from safe<T1> type */
100 t2
= static_cast<T2
>(s2
);
101 if(! (safe_compare::equal(t1
, t2
))){
103 << "failed to detect error in construction "
104 << "safe<" << t1_name
<< "> (safe<" << t2_name
<< ">(" << t1
<< "))"
110 catch(const std::exception
&){
111 if(safe_compare::equal(t1
, t2
)){
113 << "erroneously detected error in construction "
114 << "safe<" << t2_name
<< "> (safe<" << t1_name
<< ">(" << t1
<< "))"
119 catch(const std::exception
&){}
127 #include <boost/mp11/algorithm.hpp>
128 #include <boost/core/demangle.hpp>
129 #include "test_values.hpp"
131 using namespace boost::mp11
;
135 test(bool b
= true) : m_error(b
) {}
140 void operator()(const T
&){
141 static_assert(mp_is_list
<T
>(), "must be a list of two types");
142 using T1
= mp_first
<T
>; // first element is a type
143 // second element is an integral constant
144 using T2
= typename mp_second
<T
>::value_type
; // get it's type
145 constexpr T2 v2
= mp_second
<T
>(); // get it's value
146 m_error
&= test_construction
<T1
>(
148 boost::core::demangle(typeid(T1
).name()).c_str(),
149 boost::core::demangle(typeid(T2
).name()).c_str()
155 using extract_value_type
= typename
T::value_type
;
156 using test_types
= mp_unique
<
167 mp_product
<mp_list
, test_types
, test_values
>
170 std::cout
<< (rval
? "success!" : "failure") << std::endl
;