Mercurial > lasercutter
view src/clojure/test_clojure/sequences.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 ; Copyright (c) Rich Hickey. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ; which can be found in the file epl-v10.html at the root of this distribution.5 ; By using this software in any fashion, you are agreeing to be bound by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ; Author: Frantisek Sodomka10 ; Contributors: Stuart Halloway12 (ns clojure.test-clojure.sequences13 (:use clojure.test))15 ;; *** Tests ***17 ; TODO:18 ; apply, map, filter, remove19 ; and more...21 (deftest test-reduce-from-chunked-into-unchunked22 (= [1 2 \a \b] (into [] (concat [1 2] "ab"))))24 (deftest test-reduce25 (let [int+ (fn [a b] (+ (int a) (int b)))26 arange (range 100) ;; enough to cross nodes27 avec (into [] arange)28 alist (into () arange)29 obj-array (into-array arange)30 int-array (into-array Integer/TYPE arange)31 long-array (into-array Long/TYPE arange)32 float-array (into-array Float/TYPE arange)33 char-array (into-array Character/TYPE (map char arange))34 double-array (into-array Double/TYPE arange)35 byte-array (into-array Byte/TYPE (map byte arange))36 int-vec (into (vector-of :int) arange)37 long-vec (into (vector-of :long) arange)38 float-vec (into (vector-of :float) arange)39 char-vec (into (vector-of :char) (map char arange))40 double-vec (into (vector-of :double) arange)41 byte-vec (into (vector-of :byte) (map byte arange))42 all-true (into-array Boolean/TYPE (repeat 10 true))]43 (is (= 495044 (reduce + arange)45 (reduce + avec)46 (reduce + alist)47 (reduce + obj-array)48 (reduce + int-array)49 (reduce + long-array)50 (reduce + float-array)51 (reduce int+ char-array)52 (reduce + double-array)53 (reduce int+ byte-array)54 (reduce + int-vec)55 (reduce + long-vec)56 (reduce + float-vec)57 (reduce int+ char-vec)58 (reduce + double-vec)59 (reduce int+ byte-vec)))60 (is (= 495161 (reduce + 1 arange)62 (reduce + 1 avec)63 (reduce + 1 alist)64 (reduce + 1 obj-array)65 (reduce + 1 int-array)66 (reduce + 1 long-array)67 (reduce + 1 float-array)68 (reduce int+ 1 char-array)69 (reduce + 1 double-array)70 (reduce int+ 1 byte-array)71 (reduce + 1 int-vec)72 (reduce + 1 long-vec)73 (reduce + 1 float-vec)74 (reduce int+ 1 char-vec)75 (reduce + 1 double-vec)76 (reduce int+ 1 byte-vec)))77 (is (= true78 (reduce #(and %1 %2) all-true)79 (reduce #(and %1 %2) true all-true)))))81 (deftest test-equality82 ; lazy sequences83 (are [x y] (= x y)84 ; fixed SVN 1288 - LazySeq and EmptyList equals/equiv85 ; http://groups.google.com/group/clojure/browse_frm/thread/286d807be9cae2a5#86 (map inc nil) ()87 (map inc ()) ()88 (map inc []) ()89 (map inc #{}) ()90 (map inc {}) () ))93 (deftest test-lazy-seq94 (are [x] (seq? x)95 (lazy-seq nil)96 (lazy-seq [])97 (lazy-seq [1 2]))99 (are [x y] (= x y)100 (lazy-seq nil) ()101 (lazy-seq [nil]) '(nil)103 (lazy-seq ()) ()104 (lazy-seq []) ()105 (lazy-seq #{}) ()106 (lazy-seq {}) ()107 (lazy-seq "") ()108 (lazy-seq (into-array [])) ()110 (lazy-seq (list 1 2)) '(1 2)111 (lazy-seq [1 2]) '(1 2)112 (lazy-seq (sorted-set 1 2)) '(1 2)113 (lazy-seq (sorted-map :a 1 :b 2)) '([:a 1] [:b 2])114 (lazy-seq "abc") '(\a \b \c)115 (lazy-seq (into-array [1 2])) '(1 2) ))118 (deftest test-seq119 (is (not (seq? (seq []))))120 (is (seq? (seq [1 2])))122 (are [x y] (= x y)123 (seq nil) nil124 (seq [nil]) '(nil)126 (seq ()) nil127 (seq []) nil128 (seq #{}) nil129 (seq {}) nil130 (seq "") nil131 (seq (into-array [])) nil133 (seq (list 1 2)) '(1 2)134 (seq [1 2]) '(1 2)135 (seq (sorted-set 1 2)) '(1 2)136 (seq (sorted-map :a 1 :b 2)) '([:a 1] [:b 2])137 (seq "abc") '(\a \b \c)138 (seq (into-array [1 2])) '(1 2) ))141 (deftest test-cons142 (is (thrown? IllegalArgumentException (cons 1 2)))143 (are [x y] (= x y)144 (cons 1 nil) '(1)145 (cons nil nil) '(nil)147 (cons \a nil) '(\a)148 (cons \a "") '(\a)149 (cons \a "bc") '(\a \b \c)151 (cons 1 ()) '(1)152 (cons 1 '(2 3)) '(1 2 3)154 (cons 1 []) [1]155 (cons 1 [2 3]) [1 2 3]157 (cons 1 #{}) '(1)158 (cons 1 (sorted-set 2 3)) '(1 2 3)160 (cons 1 (into-array [])) '(1)161 (cons 1 (into-array [2 3])) '(1 2 3) ))164 (deftest test-empty165 (are [x y] (and (= (empty x) y)166 (= (class (empty x)) (class y)))167 nil nil169 () ()170 '(1 2) ()172 [] []173 [1 2] []175 {} {}176 {:a 1 :b 2} {}178 (sorted-map) (sorted-map)179 (sorted-map :a 1 :b 2) (sorted-map)181 #{} #{}182 #{1 2} #{}184 (sorted-set) (sorted-set)185 (sorted-set 1 2) (sorted-set)187 (seq ()) nil ; (seq ()) => nil188 (seq '(1 2)) ()190 (seq []) nil ; (seq []) => nil191 (seq [1 2]) ()193 (seq "") nil ; (seq "") => nil194 (seq "ab") ()196 (lazy-seq ()) ()197 (lazy-seq '(1 2)) ()199 (lazy-seq []) ()200 (lazy-seq [1 2]) ()202 ; non-coll, non-seq => nil203 42 nil204 1.2 nil205 "abc" nil ))207 ;Tests that the comparator is preservered208 ;The first element should be the same in each set if preserved.209 (deftest test-empty-sorted210 (let [inv-compare (comp - compare)]211 (are [x y] (= (first (into (empty x) x))212 (first y))213 (sorted-set 1 2 3) (sorted-set 1 2 3)214 (sorted-set-by inv-compare 1 2 3) (sorted-set-by inv-compare 1 2 3)216 (sorted-map 1 :a 2 :b 3 :c) (sorted-map 1 :a 2 :b 3 :c)217 (sorted-map-by inv-compare 1 :a 2 :b 3 :c) (sorted-map-by inv-compare 1 :a 2 :b 3 :c))))220 (deftest test-not-empty221 ; empty coll/seq => nil222 (are [x] (= (not-empty x) nil)223 ()224 []225 {}226 #{}227 (seq ())228 (seq [])229 (lazy-seq ())230 (lazy-seq []) )232 ; non-empty coll/seq => identity233 (are [x] (and (= (not-empty x) x)234 (= (class (not-empty x)) (class x)))235 '(1 2)236 [1 2]237 {:a 1}238 #{1 2}239 (seq '(1 2))240 (seq [1 2])241 (lazy-seq '(1 2))242 (lazy-seq [1 2]) ))245 (deftest test-first246 (is (thrown? IllegalArgumentException (first)))247 (is (thrown? IllegalArgumentException (first true)))248 (is (thrown? IllegalArgumentException (first false)))249 (is (thrown? IllegalArgumentException (first 1)))250 (is (thrown? IllegalArgumentException (first 1 2)))251 (is (thrown? IllegalArgumentException (first \a)))252 (is (thrown? IllegalArgumentException (first 's)))253 (is (thrown? IllegalArgumentException (first :k)))254 (are [x y] (= x y)255 (first nil) nil257 ; string258 (first "") nil259 (first "a") \a260 (first "abc") \a262 ; list263 (first ()) nil264 (first '(1)) 1265 (first '(1 2 3)) 1267 (first '(nil)) nil268 (first '(1 nil)) 1269 (first '(nil 2)) nil270 (first '(())) ()271 (first '(() nil)) ()272 (first '(() 2 nil)) ()274 ; vector275 (first []) nil276 (first [1]) 1277 (first [1 2 3]) 1279 (first [nil]) nil280 (first [1 nil]) 1281 (first [nil 2]) nil282 (first [[]]) []283 (first [[] nil]) []284 (first [[] 2 nil]) []286 ; set287 (first #{}) nil288 (first #{1}) 1289 (first (sorted-set 1 2 3)) 1291 (first #{nil}) nil292 (first (sorted-set 1 nil)) nil293 (first (sorted-set nil 2)) nil294 (first #{#{}}) #{}295 (first (sorted-set #{} nil)) nil296 ;(first (sorted-set #{} 2 nil)) nil298 ; map299 (first {}) nil300 (first (sorted-map :a 1)) '(:a 1)301 (first (sorted-map :a 1 :b 2 :c 3)) '(:a 1)303 ; array304 (first (into-array [])) nil305 (first (into-array [1])) 1306 (first (into-array [1 2 3])) 1307 (first (to-array [nil])) nil308 (first (to-array [1 nil])) 1309 (first (to-array [nil 2])) nil ))312 (deftest test-next313 (is (thrown? IllegalArgumentException (next)))314 (is (thrown? IllegalArgumentException (next true)))315 (is (thrown? IllegalArgumentException (next false)))316 (is (thrown? IllegalArgumentException (next 1)))317 (is (thrown? IllegalArgumentException (next 1 2)))318 (is (thrown? IllegalArgumentException (next \a)))319 (is (thrown? IllegalArgumentException (next 's)))320 (is (thrown? IllegalArgumentException (next :k)))321 (are [x y] (= x y)322 (next nil) nil324 ; string325 (next "") nil326 (next "a") nil327 (next "abc") '(\b \c)329 ; list330 (next ()) nil331 (next '(1)) nil332 (next '(1 2 3)) '(2 3)334 (next '(nil)) nil335 (next '(1 nil)) '(nil)336 (next '(1 ())) '(())337 (next '(nil 2)) '(2)338 (next '(())) nil339 (next '(() nil)) '(nil)340 (next '(() 2 nil)) '(2 nil)342 ; vector343 (next []) nil344 (next [1]) nil345 (next [1 2 3]) [2 3]347 (next [nil]) nil348 (next [1 nil]) [nil]349 (next [1 []]) [[]]350 (next [nil 2]) [2]351 (next [[]]) nil352 (next [[] nil]) [nil]353 (next [[] 2 nil]) [2 nil]355 ; set356 (next #{}) nil357 (next #{1}) nil358 (next (sorted-set 1 2 3)) '(2 3)360 (next #{nil}) nil361 (next (sorted-set 1 nil)) '(1)362 (next (sorted-set nil 2)) '(2)363 (next #{#{}}) nil364 (next (sorted-set #{} nil)) '(#{})365 ;(next (sorted-set #{} 2 nil)) #{}367 ; map368 (next {}) nil369 (next (sorted-map :a 1)) nil370 (next (sorted-map :a 1 :b 2 :c 3)) '((:b 2) (:c 3))372 ; array373 (next (into-array [])) nil374 (next (into-array [1])) nil375 (next (into-array [1 2 3])) '(2 3)377 (next (to-array [nil])) nil378 (next (to-array [1 nil])) '(nil)379 ;(next (to-array [1 (into-array [])])) (list (into-array []))380 (next (to-array [nil 2])) '(2)381 (next (to-array [(into-array [])])) nil382 (next (to-array [(into-array []) nil])) '(nil)383 (next (to-array [(into-array []) 2 nil])) '(2 nil) ))386 (deftest test-last387 (are [x y] (= x y)388 (last nil) nil390 ; list391 (last ()) nil392 (last '(1)) 1393 (last '(1 2 3)) 3395 (last '(nil)) nil396 (last '(1 nil)) nil397 (last '(nil 2)) 2398 (last '(())) ()399 (last '(() nil)) nil400 (last '(() 2 nil)) nil402 ; vector403 (last []) nil404 (last [1]) 1405 (last [1 2 3]) 3407 (last [nil]) nil408 (last [1 nil]) nil409 (last [nil 2]) 2410 (last [[]]) []411 (last [[] nil]) nil412 (last [[] 2 nil]) nil414 ; set415 (last #{}) nil416 (last #{1}) 1417 (last (sorted-set 1 2 3)) 3419 (last #{nil}) nil420 (last (sorted-set 1 nil)) 1421 (last (sorted-set nil 2)) 2422 (last #{#{}}) #{}423 (last (sorted-set #{} nil)) #{}424 ;(last (sorted-set #{} 2 nil)) nil426 ; map427 (last {}) nil428 (last (sorted-map :a 1)) [:a 1]429 (last (sorted-map :a 1 :b 2 :c 3)) [:c 3]431 ; string432 (last "") nil433 (last "a") \a434 (last "abc") \c436 ; array437 (last (into-array [])) nil438 (last (into-array [1])) 1439 (last (into-array [1 2 3])) 3440 (last (to-array [nil])) nil441 (last (to-array [1 nil])) nil442 (last (to-array [nil 2])) 2 ))445 ;; (ffirst coll) = (first (first coll))446 ;;447 (deftest test-ffirst448 (is (thrown? IllegalArgumentException (ffirst)))449 (are [x y] (= x y)450 (ffirst nil) nil452 (ffirst ()) nil453 (ffirst '((1 2) (3 4))) 1455 (ffirst []) nil456 (ffirst [[1 2] [3 4]]) 1458 (ffirst {}) nil459 (ffirst {:a 1}) :a461 (ffirst #{}) nil462 (ffirst #{[1 2]}) 1 ))465 ;; (fnext coll) = (first (next coll)) = (second coll)466 ;;467 (deftest test-fnext468 (is (thrown? IllegalArgumentException (fnext)))469 (are [x y] (= x y)470 (fnext nil) nil472 (fnext ()) nil473 (fnext '(1)) nil474 (fnext '(1 2 3 4)) 2476 (fnext []) nil477 (fnext [1]) nil478 (fnext [1 2 3 4]) 2480 (fnext {}) nil481 (fnext (sorted-map :a 1)) nil482 (fnext (sorted-map :a 1 :b 2)) [:b 2]484 (fnext #{}) nil485 (fnext #{1}) nil486 (fnext (sorted-set 1 2 3 4)) 2 ))489 ;; (nfirst coll) = (next (first coll))490 ;;491 (deftest test-nfirst492 (is (thrown? IllegalArgumentException (nfirst)))493 (are [x y] (= x y)494 (nfirst nil) nil496 (nfirst ()) nil497 (nfirst '((1 2 3) (4 5 6))) '(2 3)499 (nfirst []) nil500 (nfirst [[1 2 3] [4 5 6]]) '(2 3)502 (nfirst {}) nil503 (nfirst {:a 1}) '(1)505 (nfirst #{}) nil506 (nfirst #{[1 2]}) '(2) ))509 ;; (nnext coll) = (next (next coll))510 ;;511 (deftest test-nnext512 (is (thrown? IllegalArgumentException (nnext)))513 (are [x y] (= x y)514 (nnext nil) nil516 (nnext ()) nil517 (nnext '(1)) nil518 (nnext '(1 2)) nil519 (nnext '(1 2 3 4)) '(3 4)521 (nnext []) nil522 (nnext [1]) nil523 (nnext [1 2]) nil524 (nnext [1 2 3 4]) '(3 4)526 (nnext {}) nil527 (nnext (sorted-map :a 1)) nil528 (nnext (sorted-map :a 1 :b 2)) nil529 (nnext (sorted-map :a 1 :b 2 :c 3 :d 4)) '([:c 3] [:d 4])531 (nnext #{}) nil532 (nnext #{1}) nil533 (nnext (sorted-set 1 2)) nil534 (nnext (sorted-set 1 2 3 4)) '(3 4) ))537 (deftest test-nth538 ; maps, sets are not supported539 (is (thrown? UnsupportedOperationException (nth {} 0)))540 (is (thrown? UnsupportedOperationException (nth {:a 1 :b 2} 0)))541 (is (thrown? UnsupportedOperationException (nth #{} 0)))542 (is (thrown? UnsupportedOperationException (nth #{1 2 3} 0)))544 ; out of bounds545 (is (thrown? IndexOutOfBoundsException (nth '() 0)))546 (is (thrown? IndexOutOfBoundsException (nth '(1 2 3) 5)))547 (is (thrown? IndexOutOfBoundsException (nth '() -1)))548 (is (thrown? IndexOutOfBoundsException (nth '(1 2 3) -1)))550 (is (thrown? IndexOutOfBoundsException (nth [] 0)))551 (is (thrown? IndexOutOfBoundsException (nth [1 2 3] 5)))552 (is (thrown? IndexOutOfBoundsException (nth [] -1)))553 (is (thrown? IndexOutOfBoundsException (nth [1 2 3] -1))) ; ???555 (is (thrown? IndexOutOfBoundsException (nth (into-array []) 0)))556 (is (thrown? IndexOutOfBoundsException (nth (into-array [1 2 3]) 5)))557 (is (thrown? IndexOutOfBoundsException (nth (into-array []) -1)))558 (is (thrown? IndexOutOfBoundsException (nth (into-array [1 2 3]) -1)))560 (is (thrown? StringIndexOutOfBoundsException (nth "" 0)))561 (is (thrown? StringIndexOutOfBoundsException (nth "abc" 5)))562 (is (thrown? StringIndexOutOfBoundsException (nth "" -1)))563 (is (thrown? StringIndexOutOfBoundsException (nth "abc" -1)))565 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. []) 0)))566 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) 5)))567 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. []) -1))) ; ???568 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) -1))) ; ???570 (are [x y] (= x y)571 (nth '(1) 0) 1572 (nth '(1 2 3) 0) 1573 (nth '(1 2 3 4 5) 1) 2574 (nth '(1 2 3 4 5) 4) 5575 (nth '(1 2 3) 5 :not-found) :not-found577 (nth [1] 0) 1578 (nth [1 2 3] 0) 1579 (nth [1 2 3 4 5] 1) 2580 (nth [1 2 3 4 5] 4) 5581 (nth [1 2 3] 5 :not-found) :not-found583 (nth (into-array [1]) 0) 1584 (nth (into-array [1 2 3]) 0) 1585 (nth (into-array [1 2 3 4 5]) 1) 2586 (nth (into-array [1 2 3 4 5]) 4) 5587 (nth (into-array [1 2 3]) 5 :not-found) :not-found589 (nth "a" 0) \a590 (nth "abc" 0) \a591 (nth "abcde" 1) \b592 (nth "abcde" 4) \e593 (nth "abc" 5 :not-found) :not-found595 (nth (java.util.ArrayList. [1]) 0) 1596 (nth (java.util.ArrayList. [1 2 3]) 0) 1597 (nth (java.util.ArrayList. [1 2 3 4 5]) 1) 2598 (nth (java.util.ArrayList. [1 2 3 4 5]) 4) 5599 (nth (java.util.ArrayList. [1 2 3]) 5 :not-found) :not-found )601 ; regex Matchers602 (let [m (re-matcher #"(a)(b)" "ababaa")]603 (re-find m) ; => ["ab" "a" "b"]604 (are [x y] (= x y)605 (nth m 0) "ab"606 (nth m 1) "a"607 (nth m 2) "b"608 (nth m 3 :not-found) :not-found609 (nth m -1 :not-found) :not-found )610 (is (thrown? IndexOutOfBoundsException (nth m 3)))611 (is (thrown? IndexOutOfBoundsException (nth m -1))))613 (let [m (re-matcher #"c" "ababaa")]614 (re-find m) ; => nil615 (are [x y] (= x y)616 (nth m 0 :not-found) :not-found617 (nth m 2 :not-found) :not-found618 (nth m -1 :not-found) :not-found )619 (is (thrown? IllegalStateException (nth m 0)))620 (is (thrown? IllegalStateException (nth m 2)))621 (is (thrown? IllegalStateException (nth m -1)))))624 ; distinct was broken for nil & false:625 ; fixed in rev 1278:626 ; http://code.google.com/p/clojure/source/detail?r=1278627 ;628 (deftest test-distinct629 (are [x y] (= x y)630 (distinct ()) ()631 (distinct '(1)) '(1)632 (distinct '(1 2 3)) '(1 2 3)633 (distinct '(1 2 3 1 1 1)) '(1 2 3)634 (distinct '(1 1 1 2)) '(1 2)635 (distinct '(1 2 1 2)) '(1 2)637 (distinct []) ()638 (distinct [1]) '(1)639 (distinct [1 2 3]) '(1 2 3)640 (distinct [1 2 3 1 2 2 1 1]) '(1 2 3)641 (distinct [1 1 1 2]) '(1 2)642 (distinct [1 2 1 2]) '(1 2)644 (distinct "") ()645 (distinct "a") '(\a)646 (distinct "abc") '(\a \b \c)647 (distinct "abcabab") '(\a \b \c)648 (distinct "aaab") '(\a \b)649 (distinct "abab") '(\a \b) )651 (are [x] (= (distinct [x x]) [x])652 nil653 false true654 0 42655 0.0 3.14656 2/3657 0M 1M658 \c659 "" "abc"660 'sym661 :kw662 () '(1 2)663 [] [1 2]664 {} {:a 1 :b 2}665 #{} #{1 2} ))668 (deftest test-interpose669 (are [x y] (= x y)670 (interpose 0 []) ()671 (interpose 0 [1]) '(1)672 (interpose 0 [1 2]) '(1 0 2)673 (interpose 0 [1 2 3]) '(1 0 2 0 3) ))676 (deftest test-interleave677 (are [x y] (= x y)678 (interleave [1 2] [3 4]) '(1 3 2 4)680 (interleave [1] [3 4]) '(1 3)681 (interleave [1 2] [3]) '(1 3)683 (interleave [] [3 4]) ()684 (interleave [1 2] []) ()685 (interleave [] []) () ))688 (deftest test-zipmap689 (are [x y] (= x y)690 (zipmap [:a :b] [1 2]) {:a 1 :b 2}692 (zipmap [:a] [1 2]) {:a 1}693 (zipmap [:a :b] [1]) {:a 1}695 (zipmap [] [1 2]) {}696 (zipmap [:a :b] []) {}697 (zipmap [] []) {} ))700 (deftest test-concat701 (are [x y] (= x y)702 (concat) ()704 (concat []) ()705 (concat [1 2]) '(1 2)707 (concat [1 2] [3 4]) '(1 2 3 4)708 (concat [] [3 4]) '(3 4)709 (concat [1 2] []) '(1 2)710 (concat [] []) ()712 (concat [1 2] [3 4] [5 6]) '(1 2 3 4 5 6) ))715 (deftest test-cycle716 (are [x y] (= x y)717 (cycle []) ()719 (take 3 (cycle [1])) '(1 1 1)720 (take 5 (cycle [1 2 3])) '(1 2 3 1 2)722 (take 3 (cycle [nil])) '(nil nil nil) ))725 (deftest test-partition726 (are [x y] (= x y)727 (partition 2 [1 2 3]) '((1 2))728 (partition 2 [1 2 3 4]) '((1 2) (3 4))729 (partition 2 []) ()731 (partition 2 3 [1 2 3 4 5 6 7]) '((1 2) (4 5))732 (partition 2 3 [1 2 3 4 5 6 7 8]) '((1 2) (4 5) (7 8))733 (partition 2 3 []) ()735 (partition 1 []) ()736 (partition 1 [1 2 3]) '((1) (2) (3))738 (partition 5 [1 2 3]) ()740 ; (partition 0 [1 2 3]) (repeat nil) ; infinite sequence of nil741 (partition -1 [1 2 3]) ()742 (partition -2 [1 2 3]) () ))745 (deftest test-reverse746 (are [x y] (= x y)747 (reverse nil) () ; since SVN 1294748 (reverse []) ()749 (reverse [1]) '(1)750 (reverse [1 2 3]) '(3 2 1) ))753 (deftest test-take754 (are [x y] (= x y)755 (take 1 [1 2 3 4 5]) '(1)756 (take 3 [1 2 3 4 5]) '(1 2 3)757 (take 5 [1 2 3 4 5]) '(1 2 3 4 5)758 (take 9 [1 2 3 4 5]) '(1 2 3 4 5)760 (take 0 [1 2 3 4 5]) ()761 (take -1 [1 2 3 4 5]) ()762 (take -2 [1 2 3 4 5]) () ))765 (deftest test-drop766 (are [x y] (= x y)767 (drop 1 [1 2 3 4 5]) '(2 3 4 5)768 (drop 3 [1 2 3 4 5]) '(4 5)769 (drop 5 [1 2 3 4 5]) ()770 (drop 9 [1 2 3 4 5]) ()772 (drop 0 [1 2 3 4 5]) '(1 2 3 4 5)773 (drop -1 [1 2 3 4 5]) '(1 2 3 4 5)774 (drop -2 [1 2 3 4 5]) '(1 2 3 4 5) ))777 (deftest test-take-nth778 (are [x y] (= x y)779 (take-nth 1 [1 2 3 4 5]) '(1 2 3 4 5)780 (take-nth 2 [1 2 3 4 5]) '(1 3 5)781 (take-nth 3 [1 2 3 4 5]) '(1 4)782 (take-nth 4 [1 2 3 4 5]) '(1 5)783 (take-nth 5 [1 2 3 4 5]) '(1)784 (take-nth 9 [1 2 3 4 5]) '(1)786 ; infinite seq of 1s = (repeat 1)787 ;(take-nth 0 [1 2 3 4 5])788 ;(take-nth -1 [1 2 3 4 5])789 ;(take-nth -2 [1 2 3 4 5])790 ))793 (deftest test-take-while794 (are [x y] (= x y)795 (take-while pos? []) ()796 (take-while pos? [1 2 3 4]) '(1 2 3 4)797 (take-while pos? [1 2 3 -1]) '(1 2 3)798 (take-while pos? [1 -1 2 3]) '(1)799 (take-while pos? [-1 1 2 3]) ()800 (take-while pos? [-1 -2 -3]) () ))803 (deftest test-drop-while804 (are [x y] (= x y)805 (drop-while pos? []) ()806 (drop-while pos? [1 2 3 4]) ()807 (drop-while pos? [1 2 3 -1]) '(-1)808 (drop-while pos? [1 -1 2 3]) '(-1 2 3)809 (drop-while pos? [-1 1 2 3]) '(-1 1 2 3)810 (drop-while pos? [-1 -2 -3]) '(-1 -2 -3) ))813 (deftest test-butlast814 (are [x y] (= x y)815 (butlast []) nil816 (butlast [1]) nil817 (butlast [1 2 3]) '(1 2) ))820 (deftest test-drop-last821 (are [x y] (= x y)822 ; as butlast823 (drop-last []) ()824 (drop-last [1]) ()825 (drop-last [1 2 3]) '(1 2)827 ; as butlast, but lazy828 (drop-last 1 []) ()829 (drop-last 1 [1]) ()830 (drop-last 1 [1 2 3]) '(1 2)832 (drop-last 2 []) ()833 (drop-last 2 [1]) ()834 (drop-last 2 [1 2 3]) '(1)836 (drop-last 5 []) ()837 (drop-last 5 [1]) ()838 (drop-last 5 [1 2 3]) ()840 (drop-last 0 []) ()841 (drop-last 0 [1]) '(1)842 (drop-last 0 [1 2 3]) '(1 2 3)844 (drop-last -1 []) ()845 (drop-last -1 [1]) '(1)846 (drop-last -1 [1 2 3]) '(1 2 3)848 (drop-last -2 []) ()849 (drop-last -2 [1]) '(1)850 (drop-last -2 [1 2 3]) '(1 2 3) ))853 (deftest test-split-at854 (is (vector? (split-at 2 [])))855 (is (vector? (split-at 2 [1 2 3])))857 (are [x y] (= x y)858 (split-at 2 []) [() ()]859 (split-at 2 [1 2 3 4 5]) [(list 1 2) (list 3 4 5)]861 (split-at 5 [1 2 3]) [(list 1 2 3) ()]862 (split-at 0 [1 2 3]) [() (list 1 2 3)]863 (split-at -1 [1 2 3]) [() (list 1 2 3)]864 (split-at -5 [1 2 3]) [() (list 1 2 3)] ))867 (deftest test-split-with868 (is (vector? (split-with pos? [])))869 (is (vector? (split-with pos? [1 2 -1 0 3 4])))871 (are [x y] (= x y)872 (split-with pos? []) [() ()]873 (split-with pos? [1 2 -1 0 3 4]) [(list 1 2) (list -1 0 3 4)]875 (split-with pos? [-1 2 3 4 5]) [() (list -1 2 3 4 5)]876 (split-with number? [1 -2 "abc" \x]) [(list 1 -2) (list "abc" \x)] ))879 (deftest test-repeat880 (is (thrown? IllegalArgumentException (repeat)))882 ; infinite sequence => use take883 (are [x y] (= x y)884 (take 0 (repeat 7)) ()885 (take 1 (repeat 7)) '(7)886 (take 2 (repeat 7)) '(7 7)887 (take 5 (repeat 7)) '(7 7 7 7 7) )889 ; limited sequence890 (are [x y] (= x y)891 (repeat 0 7) ()892 (repeat 1 7) '(7)893 (repeat 2 7) '(7 7)894 (repeat 5 7) '(7 7 7 7 7)896 (repeat -1 7) ()897 (repeat -3 7) () )899 ; test different data types900 (are [x] (= (repeat 3 x) (list x x x))901 nil902 false true903 0 42904 0.0 3.14905 2/3906 0M 1M907 \c908 "" "abc"909 'sym910 :kw911 () '(1 2)912 [] [1 2]913 {} {:a 1 :b 2}914 #{} #{1 2} ))917 (deftest test-range918 (are [x y] (= x y)919 (range 0) () ; exclusive end!920 (range 1) '(0)921 (range 5) '(0 1 2 3 4)923 (range -1) ()924 (range -3) ()926 (range 2.5) '(0 1 2)927 (range 7/3) '(0 1 2)929 (range 0 3) '(0 1 2)930 (range 0 1) '(0)931 (range 0 0) ()932 (range 0 -3) ()934 (range 3 6) '(3 4 5)935 (range 3 4) '(3)936 (range 3 3) ()937 (range 3 1) ()938 (range 3 0) ()939 (range 3 -2) ()941 (range -2 5) '(-2 -1 0 1 2 3 4)942 (range -2 0) '(-2 -1)943 (range -2 -1) '(-2)944 (range -2 -2) ()945 (range -2 -5) ()947 (range 3 9 0) ()948 (range 3 9 1) '(3 4 5 6 7 8)949 (range 3 9 2) '(3 5 7)950 (range 3 9 3) '(3 6)951 (range 3 9 10) '(3)952 (range 3 9 -1) () ))955 (deftest test-empty?956 (are [x] (empty? x)957 nil958 ()959 (lazy-seq nil) ; => ()960 []961 {}962 #{}963 ""964 (into-array []) )966 (are [x] (not (empty? x))967 '(1 2)968 (lazy-seq [1 2])969 [1 2]970 {:a 1 :b 2}971 #{1 2}972 "abc"973 (into-array [1 2]) ))976 (deftest test-every?977 ; always true for nil or empty coll/seq978 (are [x] (= (every? pos? x) true)979 nil980 () [] {} #{}981 (lazy-seq [])982 (into-array []) )984 (are [x y] (= x y)985 true (every? pos? [1])986 true (every? pos? [1 2])987 true (every? pos? [1 2 3 4 5])989 false (every? pos? [-1])990 false (every? pos? [-1 -2])991 false (every? pos? [-1 -2 3])992 false (every? pos? [-1 2])993 false (every? pos? [1 -2])994 false (every? pos? [1 2 -3])995 false (every? pos? [1 2 -3 4]) )997 (are [x y] (= x y)998 true (every? #{:a} [:a :a])999 ;! false (every? #{:a} [:a :b]) ; Issue 68: every? returns nil instead of false1000 ;! false (every? #{:a} [:b :b]) ; http://code.google.com/p/clojure/issues/detail?id=681001 ))1004 (deftest test-not-every?1005 ; always false for nil or empty coll/seq1006 (are [x] (= (not-every? pos? x) false)1007 nil1008 () [] {} #{}1009 (lazy-seq [])1010 (into-array []) )1012 (are [x y] (= x y)1013 false (not-every? pos? [1])1014 false (not-every? pos? [1 2])1015 false (not-every? pos? [1 2 3 4 5])1017 true (not-every? pos? [-1])1018 true (not-every? pos? [-1 -2])1019 true (not-every? pos? [-1 -2 3])1020 true (not-every? pos? [-1 2])1021 true (not-every? pos? [1 -2])1022 true (not-every? pos? [1 2 -3])1023 true (not-every? pos? [1 2 -3 4]) )1025 (are [x y] (= x y)1026 false (not-every? #{:a} [:a :a])1027 true (not-every? #{:a} [:a :b])1028 true (not-every? #{:a} [:b :b]) ))1031 (deftest test-not-any?1032 ; always true for nil or empty coll/seq1033 (are [x] (= (not-any? pos? x) true)1034 nil1035 () [] {} #{}1036 (lazy-seq [])1037 (into-array []) )1039 (are [x y] (= x y)1040 false (not-any? pos? [1])1041 false (not-any? pos? [1 2])1042 false (not-any? pos? [1 2 3 4 5])1044 true (not-any? pos? [-1])1045 true (not-any? pos? [-1 -2])1047 false (not-any? pos? [-1 -2 3])1048 false (not-any? pos? [-1 2])1049 false (not-any? pos? [1 -2])1050 false (not-any? pos? [1 2 -3])1051 false (not-any? pos? [1 2 -3 4]) )1053 (are [x y] (= x y)1054 false (not-any? #{:a} [:a :a])1055 false (not-any? #{:a} [:a :b])1056 true (not-any? #{:a} [:b :b]) ))1059 (deftest test-some1060 ;; always nil for nil or empty coll/seq1061 (are [x] (= (some pos? x) nil)1062 nil1063 () [] {} #{}1064 (lazy-seq [])1065 (into-array []))1067 (are [x y] (= x y)1068 nil (some nil nil)1070 true (some pos? [1])1071 true (some pos? [1 2])1073 nil (some pos? [-1])1074 nil (some pos? [-1 -2])1075 true (some pos? [-1 2])1076 true (some pos? [1 -2])1078 :a (some #{:a} [:a :a])1079 :a (some #{:a} [:b :a])1080 nil (some #{:a} [:b :b])1082 :a (some #{:a} '(:a :b))1083 :a (some #{:a} #{:a :b})1084 ))1086 (deftest test-flatten-present1087 (are [expected nested-val] (= (flatten nested-val) expected)1088 ;simple literals1089 [] nil1090 [] 11091 [] 'test1092 [] :keyword1093 [] 1/21094 [] #"[\r\n]"1095 [] true1096 [] false1097 ;vectors1098 [1 2 3 4 5] [[1 2] [3 4 [5]]]1099 [1 2 3 4 5] [1 2 3 4 5]1100 [#{1 2} 3 4 5] [#{1 2} 3 4 5]1101 ;sets1102 [] #{}1103 [] #{#{1 2} 3 4 5}1104 [] #{1 2 3 4 5}1105 [] #{#{1 2} 3 4 5}1106 ;lists1107 [] '()1108 [1 2 3 4 5] `(1 2 3 4 5)1109 ;maps1110 [] {:a 1 :b 2}1111 [:a 1 :b 2] (seq {:a 1 :b 2})1112 [] {[:a :b] 1 :c 2}1113 [:a :b 1 :c 2] (seq {[:a :b] 1 :c 2})1114 [:a 1 2 :b 3] (seq {:a [1 2] :b 3})1115 ;Strings1116 [] "12345"1117 [\1 \2 \3 \4 \5] (seq "12345")1118 ;fns1119 [] count1120 [count even? odd?] [count even? odd?]))1122 (deftest test-group-by1123 (is (= (group-by even? [1 2 3 4 5])1124 {false [1 3 5], true [2 4]})))1126 (deftest test-partition-by1127 (are [test-seq] (= (partition-by (comp even? count) test-seq)1128 [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]])1129 ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"]1130 '("a" "bb" "cccc" "dd" "eee" "f" "" "hh"))1131 (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm")1132 [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]])))1134 (deftest test-frequencies1135 (are [expected test-seq] (= (frequencies test-seq) expected)1136 {\p 2, \s 4, \i 4, \m 1} "mississippi"1137 {1 4 2 2 3 1} [1 1 1 1 2 2 3]1138 {1 4 2 2 3 1} '(1 1 1 1 2 2 3)))1140 (deftest test-reductions1141 (is (= (reductions + nil)1142 [0]))1143 (is (= (reductions + [1 2 3 4 5])1144 [1 3 6 10 15]))1145 (is (= (reductions + 10 [1 2 3 4 5])1146 [10 11 13 16 20 25])))1148 (deftest test-rand-nth-invariants1149 (let [elt (rand-nth [:a :b :c :d])]1150 (is (#{:a :b :c :d} elt))))1152 (deftest test-partition-all1153 (is (= (partition-all 4 [1 2 3 4 5 6 7 8 9])1154 [[1 2 3 4] [5 6 7 8] [9]]))1155 (is (= (partition-all 4 2 [1 2 3 4 5 6 7 8 9])1156 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))1158 (deftest test-shuffle-invariants1159 (is (= (count (shuffle [1 2 3 4])) 4))1160 (let [shuffled-seq (shuffle [1 2 3 4])]1161 (is (every? #{1 2 3 4} shuffled-seq))))