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
14template<class T>
15constexpr 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
44template<typename T, typename First, typename Second>
45struct 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
55template<typename T, typename First, typename Second>
56struct 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
70int main(){
71 static_assert(
72 check_symmetry(value: signed_xor_results),
73 "sanity check on test matrix - should be symmetrical"
74 );
75 static_assert(
76 check_symmetry(value: unsigned_xor_results),
77 "sanity check on test matrix - should be symmetrical"
78 );
79
80 using namespace boost::mp11;
81
82 static_assert(
83 mp_all_of<
84 mp_product<
85 test_signed_pair,
86 signed_test_types,
87 signed_value_indices, signed_value_indices
88 >,
89 mp_to_bool
90 >(),
91 "all values for all signed types correctly xor'ed"
92 );
93
94 static_assert(
95 mp_all_of<
96 mp_product<
97 test_unsigned_pair,
98 unsigned_test_types,
99 unsigned_value_indices, unsigned_value_indices
100 >,
101 mp_to_bool
102 >(),
103 "all values for all unsigned types correctly xor'ed"
104 );
105
106 return 0;
107}
108

source code of boost/libs/safe_numerics/test/test_checked_xor_constexpr.cpp