Skip to content

Commit

Permalink
Add PublicTrade unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 24, 2024
1 parent 0566c85 commit 53d4250
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/src/processcommandsfromcli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 10 additions & 2 deletions src/objects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions src/objects/include/publictrade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/objects/src/publictrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 56 additions & 0 deletions src/objects/test/publictrade_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "publictrade.hpp"

#include <gtest/gtest.h>

namespace cct {
class PublicTradeTest : public ::testing::Test {
protected:
TimePoint tp1{milliseconds{std::numeric_limits<int64_t>::max() / 10000000}};
TimePoint tp2{milliseconds{std::numeric_limits<int64_t>::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<TradeSide>(-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
5 changes: 5 additions & 0 deletions src/tech/include/cachedresultvault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <memory>

#include "cct_type_traits.hpp"
#include "cct_vector.hpp"
#include "timedef.hpp"

Expand Down Expand Up @@ -54,6 +55,10 @@ class CachedResultVaultT {
private:
using CachedResultPtrs = vector<CachedResultBase<DurationT> *>;

public:
using trivially_relocatable = is_trivially_relocatable<CachedResultPtrs>::type;

private:
CachedResultPtrs _cachedResults;
bool _allFrozen = false;
};
Expand Down

0 comments on commit 53d4250

Please sign in to comment.