File size: 5,660 Bytes
a3b10ab 44cddac a3b10ab 44cddac a3b10ab 44cddac a3b10ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | // ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// ClearingHouseActor tests
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#include "actors/ClearingHouseActor.hpp"
#include "actors/MECoreActor.hpp"
#include "actors/OEGActor.hpp"
#include "actors/MDGActor.hpp"
#include <iostream>
#include <cassert>
using namespace eunex;
static int testsPassed = 0;
static int testsFailed = 0;
#define TEST(name) \
std::cout << " " << #name << "... "; \
try { test_##name(); std::cout << "PASS\n"; ++testsPassed; } \
catch (const std::exception& e) { std::cout << "FAIL: " << e.what() << "\n"; ++testsFailed; }
#define ASSERT_EQ(a, b) \
if ((a) != (b)) throw std::runtime_error( \
std::string("Expected ") + std::to_string(static_cast<long long>(b)) + " got " + std::to_string(static_cast<long long>(a)))
#define ASSERT_TRUE(x) \
if (!(x)) throw std::runtime_error("Assertion failed: " #x)
// ββ Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
void test_initial_members() {
ClearingHouseActor ch;
auto lb = ch.getLeaderboard();
ASSERT_EQ(static_cast<int>(lb.size()), 10);
for (auto& e : lb) {
ASSERT_EQ(static_cast<int>(e.capital), 100000);
ASSERT_EQ(static_cast<int>(e.pnl), 0);
ASSERT_EQ(e.tradeCount, 0);
}
}
void test_session_mapping() {
ClearingHouseActor ch;
ch.mapSession(100, 1);
ch.mapSession(101, 2);
Trade t{};
t.symbolIdx = 1;
t.price = toFixedPrice(150.0);
t.quantity = 10;
t.buySessionId = 100;
t.sellSessionId = 101;
TradeEvent evt(t);
ch.onEvent(evt);
auto lb = ch.getLeaderboard();
int totalTrades = 0;
for (auto& e : lb) totalTrades += e.tradeCount;
ASSERT_EQ(totalTrades, 2);
}
void test_buy_reduces_capital() {
ClearingHouseActor ch;
ch.mapSession(100, 1);
Trade t{};
t.symbolIdx = 1;
t.price = toFixedPrice(100.0);
t.quantity = 10;
t.buySessionId = 100;
t.sellSessionId = 999;
TradeEvent evt(t);
ch.onEvent(evt);
auto* m = ch.getMember(1);
ASSERT_TRUE(m != nullptr);
ASSERT_TRUE(m->capital < 100000.0);
ASSERT_EQ(static_cast<int>(m->capital), 99000);
}
void test_sell_increases_capital() {
ClearingHouseActor ch;
ch.mapSession(100, 1);
Trade t{};
t.symbolIdx = 1;
t.price = toFixedPrice(100.0);
t.quantity = 10;
t.buySessionId = 999;
t.sellSessionId = 100;
TradeEvent evt(t);
ch.onEvent(evt);
auto* m = ch.getMember(1);
ASSERT_TRUE(m != nullptr);
ASSERT_TRUE(m->capital > 100000.0);
ASSERT_EQ(static_cast<int>(m->capital), 101000);
}
void test_holdings_tracked() {
ClearingHouseActor ch;
ch.mapSession(100, 1);
Trade t{};
t.symbolIdx = 1;
t.price = toFixedPrice(150.0);
t.quantity = 10;
t.buySessionId = 100;
t.sellSessionId = 999;
TradeEvent evt(t);
ch.onEvent(evt);
auto* m = ch.getMember(1);
ASSERT_TRUE(m != nullptr);
ASSERT_EQ(static_cast<int>(m->holdings.size()), 1);
auto hIt = m->holdings.find(1);
ASSERT_TRUE(hIt != m->holdings.end());
ASSERT_EQ(hIt->second.quantity, 10);
}
void test_leaderboard_sorted() {
ClearingHouseActor ch;
ch.mapSession(100, 1);
ch.mapSession(101, 2);
Trade t1{};
t1.symbolIdx = 1;
t1.price = toFixedPrice(100.0);
t1.quantity = 50;
t1.buySessionId = 100;
t1.sellSessionId = 101;
TradeEvent evt1(t1);
ch.onEvent(evt1);
auto lb = ch.getLeaderboard();
ASSERT_TRUE(lb[0].capital >= lb[1].capital);
}
void test_trade_with_clearing_pipe() {
auto oe = std::make_unique<OEGActor>();
auto md = std::make_unique<MDGActor>();
auto ch = std::make_unique<ClearingHouseActor>();
ch->mapSession(1, 1);
ch->mapSession(2, 2);
auto book = std::make_unique<MECoreActor>(
1, oe->getActorId(), md->getActorId(), ch->getActorId());
oe->mapSymbol(1, book->getActorId());
oe->submitNewOrder(1, 1, Side::Sell, OrderType::Limit,
TimeInForce::Day, toFixedPrice(100.0), 50, 1);
oe->submitNewOrder(2, 1, Side::Buy, OrderType::Limit,
TimeInForce::Day, toFixedPrice(100.0), 50, 2);
auto lb = ch->getLeaderboard();
int totalTrades = 0;
for (auto& e : lb) totalTrades += e.tradeCount;
ASSERT_TRUE(totalTrades >= 2);
}
// ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
int main() {
std::cout << "ClearingHouse Tests\n";
std::cout << "βββββββββββββββββββββββββββββββββββββββββββ\n";
TEST(initial_members);
TEST(session_mapping);
TEST(buy_reduces_capital);
TEST(sell_increases_capital);
TEST(holdings_tracked);
TEST(leaderboard_sorted);
TEST(trade_with_clearing_pipe);
std::cout << "βββββββββββββββββββββββββββββββββββββββββββ\n";
std::cout << testsPassed << " passed, " << testsFailed << " failed\n";
return testsFailed > 0 ? 1 : 0;
}
|