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 the
3 ; 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 by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 ; Author: Frantisek Sodomka
10 ; Contributors: Stuart Halloway
12 (ns clojure.test-clojure.sequences
13 (:use clojure.test))
15 ;; *** Tests ***
17 ; TODO:
18 ; apply, map, filter, remove
19 ; and more...
21 (deftest test-reduce-from-chunked-into-unchunked
22 (= [1 2 \a \b] (into [] (concat [1 2] "ab"))))
24 (deftest test-reduce
25 (let [int+ (fn [a b] (+ (int a) (int b)))
26 arange (range 100) ;; enough to cross nodes
27 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 (= 4950
44 (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 (= 4951
61 (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 (= true
78 (reduce #(and %1 %2) all-true)
79 (reduce #(and %1 %2) true all-true)))))
81 (deftest test-equality
82 ; lazy sequences
83 (are [x y] (= x y)
84 ; fixed SVN 1288 - LazySeq and EmptyList equals/equiv
85 ; 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-seq
94 (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-seq
119 (is (not (seq? (seq []))))
120 (is (seq? (seq [1 2])))
122 (are [x y] (= x y)
123 (seq nil) nil
124 (seq [nil]) '(nil)
126 (seq ()) nil
127 (seq []) nil
128 (seq #{}) nil
129 (seq {}) nil
130 (seq "") nil
131 (seq (into-array [])) nil
133 (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-cons
142 (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-empty
165 (are [x y] (and (= (empty x) y)
166 (= (class (empty x)) (class y)))
167 nil nil
169 () ()
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 ()) => nil
188 (seq '(1 2)) ()
190 (seq []) nil ; (seq []) => nil
191 (seq [1 2]) ()
193 (seq "") nil ; (seq "") => nil
194 (seq "ab") ()
196 (lazy-seq ()) ()
197 (lazy-seq '(1 2)) ()
199 (lazy-seq []) ()
200 (lazy-seq [1 2]) ()
202 ; non-coll, non-seq => nil
203 42 nil
204 1.2 nil
205 "abc" nil ))
207 ;Tests that the comparator is preservered
208 ;The first element should be the same in each set if preserved.
209 (deftest test-empty-sorted
210 (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-empty
221 ; empty coll/seq => nil
222 (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 => identity
233 (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-first
246 (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) nil
257 ; string
258 (first "") nil
259 (first "a") \a
260 (first "abc") \a
262 ; list
263 (first ()) nil
264 (first '(1)) 1
265 (first '(1 2 3)) 1
267 (first '(nil)) nil
268 (first '(1 nil)) 1
269 (first '(nil 2)) nil
270 (first '(())) ()
271 (first '(() nil)) ()
272 (first '(() 2 nil)) ()
274 ; vector
275 (first []) nil
276 (first [1]) 1
277 (first [1 2 3]) 1
279 (first [nil]) nil
280 (first [1 nil]) 1
281 (first [nil 2]) nil
282 (first [[]]) []
283 (first [[] nil]) []
284 (first [[] 2 nil]) []
286 ; set
287 (first #{}) nil
288 (first #{1}) 1
289 (first (sorted-set 1 2 3)) 1
291 (first #{nil}) nil
292 (first (sorted-set 1 nil)) nil
293 (first (sorted-set nil 2)) nil
294 (first #{#{}}) #{}
295 (first (sorted-set #{} nil)) nil
296 ;(first (sorted-set #{} 2 nil)) nil
298 ; map
299 (first {}) nil
300 (first (sorted-map :a 1)) '(:a 1)
301 (first (sorted-map :a 1 :b 2 :c 3)) '(:a 1)
303 ; array
304 (first (into-array [])) nil
305 (first (into-array [1])) 1
306 (first (into-array [1 2 3])) 1
307 (first (to-array [nil])) nil
308 (first (to-array [1 nil])) 1
309 (first (to-array [nil 2])) nil ))
312 (deftest test-next
313 (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) nil
324 ; string
325 (next "") nil
326 (next "a") nil
327 (next "abc") '(\b \c)
329 ; list
330 (next ()) nil
331 (next '(1)) nil
332 (next '(1 2 3)) '(2 3)
334 (next '(nil)) nil
335 (next '(1 nil)) '(nil)
336 (next '(1 ())) '(())
337 (next '(nil 2)) '(2)
338 (next '(())) nil
339 (next '(() nil)) '(nil)
340 (next '(() 2 nil)) '(2 nil)
342 ; vector
343 (next []) nil
344 (next [1]) nil
345 (next [1 2 3]) [2 3]
347 (next [nil]) nil
348 (next [1 nil]) [nil]
349 (next [1 []]) [[]]
350 (next [nil 2]) [2]
351 (next [[]]) nil
352 (next [[] nil]) [nil]
353 (next [[] 2 nil]) [2 nil]
355 ; set
356 (next #{}) nil
357 (next #{1}) nil
358 (next (sorted-set 1 2 3)) '(2 3)
360 (next #{nil}) nil
361 (next (sorted-set 1 nil)) '(1)
362 (next (sorted-set nil 2)) '(2)
363 (next #{#{}}) nil
364 (next (sorted-set #{} nil)) '(#{})
365 ;(next (sorted-set #{} 2 nil)) #{}
367 ; map
368 (next {}) nil
369 (next (sorted-map :a 1)) nil
370 (next (sorted-map :a 1 :b 2 :c 3)) '((:b 2) (:c 3))
372 ; array
373 (next (into-array [])) nil
374 (next (into-array [1])) nil
375 (next (into-array [1 2 3])) '(2 3)
377 (next (to-array [nil])) nil
378 (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 [])])) nil
382 (next (to-array [(into-array []) nil])) '(nil)
383 (next (to-array [(into-array []) 2 nil])) '(2 nil) ))
386 (deftest test-last
387 (are [x y] (= x y)
388 (last nil) nil
390 ; list
391 (last ()) nil
392 (last '(1)) 1
393 (last '(1 2 3)) 3
395 (last '(nil)) nil
396 (last '(1 nil)) nil
397 (last '(nil 2)) 2
398 (last '(())) ()
399 (last '(() nil)) nil
400 (last '(() 2 nil)) nil
402 ; vector
403 (last []) nil
404 (last [1]) 1
405 (last [1 2 3]) 3
407 (last [nil]) nil
408 (last [1 nil]) nil
409 (last [nil 2]) 2
410 (last [[]]) []
411 (last [[] nil]) nil
412 (last [[] 2 nil]) nil
414 ; set
415 (last #{}) nil
416 (last #{1}) 1
417 (last (sorted-set 1 2 3)) 3
419 (last #{nil}) nil
420 (last (sorted-set 1 nil)) 1
421 (last (sorted-set nil 2)) 2
422 (last #{#{}}) #{}
423 (last (sorted-set #{} nil)) #{}
424 ;(last (sorted-set #{} 2 nil)) nil
426 ; map
427 (last {}) nil
428 (last (sorted-map :a 1)) [:a 1]
429 (last (sorted-map :a 1 :b 2 :c 3)) [:c 3]
431 ; string
432 (last "") nil
433 (last "a") \a
434 (last "abc") \c
436 ; array
437 (last (into-array [])) nil
438 (last (into-array [1])) 1
439 (last (into-array [1 2 3])) 3
440 (last (to-array [nil])) nil
441 (last (to-array [1 nil])) nil
442 (last (to-array [nil 2])) 2 ))
445 ;; (ffirst coll) = (first (first coll))
446 ;;
447 (deftest test-ffirst
448 (is (thrown? IllegalArgumentException (ffirst)))
449 (are [x y] (= x y)
450 (ffirst nil) nil
452 (ffirst ()) nil
453 (ffirst '((1 2) (3 4))) 1
455 (ffirst []) nil
456 (ffirst [[1 2] [3 4]]) 1
458 (ffirst {}) nil
459 (ffirst {:a 1}) :a
461 (ffirst #{}) nil
462 (ffirst #{[1 2]}) 1 ))
465 ;; (fnext coll) = (first (next coll)) = (second coll)
466 ;;
467 (deftest test-fnext
468 (is (thrown? IllegalArgumentException (fnext)))
469 (are [x y] (= x y)
470 (fnext nil) nil
472 (fnext ()) nil
473 (fnext '(1)) nil
474 (fnext '(1 2 3 4)) 2
476 (fnext []) nil
477 (fnext [1]) nil
478 (fnext [1 2 3 4]) 2
480 (fnext {}) nil
481 (fnext (sorted-map :a 1)) nil
482 (fnext (sorted-map :a 1 :b 2)) [:b 2]
484 (fnext #{}) nil
485 (fnext #{1}) nil
486 (fnext (sorted-set 1 2 3 4)) 2 ))
489 ;; (nfirst coll) = (next (first coll))
490 ;;
491 (deftest test-nfirst
492 (is (thrown? IllegalArgumentException (nfirst)))
493 (are [x y] (= x y)
494 (nfirst nil) nil
496 (nfirst ()) nil
497 (nfirst '((1 2 3) (4 5 6))) '(2 3)
499 (nfirst []) nil
500 (nfirst [[1 2 3] [4 5 6]]) '(2 3)
502 (nfirst {}) nil
503 (nfirst {:a 1}) '(1)
505 (nfirst #{}) nil
506 (nfirst #{[1 2]}) '(2) ))
509 ;; (nnext coll) = (next (next coll))
510 ;;
511 (deftest test-nnext
512 (is (thrown? IllegalArgumentException (nnext)))
513 (are [x y] (= x y)
514 (nnext nil) nil
516 (nnext ()) nil
517 (nnext '(1)) nil
518 (nnext '(1 2)) nil
519 (nnext '(1 2 3 4)) '(3 4)
521 (nnext []) nil
522 (nnext [1]) nil
523 (nnext [1 2]) nil
524 (nnext [1 2 3 4]) '(3 4)
526 (nnext {}) nil
527 (nnext (sorted-map :a 1)) nil
528 (nnext (sorted-map :a 1 :b 2)) nil
529 (nnext (sorted-map :a 1 :b 2 :c 3 :d 4)) '([:c 3] [:d 4])
531 (nnext #{}) nil
532 (nnext #{1}) nil
533 (nnext (sorted-set 1 2)) nil
534 (nnext (sorted-set 1 2 3 4)) '(3 4) ))
537 (deftest test-nth
538 ; maps, sets are not supported
539 (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 bounds
545 (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) 1
572 (nth '(1 2 3) 0) 1
573 (nth '(1 2 3 4 5) 1) 2
574 (nth '(1 2 3 4 5) 4) 5
575 (nth '(1 2 3) 5 :not-found) :not-found
577 (nth [1] 0) 1
578 (nth [1 2 3] 0) 1
579 (nth [1 2 3 4 5] 1) 2
580 (nth [1 2 3 4 5] 4) 5
581 (nth [1 2 3] 5 :not-found) :not-found
583 (nth (into-array [1]) 0) 1
584 (nth (into-array [1 2 3]) 0) 1
585 (nth (into-array [1 2 3 4 5]) 1) 2
586 (nth (into-array [1 2 3 4 5]) 4) 5
587 (nth (into-array [1 2 3]) 5 :not-found) :not-found
589 (nth "a" 0) \a
590 (nth "abc" 0) \a
591 (nth "abcde" 1) \b
592 (nth "abcde" 4) \e
593 (nth "abc" 5 :not-found) :not-found
595 (nth (java.util.ArrayList. [1]) 0) 1
596 (nth (java.util.ArrayList. [1 2 3]) 0) 1
597 (nth (java.util.ArrayList. [1 2 3 4 5]) 1) 2
598 (nth (java.util.ArrayList. [1 2 3 4 5]) 4) 5
599 (nth (java.util.ArrayList. [1 2 3]) 5 :not-found) :not-found )
601 ; regex Matchers
602 (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-found
609 (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) ; => nil
615 (are [x y] (= x y)
616 (nth m 0 :not-found) :not-found
617 (nth m 2 :not-found) :not-found
618 (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=1278
627 ;
628 (deftest test-distinct
629 (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 nil
653 false true
654 0 42
655 0.0 3.14
656 2/3
657 0M 1M
658 \c
659 "" "abc"
660 'sym
661 :kw
662 () '(1 2)
663 [] [1 2]
664 {} {:a 1 :b 2}
665 #{} #{1 2} ))
668 (deftest test-interpose
669 (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-interleave
677 (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-zipmap
689 (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-concat
701 (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-cycle
716 (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-partition
726 (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 nil
741 (partition -1 [1 2 3]) ()
742 (partition -2 [1 2 3]) () ))
745 (deftest test-reverse
746 (are [x y] (= x y)
747 (reverse nil) () ; since SVN 1294
748 (reverse []) ()
749 (reverse [1]) '(1)
750 (reverse [1 2 3]) '(3 2 1) ))
753 (deftest test-take
754 (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-drop
766 (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-nth
778 (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-while
794 (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-while
804 (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-butlast
814 (are [x y] (= x y)
815 (butlast []) nil
816 (butlast [1]) nil
817 (butlast [1 2 3]) '(1 2) ))
820 (deftest test-drop-last
821 (are [x y] (= x y)
822 ; as butlast
823 (drop-last []) ()
824 (drop-last [1]) ()
825 (drop-last [1 2 3]) '(1 2)
827 ; as butlast, but lazy
828 (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-at
854 (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-with
868 (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-repeat
880 (is (thrown? IllegalArgumentException (repeat)))
882 ; infinite sequence => use take
883 (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 sequence
890 (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 types
900 (are [x] (= (repeat 3 x) (list x x x))
901 nil
902 false true
903 0 42
904 0.0 3.14
905 2/3
906 0M 1M
907 \c
908 "" "abc"
909 'sym
910 :kw
911 () '(1 2)
912 [] [1 2]
913 {} {:a 1 :b 2}
914 #{} #{1 2} ))
917 (deftest test-range
918 (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 nil
958 ()
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/seq
978 (are [x] (= (every? pos? x) true)
979 nil
980 () [] {} #{}
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 false
1000 ;! false (every? #{:a} [:b :b]) ; http://code.google.com/p/clojure/issues/detail?id=68
1001 ))
1004 (deftest test-not-every?
1005 ; always false for nil or empty coll/seq
1006 (are [x] (= (not-every? pos? x) false)
1007 nil
1008 () [] {} #{}
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/seq
1033 (are [x] (= (not-any? pos? x) true)
1034 nil
1035 () [] {} #{}
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-some
1060 ;; always nil for nil or empty coll/seq
1061 (are [x] (= (some pos? x) nil)
1062 nil
1063 () [] {} #{}
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-present
1087 (are [expected nested-val] (= (flatten nested-val) expected)
1088 ;simple literals
1089 [] nil
1090 [] 1
1091 [] 'test
1092 [] :keyword
1093 [] 1/2
1094 [] #"[\r\n]"
1095 [] true
1096 [] false
1097 ;vectors
1098 [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 ;sets
1102 [] #{}
1103 [] #{#{1 2} 3 4 5}
1104 [] #{1 2 3 4 5}
1105 [] #{#{1 2} 3 4 5}
1106 ;lists
1107 [] '()
1108 [1 2 3 4 5] `(1 2 3 4 5)
1109 ;maps
1110 [] {: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 ;Strings
1116 [] "12345"
1117 [\1 \2 \3 \4 \5] (seq "12345")
1118 ;fns
1119 [] count
1120 [count even? odd?] [count even? odd?]))
1122 (deftest test-group-by
1123 (is (= (group-by even? [1 2 3 4 5])
1124 {false [1 3 5], true [2 4]})))
1126 (deftest test-partition-by
1127 (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-frequencies
1135 (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-reductions
1141 (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-invariants
1149 (let [elt (rand-nth [:a :b :c :d])]
1150 (is (#{:a :b :c :d} elt))))
1152 (deftest test-partition-all
1153 (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-invariants
1159 (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))))