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