summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/safe_numerics/test/test0.cpp
blob: e4ef159eafaab20784aefb5802b938c4b27822bb (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
//  Copyright (c) 2012 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/safe_numerics/safe_integer_range.hpp>
#include <boost/safe_numerics/safe_integer.hpp>

bool test1(){
    std::cout << "test1" << std::endl;
    boost::safe_numerics::safe_signed_range<-64, 63> x, y, z;
    x = 1;
    y = 2;
    z = 3;
    z = x + y;
    z = x - y;

    try{
        short int yi, zi;
        yi = y;
        zi = x + yi;
    }
    catch(const std::exception & e){
        // none of the above should trap. Mark failure if they do
        std::cout << e.what() << std::endl;
        return false;
    }
    return true;
}

bool test2(){
    std::cout << "test2" << std::endl;
    boost::safe_numerics::safe_unsigned_range<0, 64> x, y, z;
    x = 1;
    y = 2;
    z = 3;

    bool success = false;
    try{
        z = x - y; // should trap here
    }
    catch(const std::exception & e){
        success = true;
    }
    if(success == false)
        return false;
    
    try{
        int yi = y;
        z = x + yi; // should trap here
    }
    catch(const std::exception & e){
        // none of the above should trap. Mark failure if they do
        std::cout << e.what() << std::endl;
        return false;
    }
    return true;
}

bool test3(){
    using namespace boost::safe_numerics;
    std::cout << "test3" << std::endl;
    safe<int> x, y, z;
    x = 1;
    y = 2;
    z = 3;
    try{
        z = x + y;
        z = x - y;
        int yi, zi;
        zi = x + yi;
        z = x + yi;
    }
    catch(const std::exception & e){
        // none of the above should trap. Mark failure if they do
        std::cout << e.what() << std::endl;
        return false;
    }
    return true;
}

bool test4(){
    std::cout << "test4" << std::endl;
    boost::safe_numerics::safe<unsigned int> x, y, z;
    x = 1;
    y = 2;
    z = 3;
    z = x + y;
    bool success = false;
    try{
        z = x - y; // should trap here
    }
    catch(const std::exception & e){
        success = true;
    }
    if(success == false)
        return false;
    unsigned int yi, zi;
    zi = x;
    zi = x + yi;
    z = x + yi;
    zi = x + y;
    return true;
}

#include <cstdint>

bool test5(){
    std::cout << "test5" << std::endl;
    boost::safe_numerics::safe<boost::uint64_t> x, y, z;
    x = 1;
    y = 2;
    z = 3;
    z = x + y;
    bool success = false;
    try{
        z = x - y; // should trap here
    }
    catch(const std::exception & e){
        success = true;
    }
    if(success == false)
        return false;
    boost::uint64_t yi, zi;
    zi = x;
    zi = x + yi;
    z = x + yi;
    zi = x + y;
    return true;
}

int main(int, char *[]){
    bool rval = (
        test1() &&
        test2() &&
        test3() &&
        test4() &&
        test5()
    );
    std::cout << (rval ? "success!" : "failure") << std::endl;
    return rval ? EXIT_SUCCESS : EXIT_FAILURE;
}