• 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 <iostream>
8 #include <boost/logic/tribool_io.hpp>
9 
10 #include <boost/core/demangle.hpp>
11 #include <boost/safe_numerics/checked_result_operations.hpp>
12 #include <boost/safe_numerics/checked_integer.hpp>
13 
14 // works for both GCC and clang
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic ignored "-Wunused-value"
17 
18 // note: T should be of type checked_result<R> for some integer type R
19 template<class T>
test_checked_equal_to(T v1,T v2,char expected_result)20 bool test_checked_equal_to(
21     T v1,
22     T v2,
23     char expected_result
24 ){
25     using namespace boost::safe_numerics;
26     const boost::logic::tribool result = (v1 == v2);
27     std::cout
28         << std::boolalpha << v1 << " == " << v2 << " -> " << result
29         << std::endl;
30 
31     switch(expected_result){
32     case '<':
33     case '>':
34         if(! result)
35             return true;
36         break;
37     case '=':
38         if(result)
39             return true;
40         break;
41     case '!':
42         if(indeterminate(result))
43             return true;
44         break;
45     }
46     std::cout
47         << "failed to detect error in addition "
48         << std::hex << result << "(" << std::dec << result << ")"
49         << " != "<< v1 << " == " << v2
50         << std::endl;
51     v1 == v2;
52     return false;
53 }
54 
55 #pragma GCC diagnostic pop
56 
57 #include "test_checked_comparison.hpp"
58 
59 template<typename T, typename First, typename Second>
60 struct test_signed_pair {
operator ()test_signed_pair61     bool operator()() const {
62         std::size_t i = First();
63         std::size_t j = Second();
64         std::cout << std::dec << i << ',' << j << ','
65         << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
66         return test_checked_equal_to(
67             signed_values<T>[i],
68             signed_values<T>[j],
69             signed_comparison_results[i][j]
70         );
71     };
72 };
73 
74 template<typename T, typename First, typename Second>
75 struct test_unsigned_pair {
operator ()test_unsigned_pair76     bool operator()() const {
77         std::size_t i = First();
78         std::size_t j = Second();
79         std::cout << std::dec << i << ',' << j << ','
80         << "testing " << boost::core::demangle(typeid(T).name()) << ' ';
81         return test_checked_equal_to(
82             unsigned_values<T>[i],
83             unsigned_values<T>[j],
84             unsigned_comparison_results[i][j]
85         );
86     };
87 };
88 
89 #include <boost/mp11/algorithm.hpp>
90 
main()91 int main(){
92     using namespace boost::mp11;
93     bool rval = true;
94 
95     mp_for_each<
96         mp_product<
97             test_signed_pair,
98             signed_test_types,
99             signed_value_indices,
100             signed_value_indices
101         >
102     >([&](auto I){
103         rval &= I();
104     });
105 
106     mp_for_each<
107         mp_product<
108             test_unsigned_pair,
109             unsigned_test_types,
110             unsigned_value_indices, unsigned_value_indices
111         >
112     >([&](auto I){
113         rval &= I();
114     });
115 
116     std::cout << (rval ? "success!" : "failure") << std::endl;
117     return rval ? 0 : 1;
118 }
119