00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef quantlib_test_utilities_hpp
00028 #define quantlib_test_utilities_hpp
00029
00030 #include <ql/instruments/payoffs.hpp>
00031 #include <ql/exercise.hpp>
00032 #include <ql/termstructures/yieldtermstructure.hpp>
00033 #include <ql/termstructures/volatility/equityfx/blackvoltermstructure.hpp>
00034 #include <ql/quote.hpp>
00035 #include <ql/patterns/observable.hpp>
00036 #include <ql/time/daycounters/actual365fixed.hpp>
00037
00038 #include <boost/test/unit_test.hpp>
00039 #include <boost/function.hpp>
00040 #include <vector>
00041 #include <string>
00042 #include <numeric>
00043 #include <iomanip>
00044
00045 extern "C" {
00046 const char * demangle(const char *);
00047 }
00048
00050 #define LENGTH(a) (sizeof(a)/sizeof(a[0]))
00051
00054 #if defined(QL_WORKING_BOOST_STREAMS)
00055 #define QL_FIXED std::fixed
00056 #define QL_SCIENTIFIC std::scientific
00057 #else
00058 #define QL_FIXED ""
00059 #define QL_SCIENTIFIC ""
00060 #endif
00061
00062
00065 #if defined(QL_DISPLAY_TEST_TIME)
00066 #define QL_TEST_START_TIMING boost::progress_timer t;
00067 #else
00068 #define QL_TEST_START_TIMING
00069 #endif
00070
00071 #if BOOST_VERSION < 103500
00072 #define QUANTLIB_TEST_CASE(f) BOOST_TEST_CASE(f)
00073 #else
00074 #define QUANTLIB_TEST_CASE(f) BOOST_TEST_CASE(QuantLib::detail::quantlib_test_case(f))
00075 #endif
00076
00077 namespace QuantLib {
00078
00081 namespace detail {
00082
00084 class quantlib_test_case {
00085 boost::function0<void> test_;
00086 public:
00087 template <class F>
00088 quantlib_test_case(F test) : test_(test) {}
00089 void operator()() const {
00090 Date before = Settings::instance().evaluationDate();
00091 BOOST_CHECK(true);
00092 test_();
00093 Date after = Settings::instance().evaluationDate();
00094 if (before != after)
00095 BOOST_ERROR("Evaluation date not reset"
00096 << "\n before: " << before
00097 << "\n after: " << after);
00098 }
00099 };
00100
00101 }
00102
00103 std::string payoffTypeToString(const boost::shared_ptr<Payoff>&);
00104 std::string exerciseTypeToString(const boost::shared_ptr<Exercise>&);
00105
00106
00107 boost::shared_ptr<YieldTermStructure>
00108 flatRate(const Date& today,
00109 const boost::shared_ptr<Quote>& forward,
00110 const DayCounter& dc);
00111
00112 boost::shared_ptr<YieldTermStructure>
00113 flatRate(const Date& today,
00114 Rate forward,
00115 const DayCounter& dc);
00116
00117 boost::shared_ptr<YieldTermStructure>
00118 flatRate(const boost::shared_ptr<Quote>& forward,
00119 const DayCounter& dc);
00120
00121 boost::shared_ptr<YieldTermStructure>
00122 flatRate(Rate forward,
00123 const DayCounter& dc);
00124
00125
00126 boost::shared_ptr<BlackVolTermStructure>
00127 flatVol(const Date& today,
00128 const boost::shared_ptr<Quote>& volatility,
00129 const DayCounter& dc);
00130
00131 boost::shared_ptr<BlackVolTermStructure>
00132 flatVol(const Date& today,
00133 Volatility volatility,
00134 const DayCounter& dc);
00135
00136 boost::shared_ptr<BlackVolTermStructure>
00137 flatVol(const boost::shared_ptr<Quote>& volatility,
00138 const DayCounter& dc);
00139
00140 boost::shared_ptr<BlackVolTermStructure>
00141 flatVol(Volatility volatility,
00142 const DayCounter& dc);
00143
00144
00145 Real relativeError(Real x1, Real x2, Real reference);
00146
00147 class Flag : public QuantLib::Observer {
00148 private:
00149 bool up_;
00150 public:
00151 Flag() : up_(false) {}
00152 void raise() { up_ = true; }
00153 void lower() { up_ = false; }
00154 bool isUp() const { return up_; }
00155 void update() { raise(); }
00156 };
00157
00158 template<class Iterator>
00159 Real norm(const Iterator& begin, const Iterator& end, Real h) {
00161 std::vector<Real> f2(end-begin);
00162 std::transform(begin,end,begin,f2.begin(),
00163 std::multiplies<Real>());
00165 Real I = h * (std::accumulate(f2.begin(),f2.end(),0.0)
00166 - 0.5*f2.front() - 0.5*f2.back());
00167 return std::sqrt(I);
00168 }
00169
00170
00172 class IndexHistoryCleaner {
00173 public:
00174 IndexHistoryCleaner();
00175 ~IndexHistoryCleaner();
00176 };
00177
00178 }
00179
00180
00181 #endif