summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/safe_numerics/test/test_multiply.hpp
blob: fe44ab1f1d15dc4ff6e31dd1406e068f4234fb0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef BOOST_TEST_MULTIPLY_HPP
#define BOOST_TEST_MULTIPLY_HPP

//  Copyright (c) 2015 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <boost/core/demangle.hpp>

#include <boost/safe_numerics/safe_integer.hpp>
#include <boost/safe_numerics/range_value.hpp>

template<class T1, class T2>
bool test_multiply(
    T1 v1,
    T2 v2,
    const char *av1,
    const char *av2,
    char expected_result
){
    std::cout << "testing"<< std::endl;
    {
        safe_t<T1> t1 = v1;

        using result_type = decltype(t1 * v2);
        std::cout << "safe<" << av1 << "> * " << av2 << " -> ";
        static_assert(
            boost::safe_numerics::is_safe<safe_t<T1> >::value,
            "safe_t not safe!"
        );
        static_assert(
            boost::safe_numerics::is_safe<result_type>::value,
            "Expression failed to return safe type"
        );

        try{
            // use auto to avoid checking assignment.
            auto result = t1 * v2;
            std::cout << make_result_display(result);
            if(expected_result == 'x'){
                const std::type_info & ti = typeid(result);
                std::cout
                    << " *** failed to detect error in multiplication"
                    << boost::core::demangle(ti.name())
                    << std::endl;
                t1 * v2;
                return false;
            }
            std::cout << std::endl;
        }
        catch(const std::exception &){
            if(expected_result == '.'){
                std::cout
                    << " *** erroneously detected error in multiplication"
                    << std::endl;
                try{
                    t1 * v2;
                }
                catch(const std::exception &){}
                return false;
            }
        }
    }
    {
        safe_t<T2> t2 = v2;
        using result_type = decltype(v1 * t2);
        std::cout << av1 << " * " << "safe<" << av2 << "> -> ";
        static_assert(
            boost::safe_numerics::is_safe<safe_t<T2> >::value,
            "safe_t not safe!"
        );
        static_assert(
            boost::safe_numerics::is_safe<result_type>::value,
            "Expression failed to return safe type"
        );

        try{
            // use auto to avoid checking assignment.
            auto result = v1 * t2;
            std::cout << make_result_display(result);
            if(expected_result == 'x'){
                const std::type_info & ti = typeid(result);
                std::cout
                    << " *** failed to detect error in multiplication"
                    << boost::core::demangle(ti.name())
                    << std::endl;
                v1 * t2;
                return false;
            }
            std::cout << std::endl;
        }
        catch(const std::exception &){
            if(expected_result == '.'){
                std::cout
                    << " *** erroneously detected error in multiplication"
                    << std::endl;
                try{
                    v1 * t2;
                }
                catch(const std::exception &){}
                return false;
            }
        }
    }
    {
        safe_t<T1> t1 = v1;
        safe_t<T2> t2 = v2;

        using result_type = decltype(t1 * t2);
        std::cout << "safe<" << av1 << "> * " << "safe<" << av2 << "> -> ";
        static_assert(
            boost::safe_numerics::is_safe<result_type>::value,
            "Expression failed to return safe type"
        );

        try{
            // use auto to avoid checking assignment.
            auto result = t1 * t2;
            std::cout << make_result_display(result);
            if(expected_result == 'x'){
                const std::type_info & ti = typeid(result);
                std::cout
                    << " *** failed to detect error in multiplication"
                    << boost::core::demangle(ti.name())
                    << std::endl;
                t1 * t2;
                return false;
            }
            std::cout << std::endl;
        }
        catch(const std::exception &){
            if(expected_result == '.'){
                std::cout
                    << " *** erroneously detected error in multiplication"
                    << std::endl;
                try{
                    t1 * t2;
                }
                catch(const std::exception &){}
                return false;
            }
        }
    }
    return true;
}


#endif // BOOST_TEST_MULTIPLY