more tests that passes
This commit is contained in:
1 parent
8fb9335411
commit
0d126be10c
4 files changed
+18
-12
No files matched your search
@@ -10,3 +10,4 @@ pom.xml.asc
|
||||
.hgignore
|
||||
.hg/
|
||||
.idea/
|
||||
book.iml
|
||||
@@ -15,7 +15,9 @@
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Leiningen: clojure-complete:0.2.4" level="project" />
|
||||
<orderEntry type="library" name="Leiningen: org.clojure/clojure:1.8.0" level="project" />
|
||||
<orderEntry type="library" name="Leiningen: org.clojure/clojure:1.9.0" level="project" />
|
||||
<orderEntry type="library" name="Leiningen: org.clojure/core.specs.alpha:0.1.24" level="project" />
|
||||
<orderEntry type="library" name="Leiningen: org.clojure/spec.alpha:0.1.143" level="project" />
|
||||
<orderEntry type="library" name="Leiningen: org.clojure/tools.nrepl:0.2.12" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
+10
-8
@@ -31,21 +31,23 @@
|
||||
|
||||
(defn create-sell-trade-factory [order-to-fulfill]
|
||||
(fn [trade book-order]
|
||||
(let [qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) (:already-fulfilled trade 0)))
|
||||
can-fulfill? (< (:price book-order) (:price order-to-fulfill))]
|
||||
(when (and can-fulfill? (< qtn-to-fulfill 0))
|
||||
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
|
||||
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
|
||||
can-fulfill? (<= (:price book-order) (:price order-to-fulfill))]
|
||||
(when (and can-fulfill? (< 0 qtn-to-fulfill))
|
||||
#:book.order{:type :sell
|
||||
:quantity qtn-to-fulfill
|
||||
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
|
||||
:price (min (:price book-order) (:price order-to-fulfill))
|
||||
:order-id (:book.order/id order-to-fulfill)
|
||||
:book-id (:book.order/id book-order)}))))
|
||||
|
||||
|
||||
(defn create-sell-trades [order-to-fulfill sell-book]
|
||||
(take-while nil?
|
||||
(reductions (create-sell-trade-factory order-to-fulfill) sell-book)))
|
||||
|
||||
|
||||
(let [create-sell-trade (create-sell-trade-factory order-to-fulfill)
|
||||
trades (reductions create-sell-trade {} sell-book)
|
||||
ret (rest (take-while some? trades))]
|
||||
(map #(dissoc % :book.order/already-fulfilled) ret)))
|
||||
|
||||
(defn partial-trade? [trade order]
|
||||
(not= (:quantity order) (:book.order/quantity trade)))
|
||||
@@ -65,7 +67,7 @@
|
||||
|
||||
|
||||
(defn match-buy-order-in-sell-book [sell-book order]
|
||||
(let [trades (create-trade order (first sell-book))
|
||||
(let [trades-1 [(create-trade order (first sell-book))]
|
||||
trades (create-sell-trades order sell-book)
|
||||
new-book (compute-new-sell-book-after-trades sell-book trades)]
|
||||
[trades new-book]))
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
|
||||
|
||||
|
||||
(deftest test-match-buy-order-in-sell-book
|
||||
(testing "Simplest case."
|
||||
(let [sell-book (create-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
|
||||
@@ -27,9 +28,9 @@
|
||||
(is (= trades [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :sell}]))))
|
||||
|
||||
(testing "Simplest case IV."
|
||||
(let [sell-book (create-book {:type :sell :quantity 2 :price 12 :book.order/id 107} {:type :sell :quantity 8 :price 12 :book.order/id 108})
|
||||
order {:type :buy :quantity 10 :price 12 :book.order/id 109}
|
||||
(let [sell-book (create-book {:type :sell :quantity 2 :price 12 :book.order/id 107} {:type :sell :quantity 8 :price 13 :book.order/id 108})
|
||||
order {:type :buy :quantity 10 :price 13 :book.order/id 109}
|
||||
[trades new-book] (match-buy-order-in-sell-book sell-book order)]
|
||||
(is (= new-book []))
|
||||
(is (= trades [#:book.order{:quantity 2 :price 12 :book-id 107 :order-id 109 :type :sell}
|
||||
#:book.order{:quantity 8 :price 12 :book-id 108 :order-id 109 :type :sell}])))))
|
||||
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :sell}])))))
|
||||
Reference in new issue
Block a user