• 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 <cstdint> // std::int8, ...
8 
9 #include <boost/safe_numerics/checked_result.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_divide(const T & v1,const T & v2,char expected_result)15 constexpr bool test_checked_divide(
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     switch(expected_result){
23     case '0':
24     case '.':
25         return ! result.exception();
26     case '-':
27         return safe_numerics_error::negative_overflow_error == result.m_e;
28     case '+':
29         return safe_numerics_error::positive_overflow_error == result.m_e;
30     case '!':
31         return safe_numerics_error::range_error == result.m_e;
32     }
33     return false;
34 }
35 
36 #include "test_checked_divide.hpp"
37 
38 template<typename T, typename First, typename Second>
39 struct test_signed_pair {
40     static const std::size_t i = First();
41     static const std::size_t j = Second();
42     // note: is constexpr really required here?  compilers disagree!
43     constexpr static const bool value = test_checked_divide(
44         signed_values<T>[i],
45         signed_values<T>[j],
46         signed_division_results[i][j]
47     );
48 };
49 
50 template<typename T, typename First, typename Second>
51 struct test_unsigned_pair {
52     static const std::size_t i = First();
53     static const std::size_t j = Second();
54     // note: is constexpr really required here?  compilers disagree!
55     constexpr static const bool value = test_checked_divide(
56         unsigned_values<T>[i],
57         unsigned_values<T>[j],
58         unsigned_division_results[i][j]
59     );
60 };
61 
62 #include <boost/mp11/algorithm.hpp>
63 #include <boost/mp11/function.hpp>
64 
main()65 int main(){
66     using namespace boost::mp11;
67 
68     static_assert(
69         mp_all_of<
70             mp_product<
71                 test_signed_pair,
72                 signed_test_types,
73                 signed_value_indices, signed_value_indices
74             >,
75             mp_to_bool
76         >(),
77         "all values for all signed types correctly divided"
78     );
79 
80     static_assert(
81         mp_all_of<
82             mp_product<
83                 test_unsigned_pair,
84                 unsigned_test_types,
85                 unsigned_value_indices, unsigned_value_indices
86             >,
87             mp_to_bool
88         >(),
89         "all values for all unsigned types correctly divided"
90     );
91     return 0;
92 }
93