Work on flows, close to complete but no cigar.

This commit is contained in:
Simon Brooke 2023-07-09 22:27:20 +01:00
parent f4d4e9b694
commit 5ef93ef4df
5 changed files with 263 additions and 75 deletions

View file

@ -0,0 +1,55 @@
(ns mw-engine.flow-test
(:require [clojure.test :refer [deftest is testing]]
[mw-engine.flow :refer [coordinate? execute execute-flows flow?
location?]]
[mw-engine.utils :refer [get-cell merge-cell]]
[mw-engine.world :refer [make-world]]))
(deftest coordinate-tests
(testing "coordinates"
(let [world (make-world 3 3)]
(is (not (coordinate? -1 world)) "Not a coordinate: negative")
(is (not (coordinate? 4 world)) "Not a coordinate: out of bounds")
(is (not (coordinate? 3 world)) "Not a coordinate: boundary")
(is (not (coordinate? :three world)) "Not a coordinate: keyword")
(is (not (coordinate? 3.14 world)) "Not a coordinate: floating point")
(is (coordinate? 0 world) "should be a coordinate: zero")
(is (coordinate? 1 world) "should be a coordinate: middle"))))
(deftest location-tests
(testing "locations"
(let [world (make-world 3 3)
in1 {:x 0 :y 0}
in2 {:x 1 :y 2}
out1 {:p 0 :q 0}
out2 {:x -1 :y 2}]
(is (location? in1 world) "should be a location: top left")
(is (location? in2 world) "should be a location: middle bottom")
(is (not (location? out1 world)) "should not be a location: wrong keys")
(is (not (location? out2 world)) "should not be a location: negative coordinate"))))
(deftest flow-tests
(testing "flows"
(let [world (make-world 3 3)
world' (merge-cell world {:x 0, :y 0, :state :new :q 5.3})
valid {:source {:x 0 :y 0}
:destination {:x 1 :y 1}
:property :q
:quantity 2.4}]
(is (flow? valid world))
(let [transferred (execute valid world')
source-q (:q (get-cell transferred 0 0))
dest-q (:q (get-cell transferred 1 1))]
(is (= source-q 2.9))
(is (= dest-q 2.4)))
(let [valid2 {:source {:x 1 :y 1}
:destination {:x 0 :y 1}
:property :q
:quantity 1}
transferred (execute-flows (list valid valid2) world')
source-q (:q (get-cell transferred 0 0))
inter-q (:q (get-cell transferred 1 1))
dest-q (:q (get-cell transferred 0 1))]
(is (= source-q 2.9))
(is (= inter-q 1.4))
(is (= dest-q 1))))))

View file

@ -178,4 +178,20 @@
(is (:test (get-cell w3c 2 2))
"The cell with :test set is at 2, 2"))))
(deftest most-least-tests
(let [cells [{:x 0, :y 0, :state :new, :prop 0.4406204774301924}
{:x 1, :y 0, :state :new, :prop 0.26475629405490275}
{:x 2, :y 0, :state :new, :prop 0.34018209505715813}
{:x 0, :y 1, :state :new, :prop 0.35104719397171424}
{:x 1, :y 1, :state :new, :prop 0.6009298123397215} ;; <- max
{:x 2, :y 1, :state :new, :prop 0.5580383897506066}
{:x 0, :y 2, :state :new, :prop 0.1780241365266907} ;; <- min
{:x 1, :y 2, :state :new, :prop 0.3255028139128574}
{:x 2, :y 2, :state :new, :prop 0.3449965660347397}]]
(let [expected {:x 1, :y 1, :state :new, :prop 0.6009298123397215}
actual (get-most-cell cells :prop)]
(is (= actual expected) "get-most-cell failed")
)
(let [expected {:x 0, :y 2, :state :new, :prop 0.1780241365266907}
actual (get-least-cell cells :prop)]
(is (= actual expected) "get-least-cell failed"))))