view src/clojure/test_clojure/reader.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
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.
9 ; Author: Stephen C. Gilardi
11 ;;
12 ;; Tests for the Clojure functions documented at the URL:
13 ;;
14 ;; http://clojure.org/Reader
15 ;;
16 ;; scgilardi (gmail)
17 ;; Created 22 October 2008
19 (ns clojure.test-clojure.reader
20 (:use clojure.test))
22 ;; Symbols
24 (deftest Symbols
25 (is (= 'abc (symbol "abc")))
26 (is (= '*+!-_? (symbol "*+!-_?")))
27 (is (= 'abc:def:ghi (symbol "abc:def:ghi")))
28 (is (= 'abc/def (symbol "abc" "def")))
29 (is (= 'abc.def/ghi (symbol "abc.def" "ghi")))
30 (is (= 'abc/def.ghi (symbol "abc" "def.ghi")))
31 (is (= 'abc:def/ghi:jkl.mno (symbol "abc:def" "ghi:jkl.mno")))
32 (is (instance? clojure.lang.Symbol 'alphabet))
33 )
35 ;; Literals
37 (deftest Literals
38 ; 'nil 'false 'true are reserved by Clojure and are not symbols
39 (is (= 'nil nil))
40 (is (= 'false false))
41 (is (= 'true true)) )
43 ;; Strings
45 (deftest Strings
46 (is (= "abcde" (str \a \b \c \d \e)))
47 (is (= "abc
48 def" (str \a \b \c \newline \space \space \d \e \f)))
49 )
51 ;; Numbers
53 (deftest Numbers
55 ; Read Integer
56 (is (instance? Integer 2147483647))
57 (is (instance? Integer +1))
58 (is (instance? Integer 1))
59 (is (instance? Integer +0))
60 (is (instance? Integer 0))
61 (is (instance? Integer -0))
62 (is (instance? Integer -1))
63 (is (instance? Integer -2147483648))
65 ; Read Long
66 (is (instance? Long 2147483648))
67 (is (instance? Long -2147483649))
68 (is (instance? Long 9223372036854775807))
69 (is (instance? Long -9223372036854775808))
71 ;; Numeric constants of different types don't wash out. Regression fixed in
72 ;; r1157. Previously the compiler saw 0 and 0.0 as the same constant and
73 ;; caused the sequence to be built of Doubles.
74 (let [x 0.0]
75 (let [sequence (loop [i 0 l '()]
76 (if (< i 5)
77 (recur (inc i) (conj l i))
78 l))]
79 (is (= [4 3 2 1 0] sequence))
80 (is (every? #(instance? Integer %)
81 sequence))))
83 ; Read BigInteger
84 (is (instance? BigInteger 9223372036854775808))
85 (is (instance? BigInteger -9223372036854775809))
86 (is (instance? BigInteger 10000000000000000000000000000000000000000000000000))
87 (is (instance? BigInteger -10000000000000000000000000000000000000000000000000))
89 ; Read Double
90 (is (instance? Double +1.0e+1))
91 (is (instance? Double +1.e+1))
92 (is (instance? Double +1e+1))
94 (is (instance? Double +1.0e1))
95 (is (instance? Double +1.e1))
96 (is (instance? Double +1e1))
98 (is (instance? Double +1.0e-1))
99 (is (instance? Double +1.e-1))
100 (is (instance? Double +1e-1))
102 (is (instance? Double 1.0e+1))
103 (is (instance? Double 1.e+1))
104 (is (instance? Double 1e+1))
106 (is (instance? Double 1.0e1))
107 (is (instance? Double 1.e1))
108 (is (instance? Double 1e1))
110 (is (instance? Double 1.0e-1))
111 (is (instance? Double 1.e-1))
112 (is (instance? Double 1e-1))
114 (is (instance? Double -1.0e+1))
115 (is (instance? Double -1.e+1))
116 (is (instance? Double -1e+1))
118 (is (instance? Double -1.0e1))
119 (is (instance? Double -1.e1))
120 (is (instance? Double -1e1))
122 (is (instance? Double -1.0e-1))
123 (is (instance? Double -1.e-1))
124 (is (instance? Double -1e-1))
126 (is (instance? Double +1.0))
127 (is (instance? Double +1.))
129 (is (instance? Double 1.0))
130 (is (instance? Double 1.))
132 (is (instance? Double +0.0))
133 (is (instance? Double +0.))
135 (is (instance? Double 0.0))
136 (is (instance? Double 0.))
138 (is (instance? Double -0.0))
139 (is (instance? Double -0.))
141 (is (instance? Double -1.0))
142 (is (instance? Double -1.))
144 ; Read BigDecimal
145 (is (instance? BigDecimal 9223372036854775808M))
146 (is (instance? BigDecimal -9223372036854775809M))
147 (is (instance? BigDecimal 2147483647M))
148 (is (instance? BigDecimal +1M))
149 (is (instance? BigDecimal 1M))
150 (is (instance? BigDecimal +0M))
151 (is (instance? BigDecimal 0M))
152 (is (instance? BigDecimal -0M))
153 (is (instance? BigDecimal -1M))
154 (is (instance? BigDecimal -2147483648M))
156 (is (instance? BigDecimal +1.0e+1M))
157 (is (instance? BigDecimal +1.e+1M))
158 (is (instance? BigDecimal +1e+1M))
160 (is (instance? BigDecimal +1.0e1M))
161 (is (instance? BigDecimal +1.e1M))
162 (is (instance? BigDecimal +1e1M))
164 (is (instance? BigDecimal +1.0e-1M))
165 (is (instance? BigDecimal +1.e-1M))
166 (is (instance? BigDecimal +1e-1M))
168 (is (instance? BigDecimal 1.0e+1M))
169 (is (instance? BigDecimal 1.e+1M))
170 (is (instance? BigDecimal 1e+1M))
172 (is (instance? BigDecimal 1.0e1M))
173 (is (instance? BigDecimal 1.e1M))
174 (is (instance? BigDecimal 1e1M))
176 (is (instance? BigDecimal 1.0e-1M))
177 (is (instance? BigDecimal 1.e-1M))
178 (is (instance? BigDecimal 1e-1M))
180 (is (instance? BigDecimal -1.0e+1M))
181 (is (instance? BigDecimal -1.e+1M))
182 (is (instance? BigDecimal -1e+1M))
184 (is (instance? BigDecimal -1.0e1M))
185 (is (instance? BigDecimal -1.e1M))
186 (is (instance? BigDecimal -1e1M))
188 (is (instance? BigDecimal -1.0e-1M))
189 (is (instance? BigDecimal -1.e-1M))
190 (is (instance? BigDecimal -1e-1M))
192 (is (instance? BigDecimal +1.0M))
193 (is (instance? BigDecimal +1.M))
195 (is (instance? BigDecimal 1.0M))
196 (is (instance? BigDecimal 1.M))
198 (is (instance? BigDecimal +0.0M))
199 (is (instance? BigDecimal +0.M))
201 (is (instance? BigDecimal 0.0M))
202 (is (instance? BigDecimal 0.M))
204 (is (instance? BigDecimal -0.0M))
205 (is (instance? BigDecimal -0.M))
207 (is (instance? BigDecimal -1.0M))
208 (is (instance? BigDecimal -1.M))
209 )
211 ;; Characters
213 (deftest t-Characters)
215 ;; nil
217 (deftest t-nil)
219 ;; Booleans
221 (deftest t-Booleans)
223 ;; Keywords
225 (deftest t-Keywords
226 (is (= :abc (keyword "abc")))
227 (is (= :abc (keyword 'abc)))
228 (is (= :*+!-_? (keyword "*+!-_?")))
229 (is (= :abc:def:ghi (keyword "abc:def:ghi")))
230 (is (= :abc/def (keyword "abc" "def")))
231 (is (= :abc/def (keyword 'abc/def)))
232 (is (= :abc.def/ghi (keyword "abc.def" "ghi")))
233 (is (= :abc/def.ghi (keyword "abc" "def.ghi")))
234 (is (= :abc:def/ghi:jkl.mno (keyword "abc:def" "ghi:jkl.mno")))
235 (is (instance? clojure.lang.Keyword :alphabet))
236 )
238 (deftest reading-keywords
239 (are [x y] (= x (read-string y))
240 :foo ":foo"
241 :foo/bar ":foo/bar"
242 :user/foo "::foo")
243 (are [err msg form] (thrown-with-msg? err msg (read-string form))
244 Exception #"Invalid token: foo:" "foo:"
245 Exception #"Invalid token: :bar/" ":bar/"
246 Exception #"Invalid token: ::does.not/exist" "::does.not/exist"))
247 ;; Lists
249 (deftest t-Lists)
251 ;; Vectors
253 (deftest t-Vectors)
255 ;; Maps
257 (deftest t-Maps)
259 ;; Sets
261 (deftest t-Sets)
263 ;; Macro characters
265 ;; Quote (')
267 (deftest t-Quote)
269 ;; Character (\)
271 (deftest t-Character)
273 ;; Comment (;)
275 (deftest t-Comment)
277 ;; Meta (^)
279 (deftest t-Meta)
281 ;; Deref (@)
283 (deftest t-Deref)
285 ;; Dispatch (#)
287 ;; #{} - see Sets above
289 ;; Regex patterns (#"pattern")
291 (deftest t-Regex)
293 ;; Metadata (#^)
295 (deftest t-Metadata)
297 ;; Var-quote (#')
299 (deftest t-Var-quote)
301 ;; Anonymous function literal (#())
303 (deftest t-Anonymouns-function-literal)
305 ;; Syntax-quote (`, note, the "backquote" character), Unquote (~) and
306 ;; Unquote-splicing (~@)
308 (deftest t-Syntax-quote
309 (are [x y] (= x y)
310 `() () ; was NPE before SVN r1337
311 ))
313 ;; (read)
314 ;; (read stream)
315 ;; (read stream eof-is-error)
316 ;; (read stream eof-is-error eof-value)
317 ;; (read stream eof-is-error eof-value is-recursive)
319 (deftest t-read)