• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static_assert(
71         check_symmetry(signed_or_results),
72         "sanity check on test matrix - should be symmetrical"
73     );
74     static_assert(
75         check_symmetry(unsigned_or_results),
76         "sanity check on test matrix - should be symmetrical"
77     );
78 
79     static_assert(
80         mp_all_of<
81             mp_product<
82                 test_signed_pair,
83                 signed_test_types,
84                 signed_value_indices, signed_value_indices
85             >,
86             mp_to_bool
87         >(),
88         "all values for all signed types correctly or'ed"
89     );
90 
91     static_assert(
92         mp_all_of<
93             mp_product<
94                 test_unsigned_pair,
95                 unsigned_test_types,
96                 unsigned_value_indices, unsigned_value_indices
97             >,
98             mp_to_bool
99         >(),
100         "all values for all unsigned types correctly or'ed"
101     );
102     return 0;
103 }
104