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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
///////////////////////////////////////////////////////////////
// Copyright 2012 John Maddock. 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_
//
#ifndef BOOST_MULTIPRECISION_TEST_HPP
#define BOOST_MULTIPRECISION_TEST_HPP
#include <limits>
#include <cmath>
#include <typeinfo>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <boost/core/lightweight_test.hpp>
#include <boost/current_function.hpp>
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
enum
{
warn_on_fail,
error_on_fail,
abort_on_fail
};
template <class T>
inline int digits_of(const T&)
{
return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits : 18;
}
inline std::ostream& report_where(const char* file, int line, const char* function)
{
if(function)
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
}
#define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
inline void report_severity(int severity)
{
if(severity == error_on_fail)
++boost::detail::test_errors();
else if(severity == abort_on_fail)
{
++boost::detail::test_errors();
abort();
}
}
#define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
template <class E>
void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
{
report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
BOOST_MP_REPORT_SEVERITY(severity);
}
#define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
catch(const std::exception& e) \
{ report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); }\
catch(...)\
{ BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Exception of unknown type was thrown" << std::endl; report_severity(severity); }
#define BOOST_CHECK_IMP(x, severity)\
try{ if(x){}else{\
BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
#define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
#define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
#define BOOST_EQUAL_IMP(x, y, severity)\
try{ if(!((x) == (y))){\
BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << (x) << "\n"\
<< "Value of RHS was: " << (y) << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_NE_IMP(x, y, severity)\
try{ if(!(x != y)){\
BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << x << "\n"\
<< "Value of RHS was: " << y << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LT_IMP(x, y, severity)\
try{ if(!(x < y)){\
BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << x << "\n"\
<< "Value of RHS was: " << y << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GT_IMP(x, y, severity)\
try{ if(!(x > y)){\
BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << x << "\n"\
<< "Value of RHS was: " << y << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_LE_IMP(x, y, severity)\
try{ if(!(x <= y)){\
BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << x << "\n"\
<< "Value of RHS was: " << y << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_GE_IMP(x, y, severity)\
try{ if(!(x >= y)){\
BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
<< std::setprecision(digits_of(x)) << std::scientific\
<< "Value of LHS was: " << x << "\n"\
<< "Value of RHS was: " << y << "\n"\
<< std::setprecision(3) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\
try{ \
x;\
BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
catch(const E&){}\
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
template <class I, class J>
bool check_equal_collections(I a, I b, J x, J y)
{
int i = 0;
while(a != b)
{
if(x == y)
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM << " Unexpected end of second sequence" << std::endl;
return false;
}
if(*a != *x)
{
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Error occured in position " << i << " of the collection." << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "First value was " << std::setprecision(digits_of(x)) << std::scientific << *a << std::endl;
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Second value was " << std::setprecision(digits_of(x)) << std::scientific << *x << std::endl;
return false;
}
++a;
++x;
}
return true;
}
#define BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, severity)\
try{ \
if(!check_equal_collections(a, b, x, y))\
{\
BOOST_MP_REPORT_WHERE << " Collections were not equal" << std::endl;\
BOOST_MP_REPORT_SEVERITY(severity);\
}\
}\
BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
#define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
#define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
#define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
#define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
#define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
#define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
#define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
#define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
#define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
#define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
#define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)
#define BOOST_CHECK_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, error_on_fail)
#define BOOST_WARN_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, warn_on_fail)
#define BOOST_REQUIRE_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, abort_on_fail)
#endif
|