aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/test_clojure/sequences.clj
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2009-02-10 05:24:00 +0000
committerscgilardi <scgilardi@gmail.com>2009-02-10 05:24:00 +0000
commit51e56f4a3dc6dfb96d14b6f9a473cd745d11a927 (patch)
tree38ed1f29c702cd6a140afcff0faf2ef92f034471 /src/clojure/contrib/test_clojure/sequences.clj
parent5a92e9b219188388456ed229d675e44af655714b (diff)
fix issue 22: additional tests for sequences: from Frantisek Sodomka
Diffstat (limited to 'src/clojure/contrib/test_clojure/sequences.clj')
-rw-r--r--src/clojure/contrib/test_clojure/sequences.clj274
1 files changed, 265 insertions, 9 deletions
diff --git a/src/clojure/contrib/test_clojure/sequences.clj b/src/clojure/contrib/test_clojure/sequences.clj
index 83cbf4dc..15dffac1 100644
--- a/src/clojure/contrib/test_clojure/sequences.clj
+++ b/src/clojure/contrib/test_clojure/sequences.clj
@@ -6,15 +6,63 @@
;; terms of this license. You must not remove this notice, or any other,
;; from this software.
;;
-;; clojure.contrib.test-clojure.sequences
-;;
-;; Created 27 December 2008
(ns clojure.contrib.test-clojure.sequences
(:use clojure.contrib.test-is))
-;; first
-;;
+;; *** Helper functions ***
+
+(defn exception []
+ (throw (new Exception "Exception which should never occur")))
+
+
+;; *** Tests ***
+
+(deftest test-seq
+ (are (= _1 _2)
+ (seq nil) nil
+ (seq [nil]) '(nil)
+
+ (seq ()) nil
+ (seq []) nil
+ (seq #{}) nil
+ (seq {}) nil
+ (seq "") nil
+ (seq (into-array [])) nil
+
+ (seq (list 1 2)) '(1 2)
+ (seq [1 2]) '(1 2)
+ (seq (sorted-set 1 2)) '(1 2)
+ (seq (sorted-map :a 1 :b 2)) '([:a 1] [:b 2])
+ (seq "abc") '(\a \b \c)
+ (seq (into-array [1 2])) '(1 2)
+ )
+)
+
+(deftest test-cons
+ (is (thrown? IllegalArgumentException (cons 1 2)))
+ (are (= _1 _2)
+ (cons 1 nil) '(1)
+ (cons nil nil) '(nil)
+
+ (cons \a nil) '(\a)
+ (cons \a "") '(\a)
+ (cons \a "bc") '(\a \b \c)
+
+ (cons 1 ()) '(1)
+ (cons 1 '(2 3)) '(1 2 3)
+
+ (cons 1 []) [1]
+ (cons 1 [2 3]) [1 2 3]
+
+ (cons 1 #{}) '(1)
+ (cons 1 (sorted-set 2 3)) '(1 2 3)
+
+ (cons 1 (into-array [])) '(1)
+ (cons 1 (into-array [2 3])) '(1 2 3)
+ )
+)
+
(deftest test-first
(is (thrown? IllegalArgumentException (first)))
(is (thrown? IllegalArgumentException (first true)))
@@ -83,8 +131,6 @@
)
)
-;; rest
-;;
(deftest test-rest
(is (thrown? IllegalArgumentException (rest)))
(is (thrown? IllegalArgumentException (rest true)))
@@ -257,8 +303,218 @@
)
-;; empty?
-;;
+(deftest test-interpose
+ (are (= _1 _2)
+ (interpose 0 []) nil
+ (interpose 0 [1]) '(1)
+ (interpose 0 [1 2]) '(1 0 2)
+ (interpose 0 [1 2 3]) '(1 0 2 0 3)
+ )
+)
+
+(deftest test-interleave
+ (are (= _1 _2)
+ (interleave [1 2] [3 4]) '(1 3 2 4)
+
+ (interleave [1] [3 4]) '(1 3)
+ (interleave [1 2] [3]) '(1 3)
+
+ (interleave [] [3 4]) nil
+ (interleave [1 2] []) nil
+ (interleave [] []) nil
+ )
+)
+
+(deftest test-concat
+ (are (= _1 _2)
+ (concat) nil
+
+ (concat []) nil
+ (concat [1 2]) '(1 2)
+
+ (concat [1 2] [3 4]) '(1 2 3 4)
+ (concat [] [3 4]) '(3 4)
+ (concat [1 2] []) '(1 2)
+ (concat [] []) nil
+
+ (concat [1 2] [3 4] [5 6]) '(1 2 3 4 5 6)
+ )
+)
+
+(deftest test-cycle
+ (are (= _1 _2)
+ (cycle []) nil
+
+ (take 3 (cycle [1])) '(1 1 1)
+ (take 5 (cycle [1 2 3])) '(1 2 3 1 2)
+
+ (take 3 (cycle [nil])) '(nil nil nil)
+ )
+)
+
+(deftest test-partition
+ (are (= _1 _2)
+ (partition 2 [1 2 3]) '((1 2))
+ (partition 2 [1 2 3 4]) '((1 2) (3 4))
+ (partition 2 []) nil
+
+ (partition 2 3 [1 2 3 4 5 6 7]) '((1 2) (4 5))
+ (partition 2 3 [1 2 3 4 5 6 7 8]) '((1 2) (4 5) (7 8))
+ (partition 2 3 []) nil
+
+ (partition 1 []) nil
+ (partition 1 [1 2 3]) '((1) (2) (3))
+
+ (partition 5 [1 2 3]) nil
+
+; (partition 0 [1 2 3]) (repeat nil) ; infinite sequence of nil
+ (partition -1 [1 2 3]) nil
+ (partition -2 [1 2 3]) nil
+ )
+)
+
+(deftest test-reverse
+ (are (= _1 _2)
+ (reverse []) nil
+ (reverse [1]) '(1)
+ (reverse [1 2 3]) '(3 2 1)
+ )
+)
+
+(deftest test-take
+ (are (= _1 _2)
+ (take 1 [1 2 3 4 5]) '(1)
+ (take 3 [1 2 3 4 5]) '(1 2 3)
+ (take 5 [1 2 3 4 5]) '(1 2 3 4 5)
+ (take 9 [1 2 3 4 5]) '(1 2 3 4 5)
+
+ (take 0 [1 2 3 4 5]) nil
+ (take -1 [1 2 3 4 5]) nil
+ (take -2 [1 2 3 4 5]) nil
+ )
+)
+
+(deftest test-drop
+ (are (= _1 _2)
+ (drop 1 [1 2 3 4 5]) '(2 3 4 5)
+ (drop 3 [1 2 3 4 5]) '(4 5)
+ (drop 5 [1 2 3 4 5]) nil
+ (drop 9 [1 2 3 4 5]) nil
+
+ (drop 0 [1 2 3 4 5]) '(1 2 3 4 5)
+ (drop -1 [1 2 3 4 5]) '(1 2 3 4 5)
+ (drop -2 [1 2 3 4 5]) '(1 2 3 4 5)
+ )
+)
+
+(deftest test-take-nth
+ (are (= _1 _2)
+ (take-nth 1 [1 2 3 4 5]) '(1 2 3 4 5)
+ (take-nth 2 [1 2 3 4 5]) '(1 3 5)
+ (take-nth 3 [1 2 3 4 5]) '(1 4)
+ (take-nth 4 [1 2 3 4 5]) '(1 5)
+ (take-nth 5 [1 2 3 4 5]) '(1)
+ (take-nth 9 [1 2 3 4 5]) '(1)
+
+ ; infinite seq of 1s = (repeat 1)
+ ;(take-nth 0 [1 2 3 4 5])
+ ;(take-nth -1 [1 2 3 4 5])
+ ;(take-nth -2 [1 2 3 4 5])
+ )
+)
+
+(deftest test-take-while
+ (are (= _1 _2)
+ (take-while pos? []) nil
+ (take-while pos? [1 2 3 4]) '(1 2 3 4)
+ (take-while pos? [1 2 3 -1]) '(1 2 3)
+ (take-while pos? [1 -1 2 3]) '(1)
+ (take-while pos? [-1 1 2 3]) nil
+ (take-while pos? [-1 -2 -3]) nil
+ )
+)
+
+(deftest test-drop-while
+ (are (= _1 _2)
+ (drop-while pos? []) nil
+ (drop-while pos? [1 2 3 4]) nil
+ (drop-while pos? [1 2 3 -1]) '(-1)
+ (drop-while pos? [1 -1 2 3]) '(-1 2 3)
+ (drop-while pos? [-1 1 2 3]) '(-1 1 2 3)
+ (drop-while pos? [-1 -2 -3]) '(-1 -2 -3)
+ )
+)
+
+(deftest test-butlast
+ (are (= _1 _2)
+ (butlast []) nil
+ (butlast [1]) nil
+ (butlast [1 2 3]) '(1 2)
+ )
+)
+
+(deftest test-drop-last
+ (are (= _1 _2)
+ ; as butlast
+ (drop-last []) nil
+ (drop-last [1]) nil
+ (drop-last [1 2 3]) '(1 2)
+
+ ; as butlast
+ (drop-last 1 []) nil
+ (drop-last 1 [1]) nil
+ (drop-last 1 [1 2 3]) '(1 2)
+
+ (drop-last 2 []) nil
+ (drop-last 2 [1]) nil
+ (drop-last 2 [1 2 3]) '(1)
+
+ (drop-last 5 []) nil
+ (drop-last 5 [1]) nil
+ (drop-last 5 [1 2 3]) nil
+
+ (drop-last 0 []) nil
+ (drop-last 0 [1]) '(1)
+ (drop-last 0 [1 2 3]) '(1 2 3)
+
+ (drop-last -1 []) nil
+ (drop-last -1 [1]) '(1)
+ (drop-last -1 [1 2 3]) '(1 2 3)
+
+ (drop-last -2 []) nil
+ (drop-last -2 [1]) '(1)
+ (drop-last -2 [1 2 3]) '(1 2 3)
+ )
+)
+
+(deftest test-split-at
+ (is (vector? (split-at 2 [])))
+ (is (vector? (split-at 2 [1 2 3])))
+
+ (are (= _1 _2)
+ (split-at 2 []) [nil nil]
+ (split-at 2 [1 2 3 4 5]) [(list 1 2) (list 3 4 5)]
+
+ (split-at 5 [1 2 3]) [(list 1 2 3) nil]
+ (split-at 0 [1 2 3]) [nil (list 1 2 3)]
+ (split-at -1 [1 2 3]) [nil (list 1 2 3)]
+ (split-at -5 [1 2 3]) [nil (list 1 2 3)]
+ )
+)
+
+(deftest test-split-with
+ (is (vector? (split-with pos? [])))
+ (is (vector? (split-with pos? [1 2 -1 0 3 4])))
+
+ (are (= _1 _2)
+ (split-with pos? []) [nil nil]
+ (split-with pos? [1 2 -1 0 3 4]) [(list 1 2) (list -1 0 3 4)]
+
+ (split-with pos? [-1 2 3 4 5]) [nil (list -1 2 3 4 5)]
+ (split-with number? [1 -2 "abc" \x]) [(list 1 -2) (list "abc" \x)]
+ )
+)
+
(deftest test-empty?
(are (empty? _)
nil