|
| 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 clojure.test-clojure.streams |
| 10 | + (:use clojure.test) |
| 11 | + (:import [java.util.stream Stream LongStream] |
| 12 | + [java.util.function Consumer Predicate Supplier])) |
| 13 | + |
| 14 | +(deftest test-stream-reduce! |
| 15 | + (is (= :only-val (stream-reduce! + (.stream [:only-val])))) |
| 16 | + (is (= 0 (stream-reduce! + (.stream [])))) |
| 17 | + (is (= 5 (stream-reduce! + (.stream [1 4])))) |
| 18 | + (is (= 6 (stream-reduce! + (.stream [1 2 3])))) |
| 19 | + (is (= [2 3 4] |
| 20 | + (stream-reduce! (fn [v i] (conj v (inc i))) |
| 21 | + [] |
| 22 | + (.stream [1 2 3])))) |
| 23 | + |
| 24 | + (is (= 15 (stream-reduce! + (LongStream/rangeClosed 1 5)))) |
| 25 | + |
| 26 | + (is (= 9 (stream-reduce! + (-> (Stream/of (to-array [1 2 3 4 5])) |
| 27 | + (.filter (reify Predicate (test [_ v] (odd? v)))))))) |
| 28 | + (is (= {:a 1} |
| 29 | + (meta |
| 30 | + (stream-reduce! (fn [v i] (conj v (inc i))) |
| 31 | + (with-meta [] {:a 1}) |
| 32 | + (.stream [1 2 3])))))) |
| 33 | + |
| 34 | +(deftest test-stream-reduced! |
| 35 | + (is (= 5 (stream-reduce! |
| 36 | + (fn [x y] (reduced (+ x y))) |
| 37 | + (.stream [1 4])))) |
| 38 | + |
| 39 | + (is (= 45 (stream-reduce! |
| 40 | + (fn [acc v] |
| 41 | + (if (= v 10) |
| 42 | + (reduced acc) |
| 43 | + (+ acc v))) |
| 44 | + (LongStream/rangeClosed 1 10))))) |
| 45 | + |
| 46 | +(deftest stream-seq!-test |
| 47 | + (let [none (.stream []) |
| 48 | + one (Stream/of "a") |
| 49 | + n (.stream ["a" "b" "c"]) |
| 50 | + inf (Stream/generate (reify Supplier (get [_] 42))) |
| 51 | + st (stream-seq! one) |
| 52 | + l100 (LongStream/range 0 100)] |
| 53 | + (is (empty? (map identity (stream-seq! none)))) |
| 54 | + (is (seq? st)) |
| 55 | + (is (= ["a"] (map identity st))) |
| 56 | + (is (= ["a" "b" "c"] (map identity (stream-seq! n)))) |
| 57 | + (is (= [42 42 42 42 42] (take 5 (stream-seq! inf)))) |
| 58 | + (is (= 4950 (reduce + (stream-seq! l100)))) 4950)) |
| 59 | + |
| 60 | +(deftest stream-transduce!-test |
| 61 | + (let [xf (comp (filter odd?) (take 5))] |
| 62 | + (let [st (Stream/of (to-array [1 2 3 4 5 6 7 8 9]))] |
| 63 | + (is (= [1 3 5 7 9] (stream-transduce! xf conj st)))) |
| 64 | + |
| 65 | + (let [inf (Stream/generate (reify Supplier (get [_] 0)))] |
| 66 | + (is (empty? (stream-transduce! xf conj (.limit inf 50))))) |
| 67 | + |
| 68 | + (let [inf (Stream/generate (reify Supplier (get [_] 43)))] |
| 69 | + (is (= [43 43 43 43 43] (stream-transduce! xf conj (.limit inf 50))))) |
| 70 | + |
| 71 | + (let [inf (Stream/generate (reify Supplier (get [_] 43)))] |
| 72 | + (is (= 215 (stream-transduce! xf + (.limit inf 50))))) |
| 73 | + |
| 74 | + (let [inf (Stream/generate (reify Supplier (get [_] 43)))] |
| 75 | + (is (= 315 (stream-transduce! xf + 100 (.limit inf 50))))) |
| 76 | + |
| 77 | + (let [inf (Stream/generate (reify Supplier (get [_] 43)))] |
| 78 | + (is (= "4343434343" (stream-transduce! xf str (.limit inf 50))))))) |
| 79 | + |
| 80 | +(deftest stream-into!-test |
| 81 | + (let [none (.stream []) |
| 82 | + one (Stream/of "a") |
| 83 | + n (.stream ["a" "b" "c"]) |
| 84 | + inf (Stream/generate (reify Supplier (get [_] 42))) |
| 85 | + par (-> (LongStream/rangeClosed 1 10) .boxed .parallel) |
| 86 | + xf (comp (map #(+ 2 %)) (filter odd?)) |
| 87 | + par2 (-> (LongStream/rangeClosed 1 10) .boxed .parallel)] |
| 88 | + (is (empty? (stream-into! [] none))) |
| 89 | + (is (= ["a"] (stream-into! [] one))) |
| 90 | + (is (= ["a" "b" "c"] (stream-into! [] n))) |
| 91 | + (is (= [42 42 42 42 42] |
| 92 | + (stream-into! [] (.limit inf 5)))) |
| 93 | + (is (= [1 2 3 4 5 6 7 8 9 10] |
| 94 | + (stream-into! [] par))) |
| 95 | + (is (= {:a 1} |
| 96 | + (meta |
| 97 | + (stream-into! (with-meta [] {:a 1}) |
| 98 | + (.stream [1 2 3]))))) |
| 99 | + (is (= {:a 1} |
| 100 | + (meta |
| 101 | + (stream-into! (with-meta clojure.lang.PersistentQueue/EMPTY {:a 1}) |
| 102 | + (.stream [1 2 3]))))) |
| 103 | + (is (= [-1 -2 3 5 7 9 11] (stream-into! [-1 -2] xf par2))))) |
0 commit comments