1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Joseph Wang
5 Copyright (C) 2010 Liquidnet Holdings, Inc.
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include "timeseries.hpp"
22#include "utilities.hpp"
23#include <ql/timeseries.hpp>
24#include <ql/prices.hpp>
25#include <ql/time/calendars/unitedstates.hpp>
26#include <boost/unordered_map.hpp>
27
28using namespace QuantLib;
29using namespace boost::unit_test_framework;
30
31void TimeSeriesTest::testConstruction() {
32
33 BOOST_TEST_MESSAGE("Testing time series construction...");
34
35 TimeSeries<Real> ts;
36 ts[Date(25, March, 2005)] = 1.2;
37 ts[Date(29, March, 2005)] = 2.3;
38 ts[Date(15, March, 2005)] = 0.3;
39
40 auto cur = ts.begin();
41 if (cur->first != Date(15, March, 2005)) {
42 BOOST_ERROR("date does not match");
43 }
44 if (cur->second != 0.3) {
45 BOOST_ERROR("value does not match");
46 }
47
48 ts[Date(15, March, 2005)] = 4.0;
49 cur = ts.begin();
50 if (cur->second != 4.0) {
51 BOOST_ERROR("replaced value does not match" << cur->second << "\n");
52 }
53
54 ts[Date(15, March, 2005)] = 3.5;
55
56 if (cur->second != 3.5) {
57 BOOST_ERROR("set value operator not match" << cur->second << "\n");
58 }
59}
60
61void TimeSeriesTest::testIntervalPrice() {
62 BOOST_TEST_MESSAGE("Testing time series interval price...");
63
64 std::vector<Date> date = {Date(25, March, 2005), Date(29, March, 2005)};
65
66 std::vector<Real> open = {1.3, 2.3},
67 close = {2.3, 3.4},
68 high = {3.4, 3.5},
69 low = {3.4, 3.2};
70
71 TimeSeries<IntervalPrice> tsiq = IntervalPrice::makeSeries(d: date,
72 open,
73 close,
74 high,
75 low);
76}
77
78void TimeSeriesTest::testIterators() {
79 BOOST_TEST_MESSAGE("Testing time series iterators...");
80
81 std::vector<Date> dates = {Date(25, March, 2005),
82 Date(29, March, 2005),
83 Date(15, March, 2005)};
84
85 std::vector<Real> prices = {25, 23, 20};
86
87 TimeSeries<Real> ts(dates.begin(), dates.end(), prices.begin());
88
89 // projection iterators
90
91 QL_DEPRECATED_DISABLE_WARNING
92
93 std::copy(first: ts.cbegin_time(), last: ts.cend_time(), result: dates.begin());
94 if (dates[0] != Date(15, March, 2005)) {
95 BOOST_ERROR("date does not match");
96 }
97
98 std::copy(first: ts.cbegin_values(), last: ts.cend_values(), result: prices.begin());
99 if (prices[0] != 20) {
100 BOOST_ERROR("value does not match");
101 }
102
103 dates = ts.dates();
104 if (dates[0] != Date(15, March, 2005)) {
105 BOOST_ERROR("date does not match");
106 }
107
108 prices = ts.values();
109 if (prices[0] != 20) {
110 BOOST_ERROR("value does not match");
111 }
112
113 // unordered container
114 typedef TimeSeries<int, boost::unordered_map<Date, int> >
115 TimeSeriesUnordered;
116 TimeSeriesUnordered ts1;
117 Date d0(25, March, 2005), d1(25, April, 2005), d = d0;
118 UnitedStates calendar(UnitedStates::NYSE);
119 for (int i = 0; d < d1; ++i, d = calendar.advance(d, n: 1, unit: Days)) {
120 ts1[d] = i;
121 }
122
123 d = d0;
124 for (int i = 0; d < d1; ++i, d = calendar.advance(d, n: 1, unit: Days)) {
125 if (ts1[d] != int(i)) {
126 BOOST_ERROR("value does not match");
127 }
128 }
129
130 // reverse iterators
131
132 std::vector<std::pair<Date,Real> > data(prices.size());
133 std::copy(first: ts.crbegin(), last: ts.crend(), result: data.begin());
134 if (data[2].second != 20) {
135 BOOST_ERROR("value does not match");
136 }
137 if (data[2].first != Date(15, March, 2005)) {
138 BOOST_ERROR("date does not match");
139 }
140
141 std::copy(first: ts.crbegin_time(), last: ts.crend_time(), result: dates.begin());
142 if (dates[0] != Date(29, March, 2005)) {
143 BOOST_ERROR("date does not match");
144 }
145
146 std::copy(first: ts.crbegin_values(), last: ts.crend_values(), result: prices.begin());
147 if (prices[0] != 23) {
148 BOOST_ERROR("value does not match");
149 }
150
151 QL_DEPRECATED_ENABLE_WARNING
152
153 // The following should not compile:
154 // std::transform(ts1.crbegin(), ts1.crend(), prices.begin(),
155 // TimeSeriesUnordered::get_value);
156 // std::copy(ts1.crbegin_values(), ts1.crend_values(), prices.begin());
157 // ts1.lastDate();
158
159 // last date
160 if (ts.lastDate() != Date(29, March, 2005)) {
161 BOOST_ERROR("lastDate does not match");
162 }
163}
164
165test_suite* TimeSeriesTest::suite() {
166 auto* suite = BOOST_TEST_SUITE("time series tests");
167 suite->add(QUANTLIB_TEST_CASE(&TimeSeriesTest::testConstruction));
168 suite->add(QUANTLIB_TEST_CASE(&TimeSeriesTest::testIntervalPrice));
169 suite->add(QUANTLIB_TEST_CASE(&TimeSeriesTest::testIterators));
170 return suite;
171}
172
173

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.