[go: up one dir, main page]

1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004 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 "money.hpp"
21#include "utilities.hpp"
22#include <ql/money.hpp>
23#include <ql/currencies/europe.hpp>
24#include <ql/currencies/america.hpp>
25#include <ql/currencies/exchangeratemanager.hpp>
26
27using namespace QuantLib;
28using namespace boost::unit_test_framework;
29
30void MoneyTest::testNone() {
31
32 BOOST_TEST_MESSAGE("Testing money arithmetic without conversions...");
33
34 Currency EUR = EURCurrency();
35
36 Money m1 = 50000.0 * EUR;
37 Money m2 = 100000.0 * EUR;
38 Money m3 = 500000.0 * EUR;
39
40 Money::Settings::instance().conversionType() = Money::NoConversion;
41
42 Money calculated = m1*3.0 + 2.5*m2 - m3/5.0;
43 Decimal x = m1.value()*3.0 + 2.5*m2.value() - m3.value()/5.0;
44 Money expected(x, EUR);
45
46 if (calculated != expected) {
47 BOOST_FAIL("Wrong result: \n"
48 << " expected: " << expected << "\n"
49 << " calculated: " << calculated);
50 }
51}
52
53
54void MoneyTest::testBaseCurrency() {
55
56 BOOST_TEST_MESSAGE("Testing money arithmetic with conversion "
57 "to base currency...");
58
59 Currency EUR = EURCurrency(), GBP = GBPCurrency(), USD = USDCurrency();
60
61 Money m1 = 50000.0 * GBP;
62 Money m2 = 100000.0 * EUR;
63 Money m3 = 500000.0 * USD;
64
65 ExchangeRateManager::instance().clear();
66 ExchangeRate eur_usd = ExchangeRate(EUR, USD, 1.2042);
67 ExchangeRate eur_gbp = ExchangeRate(EUR, GBP, 0.6612);
68 ExchangeRateManager::instance().add(eur_usd);
69 ExchangeRateManager::instance().add(eur_gbp);
70
71 auto & money_settings = Money::Settings::instance();
72 money_settings.conversionType() = Money::BaseCurrencyConversion;
73 money_settings.baseCurrency() = EUR;
74
75 Money calculated = m1*3.0 + 2.5*m2 - m3/5.0;
76
77 Rounding round = money_settings.baseCurrency().rounding();
78 Decimal x = round(m1.value()*3.0/eur_gbp.rate()) + 2.5*m2.value()
79 - round(m3.value()/(5.0*eur_usd.rate()));
80 Money expected(x, EUR);
81
82 money_settings.conversionType() = Money::NoConversion;
83
84 if (calculated != expected) {
85 BOOST_FAIL("Wrong result: \n"
86 << " expected: " << expected << "\n"
87 << " calculated: " << calculated);
88 }
89}
90
91
92void MoneyTest::testAutomated() {
93
94 BOOST_TEST_MESSAGE("Testing money arithmetic with automated conversion...");
95
96 Currency EUR = EURCurrency(), GBP = GBPCurrency(), USD = USDCurrency();
97
98 Money m1 = 50000.0 * GBP;
99 Money m2 = 100000.0 * EUR;
100 Money m3 = 500000.0 * USD;
101
102 ExchangeRateManager::instance().clear();
103 ExchangeRate eur_usd = ExchangeRate(EUR, USD, 1.2042);
104 ExchangeRate eur_gbp = ExchangeRate(EUR, GBP, 0.6612);
105 ExchangeRateManager::instance().add(eur_usd);
106 ExchangeRateManager::instance().add(eur_gbp);
107
108 auto & money_settings = Money::Settings::instance();
109 money_settings.conversionType() = Money::AutomatedConversion;
110
111 Money calculated = (m1*3.0 + 2.5*m2) - m3/5.0;
112
113 Rounding round = m1.currency().rounding();
114 Decimal x = m1.value()*3.0 + round(2.5*m2.value()*eur_gbp.rate())
115 - round((m3.value()/5.0)*eur_gbp.rate()/eur_usd.rate());
116 Money expected(x, GBP);
117
118 money_settings.conversionType() = Money::NoConversion;
119
120 if (calculated != expected) {
121 BOOST_FAIL("Wrong result: \n"
122 << " expected: " << expected << "\n"
123 << " calculated: " << calculated);
124 }
125}
126
127test_suite* MoneyTest::suite() {
128 auto* suite = BOOST_TEST_SUITE("Money tests");
129 suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testNone));
130 suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testBaseCurrency));
131 suite->add(QUANTLIB_TEST_CASE(&MoneyTest::testAutomated));
132 return suite;
133}
134
135

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