view src/clojure/contrib/test_contrib/test_dataflow.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and
2 ;; distribution terms for this software are covered by the Eclipse Public
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
4 ;; be found in the file epl-v10.html at the root of this distribution. By
5 ;; using this software in any fashion, you are agreeing to be bound by the
6 ;; terms of this license. You must not remove this notice, or any other,
7 ;; from this software.
8 ;;
9 ;; test-dataflow
10 ;;
11 ;; A Library to Support a Dataflow Model of State - Tests
12 ;;
13 ;; straszheimjeffrey (gmail)
14 ;; Created 11 March 2009
17 (ns clojure.contrib.test-dataflow
18 (:use clojure.test)
19 (:use clojure.contrib.dataflow))
21 (def df-1
22 (build-dataflow
23 [(cell :source base 0)
24 (cell :source items ())
25 (cell product (* ?base (apply + ?items)))
26 (cell :validator (when (number? ?-product)
27 (assert (>= ?product ?-product))))]))
29 (deftest test-df-1
30 (is (= (get-value df-1 'product) 0))
31 (is (do (update-values df-1 {'items [4 5]})
32 (= (get-value df-1 'product) 0)))
33 (is (do (update-values df-1 {'base 2})
34 (= (get-value df-1 'product) 18)))
35 (is (thrown? AssertionError (update-values df-1 {'base 0})))
36 (is (= (get-value df-1 'product) 18)))
38 (def df-2
39 (build-dataflow
40 [(cell :source strength 10)
41 (cell :source agility 10)
42 (cell :source magic 10)
44 (cell total-cost (apply + ?*cost))
46 (cell cost (- ?strength 10))
47 (cell cost (- ?agility 10))
48 (cell cost (- ?magic 10))
50 (cell combat (+ ?strength ?agility ?combat-mod))
51 (cell speed (+ ?agility (/ ?strength 10.0) ?speed-mod))
52 (cell casting (+ ?agility ?magic ?magic-mod))
54 (cell combat-mod (apply + ?*combat-mods))
55 (cell speed-mod (apply + ?*speed-mods))
56 (cell magic-mod (apply + ?*magic-mods))]))
58 (def magic-skill
59 [(cell cost 5)
60 (cell speed-mods 1)
61 (cell magic-mods 2)])
63 (defn gv [n] (get-value df-2 n))
65 (deftest test-df-2
66 (is (and (= (gv 'total-cost) 0)
67 (= (gv 'strength) 10)
68 (= (gv 'casting) 20)))
69 (is (do (update-values df-2 {'magic 12})
70 (and (= (gv 'total-cost) 2)
71 (= (gv 'casting) 22))))
72 (is (do (add-cells df-2 magic-skill)
73 (and (= (gv 'total-cost) 7)
74 (= (gv 'casting) 24))))
75 (is (do (remove-cells df-2 magic-skill)
76 (and (= (gv 'total-cost) 2)
77 (= (gv 'casting) 22)))))
80 (comment
81 (run-tests)
83 (use :reload 'clojure.contrib.dataflow)
84 (use 'clojure.contrib.stacktrace) (e)
85 (use 'clojure.contrib.trace)
87 )
90 ;; End of file