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.
8
+
9
+ (ns cljs.seqs-test
10
+ (:refer-clojure :exclude [iter])
11
+ (:require [cljs.test :refer-macros [deftest testing is]]
12
+ [clojure.string :as s]
13
+ [clojure.set :as set]))
14
+
15
+ (deftest test-sequential-equality
16
+ (testing " Testing ISequential equality"
17
+ (is (= (list 3 2 1 ) [3 2 1 ]))
18
+ (is (= [3 2 1 ] (seq (array 3 2 1 ))))))
19
+
20
+ (deftest test-seq-operations
21
+ (testing " Testing basic seq operations"
22
+ (is (= () (rest nil )))
23
+ (is (= nil (seq (array ))))
24
+ (is (= nil (seq " " )))
25
+ (is (= nil (seq [])))
26
+ (is (= nil (seq {})))
27
+ (is (= () (rest ())))
28
+ (is (= () (rest [1 ])))
29
+ (is (= () (rest (array 1 ))))))
30
+
31
+ (deftest test-empy-and-seq
32
+ (testing " Testing empty & seq"
33
+ (is (nil? (empty nil )))
34
+ (let [e-lazy-seq (empty (with-meta (lazy-seq (cons :a nil )) {:b :c }))]
35
+ (testing " lazy seq"
36
+ (is (seq? e-lazy-seq))
37
+ (is (empty? e-lazy-seq))
38
+ (is (= {:b :c } (meta e-lazy-seq)))))
39
+ (let [e-list (empty '^{:b :c } (1 2 3 ))]
40
+ (testing " list"
41
+ (is (seq? e-list))
42
+ (is (empty? e-list))
43
+ (is (= {:b :c } (meta e-list)))))
44
+ (let [e-elist (empty '^{:b :c } ())]
45
+ (testing " empty list with metadata"
46
+ (is (seq? e-elist))
47
+ (is (empty? e-elist))
48
+ (is (= :c (get (meta e-elist) :b )))))
49
+ (let [e-cons (empty (with-meta (cons :a nil ) {:b :c }))]
50
+ (testing " cons"
51
+ (is (seq? e-cons))
52
+ (is (empty? e-cons))
53
+ (is (= {:b :c } (meta e-cons)))))
54
+ (let [e-vec (empty ^{:b :c } [:a :d :g ])]
55
+ (testing " vector"
56
+ (is (vector? e-vec))
57
+ (is (empty? e-vec))
58
+ (is (= {:b :c } (meta e-vec)))))
59
+ (let [e-omap (empty ^{:b :c } {:a :d :g :h })]
60
+ (testing " map"
61
+ (is (map? e-omap))
62
+ (is (empty? e-omap))
63
+ (is (= {:b :c } (meta e-omap)))))
64
+ (let [e-hmap (empty ^{:b :c } {[1 2 ] :d :g :h })]
65
+ (testing " map with complex keys"
66
+ (is (map? e-hmap))
67
+ (is (empty? e-hmap))
68
+ (is (= {:b :c } (meta e-hmap)))))
69
+ (let [smap (with-meta (sorted-map-by (comp - compare) 2 :a 1 :b 5 :c ) {:b :c })
70
+ e-smap (empty smap)]
71
+ (testing " sorted-map-by"
72
+ (is (map? e-smap))
73
+ (is (empty? e-smap))
74
+ (is (= {:b :c } (meta e-smap)))
75
+ (is (identical? (-comparator smap) (-comparator e-smap)))
76
+ (is (= [[5 :c ] [2 :a ] [1 :b ]] (seq (assoc e-smap 2 :a 1 :b 5 :c ))))))
77
+ (let [sset (with-meta (sorted-set-by (comp - compare) 5 1 2 ) {:b :c })
78
+ e-sset (empty sset)]
79
+ (testing " sorted-set-by"
80
+ (is (set? e-sset))
81
+ (is (empty? e-sset))
82
+ (is (= {:b :c } (meta e-sset)))
83
+ (is (identical? (-comparator sset) (-comparator e-sset)))
84
+ (is (= [5 2 1 ] (seq (conj e-sset 5 1 2 ))))))
85
+ (let [e-queue (empty (with-meta (.-EMPTY PersistentQueue) {:b :c }))]
86
+ (testing " queue"
87
+ (is (identical? (type e-queue) PersistentQueue))
88
+ (is (empty? e-queue))
89
+ (is (= {:b :c } (meta e-queue)))))))
90
+
91
+ (deftest test-distinct
92
+ (testing " Testing distinct? & distinct"
93
+ (is (distinct? 1 2 3 ))
94
+ (is (not (distinct? 1 2 3 1 )))
95
+ (is (= (distinct ()) ()))
96
+ (is (= (distinct '(1 )) '(1 )))
97
+ (is (= (distinct '(1 2 3 1 1 1 )) '(1 2 3 )))
98
+ (is (= (distinct [1 1 1 2 ]) '(1 2 )))
99
+ (is (= (distinct [1 2 1 2 ]) '(1 2 )))
100
+ (is (= (distinct " a" ) [" a" ]))
101
+ (is (= (distinct " abcabab" ) [" a" " b" " c" ]))
102
+ (is (= (distinct [" abc" " abc" ]) [" abc" ]))
103
+ (is (= (distinct [nil nil ]) [nil ]))
104
+ (is (= (distinct [0.0 0.0 ]) [0.0 ]))
105
+ (is (= (distinct ['sym 'sym]) '[sym]))
106
+ (is (= (distinct [:kw :kw ]) [:kw ]))
107
+ (is (= (distinct [42 42 ]) [42 ]))
108
+ (is (= (distinct [[] []]) [[]]))
109
+ (is (= (distinct ['(1 2 ) '(1 2 )]) '[(1 2 )]))
110
+ (is (= (distinct [() ()]) [()]))
111
+ (is (= (distinct [[1 2 ] [1 2 ]]) [[1 2 ]]))
112
+ (is (= (distinct [{:a 1 :b 2 } {:a 1 :b 2 }]) [{:a 1 :b 2 }]))
113
+ (is (= (distinct [{} {}]) [{}]))
114
+ (is (= (distinct [#{1 2 } #{1 2 }]) [#{1 2 }]))
115
+ (is (= (distinct [#{} #{}]) [#{}]))))
116
+
117
+ (deftest test-rearrange-sequential
118
+ (testing " Test rearranging sequential collections"
119
+ (is (= [1 2 3 4 5 ] (sort [5 3 1 4 2 ])))
120
+ (is (= [1 2 3 4 5 ] (sort < [5 3 1 4 2 ])))
121
+ (is (= [5 4 3 2 1 ] (sort > [5 3 1 4 2 ])))
122
+ (is (= [" a" [ 1 2 ] " foo" ] (sort-by count [" foo" " a" [1 2 ]])))
123
+ (is (= [" foo" [1 2 ] " a" ] (sort-by count > [" foo" " a" [1 2 ]])))
124
+ (let [coll [1 2 3 4 5 6 7 8 9 10 ]
125
+ ; ; while it is technically possible for this test to fail with a false negative,
126
+ ; ; it's _extraordinarily_ unlikely.
127
+ shuffles (filter #(not= coll %) (take 100 (iterate shuffle coll)))]
128
+ (is (not (empty? shuffles))))
129
+ ))
130
+
131
+ (deftest test-ISequential-indexOf
132
+ (testing " Testing JS .indexOf in ISequential types"
133
+ ; ; PersistentVector
134
+ (is (= (.indexOf [] 2 ) -1 ))
135
+ (is (= (.indexOf [] 2 3 ) -1 ))
136
+ (is (= (.indexOf [1 2 3 4 5 ] 2 ) 1 ))
137
+ (is (= (.indexOf [1 2 3 4 5 ] 6 ) -1 ))
138
+ (is (= (.indexOf [1 2 3 4 5 ] -1 ) -1 ))
139
+ (is (= (.indexOf [1 2 " x" 4 5 " a" ] " a" ) 5 ))
140
+ (is (= (.indexOf [1 2 3 4 5 ] 1 2 ) -1 ))
141
+ (is (= (.indexOf [1 2 3 4 5 ] 2 2 ) -1 ))
142
+ (is (= (.indexOf [1 2 3 1 5 ] 1 2 ) 3 ))
143
+ (is (= (.indexOf [1 2 3 4 5 ] 2 ) 1 ))
144
+ (is (= (.indexOf '(1 2 3 4 5 ) 2 ) 1 ))
145
+ (is (= (.indexOf (list 1 2 3 ) 3 ) 2 ))
146
+ (is (= (.indexOf (lazy-seq [1 2 3 4 5 ]) 3 )) 2 )
147
+ (is (= (.indexOf (sequence (map inc) '(0 1 2 3 4 )) 5 ) 4 ))))
148
+
149
+ (deftest test-ISequential-lastIndexOf
150
+ (testing " Testing JS .lastIndexOf in ISequential types"
151
+ ; ; PersistentVector
152
+ (is (= (.lastIndexOf [] 2 ) -1 ))
153
+ (is (= (.lastIndexOf [] 2 3 ) -1 ))
154
+ (is (= (.lastIndexOf [1 2 3 4 5 ] 2 ) 1 ))
155
+ (is (= (.lastIndexOf [1 2 3 1 5 ] 1 ) 3 ))
156
+ (is (= (.lastIndexOf [1 2 3 1 5 ] 1 3 ) 3 ))
157
+ (is (= (.lastIndexOf [1 2 3 1 5 ] 1 2 ) 0 ))
158
+ (is (= (.lastIndexOf [1 2 3 1 ] 1 0 ) 0 ))
159
+ (is (= (.lastIndexOf [1 2 3 4 5 ] 3 100 ) 2 ))
160
+ (is (= (.lastIndexOf [1 1 1 1 1 ] 1 ) 4 ))
161
+ (is (= (.lastIndexOf [1 1 1 1 1 ] 1 6 ) 4 ))
162
+ (is (= (.lastIndexOf [1 2 1 1 1 ] 2 ) 1 ))
163
+ (is (= (.lastIndexOf [1 2 3 4 5 ] 3 -100 ) -1 ))
164
+ (is (= (.lastIndexOf [1 2 3 4 5 ] 3 -2 ) 2 ))
165
+ (is (= (.lastIndexOf '(1 2 1 4 5 ) 1 ) 2 ))
166
+ (is (= (.lastIndexOf (list 1 2 3 1 5 ) 1 ) 3 ))
167
+ (is (= (.lastIndexOf (lazy-seq [1 2 1 4 5 ]) 1 )) 2 )
168
+ (is (= (.lastIndexOf (sequence (map inc) '(0 1 0 3 4 )) 1 ) 2 ))))
169
+
170
+ (deftest test-chunked
171
+ (let [r (range 64 )
172
+ v (into [] r)]
173
+ (testing " Testing Chunked Seqs"
174
+ (is (= (hash (seq v)) (hash (seq v))))
175
+ (is (= 6 (reduce + (array-chunk (array 1 2 3 )))))
176
+ (is (instance? ChunkedSeq (seq v)))
177
+ (is (= r (seq v)))
178
+ (is (= (map inc r) (map inc v)))
179
+ (is (= (filter even? r) (filter even? v)))
180
+ (is (= (filter odd? r) (filter odd? v)))
181
+ (is (= (concat r r r) (concat v v v)))
182
+ (is (satisfies? IReduce (seq v)))
183
+ (is (== 2010 (reduce + (nnext (nnext (seq v))))))
184
+ (is (== 2020 (reduce + 10 (nnext (nnext (seq v)))))))))
185
+
186
+ (deftest test-778
187
+ (testing " Testing CLJS-778, -rest, -next RSeq"
188
+ (is (= (-rest (rseq [0 ])) ()))
189
+ (is (nil? (-next (rseq [0 ]))))
190
+ (is (= (set (rseq [0 ])) #{0 }))))
0 commit comments