• 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 template<class T>
test_checked_modulus(const T & v1,const T & v2,char expected_result)12 constexpr bool test_checked_modulus(
13     const T & v1,
14     const T & v2,
15     char expected_result
16 ){
17     using namespace boost::safe_numerics;
18     const T result = v1 % v2;
19     switch(expected_result){
20     case '0':
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_modulus.hpp"
34 
35 #include <boost/mp11/algorithm.hpp>
36 
37 template<typename T, typename First, typename Second>
38 struct test_signed_pair {
39     static const std::size_t i = First();
40     static const std::size_t j = Second();
41     // note: is constexpr really required here?  compilers disagree!
42     constexpr static const bool value = test_checked_modulus(
43         signed_values<T>[i],
44         signed_values<T>[j],
45         signed_modulus_results[i][j]
46     );
47 };
48 
49 template<typename T, typename First, typename Second>
50 struct test_unsigned_pair {
51     static const std::size_t i = First();
52     static const std::size_t j = Second();
53     // note: is constexpr really required here?  compilers disagree!
54     constexpr static const bool value = test_checked_modulus(
55         unsigned_values<T>[i],
56         unsigned_values<T>[j],
57         unsigned_modulus_results[i][j]
58     );
59 };
60 
61 #include <boost/mp11/algorithm.hpp>
62 #include <boost/mp11/function.hpp>
63 
main()64 int main(){
65     using namespace boost::mp11;
66     static_assert(
67         mp_all_of<
68             mp_product<
69                 test_signed_pair,
70                 signed_test_types,
71                 signed_value_indices, signed_value_indices
72             >,
73             mp_to_bool
74         >(),
75         "modulus for all signed values correct"
76     );
77 
78     static_assert(
79         mp_all_of<
80             mp_product<
81                 test_unsigned_pair,
82                 unsigned_test_types,
83                 unsigned_value_indices, unsigned_value_indices
84             >,
85             mp_to_bool
86         >(),
87         "modulus for all unsigned values correct"
88     );
89     return 0;
90 }
91