| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | This file is part of QuantLib, a free - software / open - source library |
| 5 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 6 | |
| 7 | QuantLib is free software : you can redistribute it and/or modify it |
| 8 | under the terms of the QuantLib license.You should have received a |
| 9 | copy of the license along with this program; if not, please email |
| 10 | <quantlib - dev@lists.sf.net>.The license is also available online at |
| 11 | <http://quantlib.org/license.shtml>. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | FOR A PARTICULAR PURPOSE.See the license for more details. |
| 16 | */ |
| 17 | |
| 18 | #include "ql/timegrid.hpp" |
| 19 | #include "timegrid.hpp" |
| 20 | #include "utilities.hpp" |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace QuantLib; |
| 26 | using namespace boost::unit_test_framework; |
| 27 | |
| 28 | void TimeGridTest::testConstructorAdditionalSteps() |
| 29 | { |
| 30 | BOOST_TEST_MESSAGE("Testing TimeGrid construction with additional steps..." ); |
| 31 | |
| 32 | const TimeGrid tg = {{1.0, 2.0, 4.0}, 8}; |
| 33 | |
| 34 | // Expect 8 evenly sized steps over the interval [0, 4]. |
| 35 | std::vector<Time> expected_times = {0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0}; |
| 36 | |
| 37 | BOOST_CHECK_EQUAL_COLLECTIONS(tg.begin(), tg.end(), expected_times.begin(), |
| 38 | expected_times.end()); |
| 39 | } |
| 40 | |
| 41 | void TimeGridTest::testConstructorMandatorySteps() |
| 42 | { |
| 43 | BOOST_TEST_MESSAGE("Testing TimeGrid construction with only mandatory points..." ); |
| 44 | |
| 45 | const TimeGrid tg = {0.0, 1.0, 2.0, 4.0}; |
| 46 | |
| 47 | // Time grid must include all times from passed iterator. |
| 48 | // Further no additional times can be added. |
| 49 | std::vector<Time> expected_times = {0.0, 1.0, 2.0, 4.0}; |
| 50 | |
| 51 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 52 | tg.begin(), tg.end(), expected_times.begin(), expected_times.end()); |
| 53 | } |
| 54 | |
| 55 | void TimeGridTest::testConstructorEvenSteps() |
| 56 | { |
| 57 | BOOST_TEST_MESSAGE("Testing TimeGrid construction with n evenly spaced points..." ); |
| 58 | |
| 59 | Time end_time = 10; |
| 60 | Size steps = 5; |
| 61 | const TimeGrid tg(end_time, steps); |
| 62 | |
| 63 | std::vector<Time> expected_times = {0.0, 2.0, 4.0, 6.0, 8.0, 10.0}; |
| 64 | |
| 65 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 66 | tg.begin(), tg.end(), expected_times.begin(), expected_times.end() |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | void TimeGridTest::testConstructorEmptyIterator() |
| 71 | { |
| 72 | BOOST_TEST_MESSAGE( |
| 73 | "Testing that the TimeGrid constructor raises an error for empty iterators..." |
| 74 | ); |
| 75 | |
| 76 | const std::vector<Time> times; |
| 77 | BOOST_CHECK_THROW(const TimeGrid tg(times.begin(), times.end()), Error); |
| 78 | } |
| 79 | |
| 80 | void TimeGridTest::testConstructorNegativeValuesInIterator() |
| 81 | { |
| 82 | BOOST_TEST_MESSAGE("Testing that the TimeGrid constructor raises an error for negative time values..." ); |
| 83 | |
| 84 | std::vector<Time> times = {-3.0, 1.0, 4.0, 5.0}; |
| 85 | BOOST_CHECK_THROW(const TimeGrid tg(times.begin(), times.end()), Error); |
| 86 | } |
| 87 | |
| 88 | void TimeGridTest::testClosestIndex() |
| 89 | { |
| 90 | BOOST_TEST_MESSAGE("Testing that the returned index is closest to the requested time..." ); |
| 91 | |
| 92 | const TimeGrid tg = {1.0, 2.0, 5.0}; |
| 93 | const Size expected_index = 3; |
| 94 | |
| 95 | QL_ASSERT(tg.closestIndex(4) == expected_index, |
| 96 | "Expected index: " << expected_index << ", which does not match " << |
| 97 | "the returned index: " << tg.closestIndex(4)); |
| 98 | } |
| 99 | |
| 100 | void TimeGridTest::testClosestTime() |
| 101 | { |
| 102 | BOOST_TEST_MESSAGE("Testing that the returned time matches the requested index..." ); |
| 103 | const TimeGrid tg = {1.0, 2.0, 5.0}; |
| 104 | const Size expected_time = 5; |
| 105 | |
| 106 | QL_ASSERT(tg.closestTime(4) == expected_time, |
| 107 | "Expected time of: " << expected_time << ", which does not match " << |
| 108 | "the returned time: " << tg.closestTime(4)); |
| 109 | } |
| 110 | |
| 111 | void TimeGridTest::testMandatoryTimes() |
| 112 | { |
| 113 | BOOST_TEST_MESSAGE("Testing that mandatory times are recalled correctly..." ); |
| 114 | std::vector<Time> test_times = {1.0, 2.0, 4.0}; |
| 115 | const TimeGrid tg(test_times.begin(), test_times.end(), 8); |
| 116 | |
| 117 | // Mandatory times are those provided by the original iterator. |
| 118 | const std::vector<Time>& tg_mandatory_times = tg.mandatoryTimes(); |
| 119 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 120 | tg_mandatory_times.begin(), tg_mandatory_times.end(), |
| 121 | test_times.begin(), test_times.end()); |
| 122 | } |
| 123 | |
| 124 | test_suite* TimeGridTest::suite() |
| 125 | { |
| 126 | auto* suite = BOOST_TEST_SUITE("Timegrid tests" ); |
| 127 | |
| 128 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testConstructorAdditionalSteps)); |
| 129 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testConstructorMandatorySteps)); |
| 130 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testConstructorEvenSteps)); |
| 131 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testConstructorEmptyIterator)); |
| 132 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testConstructorNegativeValuesInIterator)); |
| 133 | |
| 134 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testClosestIndex)); |
| 135 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testClosestTime)); |
| 136 | suite->add(QUANTLIB_TEST_CASE(&TimeGridTest::testMandatoryTimes)); |
| 137 | |
| 138 | return suite; |
| 139 | } |
| 140 | |