PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/datalog/src/test/clojure/clojure/contrib/datalog/tests/test_util.clj

https://github.com/laurentpetit/clojure-contrib
Clojure | 69 lines | 44 code | 10 blank | 15 comment | 14 complexity | 1dbe2ae40c2421fa2eed3bad20a2371e MD5 | raw file
  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-util.clj
  10. ;;
  11. ;; A Clojure implementation of Datalog -- Utilities Tests
  12. ;;
  13. ;; straszheimjeffrey (gmail)
  14. ;; Created 11 Feburary 2009
  15. (ns clojure.contrib.datalog.tests.test-util
  16. (:use clojure.test
  17. clojure.contrib.datalog.util)
  18. (:use [clojure.contrib.except :only (throwf)]))
  19. (deftest test-is-var?
  20. (is (is-var? '?x))
  21. (is (is-var? '?))
  22. (is (not (is-var? '??x)))
  23. (is (not (is-var? '??)))
  24. (is (not (is-var? 'x)))
  25. (is (not (is-var? "fred")))
  26. (is (not (is-var? :q))))
  27. (deftest test-map-values
  28. (let [map {:fred 1 :sally 2}]
  29. (is (= (map-values #(* 2 %) map) {:fred 2 :sally 4}))
  30. (is (= (map-values identity {}) {}))))
  31. (deftest test-keys-to-vals
  32. (let [map {:fred 1 :sally 2 :joey 3}]
  33. (is (= (set (keys-to-vals map [:fred :sally])) #{1 2}))
  34. (is (= (set (keys-to-vals map [:fred :sally :becky])) #{1 2}))
  35. (is (empty? (keys-to-vals map [])))
  36. (is (empty? (keys-to-vals {} [:fred])))))
  37. (deftest test-reverse-map
  38. (let [map {:fred 1 :sally 2 :joey 3}
  39. map-1 (assoc map :mary 3)]
  40. (is (= (reverse-map map) {1 :fred 2 :sally 3 :joey}))
  41. (is (or (= (reverse-map map-1) {1 :fred 2 :sally 3 :joey})
  42. (= (reverse-map map-1) {1 :fred 2 :sally 3 :mary})))))
  43. (def some-maps
  44. [
  45. { :a 1 :b 2 }
  46. { :c 3 :b 3 }
  47. { :d 4 :a 1 }
  48. { :g 4 :b 4 }
  49. { :a 2 :b 1 }
  50. { :e 1 :f 1 }
  51. ])
  52. (def reduced (preduce + some-maps))
  53. (def merged (apply merge-with + some-maps))
  54. (deftest test-preduce
  55. (is (= reduced merged)))
  56. (comment
  57. (run-tests)
  58. )
  59. ; End of file