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)
9 #include <boost/core/demangle.hpp>
10 #include <boost/safe_numerics/checked_result_operations.hpp>
11 #include <boost/safe_numerics/checked_integer.hpp>
13 // note: T should be of type checked_result<R> for some integer type R
15 bool test_checked_add(
20 using namespace boost::safe_numerics
;
21 const T result
= v1
+ v2
;
23 << "testing " << boost::core::demangle(typeid(T
).name()) << ' '
24 << v1
<< " + " << v2
<< " -> " << result
27 switch(expected_result
){
29 if(result
.exception()){
31 << "erroneously detected error in addition "
38 if(safe_numerics_error::negative_overflow_error
== result
.m_e
)
42 if(safe_numerics_error::positive_overflow_error
== result
.m_e
)
46 if(safe_numerics_error::range_error
== result
.m_e
)
51 << "failed to detect error in addition "
52 << std::hex
<< result
<< "(" << std::dec
<< result
<< ")"
53 << " != "<< v1
<< " + " << v2
59 #include "test_checked_add.hpp"
61 template<typename T
, typename First
, typename Second
>
62 struct test_signed_pair
{
63 bool operator()() const {
64 std::size_t i
= First();
65 std::size_t j
= Second();
66 std::cout
<< std::dec
<< i
<< ',' << j
<< ','
67 << "testing " << boost::core::demangle(typeid(T
).name()) << ' ';
68 return test_checked_add(
71 signed_addition_results
[i
][j
]
76 template<typename T
, typename First
, typename Second
>
77 struct test_unsigned_pair
{
78 bool operator()() const {
79 std::size_t i
= First();
80 std::size_t j
= Second();
81 std::cout
<< std::dec
<< i
<< ',' << j
<< ','
82 << "testing " << boost::core::demangle(typeid(T
).name()) << ' ';
83 return test_checked_add(
84 unsigned_values
<T
>[i
],
85 unsigned_values
<T
>[j
],
86 unsigned_addition_results
[i
][j
]
91 #include "check_symmetry.hpp"
93 #include <boost/mp11/algorithm.hpp>
96 // sanity check on test matrix - should be symetrical
97 check_symmetry(signed_addition_results
);
98 check_symmetry(unsigned_addition_results
);
100 using namespace boost::mp11
;
107 signed_value_indices
,
118 unsigned_value_indices
, unsigned_value_indices
124 std::cout
<< (rval
? "success!" : "failure") << std::endl
;