diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/test_dataflow.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,90 @@
     1.4 +;;  Copyright (c) Jeffrey Straszheim. All rights reserved.  The use and
     1.5 +;;  distribution terms for this software are covered by the Eclipse Public
     1.6 +;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
     1.7 +;;  be found in the file epl-v10.html at the root of this distribution.  By
     1.8 +;;  using this software in any fashion, you are agreeing to be bound by the
     1.9 +;;  terms of this license.  You must not remove this notice, or any other,
    1.10 +;;  from this software.
    1.11 +;;
    1.12 +;;  test-dataflow
    1.13 +;;
    1.14 +;;  A Library to Support a Dataflow Model of State - Tests
    1.15 +;;
    1.16 +;;  straszheimjeffrey (gmail)
    1.17 +;;  Created 11 March 2009
    1.18 +
    1.19 +
    1.20 +(ns clojure.contrib.test-dataflow
    1.21 +  (:use clojure.test)
    1.22 +  (:use clojure.contrib.dataflow))
    1.23 +
    1.24 +(def df-1
    1.25 +     (build-dataflow
    1.26 +      [(cell :source base 0)
    1.27 +       (cell :source items ())
    1.28 +       (cell product (* ?base (apply + ?items)))
    1.29 +       (cell :validator (when (number? ?-product)
    1.30 +                          (assert (>= ?product ?-product))))]))
    1.31 +
    1.32 +(deftest test-df-1
    1.33 +  (is (= (get-value df-1 'product) 0))
    1.34 +  (is (do (update-values df-1 {'items [4 5]})
    1.35 +          (= (get-value df-1 'product) 0)))
    1.36 +  (is (do (update-values df-1 {'base 2})
    1.37 +          (= (get-value df-1 'product) 18)))
    1.38 +  (is (thrown? AssertionError (update-values df-1 {'base 0})))
    1.39 +  (is (= (get-value df-1 'product) 18)))
    1.40 +
    1.41 +(def df-2
    1.42 +     (build-dataflow
    1.43 +      [(cell :source strength 10)
    1.44 +       (cell :source agility 10)
    1.45 +       (cell :source magic 10)
    1.46 +
    1.47 +       (cell total-cost (apply + ?*cost))
    1.48 +
    1.49 +       (cell cost (- ?strength 10))
    1.50 +       (cell cost (- ?agility 10))
    1.51 +       (cell cost (- ?magic 10))
    1.52 +
    1.53 +       (cell combat (+ ?strength ?agility ?combat-mod))
    1.54 +       (cell speed (+ ?agility (/ ?strength 10.0) ?speed-mod))
    1.55 +       (cell casting (+ ?agility ?magic ?magic-mod))
    1.56 +
    1.57 +       (cell combat-mod (apply + ?*combat-mods))
    1.58 +       (cell speed-mod (apply + ?*speed-mods))
    1.59 +       (cell magic-mod (apply + ?*magic-mods))]))
    1.60 +
    1.61 +(def magic-skill
    1.62 +     [(cell cost 5)
    1.63 +      (cell speed-mods 1)
    1.64 +      (cell magic-mods 2)])
    1.65 +
    1.66 +(defn gv [n] (get-value df-2 n))
    1.67 +
    1.68 +(deftest test-df-2
    1.69 +  (is (and (= (gv 'total-cost) 0)
    1.70 +           (= (gv 'strength) 10)
    1.71 +           (= (gv 'casting) 20)))
    1.72 +  (is (do (update-values df-2 {'magic 12})
    1.73 +          (and (= (gv 'total-cost) 2)
    1.74 +               (= (gv 'casting) 22))))
    1.75 +  (is (do (add-cells df-2 magic-skill)
    1.76 +          (and (= (gv 'total-cost) 7)
    1.77 +               (= (gv 'casting) 24))))
    1.78 +  (is (do (remove-cells df-2 magic-skill)
    1.79 +          (and (= (gv 'total-cost) 2)
    1.80 +               (= (gv 'casting) 22)))))
    1.81 +               
    1.82 +
    1.83 +(comment
    1.84 +  (run-tests)
    1.85 +
    1.86 +  (use :reload 'clojure.contrib.dataflow)
    1.87 +  (use 'clojure.contrib.stacktrace) (e)
    1.88 +  (use 'clojure.contrib.trace)
    1.89 +
    1.90 +)
    1.91 +
    1.92 +
    1.93 +;; End of file