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_less_than(T v1,T v2,char expected_result)13 constexpr bool test_checked_less_than(
14 T v1,
15 T v2,
16 char expected_result
17 ){
18 using namespace boost::safe_numerics;
19 const boost::logic::tribool result = v1 < v2;
20 switch(expected_result){
21 case '<':
22 if(result)
23 return true;
24 break;
25 case '>':
26 case '=':
27 if(!result)
28 return true;
29 break;
30 case '!':
31 if(indeterminate(result))
32 return true;
33 break;
34 }
35 return false;
36 }
37
38 #include "test_checked_comparison.hpp"
39
40 template<typename T, typename First, typename Second>
41 struct test_signed_pair {
42 static const std::size_t i = First();
43 static const std::size_t j = Second();
44 // note: is constexpr really required here? compilers disagree!
45 constexpr static const bool value = test_checked_less_than(
46 signed_values<T>[i],
47 signed_values<T>[j],
48 signed_comparison_results[i][j]
49 );
50 };
51
52 template<typename T, typename First, typename Second>
53 struct test_unsigned_pair {
54 static const std::size_t i = First();
55 static const std::size_t j = Second();
56 // note: is constexpr really required here? compilers disagree!
57 constexpr static const bool value = test_checked_less_than(
58 unsigned_values<T>[i],
59 unsigned_values<T>[j],
60 unsigned_comparison_results[i][j]
61 );
62 };
63
64 #include <boost/mp11/algorithm.hpp>
65
main()66 int main(){
67 using namespace boost::mp11;
68
69 static_assert(
70 mp_all_of<
71 mp_product<
72 test_signed_pair,
73 signed_test_types,
74 signed_value_indices, signed_value_indices
75 >,
76 mp_to_bool
77 >(),
78 "all values for all signed types correctly compared"
79 );
80
81 static_assert(
82 mp_all_of<
83 mp_product<
84 test_unsigned_pair,
85 unsigned_test_types,
86 unsigned_value_indices, unsigned_value_indices
87 >,
88 mp_to_bool
89 >(),
90 "all values for all unsigned types correctly compared"
91 );
92 return 0;
93 }
94