Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
portfolio.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <boost/make_shared.hpp>
20#include <boost/test/unit_test.hpp>
23#include <oret/toplevelfixture.hpp>
24
25using namespace QuantLib;
26using namespace boost::unit_test_framework;
27using namespace std;
28using namespace ore::data;
29
30BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
31
32BOOST_AUTO_TEST_SUITE(PortfolioTests)
33
34BOOST_AUTO_TEST_CASE(testAddTrades) {
35 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
36 QuantLib::ext::shared_ptr<FxForward> trade1 = QuantLib::ext::make_shared<FxForward>();
37 QuantLib::ext::shared_ptr<FxForward> trade2 = QuantLib::ext::make_shared<FxForward>();
38 trade1->id() = "1";
39 trade2->id() = "2";
40 BOOST_CHECK_EQUAL(portfolio->has(trade1->id()), false);
41 BOOST_CHECK_EQUAL(portfolio->size(), 0);
42 portfolio->add(trade1);
43 BOOST_CHECK_EQUAL(portfolio->has(trade1->id()), true);
44 BOOST_CHECK_EQUAL(portfolio->size(), 1);
45 portfolio->add(trade2);
46 BOOST_CHECK_EQUAL(portfolio->has(trade2->id()), true);
47 BOOST_CHECK_EQUAL(portfolio->size(), 2);
48}
49
50BOOST_AUTO_TEST_CASE(testAddTradeWithExistingId) {
51 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
52 QuantLib::ext::shared_ptr<FxForward> trade1 = QuantLib::ext::make_shared<FxForward>();
53 QuantLib::ext::shared_ptr<FxForward> trade2 = QuantLib::ext::make_shared<FxForward>();
54 portfolio->add(trade1);
55 BOOST_CHECK_THROW(portfolio->add(trade2), exception);
56}
57
59 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
60 QuantLib::ext::shared_ptr<FxForward> trade = QuantLib::ext::make_shared<FxForward>();
61 portfolio->add(trade);
62 BOOST_CHECK_EQUAL(portfolio->size(), 1);
63 portfolio->clear();
64 BOOST_CHECK_EQUAL(portfolio->size(), 0);
65}
66
68 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
69 QuantLib::ext::shared_ptr<FxForward> trade1 = QuantLib::ext::make_shared<FxForward>();
70 QuantLib::ext::shared_ptr<FxForward> trade2 = QuantLib::ext::make_shared<FxForward>();
71 trade1->id() = "1";
72 trade2->id() = "2";
73 BOOST_CHECK_EQUAL(portfolio->size(), 0);
74 portfolio->add(trade1);
75 BOOST_CHECK_EQUAL(portfolio->size(), 1);
76 portfolio->add(trade2);
77 BOOST_CHECK_EQUAL(portfolio->size(), 2);
78}
79
81 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
82 QuantLib::ext::shared_ptr<FxForward> trade = QuantLib::ext::make_shared<FxForward>();
83 BOOST_CHECK_EQUAL(portfolio->has(trade->id()), false);
84 portfolio->add(trade);
85 BOOST_CHECK_EQUAL(portfolio->has(trade->id()), true);
86 portfolio->remove(trade->id());
87 BOOST_CHECK_EQUAL(portfolio->has(trade->id()), false);
88}
89
91 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
92 QuantLib::ext::shared_ptr<FxForward> trade1 = QuantLib::ext::make_shared<FxForward>();
93 QuantLib::ext::shared_ptr<FxForward> trade2 = QuantLib::ext::make_shared<FxForward>();
94 std::map<std::string, QuantLib::ext::shared_ptr<Trade>> trade_list;
95 trade1->id() = "1";
96 trade2->id() = "2";
97 BOOST_CHECK(portfolio->trades() == trade_list);
98 portfolio->add(trade1);
99 trade_list["1"] = trade1;
100 BOOST_CHECK(portfolio->trades() == trade_list);
101 portfolio->add(trade2);
102 trade_list["2"] = trade2;
103 BOOST_CHECK(portfolio->trades() == trade_list);
104}
105
107 QuantLib::ext::shared_ptr<Portfolio> portfolio = QuantLib::ext::make_shared<Portfolio>();
108 QuantLib::ext::shared_ptr<FxForward> trade1 = QuantLib::ext::make_shared<FxForward>();
109 QuantLib::ext::shared_ptr<FxForward> trade2 = QuantLib::ext::make_shared<FxForward>();
110 trade1->id() = "1";
111 trade2->id() = "2";
112 std::set<std::string> trade_ids;
113 BOOST_CHECK(portfolio->ids() == trade_ids);
114 portfolio->add(trade1);
115 trade_ids.insert("1");
116 BOOST_CHECK(portfolio->ids() == trade_ids);
117 portfolio->add(trade2);
118 trade_ids.insert("2");
119 BOOST_CHECK(portfolio->ids() == trade_ids);
120}
121
122BOOST_AUTO_TEST_SUITE_END()
123
124BOOST_AUTO_TEST_SUITE_END()
FX Forward data model and serialization.
Portfolio class.
BOOST_AUTO_TEST_CASE(testAddTrades)
Definition: portfolio.cpp:34