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.iterator-test
10+ (:require [cljs.test :refer-macros [deftest testing is are run-tests]]))
11+
12+ (defn seq-iter-match
13+ [coll]
14+ (let [i (-iterator coll)]
15+ (loop [s (seq coll)
16+ n 0 ]
17+ (if (seq s)
18+ (do
19+ (when-not (.hasNext i)
20+ (throw
21+ (js/Error.
22+ (str " Iterator exhausted before seq at(" n " )" ))))
23+ (let [iv (.next i)
24+ sv (first s)]
25+ (when-not (= iv sv)
26+ (throw
27+ (js/Error.
28+ (str " Iterator value " iv " and seq value " sv " did not match at ( " n " )" )))))
29+ (recur (rest s) (inc n)))
30+ (if (.hasNext i)
31+ (throw
32+ (js/Error.
33+ (str " Seq exhausted before iterator at (" n " )" )))
34+ true )))))
35+
36+ (defrecord TestIterRec [a b])
37+
38+ (deftest coll-iter-seq-match
39+ (testing " Direct iterators match sequences"
40+ (let [test-map (apply hash-map (range 200 ))
41+ test-set (apply hash-set (range 200 ))
42+ test-queue (into cljs.core.PersistentQueue.EMPTY (vec (range 100 )))
43+ test-record (into (TestIterRec. 1 2 ) {:c 3 :d 4 })]
44+ (is (= true (seq-iter-match test-map)))
45+ (is (= true (seq-iter-match test-set)))
46+ (is (= true (seq-iter-match test-queue)))
47+ (is (= true (seq-iter-match test-record))))))
0 commit comments