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>
test_checked_or(T v1,T v2,char expected_result)13 constexpr bool test_checked_or(
14     T v1,
15     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_or.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_or(
47         signed_values<T>[i],
48         signed_values<T>[j],
49         signed_or_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_or(
59         unsigned_values<T>[i],
60         unsigned_values<T>[j],
61         unsigned_or_results[i][j]
62     );
63 };
64 
65 #include "check_symmetry.hpp"
66 #include <boost/mp11/algorithm.hpp>
67 
main()68 int main(){
69     using namespace boost::mp11;
70     // sanity check on test matrix - should be symetrical
71     check_symmetry(signed_or_results);
72     check_symmetry(unsigned_or_results);
73 
74     static_assert(
75         mp_all_of<
76             mp_product<
77                 test_signed_pair,
78                 signed_test_types,
79                 signed_value_indices, signed_value_indices
80             >,
81             mp_to_bool
82         >(),
83         "all values for all signed types correctly or'ed"
84     );
85 
86     static_assert(
87         mp_all_of<
88             mp_product<
89                 test_unsigned_pair,
90                 unsigned_test_types,
91                 unsigned_value_indices, unsigned_value_indices
92             >,
93             mp_to_bool
94         >(),
95         "all values for all unsigned types correctly or'ed"
96     );
97     return 0;
98 }
99