From 53d4250d1e73670afe906fece6e32fda690529d5 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sun, 24 Mar 2024 20:25:33 +0100 Subject: [PATCH] Add PublicTrade unit test --- src/main/src/processcommandsfromcli.cpp | 4 +- src/objects/CMakeLists.txt | 12 +++++- src/objects/include/publictrade.hpp | 2 + src/objects/src/publictrade.cpp | 19 +++++++++ src/objects/test/publictrade_test.cpp | 56 +++++++++++++++++++++++++ src/tech/include/cachedresultvault.hpp | 5 +++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/objects/test/publictrade_test.cpp diff --git a/src/main/src/processcommandsfromcli.cpp b/src/main/src/processcommandsfromcli.cpp index 0666e0b8..98076226 100644 --- a/src/main/src/processcommandsfromcli.cpp +++ b/src/main/src/processcommandsfromcli.cpp @@ -22,9 +22,9 @@ void ProcessCommandsFromCLI(std::string_view programName, const CoincenterComman try { Coincenter coincenter(coincenterInfo, ExchangeSecretsInfo_Create(generalOptions)); - int nbCommandsProcessed = coincenter.process(coincenterCommands); + const auto nbCommandsProcessed = coincenter.process(coincenterCommands); - if (nbCommandsProcessed > 0) { + if (nbCommandsProcessed != 0) { // Write potentially updated cache data on disk at end of program coincenter.updateFileCaches(); } diff --git a/src/objects/CMakeLists.txt b/src/objects/CMakeLists.txt index e9886873..b1468488 100644 --- a/src/objects/CMakeLists.txt +++ b/src/objects/CMakeLists.txt @@ -86,10 +86,18 @@ add_unit_test( add_unit_test( parseloglevel_test - src/parseloglevel.cpp test/parseloglevel_test.cpp LIBRARIES - coincenter_tech + coincenter_objects + DEFINITIONS + CCT_DISABLE_SPDLOG +) + +add_unit_test( + publictrade_test + test/publictrade_test.cpp + LIBRARIES + coincenter_objects DEFINITIONS CCT_DISABLE_SPDLOG ) diff --git a/src/objects/include/publictrade.hpp b/src/objects/include/publictrade.hpp index d5bf2943..236e0e50 100644 --- a/src/objects/include/publictrade.hpp +++ b/src/objects/include/publictrade.hpp @@ -28,6 +28,8 @@ class PublicTrade { string timeStr() const; + bool isValid() const; + /// 3 way operator - make compiler generate all 6 operators (including == and !=) /// we order by time first, then amount, price, etc. Do not change the fields order! std::strong_ordering operator<=>(const PublicTrade&) const noexcept = default; diff --git a/src/objects/src/publictrade.cpp b/src/objects/src/publictrade.cpp index 423275d0..36e401a6 100644 --- a/src/objects/src/publictrade.cpp +++ b/src/objects/src/publictrade.cpp @@ -7,4 +7,23 @@ namespace cct { string PublicTrade::timeStr() const { return ToString(_time); } +bool PublicTrade::isValid() const { + if (time() == TimePoint{}) { + return false; + } + if (amount() <= 0 || amount().hasNeutralCurrency()) { + return false; + } + if (price() <= 0 || price().hasNeutralCurrency()) { + return false; + } + if (amount().currencyCode() == price().currencyCode()) { + return false; + } + if (side() != TradeSide::kBuy && side() != TradeSide::kSell) { + return false; + } + return true; +} + } // namespace cct \ No newline at end of file diff --git a/src/objects/test/publictrade_test.cpp b/src/objects/test/publictrade_test.cpp new file mode 100644 index 00000000..79e8f264 --- /dev/null +++ b/src/objects/test/publictrade_test.cpp @@ -0,0 +1,56 @@ +#include "publictrade.hpp" + +#include + +namespace cct { +class PublicTradeTest : public ::testing::Test { + protected: + TimePoint tp1{milliseconds{std::numeric_limits::max() / 10000000}}; + TimePoint tp2{milliseconds{std::numeric_limits::max() / 9000000}}; + + Market market{"ETH", "USDT"}; + MonetaryAmount amount1{"3.7", market.base()}; + MonetaryAmount amount2{"0.13", market.base()}; + MonetaryAmount amount3{"0.55", market.base()}; + + MonetaryAmount price1{"1500.5", market.quote()}; + MonetaryAmount price2{"1501", market.quote()}; + + PublicTrade pt1{TradeSide::kBuy, amount1, price1, tp1}; + PublicTrade pt2{TradeSide::kSell, amount2, price2, tp2}; + PublicTrade pt3{TradeSide::kSell, amount3, price2, tp1}; +}; + +TEST_F(PublicTradeTest, Validity) { + EXPECT_TRUE(pt1.isValid()); + EXPECT_TRUE(pt2.isValid()); + EXPECT_TRUE(pt3.isValid()); + + EXPECT_FALSE(PublicTrade(static_cast(-1), amount1, price1, tp1).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, amount1, amount2, tp1).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, amount1, price1, TimePoint{}).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, MonetaryAmount{}, price1, tp1).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, amount1, MonetaryAmount{0, market.quote()}, tp1).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, -amount1, price1, tp1).isValid()); + EXPECT_FALSE(PublicTrade(TradeSide::kBuy, amount1, -price1, tp1).isValid()); +} + +TEST_F(PublicTradeTest, Members) { + EXPECT_EQ(pt1.side(), TradeSide::kBuy); + EXPECT_EQ(pt1.market(), market); + EXPECT_EQ(pt1.amount(), amount1); + EXPECT_EQ(pt1.price(), price1); + EXPECT_EQ(pt1.time(), tp1); + + EXPECT_TRUE(pt1.isValid()); + EXPECT_EQ(pt1.timeStr(), "1999-03-25T04:46:43Z"); +} + +TEST_F(PublicTradeTest, Comparison) { + EXPECT_NE(pt1, pt2); + EXPECT_NE(pt1, pt3); + + EXPECT_LT(pt1, pt2); + EXPECT_GT(pt1, pt3); +} +} // namespace cct \ No newline at end of file diff --git a/src/tech/include/cachedresultvault.hpp b/src/tech/include/cachedresultvault.hpp index 8b4252c9..c29fa6ae 100644 --- a/src/tech/include/cachedresultvault.hpp +++ b/src/tech/include/cachedresultvault.hpp @@ -2,6 +2,7 @@ #include +#include "cct_type_traits.hpp" #include "cct_vector.hpp" #include "timedef.hpp" @@ -54,6 +55,10 @@ class CachedResultVaultT { private: using CachedResultPtrs = vector *>; + public: + using trivially_relocatable = is_trivially_relocatable::type; + + private: CachedResultPtrs _cachedResults; bool _allFrozen = false; };