rlm@10
|
1 ; Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
2 ; The use and distribution terms for this software are covered by the
|
rlm@10
|
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
4 ; which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
5 ; By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
6 ; the terms of this license.
|
rlm@10
|
7 ; You must not remove this notice, or any other, from this software.
|
rlm@10
|
8
|
rlm@10
|
9 ;; Tests for the Clojure 'for' macro
|
rlm@10
|
10 ;;
|
rlm@10
|
11 ;; by Chouser
|
rlm@10
|
12 ;; Created Dec 2008
|
rlm@10
|
13
|
rlm@10
|
14 (ns clojure.test-clojure.for
|
rlm@10
|
15 (:use clojure.test))
|
rlm@10
|
16
|
rlm@10
|
17 (deftest Docstring-Example
|
rlm@10
|
18 (is (= (take 100 (for [x (range 100000000)
|
rlm@10
|
19 y (range 1000000) :while (< y x)]
|
rlm@10
|
20 [x y]))
|
rlm@10
|
21 '([1 0] [2 0] [2 1] [3 0] [3 1] [3 2] [4 0] [4 1] [4 2] [4 3]
|
rlm@10
|
22 [5 0] [5 1] [5 2] [5 3] [5 4]
|
rlm@10
|
23 [6 0] [6 1] [6 2] [6 3] [6 4] [6 5]
|
rlm@10
|
24 [7 0] [7 1] [7 2] [7 3] [7 4] [7 5] [7 6]
|
rlm@10
|
25 [8 0] [8 1] [8 2] [8 3] [8 4] [8 5] [8 6] [8 7]
|
rlm@10
|
26 [9 0] [9 1] [9 2] [9 3] [9 4] [9 5] [9 6] [9 7] [9 8]
|
rlm@10
|
27 [10 0] [10 1] [10 2] [10 3] [10 4] [10 5] [10 6] [10 7] [10 8] [10 9]
|
rlm@10
|
28 [11 0] [11 1] [11 2] [11 3] [11 4] [11 5] [11 6] [11 7] [11 8] [11 9]
|
rlm@10
|
29 [11 10]
|
rlm@10
|
30 [12 0] [12 1] [12 2] [12 3] [12 4] [12 5] [12 6] [12 7] [12 8] [12 9]
|
rlm@10
|
31 [12 10] [12 11]
|
rlm@10
|
32 [13 0] [13 1] [13 2] [13 3] [13 4] [13 5] [13 6] [13 7] [13 8] [13 9]
|
rlm@10
|
33 [13 10] [13 11] [13 12]
|
rlm@10
|
34 [14 0] [14 1] [14 2] [14 3] [14 4] [14 5] [14 6] [14 7] [14 8]))))
|
rlm@10
|
35
|
rlm@10
|
36 (defmacro deftest-both [txt & ises]
|
rlm@10
|
37 `(do
|
rlm@10
|
38 (deftest ~(symbol (str "For-" txt)) ~@ises)
|
rlm@10
|
39 (deftest ~(symbol (str "Doseq-" txt))
|
rlm@10
|
40 ~@(map (fn [[x-is [x-= [x-for binds body] value]]]
|
rlm@10
|
41 (when (and (= x-is 'is) (= x-= '=) (= x-for 'for))
|
rlm@10
|
42 `(is (= (let [acc# (atom [])]
|
rlm@10
|
43 (doseq ~binds (swap! acc# conj ~body))
|
rlm@10
|
44 @acc#)
|
rlm@10
|
45 ~value))))
|
rlm@10
|
46 ises))))
|
rlm@10
|
47
|
rlm@10
|
48 (deftest-both When
|
rlm@10
|
49 (is (= (for [x (range 10) :when (odd? x)] x) '(1 3 5 7 9)))
|
rlm@10
|
50 (is (= (for [x (range 4) y (range 4) :when (odd? y)] [x y])
|
rlm@10
|
51 '([0 1] [0 3] [1 1] [1 3] [2 1] [2 3] [3 1] [3 3])))
|
rlm@10
|
52 (is (= (for [x (range 4) y (range 4) :when (odd? x)] [x y])
|
rlm@10
|
53 '([1 0] [1 1] [1 2] [1 3] [3 0] [3 1] [3 2] [3 3])))
|
rlm@10
|
54 (is (= (for [x (range 4) :when (odd? x) y (range 4)] [x y])
|
rlm@10
|
55 '([1 0] [1 1] [1 2] [1 3] [3 0] [3 1] [3 2] [3 3])))
|
rlm@10
|
56 (is (= (for [x (range 5) y (range 5) :when (< x y)] [x y])
|
rlm@10
|
57 '([0 1] [0 2] [0 3] [0 4] [1 2] [1 3] [1 4] [2 3] [2 4] [3 4]))))
|
rlm@10
|
58
|
rlm@10
|
59 (defn only
|
rlm@10
|
60 "Returns a lazy seq of increasing ints starting at 0. Trying to get
|
rlm@10
|
61 the nth+1 value of the seq throws an exception. This is meant to
|
rlm@10
|
62 help detecting over-eagerness in lazy seq consumers."
|
rlm@10
|
63 [n]
|
rlm@10
|
64 (lazy-cat (range n)
|
rlm@10
|
65 (throw (Exception. "consumer went too far in lazy seq"))))
|
rlm@10
|
66
|
rlm@10
|
67 (deftest-both While
|
rlm@10
|
68 (is (= (for [x (only 6) :while (< x 5)] x) '(0 1 2 3 4)))
|
rlm@10
|
69 (is (= (for [x (range 4) y (only 4) :while (< y 3)] [x y])
|
rlm@10
|
70 '([0 0] [0 1] [0 2] [1 0] [1 1] [1 2]
|
rlm@10
|
71 [2 0] [2 1] [2 2] [3 0] [3 1] [3 2])))
|
rlm@10
|
72 (is (= (for [x (range 4) y (range 4) :while (< x 3)] [x y])
|
rlm@10
|
73 '([0 0] [0 1] [0 2] [0 3] [1 0] [1 1] [1 2] [1 3]
|
rlm@10
|
74 [2 0] [2 1] [2 2] [2 3])))
|
rlm@10
|
75 (is (= (for [x (only 4) :while (< x 3) y (range 4)] [x y])
|
rlm@10
|
76 '([0 0] [0 1] [0 2] [0 3] [1 0] [1 1] [1 2] [1 3]
|
rlm@10
|
77 [2 0] [2 1] [2 2] [2 3])))
|
rlm@10
|
78 (is (= (for [x (range 4) y (range 4) :while (even? x)] [x y])
|
rlm@10
|
79 '([0 0] [0 1] [0 2] [0 3] [2 0] [2 1] [2 2] [2 3])))
|
rlm@10
|
80 (is (= (for [x (only 2) :while (even? x) y (range 4)] [x y])
|
rlm@10
|
81 '([0 0] [0 1] [0 2] [0 3])))
|
rlm@10
|
82 (is (= (for [x (range 4) y (only 4) :while (< y x)] [x y])
|
rlm@10
|
83 '([1 0] [2 0] [2 1] [3 0] [3 1] [3 2]))))
|
rlm@10
|
84
|
rlm@10
|
85 (deftest-both While-and-When
|
rlm@10
|
86 (is (= (for [x (only 6) :while (< x 5) y (range 4) :when (odd? y)] [x y])
|
rlm@10
|
87 '([0 1] [0 3] [1 1] [1 3] [2 1] [2 3] [3 1] [3 3] [4 1] [4 3])))
|
rlm@10
|
88 (is (= (for [x (range 4) :when (odd? x) y (only 6) :while (< y 5)] [x y])
|
rlm@10
|
89 '([1 0] [1 1] [1 2] [1 3] [1 4] [3 0] [3 1] [3 2] [3 3] [3 4])))
|
rlm@10
|
90 (is (= (for [x (only 6) :while (< x 5) y (range 4) :when (odd? (+ x y))]
|
rlm@10
|
91 [x y])
|
rlm@10
|
92 '([0 1] [0 3] [1 0] [1 2] [2 1] [2 3] [3 0] [3 2] [4 1] [4 3])))
|
rlm@10
|
93 (is (= (for [x (range 4) :when (odd? x) y (only 2) :while (odd? (+ x y))]
|
rlm@10
|
94 [x y])
|
rlm@10
|
95 '([1 0] [3 0]))))
|
rlm@10
|
96
|
rlm@10
|
97 (deftest-both While-and-When-Same-Binding
|
rlm@10
|
98 (is (= (for [x (only 6) :while (< x 5) :when (odd? x)] x) '(1 3)))
|
rlm@10
|
99 (is (= (for [x (only 6)
|
rlm@10
|
100 :while (< x 5) ; if :while is false, :when should not be evaled
|
rlm@10
|
101 :when (do (if (< x 5) (odd? x)))] x) '(1 3)))
|
rlm@10
|
102 (is (= (for [a (range -2 5)
|
rlm@10
|
103 :when (not= a 0) ; :when may guard :while
|
rlm@10
|
104 :while (> (Math/abs (/ 1.0 a)) 1/3)] a) '(-2 -1 1 2))))
|
rlm@10
|
105
|
rlm@10
|
106 (deftest-both Nesting
|
rlm@10
|
107 (is (= (for [x '(a b) y (interpose x '(1 2)) z (list x y)] [x y z])
|
rlm@10
|
108 '([a 1 a] [a 1 1] [a a a] [a a a] [a 2 a] [a 2 2]
|
rlm@10
|
109 [b 1 b] [b 1 1] [b b b] [b b b] [b 2 b] [b 2 2])))
|
rlm@10
|
110 (is (= (for [x ['a nil] y [x 'b]] [x y])
|
rlm@10
|
111 '([a a] [a b] [nil nil] [nil b]))))
|
rlm@10
|
112
|
rlm@10
|
113 (deftest-both Destructuring
|
rlm@10
|
114 (is (= (for [{:syms [a b c]} (map #(zipmap '(a b c) (range % 5)) (range 3))
|
rlm@10
|
115 x [a b c]]
|
rlm@10
|
116 (Integer. (str a b c x)))
|
rlm@10
|
117 '(120 121 122 1231 1232 1233 2342 2343 2344))))
|
rlm@10
|
118
|
rlm@10
|
119 (deftest-both Let
|
rlm@10
|
120 (is (= (for [x (range 3) y (range 3) :let [z (+ x y)] :when (odd? z)] [x y z])
|
rlm@10
|
121 '([0 1 1] [1 0 1] [1 2 3] [2 1 3])))
|
rlm@10
|
122 (is (= (for [x (range 6) :let [y (rem x 2)] :when (even? y) z [8 9]] [x z])
|
rlm@10
|
123 '([0 8] [0 9] [2 8] [2 9] [4 8] [4 9]))))
|
rlm@10
|
124
|
rlm@10
|
125 ; :while must skip all subsequent chunks as well as the remainder of
|
rlm@10
|
126 ; the current chunk:
|
rlm@10
|
127 (deftest-both Chunked-While
|
rlm@10
|
128 (is (= (for [x (range 100) :while (even? x)] x) '(0))))
|