Files
safe_numerics/test/test0.cpp
Robert Ramey bce3a5536e intermediate version
passes all tests
adds documentation of library internals
implements trap_exception for compile time guarantee for program correctness
still needs update to support the above for operations in addition to + and -
2015-12-15 10:21:08 -08:00

149 lines
2.9 KiB
C++

// 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 "../include/safe_range.hpp"
#include "../include/safe_integer.hpp"
bool test1(){
std::cout << "test1" << std::endl;
boost::numeric::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(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::numeric::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(std::exception e){
success = true;
}
if(success == false)
return false;
try{
int yi = y;
z = x + yi; // should trap here
}
catch(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::numeric;
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(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::numeric::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(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 <boost/cstdint.hpp>
#include <cstdint>
bool test5(){
std::cout << "test5" << std::endl;
boost::numeric::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(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 argc, char *argv[]){
bool rval = (
test1() &&
test2() &&
test3() &&
test4() &&
test5()
);
std::cout << (rval ? "success!" : "failure") << std::endl;
return rval ? EXIT_SUCCESS : EXIT_FAILURE;
}