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
19template<class T>
20bool test_checked_less_than(
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 if(result)
34 return true;
35 break;
36 case '>':
37 case '=':
38 if(!result)
39 return true;
40 break;
41 case '!':
42 if(indeterminate(x: result))
43 return true;
44 break;
45 }
46 std::cout
47 << "failed to detect error in comparison "
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
59template<typename T, typename First, typename Second>
60struct test_signed_pair {
61 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(name: typeid(T).name()) << ' ';
66 return test_checked_less_than(
67 signed_values<T>[i],
68 signed_values<T>[j],
69 signed_comparison_results[i][j]
70 );
71 };
72};
73
74template<typename T, typename First, typename Second>
75struct test_unsigned_pair {
76 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(name: typeid(T).name()) << ' ';
81 return test_checked_less_than(
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
91int main(){
92 using namespace boost::mp11;
93
94 bool x;
95 x = test_checked_less_than(v1: 77, v2: -24, expected_result: '>');
96 x = test_checked_less_than(v1: 77, v2: 82, expected_result: '<');
97
98 bool rval = true;
99
100 mp_for_each<
101 mp_product<
102 test_signed_pair,
103 signed_test_types,
104 signed_value_indices,
105 signed_value_indices
106 >
107 >(f: [&](auto I){
108 rval &= I();
109 });
110
111 mp_for_each<
112 mp_product<
113 test_unsigned_pair,
114 unsigned_test_types,
115 unsigned_value_indices, unsigned_value_indices
116 >
117 >(f: [&](auto I){
118 rval &= I();
119 });
120
121 std::cout << (rval ? "success!" : "failure") << std::endl;
122 return rval ? 0 : 1;
123}
124

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