Mercurial > lasercutter
view 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 source
1 (ns clojure.contrib.test-seq2 (:use clojure.test)3 (:require [clojure.contrib.seq :as seq]))6 (deftest test-positions7 (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]))12 ;Upon further inspection, flatten behaves... wierd.13 ;These tests are what passes on August 7, 200914 (deftest test-flatten-present15 (are [expected nested-val] (= (seq/flatten nested-val) expected)16 ;simple literals17 [] nil18 [] 119 [] 'test20 [] :keyword21 [] 1/222 [] #"[\r\n]"23 [] true24 [] false25 ;vectors26 [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 ;sets30 [] #{}31 [] #{#{1 2} 3 4 5}32 [] #{1 2 3 4 5}33 [] #{#{1 2} 3 4 5}34 ;lists35 [] '()36 [1 2 3 4 5] `(1 2 3 4 5)37 ;maps38 [] {: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 ;Strings44 [] "12345"45 [\1 \2 \3 \4 \5] (seq "12345")46 ;fns47 [] count48 [count even? odd?] [count even? odd?]))50 (deftest test-separate51 (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)))56 ;Note - this does not make sense for maps and sets, because order is expected57 (deftest test-indexed58 (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"))63 (deftest test-group-by64 (is (= (seq/group-by even? [1 2 3 4 5])65 {false [1 3 5], true [2 4]})))67 ;Note - this does not make sense for maps and sets, because order is expected68 (deftest test-partition-by69 (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]])))76 (deftest test-frequencies77 (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)))82 ;Note - this does not make sense for maps and sets, because order is expected83 ;This is a key differnce between reductions and reduce.84 (deftest test-reductions85 (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])))90 ;Note - this does not make sense for maps and sets, because order is expected91 (deftest test-rotations92 (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]])))98 ;Note - this does not make sense for maps and sets, because order is expected99 (deftest test-partition-all100 (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]])))105 ;Thanks to Andy Fingerhut for the idea of testing invariants106 (deftest test-shuffle-invariants107 (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))))111 ;Thanks to Andy Fingerhut for the idea of testing invariants112 (deftest test-rand-elt-invariants113 (let [elt (seq/rand-elt [:a :b :c :d])]114 (is (#{:a :b :c :d} elt))))116 ;Note - this does not make sense for maps and sets, because order is expected117 (deftest test-find-first118 (is (= (seq/find-first even? [1 2 3 4 5]) 2))119 (is (= (seq/find-first even? '(1 2 3 4 5)) 2)))121 (deftest test-includes122 (are [coll k] (false? (seq/includes? coll k))123 [1 2 3] 0124 [] nil125 [:a :b] :c)126 (are [coll k] (true? (seq/includes? coll k))127 [1 2 3] 1128 [:a :b] :b))