rlm@10: (ns clojure.contrib.test-seq rlm@10: (:use clojure.test) rlm@10: (:require [clojure.contrib.seq :as seq])) rlm@10: rlm@10: rlm@10: (deftest test-positions rlm@10: (are [expected pred coll] (= expected (seq/positions pred coll)) rlm@10: [2] string? [:a :b "c"] rlm@10: () :d [:a :b :c] rlm@10: [0 2] #{:d} [:d :a :d :a])) rlm@10: rlm@10: ;Upon further inspection, flatten behaves... wierd. rlm@10: ;These tests are what passes on August 7, 2009 rlm@10: (deftest test-flatten-present rlm@10: (are [expected nested-val] (= (seq/flatten nested-val) expected) rlm@10: ;simple literals rlm@10: [] nil rlm@10: [] 1 rlm@10: [] 'test rlm@10: [] :keyword rlm@10: [] 1/2 rlm@10: [] #"[\r\n]" rlm@10: [] true rlm@10: [] false rlm@10: ;vectors rlm@10: [1 2 3 4 5] [[1 2] [3 4 [5]]] rlm@10: [1 2 3 4 5] [1 2 3 4 5] rlm@10: [#{1 2} 3 4 5] [#{1 2} 3 4 5] rlm@10: ;sets rlm@10: [] #{} rlm@10: [] #{#{1 2} 3 4 5} rlm@10: [] #{1 2 3 4 5} rlm@10: [] #{#{1 2} 3 4 5} rlm@10: ;lists rlm@10: [] '() rlm@10: [1 2 3 4 5] `(1 2 3 4 5) rlm@10: ;maps rlm@10: [] {:a 1 :b 2} rlm@10: [:a 1 :b 2] (seq {:a 1 :b 2}) rlm@10: [] {[:a :b] 1 :c 2} rlm@10: [:a :b 1 :c 2] (seq {[:a :b] 1 :c 2}) rlm@10: [:a 1 2 :b 3] (seq {:a [1 2] :b 3}) rlm@10: ;Strings rlm@10: [] "12345" rlm@10: [\1 \2 \3 \4 \5] (seq "12345") rlm@10: ;fns rlm@10: [] count rlm@10: [count even? odd?] [count even? odd?])) rlm@10: rlm@10: (deftest test-separate rlm@10: (are [test-seq] (= (seq/separate even? test-seq) [[2 4] [1 3 5]]) rlm@10: [1 2 3 4 5] rlm@10: #{1 2 3 4 5} rlm@10: '(1 2 3 4 5))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: (deftest test-indexed rlm@10: (are [expected test-seq] (= (seq/indexed test-seq) expected) rlm@10: [[0 :a] [1 :b] [2 :c] [3 :d]] [:a :b :c :d] rlm@10: [[0 :a] [1 :b] [2 :c] [3 :d]] '(:a :b :c :d) rlm@10: [[0 \1] [1 \2] [2 \3] [3 \4]] "1234")) rlm@10: rlm@10: (deftest test-group-by rlm@10: (is (= (seq/group-by even? [1 2 3 4 5]) rlm@10: {false [1 3 5], true [2 4]}))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: (deftest test-partition-by rlm@10: (are [test-seq] (= (seq/partition-by (comp even? count) test-seq) rlm@10: [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]]) rlm@10: ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"] rlm@10: '("a" "bb" "cccc" "dd" "eee" "f" "" "hh")) rlm@10: (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm") rlm@10: [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]]))) rlm@10: rlm@10: (deftest test-frequencies rlm@10: (are [expected test-seq] (= (seq/frequencies test-seq) expected) rlm@10: {\p 2, \s 4, \i 4, \m 1} "mississippi" rlm@10: {1 4 2 2 3 1} [1 1 1 1 2 2 3] rlm@10: {1 4 2 2 3 1} '(1 1 1 1 2 2 3))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: ;This is a key differnce between reductions and reduce. rlm@10: (deftest test-reductions rlm@10: (is (= (seq/reductions + [1 2 3 4 5]) rlm@10: [1 3 6 10 15])) rlm@10: (is (= (reductions + 10 [1 2 3 4 5]) rlm@10: [10 11 13 16 20 25]))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: (deftest test-rotations rlm@10: (is (= (seq/rotations [1 2 3 4]) rlm@10: [[1 2 3 4] rlm@10: [2 3 4 1] rlm@10: [3 4 1 2] rlm@10: [4 1 2 3]]))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: (deftest test-partition-all rlm@10: (is (= (seq/partition-all 4 [1 2 3 4 5 6 7 8 9]) rlm@10: [[1 2 3 4] [5 6 7 8] [9]])) rlm@10: (is (= (seq/partition-all 4 2 [1 2 3 4 5 6 7 8 9]) rlm@10: [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]]))) rlm@10: rlm@10: ;Thanks to Andy Fingerhut for the idea of testing invariants rlm@10: (deftest test-shuffle-invariants rlm@10: (is (= (count (seq/shuffle [1 2 3 4])) 4)) rlm@10: (let [shuffled-seq (seq/shuffle [1 2 3 4])] rlm@10: (is (every? #{1 2 3 4} shuffled-seq)))) rlm@10: rlm@10: ;Thanks to Andy Fingerhut for the idea of testing invariants rlm@10: (deftest test-rand-elt-invariants rlm@10: (let [elt (seq/rand-elt [:a :b :c :d])] rlm@10: (is (#{:a :b :c :d} elt)))) rlm@10: rlm@10: ;Note - this does not make sense for maps and sets, because order is expected rlm@10: (deftest test-find-first rlm@10: (is (= (seq/find-first even? [1 2 3 4 5]) 2)) rlm@10: (is (= (seq/find-first even? '(1 2 3 4 5)) 2))) rlm@10: rlm@10: (deftest test-includes rlm@10: (are [coll k] (false? (seq/includes? coll k)) rlm@10: [1 2 3] 0 rlm@10: [] nil rlm@10: [:a :b] :c) rlm@10: (are [coll k] (true? (seq/includes? coll k)) rlm@10: [1 2 3] 1 rlm@10: [:a :b] :b))