Files
safe_numerics/test/test_z copy.cpp
Robert Ramey 69d681727c re-factored compare
updated test_compare
updated safe_compare.hp
2015-06-19 08:29:53 -07:00

282 lines
7.6 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)
#if 1
/* Boost test/test_float.cpp
* test arithmetic operations on a range of intervals
*
* Copyright 2003 Guillaume Melquiond
*
* 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 <boost/numeric/interval.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/config.hpp>
//#include "bugs.hpp"
/* All the following tests should be BOOST_CHECK; however, if a test fails,
the probability is high that hundreds of other tests will fail, so it is
replaced by BOOST_REQUIRE to avoid flooding the logs. */
template<class T, class F>
void test_unary() {
typedef typename F::I I;
for(I a(-10., -9.91); a.lower() <= 10.; a += 0.3) {
if (!F::validate(a)) continue;
I rI = F::f_I(a);
T rT1 = F::f_T(a.lower()), rT2 = F::f_T(a.upper()),
rT3 = F::f_T(median(a));
BOOST_REQUIRE(in(rT1, rI));
BOOST_REQUIRE(in(rT2, rI));
BOOST_REQUIRE(in(rT3, rI));
}
}
template<class T, class F>
void test_binary() {
typedef typename F::I I;
for(I a(-10., -9.91); a.lower() <= 10.; a += 0.3) {
for(I b(-10., -9.91); b.lower() <= 10.; b += 0.3) {
if (!F::validate(a, b)) continue;
T al = a.lower(), au = a.upper(), bl = b.lower(), bu = b.upper();
I rII = F::f_II(a, b);
I rIT1 = F::f_IT(a, bl), rIT2 = F::f_IT(a, bu);
I rTI1 = F::f_TI(al, b), rTI2 = F::f_TI(au, b);
I rTT1 = F::f_TT(al, bl), rTT2 = F::f_TT(al, bu);
I rTT3 = F::f_TT(au, bl), rTT4 = F::f_TT(au, bu);
BOOST_REQUIRE(subset(rTT1, rIT1));
BOOST_REQUIRE(subset(rTT3, rIT1));
BOOST_REQUIRE(subset(rTT2, rIT2));
BOOST_REQUIRE(subset(rTT4, rIT2));
BOOST_REQUIRE(subset(rTT1, rTI1));
BOOST_REQUIRE(subset(rTT2, rTI1));
BOOST_REQUIRE(subset(rTT3, rTI2));
BOOST_REQUIRE(subset(rTT4, rTI2));
BOOST_REQUIRE(subset(rIT1, rII));
BOOST_REQUIRE(subset(rIT2, rII));
BOOST_REQUIRE(subset(rTI1, rII));
BOOST_REQUIRE(subset(rTI2, rII));
}
}
}
#define new_unary_bunch(name, op, val) \
template<class T> \
struct name { \
typedef boost::numeric::interval<T> I; \
static I f_I(const I& a) { return op(a); } \
static T f_T(const T& a) { return op(a); } \
static bool validate(const I& a) { return val; } \
}
//#ifndef BOOST_NO_STDC_NAMESPACE
using std::abs;
using std::sqrt;
//#endif
new_unary_bunch(bunch_pos, +, true);
new_unary_bunch(bunch_neg, -, true);
new_unary_bunch(bunch_sqrt, sqrt, a.lower() >= 0.);
//new_unary_bunch(bunch_abs, abs, true);
template<class T>
void test_all_unaries() {
BOOST_TEST_CHECKPOINT("pos"); test_unary<T, bunch_pos<T> >();
BOOST_TEST_CHECKPOINT("neg"); test_unary<T, bunch_neg<T> >();
BOOST_TEST_CHECKPOINT("sqrt"); test_unary<T, bunch_sqrt<T> >();
//BOOST_TEST_CHECKPOINT("abs"); test_unary<T, bunch_abs<T> >();
}
#define new_binary_bunch(name, op, val) \
template<class T> \
struct bunch_##name { \
typedef boost::numeric::interval<T> I; \
static I f_II(const I& a, const I& b) { return a op b; } \
static I f_IT(const I& a, const T& b) { return a op b; } \
static I f_TI(const T& a, const I& b) { return a op b; } \
static I f_TT(const T& a, const T& b) \
{ return boost::numeric::interval_lib::name<I>(a,b); } \
static bool validate(const I& a, const I& b) { return val; } \
}
new_binary_bunch(add, +, true);
new_binary_bunch(sub, -, true);
new_binary_bunch(mul, *, true);
new_binary_bunch(div, /, !zero_in(b));
template<class T>
void test_all_binaries() {
BOOST_TEST_CHECKPOINT("add"); test_binary<T, bunch_add<T> >();
BOOST_TEST_CHECKPOINT("sub"); test_binary<T, bunch_sub<T> >();
BOOST_TEST_CHECKPOINT("mul"); test_binary<T, bunch_mul<T> >();
BOOST_TEST_CHECKPOINT("div"); test_binary<T, bunch_div<T> >();
}
namespace boost {
namespace unit_test {
bool boost::unit_test::test_suite *
init_unit_test_suite( int argc, char* argv[] ){
return 0;
}
}
}
int test_main(int, char *[]) {
BOOST_TEST_CHECKPOINT("int tests");
test_all_unaries<int> ();
test_all_binaries<int> ();
BOOST_TEST_CHECKPOINT("unsigned int tests");
test_all_unaries<unsigned int>();
test_all_binaries<unsigned int>();
return 0;
}
#elif 0
#include "../include/safe_integer.hpp"
#include <boost/mpl/or.hpp>
#include <iostream>
struct A {};
static_assert(
! std::is_base_of<A, const int>::value,
"const int should not be a safe type"
);
int main(int argc, char * argv[]){
boost::numeric::safe<unsigned char> x = 255;
int y = 1;
static_assert(
boost::numeric::is_safe<decltype(x)>::value,
"x is not a safe type"
);
static_assert(
! boost::numeric::is_safe<decltype(y)>::value,
"y is a safe type"
);
static_assert(
boost::mpl::or_<
boost::numeric::is_safe<decltype(x)>,
boost::numeric::is_safe<decltype(y)>
>::value,
"or fails to work"
);
//x + y;
std::cout << x + y << std::endl;
return 0;
}
#elif 0
#include <iostream>
#include <boost/mpl/print.hpp>
#include "../include/safe_integer.hpp"
int main(int argc, char * argv[]){
SAFE_NUMERIC_CONSTEXPR boost::numeric::safe<unsigned char> si = 4;
typedef boost::mpl::print<
decltype(si + si)
>::type t2; // should be same as type above
std::cout << si + si;
/*
boost::numeric::safe<unsigned char> t;
boost::numeric::safe<unsigned char> u;
SAFE_NUMERIC_CONSTEXPR boost::numeric::safe<int> x = std::numeric_limits<T>::max();
SAFE_NUMERIC_CONSTEXPR const int y = boost::numeric::base_value(std::numeric_limits<T>::max());
SAFE_NUMERIC_CONSTEXPR boost::numeric::checked_result<result_base_type> maxx = boost::numeric::checked::add<result_base_type>(
boost::numeric::base_value(std::numeric_limits<T>::max()),
boost::numeric::base_value(std::numeric_limits<U>::max())
);
*/
return 0;
}
#elif 0
struct A {
int m_x;
A() = delete;
SAFE_NUMERIC_CONSTEXPR A(const int & x) :
m_x(x)
{}
SAFE_NUMERIC_CONSTEXPR A(const A & a) :
m_x(a.m_x)
{}
/*
SAFE_NUMERIC_CONSTEXPR A(const A & a) :
m_x(a.m_x)
{}
SAFE_NUMERIC_CONSTEXPR A(const int & x) :
m_x(x)
{}
*/
};
SAFE_NUMERIC_CONSTEXPR int f(int x){
return x;
}
SAFE_NUMERIC_CONSTEXPR int g(const int & x){
return x;
}
SAFE_NUMERIC_CONSTEXPR int h(const int && x){
return x;
}
SAFE_NUMERIC_CONSTEXPR const A a = {A(1)};
SAFE_NUMERIC_CONSTEXPR int b = f(0);
SAFE_NUMERIC_CONSTEXPR int c = g(0);
int main(int argc, char * argv[]){
const int a = f(argc);
const int b = g(argc);
const int c = g(argc + argc);
const A d = A(argc + 4);
const A e = A(argc);
const A g = A(f(argc));
}
#elif 0
#include <boost/integer_traits.hpp>
#include <boost/mpl/assert.hpp>
BOOST_MPL_ASSERT_RELATION(
(boost::integer_traits<unsigned long>::const_max),
>,
(boost::integer_traits<unsigned char>::const_max)
); // compile error under clang
enum { x = (
(boost::integer_traits<unsigned long>::const_max)
>
(boost::integer_traits<unsigned char>::const_max)
)
}; // compiles OK - can't figure out why
SAFE_NUMERIC_CONSTEXPR bool result = (
(boost::integer_traits<unsigned long>::const_max)
>
(boost::integer_traits<unsigned char>::const_max)
); // compiles fine
static_assert(result, "compilation error");
int main(int argc, char * argv[]){
return ! (boost::integer_traits<unsigned long>::const_max > boost::integer_traits<unsigned char>::const_max);
}
#endif