aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/test_contrib/test_dataflow.clj
blob: 8b934dc3eccea2c36564bdf3f7896fa9af0999a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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.test)
  (: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? AssertionError (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 'clojure.contrib.dataflow)
  (use 'clojure.contrib.stacktrace) (e)
  (use 'clojure.contrib.trace)

)


;; End of file