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