Sheet: invariants and test cases
The six invariants as assertions, the shape of a test case and the list of edge cases.
Reference5 min
By the end of this lesson
- Recall the six invariants and how they become assertions
- Recall the edge cases to cover and the checks after a session
In this lesson5
The invariants of a matching engine, their translation into assertions, and the edge cases to cover. Invariants 4 to 6 are design principles of the teaching engine: no reliable source describes how real platforms guarantee them.
The six invariants
- Conservation: for each order, executed + resting + cancelled + dropped = received.
- Uncrossed book (continuous):
fourchette().ecartisnullor strictly positive after each order. - Limits honoured: for each execution,
prixat most the buyer's limit, at least the seller's. - Priority: no order is filled while a better-placed compatible order on the same side is waiting.
- Monotonic timestamps: ranks strictly increase; on the BVMAC, an order is timestamped on receipt (BVMAC General Rules 2025, art. 150).
- Determinism: same input log, same executions; replay rebuilds the state.
// 2. Uncrossed book
const { ecart } = carnet.fourchette()
expect(ecart === null || ecart > 0).toBe(true)
// 3. Limits honoured
for (const e of executions) {
const a = recus.get(e.achat)!, v = recus.get(e.vente)!
expect(a.limite === null || e.prix <= a.limite).toBe(true)
expect(v.limite === null || e.prix >= v.limite).toBe(true)
}
// 6. Determinism
expect(rejouer(journal)).toEqual(rejouer(journal))
A test case, three parts
- Given: the minimal state that makes the rule observable, and only that rule.
- When: a single action (
passer,annuler,fixing). - Then: exact values (prices, quantities, identifiers,
regle).
If the test would also pass without the rule, change the data: it proves nothing.
Edge cases to cover
- Quantity: 0, negative, decimal, exactly equal to the opposite quantity.
- Price: a tie between two orders (time priority), a limit equal to the best opposite price, a non-integer limit.
- Market order: with no counterparty, alone on both sides, facing an empty book, remainder.
- Auction: no cross, volume tie, imbalances of opposite signs, only market orders crossing, no reference price.
- Life cycle: cancelling a filled order, cancelling twice, a reused identifier, an amendment that raises or lowers the quantity (Euronext Trading Manual, 2021, section 2).
- Rules the engine does not cover: all-or-nothing orders (BVMAC General Rules 2025, art. 167), fixed-term validity of at most 30 days (art. 162): as
it.todo.
Amendment, replay, entries
- Amendment: reducing keeps the rank, increasing or changing the limit loses it (Euronext Trading Manual, 2021, section 2); an invalid amendment changes nothing; a limit that crosses executes at once.
- Replay:
empreinte(live) === empreinte(rejouer(journal)); recovery from a snapshot = full replay, at every event, ranks included. - Entries: Σ shares = 0; Σ cash over all accounts, commissions included, = 0; an execution reference is integrated only once; a reversal brings every balance back to its start.
From memory: which amendment keeps time priority, and why must a snapshot keep the ranks?
After a session, on the MIMS side
- Quantity allocated from an execution = quantity executed.
- Change in position = shares bought − shares sold, per client and per security.
- Shares bought = shares sold; sum of cash movements zero before commissions.
- Price used = price published in the Official Price List.
- Re-importing an execution file counts nothing twice.
- Settlement delay: a parameter, never a constant (no text found sets it).
From memory: what are the six invariants, in order?
Read next: Règlement général de la BVMAC (2025)
A question? Ask your teacher, or an AI assistant, giving it the title of this lesson.