File: period.cpp

package info (click to toggle)
quantlib 1.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 41,768 kB
  • sloc: cpp: 398,987; makefile: 6,574; python: 214; sh: 150; lisp: 86
file content (271 lines) | stat: -rw-r--r-- 9,358 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2007, 2014 Ferdinando Ametrano

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/time/period.hpp>

using namespace QuantLib;
using namespace boost::unit_test_framework;

BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)

BOOST_AUTO_TEST_SUITE(PeriodTests)

BOOST_AUTO_TEST_CASE(testYearsMonthsAlgebra) {

    BOOST_TEST_MESSAGE("Testing period algebra on years/months...");

    Period OneYear(1, Years);
    Period SixMonths(6, Months);
    Period ThreeMonths(3, Months);

    Integer n = 4;
    if (OneYear/n!=ThreeMonths)
        BOOST_ERROR("division error: " << OneYear << "/" << n <<
                    " not equal to " << ThreeMonths);
    n = 2;
    if (OneYear/n!=SixMonths)
        BOOST_ERROR("division error: " << OneYear << "/" << n <<
                    " not equal to " << SixMonths);

    Period sum=ThreeMonths;
    sum+=SixMonths;
    if (sum!=Period(9, Months))
        BOOST_ERROR("sum error: " << ThreeMonths <<
                    " + " << SixMonths <<
                    " != " << Period(9, Months));

    sum+=OneYear;
    if (sum!=Period(21, Months))
        BOOST_ERROR("sum error: " << ThreeMonths <<
                    " + " << SixMonths <<
                    " + " << OneYear <<
                    " != " << Period(21, Months));

    Period TwelveMonths(12, Months);
    if (TwelveMonths.length()!=12)
        BOOST_ERROR("normalization error: TwelveMonths.length()" <<
                    " is " << TwelveMonths.length() <<
                    " instead of 12");
    if (TwelveMonths.units()!=Months)
        BOOST_ERROR("normalization error: TwelveMonths.units()" <<
                    " is " << TwelveMonths.units() <<
                    " instead of " << Months);

    Period NormalizedTwelveMonths(12, Months);
    NormalizedTwelveMonths.normalize();
    if (NormalizedTwelveMonths.length()!=1)
        BOOST_ERROR("normalization error: NormalizedTwelveMonths.length()" <<
                    " is " << NormalizedTwelveMonths.length() <<
                    " instead of 1");
    if (NormalizedTwelveMonths.units()!=Years)
        BOOST_ERROR("normalization error: NormalizedTwelveMonths.units()" <<
                    " is " << NormalizedTwelveMonths.units() <<
                    " instead of " << Years);
}

BOOST_AUTO_TEST_CASE(testWeeksDaysAlgebra) {

    BOOST_TEST_MESSAGE("Testing period algebra on weeks/days...");

    Period TwoWeeks(2, Weeks);
    Period OneWeek(1, Weeks);
    Period ThreeDays(3, Days);
    Period OneDay(1, Days);
    Period ZeroDays(0, Days);

    Integer n = 2;
    if (TwoWeeks/n!=OneWeek)
        BOOST_ERROR("division error: " << TwoWeeks << "/" << n <<
                    " not equal to " << OneWeek);
    n = 7;
    if (OneWeek/n!=OneDay)
        BOOST_ERROR("division error: " << OneWeek << "/" << n <<
                    " not equal to " << OneDay);

    Period sum=ThreeDays;
    sum+=OneDay;
    if (sum!=Period(4, Days))
        BOOST_ERROR("sum error: " << ThreeDays <<
                    " + " << OneDay <<
                    " != " << Period(4, Days));

    sum+=OneWeek;
    if (sum!=Period(11, Days))
        BOOST_ERROR("sum error: " << ThreeDays <<
                    " + " << OneDay <<
                    " + " << OneWeek <<
                    " != " << Period(11, Days));

    BOOST_TEST((OneWeek + ZeroDays) == OneWeek);
    BOOST_TEST((OneWeek + 3*OneDay) == Period(10, Days));
    BOOST_TEST((OneWeek + 7*OneDay) == TwoWeeks);

    Period SevenDays(7, Days);
    if (SevenDays.length()!=7)
        BOOST_ERROR("normalization error: SevenDays.length()" <<
                    " is " << SevenDays.length() <<
                    " instead of 7");
    if (SevenDays.units()!=Days)
        BOOST_ERROR("normalization error: SevenDays.units()" <<
                    " is " << SevenDays.units() <<
                    " instead of " << Days);
}

BOOST_AUTO_TEST_CASE(testOperators) {
    BOOST_TEST_MESSAGE("Testing period operators...");

    Period p(3, Days);
    p *= 2;
    BOOST_TEST(p == Period(6, Days));

    p -= Period(2, Days);
    BOOST_TEST(p == Period(4, Days));
}

BOOST_AUTO_TEST_CASE(testConvertToYears) {
    BOOST_TEST_MESSAGE("Testing conversion of periods to years...");

    BOOST_TEST(years(Period(0, Years)) == 0);
    BOOST_TEST(years(Period(1, Years)) == 1);
    BOOST_TEST(years(Period(5, Years)) == 5);

    const auto tol = boost::test_tools::tolerance<Real>(1e-15);
    BOOST_TEST(years(Period(1, Months)) == 1.0/12.0, tol);
    BOOST_TEST(years(Period(8, Months)) == 8.0/12.0, tol);
    BOOST_TEST(years(Period(12, Months)) == 1);
    BOOST_TEST(years(Period(18, Months)) == 1.5, tol);
}

BOOST_AUTO_TEST_CASE(testConvertToMonths) {
    BOOST_TEST_MESSAGE("Testing conversion of periods to months...");

    BOOST_TEST(months(Period(0, Months)) == 0);
    BOOST_TEST(months(Period(1, Months)) == 1);
    BOOST_TEST(months(Period(5, Months)) == 5);

    BOOST_TEST(months(Period(1, Years)) == 12);
    BOOST_TEST(months(Period(3, Years)) == 36);
}

BOOST_AUTO_TEST_CASE(testConvertToWeeks) {
    BOOST_TEST_MESSAGE("Testing conversion of periods to weeks...");

    BOOST_TEST(weeks(Period(0, Weeks)) == 0);
    BOOST_TEST(weeks(Period(1, Weeks)) == 1);
    BOOST_TEST(weeks(Period(5, Weeks)) == 5);

    const auto tol = boost::test_tools::tolerance<Real>(1e-15);
    BOOST_TEST(weeks(Period(1, Days)) == 1.0/7.0, tol);
    BOOST_TEST(weeks(Period(3, Days)) == 3.0/7.0, tol);
    BOOST_TEST(weeks(Period(11, Days)) == 11.0/7.0, tol);
}

BOOST_AUTO_TEST_CASE(testNormalization) {

    BOOST_TEST_MESSAGE("Testing period normalization...");

    Period test_values[] = {
        0 * Days,
        0 * Weeks,
        0 * Months,
        0 * Years,
        3 * Days,
        7 * Days,
        14 * Days,
        30 * Days,
        60 * Days,
        365 * Days,
        1 * Weeks,
        2 * Weeks,
        4 * Weeks,
        8 * Weeks,
        52 * Weeks,
        1 * Months,
        2 * Months,
        6 * Months,
        12 * Months,
        18 * Months,
        24 * Months,
        1 * Years,
        2 * Years
    };

    for (Period p1 : test_values) {
        auto n1 = p1.normalized();
        if (n1 != p1) {
            BOOST_ERROR("Normalizing " << p1 << " yields " << n1 << ", which compares different");
        }

        for (Period p2 : test_values) {
            auto n2 = p2.normalized();
            ext::optional<bool> comparison;
            try {
                comparison = (p1 == p2);
            } catch (Error&) {
                ;
            }

            if (comparison && *comparison) {
                // periods which compare equal must normalize to exactly the same period
                if (n1.units() != n2.units() || n1.length() != n2.length()) {
                    BOOST_ERROR(p1 << " and " << p2 << " compare equal, but normalize to "
                                << n1 << " and " << n2 << " respectively");
                }
            }

            if (n1.units() == n2.units() && n1.length() == n2.length()) {
                // periods normalizing to exactly the same period must compare equal
                if (p1 != p2) {
                    BOOST_ERROR(p1 << " and " << p2 << " compare different, but normalize to "
                                << n1 << " and " << n2 << " respectively");
                }
            }
        }
    }

}

BOOST_AUTO_TEST_CASE(testFrequencyComputation) {
    BOOST_TEST_MESSAGE("Testing computation of frequency from period...");

    // frequency -> period -> frequency == initial frequency?
    for (const Frequency f : {NoFrequency, Once, Annual, Semiannual, EveryFourthMonth, Quarterly,
                              Bimonthly, Monthly, EveryFourthWeek, Biweekly, Weekly, Daily}) {
        BOOST_TEST(Period(f).frequency() == f);
    }
    BOOST_CHECK_THROW(Period(OtherFrequency).frequency(), QuantLib::Error);

    // test Period(count, timeUnit).frequency()
    BOOST_TEST(Period(1, Years).frequency() == Annual);
    BOOST_TEST(Period(6, Months).frequency() == Semiannual);
    BOOST_TEST(Period(4, Months).frequency() == EveryFourthMonth);
    BOOST_TEST(Period(3, Months).frequency() == Quarterly);
    BOOST_TEST(Period(2, Months).frequency() == Bimonthly);
    BOOST_TEST(Period(1, Months).frequency() == Monthly);
    BOOST_TEST(Period(4, Weeks).frequency() == EveryFourthWeek);
    BOOST_TEST(Period(2, Weeks).frequency() == Biweekly);
    BOOST_TEST(Period(1, Weeks).frequency() == Weekly);
    BOOST_TEST(Period(1, Days).frequency() == Daily);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()