| 1 | // Copyright (c) 2012 Robert Ramey |
| 2 | // |
| 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) |
| 6 | |
| 7 | #include <boost/safe_numerics/checked_result.hpp> |
| 8 | #include <boost/safe_numerics/checked_result_operations.hpp> |
| 9 | #include <boost/safe_numerics/checked_integer.hpp> |
| 10 | |
| 11 | // note: T should be of tyme checked_result<R> for some integer type R |
| 12 | template<class T> |
| 13 | constexpr bool test_checked_subtract( |
| 14 | const T & v1, |
| 15 | const T & v2, |
| 16 | char expected_result |
| 17 | ){ |
| 18 | using namespace boost::safe_numerics; |
| 19 | const T result = v1 - v2; |
| 20 | switch(expected_result){ |
| 21 | case '.': |
| 22 | if(result.exception()){ |
| 23 | return false; |
| 24 | } |
| 25 | return true; |
| 26 | case '-': |
| 27 | if(safe_numerics_error::negative_overflow_error == result.m_e) |
| 28 | return true; |
| 29 | case '+': |
| 30 | if(safe_numerics_error::positive_overflow_error == result.m_e) |
| 31 | return true; |
| 32 | case '!': |
| 33 | if(safe_numerics_error::range_error == result.m_e) |
| 34 | return true; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | #include "test_checked_subtract.hpp" |
| 40 | |
| 41 | template<typename T, typename First, typename Second> |
| 42 | struct test_signed_pair { |
| 43 | static const std::size_t i = First(); |
| 44 | static const std::size_t j = Second(); |
| 45 | // note: is constexpr really required here? compilers disagree! |
| 46 | constexpr static const bool value = test_checked_subtract( |
| 47 | signed_values<T>[i], |
| 48 | signed_values<T>[j], |
| 49 | signed_subtraction_results[i][j] |
| 50 | ); |
| 51 | }; |
| 52 | |
| 53 | template<typename T, typename First, typename Second> |
| 54 | struct test_unsigned_pair { |
| 55 | static const std::size_t i = First(); |
| 56 | static const std::size_t j = Second(); |
| 57 | // note: is constexpr really required here? compilers disagree! |
| 58 | constexpr static const bool value = test_checked_subtract( |
| 59 | unsigned_values<T>[i], |
| 60 | unsigned_values<T>[j], |
| 61 | unsigned_subtraction_results[i][j] |
| 62 | ); |
| 63 | }; |
| 64 | |
| 65 | #include <boost/mp11/algorithm.hpp> |
| 66 | |
| 67 | int main(){ |
| 68 | using namespace boost::mp11; |
| 69 | |
| 70 | static_assert( |
| 71 | mp_all_of< |
| 72 | mp_product< |
| 73 | test_signed_pair, |
| 74 | signed_test_types, |
| 75 | signed_value_indices, signed_value_indices |
| 76 | >, |
| 77 | mp_to_bool |
| 78 | >(), |
| 79 | "all values for all signed types correctly subtracted" |
| 80 | ); |
| 81 | |
| 82 | static_assert( |
| 83 | mp_all_of< |
| 84 | mp_product< |
| 85 | test_unsigned_pair, |
| 86 | unsigned_test_types, |
| 87 | unsigned_value_indices, unsigned_value_indices |
| 88 | >, |
| 89 | mp_to_bool |
| 90 | >(), |
| 91 | "all values for all unsigned types correctly subtracted" |
| 92 | ); |
| 93 | return 0; |
| 94 | } |
| 95 | |