diff src/clojure/contrib/test_contrib/test_seq.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_seq.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,128 @@
     1.4 +(ns clojure.contrib.test-seq
     1.5 +  (:use clojure.test)
     1.6 +  (:require [clojure.contrib.seq :as seq]))
     1.7 +
     1.8 +
     1.9 +(deftest test-positions
    1.10 +  (are [expected pred coll] (= expected (seq/positions pred coll))
    1.11 +       [2] string? [:a :b "c"]
    1.12 +       () :d [:a :b :c]
    1.13 +       [0 2] #{:d} [:d :a :d :a]))
    1.14 +
    1.15 +;Upon further inspection, flatten behaves... wierd.
    1.16 +;These tests are what passes on August 7, 2009
    1.17 +(deftest test-flatten-present
    1.18 +  (are [expected nested-val] (= (seq/flatten nested-val) expected)
    1.19 +       ;simple literals
    1.20 +       [] nil
    1.21 +       [] 1
    1.22 +       [] 'test
    1.23 +       [] :keyword
    1.24 +       [] 1/2
    1.25 +       [] #"[\r\n]"
    1.26 +       [] true
    1.27 +       [] false
    1.28 +       ;vectors
    1.29 +       [1 2 3 4 5] [[1 2] [3 4 [5]]]
    1.30 +       [1 2 3 4 5] [1 2 3 4 5]
    1.31 +       [#{1 2} 3 4 5] [#{1 2} 3 4 5]
    1.32 +       ;sets
    1.33 +       [] #{}
    1.34 +       [] #{#{1 2} 3 4 5}
    1.35 +       [] #{1 2 3 4 5}
    1.36 +       [] #{#{1 2} 3 4 5}
    1.37 +       ;lists
    1.38 +       [] '()
    1.39 +       [1 2 3 4 5] `(1 2 3 4 5)
    1.40 +       ;maps
    1.41 +       [] {:a 1 :b 2}
    1.42 +       [:a 1 :b 2] (seq {:a 1 :b 2})
    1.43 +       [] {[:a :b] 1 :c 2}
    1.44 +       [:a :b 1 :c 2] (seq {[:a :b] 1 :c 2})
    1.45 +       [:a 1 2 :b 3] (seq {:a [1 2] :b 3})
    1.46 +       ;Strings
    1.47 +       [] "12345"
    1.48 +       [\1 \2 \3 \4 \5] (seq "12345")
    1.49 +       ;fns
    1.50 +       [] count
    1.51 +       [count even? odd?] [count even? odd?]))
    1.52 +
    1.53 +(deftest test-separate
    1.54 +  (are [test-seq] (= (seq/separate even? test-seq) [[2 4] [1 3 5]])
    1.55 +       [1 2 3 4 5]
    1.56 +       #{1 2 3 4 5}
    1.57 +       '(1 2 3 4 5)))
    1.58 +
    1.59 +;Note - this does not make sense for maps and sets, because order is expected
    1.60 +(deftest test-indexed
    1.61 +  (are [expected test-seq] (= (seq/indexed test-seq) expected)
    1.62 +       [[0 :a] [1 :b] [2 :c] [3 :d]] [:a :b :c :d]
    1.63 +       [[0 :a] [1 :b] [2 :c] [3 :d]] '(:a :b :c :d)
    1.64 +       [[0 \1] [1 \2] [2 \3] [3 \4]] "1234"))
    1.65 +
    1.66 +(deftest test-group-by
    1.67 +  (is (= (seq/group-by even? [1 2 3 4 5]) 
    1.68 +	 {false [1 3 5], true [2 4]})))
    1.69 +
    1.70 +;Note - this does not make sense for maps and sets, because order is expected
    1.71 +(deftest test-partition-by
    1.72 +  (are [test-seq] (= (seq/partition-by (comp even? count) test-seq)
    1.73 +		     [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]])
    1.74 +       ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"]
    1.75 +       '("a" "bb" "cccc" "dd" "eee" "f" "" "hh"))
    1.76 +  (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm")
    1.77 +       [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]])))
    1.78 +
    1.79 +(deftest test-frequencies
    1.80 +  (are [expected test-seq] (= (seq/frequencies test-seq) expected)
    1.81 +       {\p 2, \s 4, \i 4, \m 1} "mississippi"
    1.82 +       {1 4 2 2 3 1} [1 1 1 1 2 2 3]
    1.83 +       {1 4 2 2 3 1} '(1 1 1 1 2 2 3)))
    1.84 +
    1.85 +;Note - this does not make sense for maps and sets, because order is expected
    1.86 +;This is a key differnce between reductions and reduce.
    1.87 +(deftest test-reductions
    1.88 +  (is (= (seq/reductions + [1 2 3 4 5])
    1.89 +	 [1 3 6 10 15]))
    1.90 +  (is (= (reductions + 10 [1 2 3 4 5])
    1.91 +	 [10 11 13 16 20 25])))
    1.92 +
    1.93 +;Note - this does not make sense for maps and sets, because order is expected
    1.94 +(deftest test-rotations
    1.95 +  (is (= (seq/rotations [1 2 3 4])
    1.96 +	 [[1 2 3 4] 
    1.97 +	  [2 3 4 1]
    1.98 +	  [3 4 1 2]
    1.99 +	  [4 1 2 3]])))
   1.100 +
   1.101 +;Note - this does not make sense for maps and sets, because order is expected
   1.102 +(deftest test-partition-all
   1.103 +  (is (= (seq/partition-all 4 [1 2 3 4 5 6 7 8 9])
   1.104 +	 [[1 2 3 4] [5 6 7 8] [9]]))
   1.105 +  (is (= (seq/partition-all 4 2 [1 2 3 4 5 6 7 8 9])
   1.106 +	 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))
   1.107 +
   1.108 +;Thanks to Andy Fingerhut for the idea of testing invariants
   1.109 +(deftest test-shuffle-invariants
   1.110 +  (is (= (count (seq/shuffle [1 2 3 4])) 4))
   1.111 +  (let [shuffled-seq (seq/shuffle [1 2 3 4])]
   1.112 +    (is (every? #{1 2 3 4} shuffled-seq))))
   1.113 +
   1.114 +;Thanks to Andy Fingerhut for the idea of testing invariants
   1.115 +(deftest test-rand-elt-invariants
   1.116 +  (let [elt (seq/rand-elt [:a :b :c :d])]
   1.117 +    (is (#{:a :b :c :d} elt))))
   1.118 +
   1.119 +;Note - this does not make sense for maps and sets, because order is expected
   1.120 +(deftest test-find-first
   1.121 +  (is (= (seq/find-first even? [1 2 3 4 5]) 2))
   1.122 +  (is (= (seq/find-first even? '(1 2 3 4 5)) 2)))
   1.123 +
   1.124 +(deftest test-includes
   1.125 +  (are [coll k] (false? (seq/includes? coll k))
   1.126 +       [1 2 3] 0
   1.127 +       [] nil
   1.128 +       [:a :b] :c)
   1.129 +  (are [coll k] (true? (seq/includes? coll k))
   1.130 +       [1 2 3] 1
   1.131 +       [:a :b] :b))