diff options
author | Jeffrey Straszheim <straszheimjeffrey@gmail.com> | 2009-03-16 01:54:17 +0000 |
---|---|---|
committer | Jeffrey Straszheim <straszheimjeffrey@gmail.com> | 2009-03-16 01:54:17 +0000 |
commit | 9313f194627cf326c3b942a4919a86ccdfada9ad (patch) | |
tree | f9f0534af4706279f5895dc779fe6b88583f35ee /src/clojure/contrib/test_contrib/test_dataflow.clj | |
parent | cf54803cf224d5ea0acb67e913667612ea79582c (diff) |
Added dataflow cells-like library
Diffstat (limited to 'src/clojure/contrib/test_contrib/test_dataflow.clj')
-rw-r--r-- | src/clojure/contrib/test_contrib/test_dataflow.clj | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/clojure/contrib/test_contrib/test_dataflow.clj b/src/clojure/contrib/test_contrib/test_dataflow.clj new file mode 100644 index 00000000..f44a9934 --- /dev/null +++ b/src/clojure/contrib/test_contrib/test_dataflow.clj @@ -0,0 +1,90 @@ +;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and +;; distribution terms for this software are covered by the Eclipse Public +;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can +;; be found in the file epl-v10.html at the root of this distribution. By +;; using this software in any fashion, you are agreeing to be bound by the +;; terms of this license. You must not remove this notice, or any other, +;; from this software. +;; +;; test-dataflow +;; +;; A Library to Support a Dataflow Model of State - Tests +;; +;; straszheimjeffrey (gmail) +;; Created 11 March 2009 + + +(ns clojure.contrib.test-contrib.test-dataflow + (:use clojure.contrib.test-is) + (:use clojure.contrib.dataflow)) + +(def df-1 + (build-dataflow + [(cell :source base 0) + (cell :source items ()) + (cell product (* ?base (apply + ?items))) + (cell :validator (when (number? ?-product) + (assert (>= ?product ?-product))))])) + +(deftest test-df-1 + (is (= (get-value df-1 'product) 0)) + (is (do (update-values df-1 {'items [4 5]}) + (= (get-value df-1 'product) 0))) + (is (do (update-values df-1 {'base 2}) + (= (get-value df-1 'product) 18))) + (is (thrown? Exception (update-values df-1 {'base 0}))) + (is (= (get-value df-1 'product) 18))) + +(def df-2 + (build-dataflow + [(cell :source strength 10) + (cell :source agility 10) + (cell :source magic 10) + + (cell total-cost (apply + ?*cost)) + + (cell cost (- ?strength 10)) + (cell cost (- ?agility 10)) + (cell cost (- ?magic 10)) + + (cell combat (+ ?strength ?agility ?combat-mod)) + (cell speed (+ ?agility (/ ?strength 10.0) ?speed-mod)) + (cell casting (+ ?agility ?magic ?magic-mod)) + + (cell combat-mod (apply + ?*combat-mods)) + (cell speed-mod (apply + ?*speed-mods)) + (cell magic-mod (apply + ?*magic-mods))])) + +(def magic-skill + [(cell cost 5) + (cell speed-mods 1) + (cell magic-mods 2)]) + +(defn gv [n] (get-value df-2 n)) + +(deftest test-df-2 + (is (and (= (gv 'total-cost) 0) + (= (gv 'strength) 10) + (= (gv 'casting) 20))) + (is (do (update-values df-2 {'magic 12}) + (and (= (gv 'total-cost) 2) + (= (gv 'casting) 22)))) + (is (do (add-cells df-2 magic-skill) + (and (= (gv 'total-cost) 7) + (= (gv 'casting) 24)))) + (is (do (remove-cells df-2 magic-skill) + (and (= (gv 'total-cost) 2) + (= (gv 'casting) 22))))) + + +(comment + (run-tests) + + (use :reload 'jls.dataflow.dataflow) + (use 'clojure.contrib.stacktrace) (e) + (use 'clojure.contrib.trace) + +) + + +;; End of file |