Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
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 | |
15 | |
16 | |
17 (ns clojure.contrib.test-dataflow | |
18 (:use clojure.test) | |
19 (:use clojure.contrib.dataflow)) | |
20 | |
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))))])) | |
28 | |
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))) | |
37 | |
38 (def df-2 | |
39 (build-dataflow | |
40 [(cell :source strength 10) | |
41 (cell :source agility 10) | |
42 (cell :source magic 10) | |
43 | |
44 (cell total-cost (apply + ?*cost)) | |
45 | |
46 (cell cost (- ?strength 10)) | |
47 (cell cost (- ?agility 10)) | |
48 (cell cost (- ?magic 10)) | |
49 | |
50 (cell combat (+ ?strength ?agility ?combat-mod)) | |
51 (cell speed (+ ?agility (/ ?strength 10.0) ?speed-mod)) | |
52 (cell casting (+ ?agility ?magic ?magic-mod)) | |
53 | |
54 (cell combat-mod (apply + ?*combat-mods)) | |
55 (cell speed-mod (apply + ?*speed-mods)) | |
56 (cell magic-mod (apply + ?*magic-mods))])) | |
57 | |
58 (def magic-skill | |
59 [(cell cost 5) | |
60 (cell speed-mods 1) | |
61 (cell magic-mods 2)]) | |
62 | |
63 (defn gv [n] (get-value df-2 n)) | |
64 | |
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))))) | |
78 | |
79 | |
80 (comment | |
81 (run-tests) | |
82 | |
83 (use :reload 'clojure.contrib.dataflow) | |
84 (use 'clojure.contrib.stacktrace) (e) | |
85 (use 'clojure.contrib.trace) | |
86 | |
87 ) | |
88 | |
89 | |
90 ;; End of file |