Skip to content

Commit

Permalink
Trade tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ttldtor committed Dec 5, 2023
1 parent 7201a9b commit 8787e81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
9 changes: 5 additions & 4 deletions include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::int64_t>(time_util::getSecondsFromTime(time)), SECONDS_SHIFT),
orOp(sal(static_cast<std::int64_t>(time_util::getMillisFromTime(time)), MILLISECONDS_MASK), getSequence()));
tradeBaseData_.timeSequence =
orOp(orOp(sal(static_cast<std::int64_t>(time_util::getSecondsFromTime(time)), SECONDS_SHIFT),
sal(static_cast<std::int64_t>(time_util::getMillisFromTime(time)), MILLISECONDS_SHIFT)),
getSequence());
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
48 changes: 46 additions & 2 deletions tests/api/EventsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -317,4 +316,49 @@ TEST_CASE("Series::setIndex() shouldn't change Sequence") {
s.setIndex(1'234'567'891'011LL);

REQUIRE(oldSequence == s.getSequence());
}
}

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<std::int64_t>(dxfcpp::time_util::getSecondsFromTime(time)),
TradeConstants::SECONDS_SHIFT),
dxfcpp::sal(static_cast<std::int64_t>(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());
}

0 comments on commit 8787e81

Please sign in to comment.