From 8787e81cb0f438497e877a9062369fb24f65dab8 Mon Sep 17 00:00:00 2001 From: ttldtor Date: Tue, 5 Dec 2023 14:46:24 +0300 Subject: [PATCH] Trade tests --- .../event/market/TradeBase.hpp | 9 ++-- tests/api/EventsTest.cpp | 48 ++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp b/include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp index 07809d80..d9fbaa43 100644 --- a/include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp +++ b/include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp @@ -129,9 +129,10 @@ class DXFCPP_EXPORT TradeBase : public MarketEvent, public LastingEvent { * @param time time of the last trade. */ void setTime(std::int64_t time) noexcept { - tradeBaseData_.timeSequence = orOp( - sal(static_cast(time_util::getSecondsFromTime(time)), SECONDS_SHIFT), - orOp(sal(static_cast(time_util::getMillisFromTime(time)), MILLISECONDS_MASK), getSequence())); + tradeBaseData_.timeSequence = + orOp(orOp(sal(static_cast(time_util::getSecondsFromTime(time)), SECONDS_SHIFT), + sal(static_cast(time_util::getMillisFromTime(time)), MILLISECONDS_SHIFT)), + getSequence()); } /** @@ -212,7 +213,7 @@ class DXFCPP_EXPORT TradeBase : public MarketEvent, public LastingEvent { * @return exchange code of last trade as UTF8 string. */ std::string getExchangeCodeString() const noexcept { - //TODO: cache [EN-8231] + // TODO: cache [EN-8231] return dxfcpp::utf16toUtf8String(tradeBaseData_.exchangeCode); } diff --git a/tests/api/EventsTest.cpp b/tests/api/EventsTest.cpp index d7b52aa5..3ca376c2 100644 --- a/tests/api/EventsTest.cpp +++ b/tests/api/EventsTest.cpp @@ -267,7 +267,6 @@ struct SeriesConstants { static constexpr std::uint32_t MAX_SEQUENCE = (1U << 22U) - 1U; }; - TEST_CASE("Series::setTime() should change TimeSequence") { auto s = Series("AAPL").withSequence(123); std::int64_t time = 1'701'703'226'578LL; @@ -317,4 +316,49 @@ TEST_CASE("Series::setIndex() shouldn't change Sequence") { s.setIndex(1'234'567'891'011LL); REQUIRE(oldSequence == s.getSequence()); -} \ No newline at end of file +} + +struct TradeConstants { + static constexpr std::uint64_t SECONDS_SHIFT = 32ULL; + static constexpr std::uint64_t MILLISECONDS_SHIFT = 22ULL; + static constexpr std::uint64_t MILLISECONDS_MASK = 0x3ffULL; + static constexpr std::uint32_t MAX_SEQUENCE = (1U << 22U) - 1U; +}; + +TEST_CASE("Trade::setTime() should change TimeSequence") { + auto tr = Trade("AAPL"); + + tr.setSequence(123); + + std::int64_t time = 1'701'703'226'577LL; + + tr.setTime(time); + + auto newTimeSequence = tr.getTimeSequence(); + auto expectedTimeSequence = + dxfcpp::orOp(dxfcpp::orOp(dxfcpp::sal(static_cast(dxfcpp::time_util::getSecondsFromTime(time)), + TradeConstants::SECONDS_SHIFT), + dxfcpp::sal(static_cast(dxfcpp::time_util::getMillisFromTime(time)), + TradeConstants::MILLISECONDS_SHIFT)), + tr.getSequence()); + auto expectedTime = dxfcpp::sar(expectedTimeSequence, TradeConstants::SECONDS_SHIFT) * 1000 + + dxfcpp::andOp(dxfcpp::sar(expectedTimeSequence, TradeConstants::MILLISECONDS_SHIFT), + TradeConstants::MILLISECONDS_MASK); + + REQUIRE(newTimeSequence == expectedTimeSequence); + REQUIRE(time == expectedTime); + REQUIRE(time == tr.getTime()); +} + +TEST_CASE("Trade::setTime() shouldn't change Sequence") { + auto tr = Trade("AAPL"); + + tr.setSequence(123); + + auto oldSequence = tr.getSequence(); + std::int64_t time = 1'701'703'226'528LL; + + tr.setTime(time); + + REQUIRE(oldSequence == tr.getSequence()); +}