Skip to content

Commit

Permalink
Merge pull request #1135.
Browse files Browse the repository at this point in the history
Currency constructor
  • Loading branch information
lballabio committed Jul 1, 2021
2 parents 17d4c6e + 96b9fb0 commit 0d91e36
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ql/currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,24 @@ namespace QuantLib {
fractionSymbol(std::move(fractionSymbol)), fractionsPerUnit(fractionsPerUnit),
rounding(rounding), triangulated(std::move(triangulationCurrency)),
formatString(std::move(formatString)) {}

Currency::Currency(const std::string& name,
const std::string& code,
Integer numericCode,
const std::string& symbol,
const std::string& fractionSymbol,
Integer fractionsPerUnit,
const Rounding& rounding,
const std::string& formatString,
const Currency& triangulationCurrency)
: data_(ext::make_shared<Currency::Data>(name,
code,
numericCode,
symbol,
fractionSymbol,
fractionsPerUnit,
rounding,
formatString,
triangulationCurrency)) {}
}

12 changes: 12 additions & 0 deletions ql/currency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ namespace QuantLib {
//! %Currency specification
class Currency {
public:
//! \name Constructors
//@{
//! default constructor
/*! Instances built via this constructor have undefined
behavior. Such instances can only act as placeholders
and must be reassigned to a valid currency before being
used.
*/
Currency() = default;
Currency(const std::string& name,
const std::string& code,
Integer numericCode,
const std::string& symbol,
const std::string& fractionSymbol,
Integer fractionsPerUnit,
const Rounding& rounding,
const std::string& formatString,
const Currency& triangulationCurrency = Currency());
//@}
//! \name Inspectors
//@{
//! currency name, e.g, "U.S. Dollar"
Expand Down
2 changes: 2 additions & 0 deletions test-suite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(QuantLib-Test_SRC
creditdefaultswap.cpp
creditriskplus.cpp
crosscurrencyratehelpers.cpp
currency.cpp
curvestates.cpp
dates.cpp
daycounters.cpp
Expand Down Expand Up @@ -206,6 +207,7 @@ set(QuantLib-Test_HDR
creditdefaultswap.hpp
creditriskplus.hpp
crosscurrencyratehelpers.hpp
currency.hpp
curvestates.hpp
dates.hpp
daycounters.hpp
Expand Down
2 changes: 2 additions & 0 deletions test-suite/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ QL_TEST_SRCS = \
creditdefaultswap.cpp \
creditriskplus.cpp \
crosscurrencyratehelpers.cpp \
currency.cpp \
curvestates.cpp \
dates.cpp \
daycounters.cpp \
Expand Down Expand Up @@ -202,6 +203,7 @@ QL_TEST_HDRS = \
creditdefaultswap.hpp \
creditriskplus.hpp \
crosscurrencyratehelpers.hpp \
currency.hpp \
curvestates.hpp \
dates.hpp \
daycounters.hpp \
Expand Down
60 changes: 60 additions & 0 deletions test-suite/currency.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2021 Marcin Rybacki
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
<http://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 "currency.hpp"
#include "utilities.hpp"
#include <ql/currency.hpp>

using namespace QuantLib;
using namespace boost::unit_test_framework;

void CurrencyTest::testBespokeConstructor() {
BOOST_TEST_MESSAGE("Testing bespoke currency constructor...");

std::string name("Some Currency");
std::string code("CCY");
std::string symbol("#");

Currency customCcy(name, code, 100, symbol, "", 100, Rounding(), "");

if (customCcy.empty())
BOOST_ERROR("Failed to create bespoke currency.");

if (customCcy.name() != name)
BOOST_ERROR("incorrect currency name\n"
<< " actual: " << customCcy.name() << "\n"
<< " expected: " << name << "\n");

if (customCcy.code() != code)
BOOST_ERROR("incorrect currency code\n"
<< " actual: " << customCcy.code() << "\n"
<< " expected: " << code << "\n");

if (customCcy.symbol() != symbol)
BOOST_ERROR("incorrect currency symbol\n"
<< " actual: " << customCcy.symbol() << "\n"
<< " expected: " << symbol << "\n");
}

test_suite* CurrencyTest::suite() {
auto* suite = BOOST_TEST_SUITE("Currency tests");

suite->add(QUANTLIB_TEST_CASE(&CurrencyTest::testBespokeConstructor));

return suite;
}
32 changes: 32 additions & 0 deletions test-suite/currency.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Marcin Rybacki
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
<http://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.
*/

#ifndef quantlib_test_currency_hpp
#define quantlib_test_currency_hpp

#include <boost/test/unit_test.hpp>

class CurrencyTest {
public:
static void testBespokeConstructor();

static boost::unit_test_framework::test_suite* suite();
};

#endif
2 changes: 2 additions & 0 deletions test-suite/quantlibtestsuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "creditdefaultswap.hpp"
#include "creditriskplus.hpp"
#include "crosscurrencyratehelpers.hpp"
#include "currency.hpp"
#include "curvestates.hpp"
#include "dates.hpp"
#include "daycounters.hpp"
Expand Down Expand Up @@ -389,6 +390,7 @@ test_suite* init_unit_test_suite(int, char* []) {
test->add(CPISwapTest::suite());
test->add(CreditDefaultSwapTest::suite());
test->add(CrossCurrencyRateHelpersTest::suite());
test->add(CurrencyTest::suite());
test->add(CurveStatesTest::suite());
test->add(DateTest::suite(speed));
test->add(DayCounterTest::suite());
Expand Down
2 changes: 2 additions & 0 deletions test-suite/testsuite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@
<ClCompile Include="covariance.cpp" />
<ClCompile Include="creditdefaultswap.cpp" />
<ClCompile Include="creditriskplus.cpp" />
<ClCompile Include="currency.cpp" />
<ClCompile Include="curvestates.cpp" />
<ClCompile Include="dates.cpp" />
<ClCompile Include="daycounters.cpp" />
Expand Down Expand Up @@ -836,6 +837,7 @@
<ClInclude Include="covariance.hpp" />
<ClInclude Include="creditdefaultswap.hpp" />
<ClInclude Include="creditriskplus.hpp" />
<ClInclude Include="currency.hpp" />
<ClInclude Include="curvestates.hpp" />
<ClInclude Include="dates.hpp" />
<ClInclude Include="daycounters.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions test-suite/testsuite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@
<ClCompile Include="zerocouponswap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="currency.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="americanoption.hpp">
Expand Down Expand Up @@ -983,5 +986,8 @@
<ClInclude Include="zerocouponswap.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="currency.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 0d91e36

Please sign in to comment.