Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
runeksvendsen committed May 13, 2019
1 parent 6e3524e commit a04c22f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/OrderBook/Graph/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module OrderBook.Graph.Build
, SortedOrders, first, rest, prepend, toList, replaceHead
, Tagged(..)
, build
, buildFromOrders
)
where

Expand Down Expand Up @@ -68,3 +69,33 @@ create =
groupByMarket = groupBy sameBaseQuote . sortOn Util.baseQuote
sameBaseQuote :: ABook -> ABook -> Bool
sameBaseQuote ob1 ob2 = Util.baseQuote ob1 == Util.baseQuote ob2

-- ^ Same as 'build' but take orders (instead of order books) as input.
-- Only present for backwards compatibility (slower than 'build').
buildFromOrders
:: (PrimMonad m)
=> SellOrderGraph (PrimState m) g "arb" -- ^ Empty graph
-> [SomeSellOrder] -- ^ Orders
-> m ()
buildFromOrders mGraph orders = do
forM_ (createFromOrders orders) (DG.insertEdge mGraph . Tagged)

-- |
createFromOrders
:: [SomeSellOrder] -- ^ A bunch of sell orders
-> [SortedOrders]
createFromOrders =
-- TODO: sort order books instead of orders
fmap (SortedOrders . NE.fromList . sortOn soPrice) . groupByMarket
. fmap assertPositivePrice
where
assertPositivePrice order
| soPrice order >= 0 = order
| otherwise = error $ "negative-price order: " ++ show order
groupByMarket = groupBy sameSrcDst . sortBy orderSrcDst
sameSrcDst oA oB =
soBase oA == soBase oB &&
soQuote oA == soQuote oB
orderSrcDst oA oB =
soBase oA `compare` soBase oB <>
soQuote oA `compare` soQuote oB
2 changes: 1 addition & 1 deletion test/Common/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ assertMatchedOrders
assertMatchedOrders sellOrders buyOrder f = void $ do
shuffledSellOrders <- Shuffle.shuffleM sellOrders
matchedOrders <- ST.stToIO $ DG.withGraph $ \mGraph -> do
Lib.build mGraph shuffledSellOrders
Lib.buildFromOrders mGraph shuffledSellOrders
(buyGraph, _) <- Lib.runArb mGraph $ Lib.arbitrages buyOrder
Lib.runMatch buyGraph $ Lib.match buyOrder
assertAscendingPriceSorted matchedOrders
Expand Down
2 changes: 1 addition & 1 deletion test/Property/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ addEdgesCheckOutgoing
addEdgesCheckOutgoing orders = do
shuffledOrders <- Shuffle.shuffleM orders
outgoingEdges <- ST.stToIO $ DG.withGraph $ \graph -> do
Build.build graph shuffledOrders
Build.buildFromOrders graph shuffledOrders
foldM (collectOutgoing graph) [] =<< DG.vertices graph
let graphOrders = concat $ Build.toList . Build.unTagged <$> concat outgoingEdges
sort graphOrders `shouldBe` sort orders
Expand Down

0 comments on commit a04c22f

Please sign in to comment.