annotate 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
rev   line source
rlm@10 1 (ns clojure.contrib.test-seq
rlm@10 2 (:use clojure.test)
rlm@10 3 (:require [clojure.contrib.seq :as seq]))
rlm@10 4
rlm@10 5
rlm@10 6 (deftest test-positions
rlm@10 7 (are [expected pred coll] (= expected (seq/positions pred coll))
rlm@10 8 [2] string? [:a :b "c"]
rlm@10 9 () :d [:a :b :c]
rlm@10 10 [0 2] #{:d} [:d :a :d :a]))
rlm@10 11
rlm@10 12 ;Upon further inspection, flatten behaves... wierd.
rlm@10 13 ;These tests are what passes on August 7, 2009
rlm@10 14 (deftest test-flatten-present
rlm@10 15 (are [expected nested-val] (= (seq/flatten nested-val) expected)
rlm@10 16 ;simple literals
rlm@10 17 [] nil
rlm@10 18 [] 1
rlm@10 19 [] 'test
rlm@10 20 [] :keyword
rlm@10 21 [] 1/2
rlm@10 22 [] #"[\r\n]"
rlm@10 23 [] true
rlm@10 24 [] false
rlm@10 25 ;vectors
rlm@10 26 [1 2 3 4 5] [[1 2] [3 4 [5]]]
rlm@10 27 [1 2 3 4 5] [1 2 3 4 5]
rlm@10 28 [#{1 2} 3 4 5] [#{1 2} 3 4 5]
rlm@10 29 ;sets
rlm@10 30 [] #{}
rlm@10 31 [] #{#{1 2} 3 4 5}
rlm@10 32 [] #{1 2 3 4 5}
rlm@10 33 [] #{#{1 2} 3 4 5}
rlm@10 34 ;lists
rlm@10 35 [] '()
rlm@10 36 [1 2 3 4 5] `(1 2 3 4 5)
rlm@10 37 ;maps
rlm@10 38 [] {:a 1 :b 2}
rlm@10 39 [:a 1 :b 2] (seq {:a 1 :b 2})
rlm@10 40 [] {[:a :b] 1 :c 2}
rlm@10 41 [:a :b 1 :c 2] (seq {[:a :b] 1 :c 2})
rlm@10 42 [:a 1 2 :b 3] (seq {:a [1 2] :b 3})
rlm@10 43 ;Strings
rlm@10 44 [] "12345"
rlm@10 45 [\1 \2 \3 \4 \5] (seq "12345")
rlm@10 46 ;fns
rlm@10 47 [] count
rlm@10 48 [count even? odd?] [count even? odd?]))
rlm@10 49
rlm@10 50 (deftest test-separate
rlm@10 51 (are [test-seq] (= (seq/separate even? test-seq) [[2 4] [1 3 5]])
rlm@10 52 [1 2 3 4 5]
rlm@10 53 #{1 2 3 4 5}
rlm@10 54 '(1 2 3 4 5)))
rlm@10 55
rlm@10 56 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 57 (deftest test-indexed
rlm@10 58 (are [expected test-seq] (= (seq/indexed test-seq) expected)
rlm@10 59 [[0 :a] [1 :b] [2 :c] [3 :d]] [:a :b :c :d]
rlm@10 60 [[0 :a] [1 :b] [2 :c] [3 :d]] '(:a :b :c :d)
rlm@10 61 [[0 \1] [1 \2] [2 \3] [3 \4]] "1234"))
rlm@10 62
rlm@10 63 (deftest test-group-by
rlm@10 64 (is (= (seq/group-by even? [1 2 3 4 5])
rlm@10 65 {false [1 3 5], true [2 4]})))
rlm@10 66
rlm@10 67 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 68 (deftest test-partition-by
rlm@10 69 (are [test-seq] (= (seq/partition-by (comp even? count) test-seq)
rlm@10 70 [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]])
rlm@10 71 ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"]
rlm@10 72 '("a" "bb" "cccc" "dd" "eee" "f" "" "hh"))
rlm@10 73 (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm")
rlm@10 74 [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]])))
rlm@10 75
rlm@10 76 (deftest test-frequencies
rlm@10 77 (are [expected test-seq] (= (seq/frequencies test-seq) expected)
rlm@10 78 {\p 2, \s 4, \i 4, \m 1} "mississippi"
rlm@10 79 {1 4 2 2 3 1} [1 1 1 1 2 2 3]
rlm@10 80 {1 4 2 2 3 1} '(1 1 1 1 2 2 3)))
rlm@10 81
rlm@10 82 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 83 ;This is a key differnce between reductions and reduce.
rlm@10 84 (deftest test-reductions
rlm@10 85 (is (= (seq/reductions + [1 2 3 4 5])
rlm@10 86 [1 3 6 10 15]))
rlm@10 87 (is (= (reductions + 10 [1 2 3 4 5])
rlm@10 88 [10 11 13 16 20 25])))
rlm@10 89
rlm@10 90 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 91 (deftest test-rotations
rlm@10 92 (is (= (seq/rotations [1 2 3 4])
rlm@10 93 [[1 2 3 4]
rlm@10 94 [2 3 4 1]
rlm@10 95 [3 4 1 2]
rlm@10 96 [4 1 2 3]])))
rlm@10 97
rlm@10 98 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 99 (deftest test-partition-all
rlm@10 100 (is (= (seq/partition-all 4 [1 2 3 4 5 6 7 8 9])
rlm@10 101 [[1 2 3 4] [5 6 7 8] [9]]))
rlm@10 102 (is (= (seq/partition-all 4 2 [1 2 3 4 5 6 7 8 9])
rlm@10 103 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))
rlm@10 104
rlm@10 105 ;Thanks to Andy Fingerhut for the idea of testing invariants
rlm@10 106 (deftest test-shuffle-invariants
rlm@10 107 (is (= (count (seq/shuffle [1 2 3 4])) 4))
rlm@10 108 (let [shuffled-seq (seq/shuffle [1 2 3 4])]
rlm@10 109 (is (every? #{1 2 3 4} shuffled-seq))))
rlm@10 110
rlm@10 111 ;Thanks to Andy Fingerhut for the idea of testing invariants
rlm@10 112 (deftest test-rand-elt-invariants
rlm@10 113 (let [elt (seq/rand-elt [:a :b :c :d])]
rlm@10 114 (is (#{:a :b :c :d} elt))))
rlm@10 115
rlm@10 116 ;Note - this does not make sense for maps and sets, because order is expected
rlm@10 117 (deftest test-find-first
rlm@10 118 (is (= (seq/find-first even? [1 2 3 4 5]) 2))
rlm@10 119 (is (= (seq/find-first even? '(1 2 3 4 5)) 2)))
rlm@10 120
rlm@10 121 (deftest test-includes
rlm@10 122 (are [coll k] (false? (seq/includes? coll k))
rlm@10 123 [1 2 3] 0
rlm@10 124 [] nil
rlm@10 125 [:a :b] :c)
rlm@10 126 (are [coll k] (true? (seq/includes? coll k))
rlm@10 127 [1 2 3] 1
rlm@10 128 [:a :b] :b))