• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2018 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_and(const T & v1,const T & v2,char expected_result)13 constexpr bool test_checked_and(
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         return ! result.exception();
23     case '-':
24         return safe_numerics_error::negative_overflow_error == result.m_e;
25     case '+':
26         return safe_numerics_error::positive_overflow_error == result.m_e;
27     case '!':
28         return safe_numerics_error::range_error == result.m_e;
29     }
30     return false;
31 }
32 
33 #include "test_checked_and.hpp"
34 
35 template<typename T, typename First, typename Second>
36 struct test_signed_pair {
37     static const std::size_t i = First();
38     static const std::size_t j = Second();
39     // note: is constexpr really required here?  compilers disagree!
40     constexpr static const bool value = test_checked_and(
41         signed_values<T>[i],
42         signed_values<T>[j],
43         signed_and_results[i][j]
44     );
45 };
46 
47 template<typename T, typename First, typename Second>
48 struct test_unsigned_pair {
49     static const std::size_t i = First();
50     static const std::size_t j = Second();
51     // note: is constexpr really required here?  compilers disagree!
52     constexpr static const bool value = test_checked_and(
53         unsigned_values<T>[i],
54         unsigned_values<T>[j],
55         unsigned_and_results[i][j]
56     );
57 };
58 
59 #include "check_symmetry.hpp"
60 #include <boost/mp11/algorithm.hpp>
61 
main()62 int main(){
63     static_assert(
64         check_symmetry(signed_and_results),
65         "sanity check on test matrix - should be symmetrical"
66     );
67     static_assert(
68         check_symmetry(unsigned_and_results),
69         "sanity check on test matrix - should be symmetrical"
70     );
71 
72     using namespace boost::mp11;
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 anded"
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 anded"
96     );
97     return 0;
98 }
99