diff --git a/bckp/option_tests.cpp b/bckp/option_tests.cpp index e0d96b2..e08e99b 100644 --- a/bckp/option_tests.cpp +++ b/bckp/option_tests.cpp @@ -5,8 +5,8 @@ #include #include "option.h" #include "payoff.h" -#include "test_support.h" -#include "mock_classes.h" +#include "../config/test_support.h" +#include "../config/mock_classes.h" using namespace ::testing; using namespace TestSupport::MockImplementations; diff --git a/include/option.h b/include/option.h index 6eb202f..9760e34 100644 --- a/include/option.h +++ b/include/option.h @@ -3,7 +3,6 @@ #include #include - #include "payoff.h" #include "market_data.h" diff --git a/include/payoff.h b/include/payoff.h index f3d1e5d..52e5510 100644 --- a/include/payoff.h +++ b/include/payoff.h @@ -3,7 +3,6 @@ #include - class Payoff { public: Payoff() = default; // Default (no parameter) constructor diff --git a/main_example.cpp b/main_example.cpp deleted file mode 100644 index 1e475e1..0000000 --- a/main_example.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include "option.h" -#include "payoff.h" -#include "market_data.h" - -int main() { - StockData stock(100.0, 0.02, 0.001); - std::cout << stock.getSigma() << std::endl; - - auto mdata = std::make_shared(); - mdata->addData("AAPL", 100.0, 0.02, 0.01); - - auto payoff_call = std::make_unique(100.0); - auto payoff_put = std::make_unique(100.0); - - - auto mdata = std::make_shared(); - mdata->addData("AAPL", 300.0, 0.24, 0.01); - mdata->addData("AMAZN", 120.0, 0.2, 0.01); - mdata->addData("TSLA", 70.0, 0.3, 0.01); - mdata->addData("LIBOR", 0.02); - mdata->addData("EURIBIBOR", 0.03); - mdata->addData("GBP", 1.25); - mdata->addData("EUR", 1.05); - - std::vector> underlyings_basket = {"AAPL", "AMAZN", "TSLA"}; - std::vector> underlyings_quanto = {"AAPL", "GBP"}; - - Option call(std::move(payoff_call), mdata, S = "AAPL", r = "LIBOR"); - BasketOption call_basket(std::move(payoff_basket_call), mdata, S = underlyings_basket, r = "LIBOR"); - QuantoOption call_quanto(std::move(payoff_quanto_call), mdata, S = underlyings_quanto, r = "LIBOR"); - - - - - - - - return 0; -} diff --git a/option_former.cpp b/option_former.cpp deleted file mode 100644 index 213bac4..0000000 --- a/option_former.cpp +++ /dev/null @@ -1,107 +0,0 @@ -// -// Created by anthony on 18/02/2024. -// - -#ifndef OPTION_PRICER_OPTION_CPP -#define OPTION_PRICER_OPTION_CPP - -#include "option.h" - - -Option::Option(std::unique_ptr _payoff, - const double& _T) - : T(_T) { - // Move the unique_ptr only after validating other parameters to avoid taking ownership if an exception is thrown - if (_T < 0) throw std::invalid_argument("Time to expiration (T) must be positive."); - // If all checks pass, then safely move the _payoff - payoff = std::move(_payoff); -} - -Option::~Option() {}; - -// other.payoff->clone() necessary because other.payoff is of type *Payoff which is abstract -Option::Option(const Option& other) - : payoff(other.payoff ? other.payoff->clone() : nullptr), - r(other.r), T(other.T), S(other.S), sigma(other.sigma) {} - -Option& Option::operator=(const Option& other) { - if (this != &other) { - payoff = other.payoff ? other.payoff->clone() : nullptr; - r = other.r; - T = other.T; - S = other.S; - sigma = other.sigma; - } - return *this; -} - - -double Option::get_r() const { - return r; -} - -double Option::get_T() const { - return T; -} - -double Option::get_S() const { - return S; -} - -double Option::get_sigma() const { - return sigma; -} - -void Option::set_r(const double& _r) { - if(_r < 0.0) { - throw std::invalid_argument("r must be a positive number!"); - } else { - r = _r; - } -} - -void Option::set_T(const double& _T) { - if(_T < 0) { - throw std::invalid_argument("T must be a positive number!"); - } else { - T = _T; - } -} - -void Option::set_S(const double& _S) { - if(_S < 0) { - throw std::invalid_argument("S must be a positive number!"); - } else { - S = _S; - } -} - -void Option::set_sigma(const double& _sigma) { - if(_sigma < 0) { - throw std::invalid_argument("sigma must be a positive number!"); - } else { - sigma = _sigma; - } -} - -bool Option::operator==(const Option& rhs) const { - return r == rhs.r && - T == rhs.T && - S == rhs.S && - sigma == rhs.sigma; -} - -bool Option::operator!=(const Option& rhs) const { - return !(rhs == *this); -} - -std::ostream &operator<<(std::ostream& os, const Option& option) { - os << " r: " << option.r << " T: " << option.T << " S: " << option.S << " sigma: " - << option.sigma; - return os; -} - - - - -#endif //OPTION_PRICER_OPTION_CPP \ No newline at end of file diff --git a/option_former.h b/option_former.h deleted file mode 100644 index c4f3cdf..0000000 --- a/option_former.h +++ /dev/null @@ -1,55 +0,0 @@ -// -// Created by anthony on 18/02/2024. -// - -#ifndef OPTION_PRICER_OPTION_H -#define OPTION_PRICER_OPTION_H - -#include -#include -#include - -#include "payoff.h" - -class Option { -protected: - std::unique_ptr payoff; - double T; - -public: - Option(std::unique_ptr _payoff, - const double& _T); // Parameter constructor - - // Virtual destructor for proper memory de-allocation in inheritance - virtual ~Option(); - - // Define deleted copy semantics because of smart pointers - Option(const Option& other); - Option& operator=(const Option& rhs); - - // Re-include the default moving constructor and assignment operator - Option(Option&&) = default; - Option& operator=(Option&&) = default; - - // Selectors (getters) for our option parameters - double get_T() const; - - // Modifiers (setters) for our option parameters - void set_T(const double& _T); - - // Equality and inequality overloaded operators - bool operator==(const Option& rhs) const; - bool operator!=(const Option& rhs) const; - - // Overloaded output stream operator - friend std::ostream &operator<<(std::ostream& os, const Option& option); - - // Helper method for checking proper moving semantics - [[nodiscard]] const std::unique_ptr& getPayoff() const { - return payoff; - } - - virtual double price() const = 0; -}; - -#endif //OPTION_PRICER_OPTION_H diff --git a/option_pricer_description.docx b/option_pricer_description.docx deleted file mode 100644 index ef03e02..0000000 Binary files a/option_pricer_description.docx and /dev/null differ diff --git a/option_tests_former.cpp b/option_tests_former.cpp deleted file mode 100644 index 9017be4..0000000 --- a/option_tests_former.cpp +++ /dev/null @@ -1,91 +0,0 @@ - -// Test Suite for Option Class - -#include - -#include "option.h" -#include "payoff.h" -#include "market_data.h" - -/* Tests the move semantics of the Option class to ensure that moving an Option object - leaves the source object in a valid, but "empty" state, and that the target object - correctly acquires the source's resources. */ - -class OptionTest : public ::testing::Test { -protected: - std::shared_ptr marketData; - - void SetUp() override { - marketData = std::make_shared(); - // Assume addData is a method to add stock data - marketData->addData("AAPL", 150.0, 0.2); - marketData->addData("LIBOR", 0.05); - - double K = 100.0; - auto payoff_call = std::make_unique(K); - Option call(std::move(payoff_call), marketData, 1.0); - - auto payoff_put = std::make_unique(K); - Option put(std::move(payoff_put), marketData, 1.0); -}; - -TEST_F(OptionTest, MovingSemantics) { - // Action: Move call into option3, then move option3 into option2 - - Option call_mov_1 = std::move(call); // Using the moving constructor - option2 = std::move(option3); // Using the moving assignment operator - - // Verify: Check that option1 and option3 are "empty", and option2 has successfully - // acquired the resources originally belonging to option1. - EXPECT_EQ(call.getPayoff().get(), nullptr); - EXPECT_NE(option2.getPayoff().get(), nullptr); - EXPECT_EQ(option3.getPayoff().get(), nullptr); - - -} - -/* Tests the copy semantics of the Option class, assuming the class supports copy operations. - This test verifies that copying an Option object results in a new object with the same - attributes but does not affect the original object's state upon modification. */ -TEST_F(OptionTest, CopySemantics) { - // Setup: Create an Option object with a PayoffCall. - double K1 = 100.0; - auto payoff_call1 = std::make_unique(K1); - auto payoff_call2 = std::make_unique(K1); - - Option option1(std::move(payoff_call1), 0.05, 1.0, 150.0, 0.2); - Option option2(std::move(payoff_call2), 0.05, 1.0, 150.0, 0.2); - - // Perform a copy operation to create option2 and option3 from option1 - Option option3(option1); // Using copy constructor - option2 = option1; // Using copy assignment operator - - // Check if the attributes have been correctly copied using guetters - EXPECT_EQ(option1.get_T(), option3.get_T()); // For copy constructor - - EXPECT_EQ(option1.get_T(), option2.get_T()); // For copy assignment operator - - //Using overloaded == operator - EXPECT_TRUE(option1 == option2); - EXPECT_TRUE(option1 == option3); - - // Modify option1 to see if it affects both option2 and option3 - double K2 = 50.0; - auto payoff_change = std::make_unique(K2); - option1 = Option(std::move(payoff_change), 0.10, 2.0, 100.0, 0.3); - - // Check that option2's and option3's payoff is not affected by changes to option1 - EXPECT_NE(option2.getPayoff().get(), nullptr); - EXPECT_NE(option3.getPayoff().get(), nullptr); - - EXPECT_EQ(option2.get_T(), 1.0); // Original maturity before option1 was changed - EXPECT_EQ(option3.get_T(), 1.0); - EXPECT_EQ(option1.get_T(), 2.0); - - // Using overloaded != operator - EXPECT_TRUE(option1 != option2); - EXPECT_TRUE(option1 != option3); -} - - - diff --git a/src/option.cpp b/src/option.cpp index e20290c..12dac28 100644 --- a/src/option.cpp +++ b/src/option.cpp @@ -5,7 +5,6 @@ #include #include #include - #include "option.h" /* ***************** diff --git a/src/payoff.cpp b/src/payoff.cpp index 01dd0e8..70bb38f 100644 --- a/src/payoff.cpp +++ b/src/payoff.cpp @@ -4,7 +4,6 @@ #include "payoff.h" #include - std::ostream& operator<<(std::ostream& os, const Payoff& payoff) { payoff.print(os); return os; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9470a9c..1ed2f0f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,11 +4,11 @@ find_package(GTest REQUIRED) # Define the test executable add_executable(Option_tester test_main.cpp - test_market_data.cpp - test_payoff.cpp - mock_classes.h - test_support.h - test_option.cpp + unit/test_market_data.cpp + unit/test_payoff.cpp + config/mock_classes.h + config/test_support.h + unit/test_option.cpp ../src/payoff.cpp ../src/market_data.cpp ../src/option.cpp diff --git a/tests/mock_classes.h b/tests/config/mock_classes.h similarity index 100% rename from tests/mock_classes.h rename to tests/config/mock_classes.h diff --git a/tests/test_support.h b/tests/config/test_support.h similarity index 96% rename from tests/test_support.h rename to tests/config/test_support.h index f0895c5..f4f0ec2 100644 --- a/tests/test_support.h +++ b/tests/config/test_support.h @@ -2,7 +2,7 @@ #define TEST_CLASSES_H #include -#include "market_data.h" +#include "../../include/market_data.h" namespace TestSupport { // Minimal Mock Option class diff --git a/tests/test_option.cpp b/tests/test_option.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_market_data.cpp b/tests/unit/test_market_data.cpp similarity index 79% rename from tests/test_market_data.cpp rename to tests/unit/test_market_data.cpp index 817dfbb..0d36470 100644 --- a/tests/test_market_data.cpp +++ b/tests/unit/test_market_data.cpp @@ -1,24 +1,7 @@ #include -#include "market_data.h" #include - -// TestOption to avoid conflict name with Option class from option.h -namespace TestOption { - // Minimal MockOption class - class Option final : public MarketDataObserver, std::enable_shared_from_this