Fixed many bugs.

This commit is contained in:
didier amyot committed 2018-06-24 17:16:44 -04:00
1 parent ad924ca5c7
commit 174319293c
10 files changed
+227 -94

No files matched your search

+59 -17
View File
@@ -1,28 +1,70 @@
(ns book.exchange-test
(:import [book.PersistentSortedList])
(:require [midje.sweet :refer :all]
[book.exchange :refer :all]
[book.match :refer [create-buy-book
create-sell-book]]
[clojure.core.async :refer [chan go >!! <!!]]))
create-sell-book]]))
(def PersistentSortedList? (comp #(= % book.PersistentSortedList) type))
(facts "the exchange as an whole"
(fact "sell order followed by a buy order trivialities "
(let [incoming-order-channel (chan)
trades-channel (chan)
new-book-order-channel (chan)
master-book {:sell (create-sell-book) :buy (create-buy-book)}]
(go (process-orders master-book incoming-order-channel trades-channel new-book-order-channel))
(>!! incoming-order-channel {:type :sell :quantity 10 :price 2})
(<!! new-book-order-channel) => {:type :sell :quantity 10 :price 2}
(>!! incoming-order-channel {:type :buy :quantity 20 :price 2})
(<!! trades-channel) => #:book.order{:book-id nil
:order-id nil
:price 2
:quantity 10
:type :sell}
(<!! new-book-order-channel) => {:type :buy :quantity 10 :price 2})))
(fact "process-sell-order should work when book empty"
(let [book empty-book
order {:type :sell :quantity 10 :price 2 :book.order/id 1}
expt-new-sell-book [order]
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-sell-order-2 book order)]
new-sell-book => expt-new-sell-book
new-buy-book => empty?
new-order => order
new-trades => empty?))
(fact "process-sell-order should work when there is a buy order that matches"
(let [sell-book (create-sell-book)
buy-book (create-buy-book {:type :buy :quantity 10 :price 3 :book.order/id 2})
book {:sell sell-book :buy buy-book}
order {:type :sell :quantity 4 :price 2 :book.order/id 3}
expt-new-buy-book [{:price 3 :quantity 6 :type :buy :book.order/id 2}]
expt-new-trades [#:book.order{:book-id 2 :order-id 3 :price 3 :quantity 4 :type :buy}]
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order-2 book order)]
new-sell-book => empty?
new-buy-book => expt-new-buy-book
new-order => nil?
new-trades => expt-new-trades))
(fact "process-buy-order should work when book empty"
(let [book empty-book
order {:type :buy :quantity 10 :price 2 :book.order/id 1}
expt-new-sell-book [order]
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order-2 book order)]
new-sell-book => empty?
new-buy-book => expt-new-sell-book
new-order => order
new-trades => empty?))
(fact "process-buy-order should work when there is a buy order that matches"
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 3 :book.order/id 2})
buy-book (create-buy-book)
book {:sell sell-book :buy buy-book}
order {:type :buy :quantity 4 :price 4 :book.order/id 3}
expt-new-sell-book [{:price 3 :quantity 6 :type :sell :book.order/id 2}]
expt-new-trades [#:book.order{:book-id 2 :order-id 3 :price 3 :quantity 4 :type :sell}]
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order-2 book order)]
new-sell-book => expt-new-sell-book
new-order => nil?
new-trades => expt-new-trades))
(fact "process-order total dance"
(let [book empty-book
order-1 {:type :buy :quantity 10 :price 100 :book.order/id 7}
order-2 {:type :buy :quantity 15 :price 99 :book.order/id 8}
order-3 {:type :sell :quantity 17 :price 99 :book.order/id 9}
order-4 {:type :sell :quantity 1 :price 102 :book.order/id 10}
expt-book-4 {:buy [{:price 99 :quantity 8 :type :buy :book.order/id 8}]
:sell [{:price 102 :quantity 1 :type :sell :book.order/id 10}]}
[book-1 _ _] (process-order-2 book order-1)
[book-2 _ _] (process-order-2 book-1 order-2)
[book-3 _ _] (process-order-2 book-2 order-3)
[book-4 _ _] (process-order-2 book-3 order-4)]
book-4 => expt-book-4)))
+21 -1
View File
@@ -2,6 +2,9 @@
(:require [midje.sweet :refer :all]
[book.match :refer :all]))
(def PersistentSortedList? (comp #(= % book.PersistentSortedList) type))
(facts "matching buy order in sell book"
(fact "Simplest case I."
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
@@ -101,4 +104,21 @@
new-book => empty?
trades => [#:book.order{:quantity 2 :price 14 :book-id 116 :order-id 118 :type :buy}
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :buy}]
leftover-order => {:type :sell :quantity 11 :price 13 :book.order/id 118})))
leftover-order => {:type :sell :quantity 11 :price 13 :book.order/id 118})))
(facts "We should write tests for each bug found"
(fact "book type should always be book.PersistentSortedList"
(let [book (create-buy-book)
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
[_ new-book _] (match-order-in-book book order)]
(count new-book) => 0
(type new-book) => book.PersistentSortedList)))
;(fact "should not fuck up"
; (let [order-1 {:type :buy :quantity 4 :price 4 :book.order/id 5}
; order-2 {:type :sell :quantity 2 :price 3 :book.order/id 6}
; book (create-buy-book order-1)
; expt-trade #:book.order{}
; [trades new-book new-order] (match-order-in-book book order-2)]
; trades => 1
; new-order => nil?
; new-book => {})))
+3
View File
@@ -30,6 +30,9 @@
(fact "drop"
(drop 2 (sorted-list 1 2 3)) => (sorted-list 3)
(drop 3 (sorted-list 1 2 3)) => (list))
(fact "empty"
(empty (sorted-list 2 3 4)) => (sorted-list)
(into (empty (sorted-list-by #(- %2 %1) 2 3 4)) [20 30]) => (sorted-list-by #(- %2 %1) 20 30))
(fact "reduce"
(reduce + (sorted-list 1 2 3)) => 6
(reduce + 10 (sorted-list 1 2 3)) => 16)