1 #ifndef BOOST_TEST_ASSIGNMENT_HPP
2 #define BOOST_TEST_ASSIGNMENT_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/core/demangle.hpp>
13 #include <boost/safe_numerics/safe_integer.hpp>
14 #include <boost/safe_numerics/range_value.hpp>
15 
16 template<class T1, class T2>
test_assignment(T1 t1,T2 t2,const char * at1,const char * at2,char expected_result)17 bool test_assignment(
18     T1 t1,
19     T2 t2,
20     const char *at1,
21     const char *at2,
22     char expected_result
23 ){
24     std::cout << "testing " << std::endl;
25     {
26         std::cout << "safe<" << at1 << "> = " << at2 << std::endl;
27         // std::cout << boost::core::demangle(typeid(t1).name()) << " = " << at2 << std::endl;
28         safe_t<T2> s2(t2);
29 
30         static_assert(
31             boost::safe_numerics::is_safe<safe_t<T1> >::value,
32             "safe_t not safe!"
33         );
34         try{
35             t1 = s2;
36             if(expected_result == 'x'){
37                     std::cout
38                         << "failed to detect error in assignment "
39                         << boost::core::demangle(typeid(t1).name())
40                         << " = "
41                         << at2
42                         << std::endl;
43                 t1 = s2;
44                 return false;
45             }
46         }
47         catch(const std::exception &){
48             if(expected_result == '.'){
49                     std::cout
50                         << "erroneously detected error in assignment "
51                         << boost::core::demangle(typeid(t1).name())
52                         << " = "
53                         << at2
54                         << std::endl;
55                 try{
56                     t1 = s2;
57                 }
58                 catch(const std::exception &){}
59                 return false;
60             }
61         }
62     }
63     {
64         safe_t<T1> s1(t1);
65         safe_t<T2> s2(t2);
66         std::cout
67             << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
68             << " = "
69             << "safe<" << at2 << '>' << std::endl;
70         try{
71             s1 = s2;
72             if(expected_result == 'x'){
73                 std::cout
74                     << "failed to detect error in assignment "
75                     << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
76                     << " = "
77                     << at2
78                     << std::endl;
79                 s1 = s2;
80                 return false;
81             }
82         }
83         catch(const std::exception &){
84             if(expected_result == '.'){
85                 std::cout
86                     << "erroneously detected error in assignment "
87                     << "safe<" << boost::core::demangle(typeid(t1).name()) << ">"
88                     << " = "
89                     << at2
90                     << std::endl;
91                 try{
92                     s1 = t2;
93                 }
94                 catch(const std::exception &){}
95                 return false;
96             }
97         }
98     }
99     return true; // correct result
100 }
101 
102 #endif // BOOST_TEST_ASSIGNMENT_HPP
103