1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include "sampledcurve.hpp"
21#include "utilities.hpp"
22#include <ql/math/sampledcurve.hpp>
23#include <ql/utilities/dataformatters.hpp>
24#include <ql/grid.hpp>
25
26using namespace QuantLib;
27using namespace boost::unit_test_framework;
28
29namespace sampled_curve_test {
30 class FSquared {
31 public:
32 Real operator()(Real x) const { return x*x;};
33 };
34}
35
36QL_DEPRECATED_DISABLE_WARNING
37
38void SampledCurveTest::testConstruction() {
39
40 BOOST_TEST_MESSAGE("Testing sampled curve construction...");
41
42 using namespace sampled_curve_test;
43
44 SampledCurve curve(BoundedGrid(xMin: -10.0,xMax: 10.0,steps: 100));
45 FSquared f2;
46 curve.sample(f: f2);
47 Real expected = 100.0;
48 if (std::fabs(x: curve.value(i: 0) - expected) > 1e-5) {
49 BOOST_ERROR("function sampling failed");
50 }
51
52 curve.value(i: 0) = 2.0;
53 if (std::fabs(x: curve.value(i: 0) - 2.0) > 1e-5) {
54 BOOST_ERROR("curve value setting failed");
55 }
56
57 Array& value = curve.values();
58 value[1] = 3.0;
59 if (std::fabs(x: curve.value(i: 1) - 3.0) > 1e-5) {
60 BOOST_ERROR("curve value grid failed");
61 }
62
63 curve.shiftGrid(s: 10.0);
64 if (std::fabs(x: curve.gridValue(i: 0) - 0.0) > 1e-5) {
65 BOOST_ERROR("sample curve shift grid failed");
66 }
67 if (std::fabs(x: curve.value(i: 0) - 2.0) > 1e-5) {
68 BOOST_ERROR("sample curve shift grid - value failed");
69 }
70
71 curve.sample(f: f2);
72 curve.regrid(new_grid: BoundedGrid(xMin: 0.0,xMax: 20.0,steps: 200));
73 Real tolerance = 1.0e-2;
74 for (Size i=0; i < curve.size(); i++) {
75 Real grid = curve.gridValue(i);
76 Real value = curve.value(i);
77 Real expected = f2(grid);
78 if (std::fabs(x: value - expected) > tolerance) {
79 BOOST_ERROR("sample curve regriding failed" <<
80 "\n at " << io::ordinal(i+1) << " point " << "(x = " << grid << ")" <<
81 "\n grid value: " << value <<
82 "\n expected: " << expected);
83 }
84 }
85}
86
87QL_DEPRECATED_ENABLE_WARNING
88
89test_suite* SampledCurveTest::suite() {
90 auto* suite = BOOST_TEST_SUITE("sampled curve tests");
91 suite->add(QUANTLIB_TEST_CASE(&SampledCurveTest::testConstruction));
92 return suite;
93}
94
95

source code of quantlib/test-suite/sampledcurve.cpp