diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/reader.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,319 @@
     1.4 +;   Copyright (c) Rich Hickey. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +; Author: Stephen C. Gilardi
    1.13 +
    1.14 +;;
    1.15 +;;  Tests for the Clojure functions documented at the URL:
    1.16 +;;
    1.17 +;;    http://clojure.org/Reader
    1.18 +;;
    1.19 +;;  scgilardi (gmail)
    1.20 +;;  Created 22 October 2008
    1.21 +
    1.22 +(ns clojure.test-clojure.reader
    1.23 +  (:use clojure.test))
    1.24 +
    1.25 +;; Symbols
    1.26 +
    1.27 +(deftest Symbols
    1.28 +  (is (= 'abc (symbol "abc")))
    1.29 +  (is (= '*+!-_? (symbol "*+!-_?")))
    1.30 +  (is (= 'abc:def:ghi (symbol "abc:def:ghi")))
    1.31 +  (is (= 'abc/def (symbol "abc" "def")))
    1.32 +  (is (= 'abc.def/ghi (symbol "abc.def" "ghi")))
    1.33 +  (is (= 'abc/def.ghi (symbol "abc" "def.ghi")))
    1.34 +  (is (= 'abc:def/ghi:jkl.mno (symbol "abc:def" "ghi:jkl.mno")))
    1.35 +  (is (instance? clojure.lang.Symbol 'alphabet))
    1.36 +  )
    1.37 +
    1.38 +;; Literals
    1.39 +
    1.40 +(deftest Literals
    1.41 +  ; 'nil 'false 'true are reserved by Clojure and are not symbols
    1.42 +  (is (= 'nil nil))
    1.43 +  (is (= 'false false))
    1.44 +  (is (= 'true true)) )
    1.45 +
    1.46 +;; Strings
    1.47 +
    1.48 +(deftest Strings
    1.49 +  (is (= "abcde" (str \a \b \c \d \e)))
    1.50 +  (is (= "abc
    1.51 +  def" (str \a \b \c \newline \space \space \d \e \f)))
    1.52 +  )
    1.53 +
    1.54 +;; Numbers
    1.55 +
    1.56 +(deftest Numbers
    1.57 +
    1.58 +  ; Read Integer
    1.59 +  (is (instance? Integer 2147483647))
    1.60 +  (is (instance? Integer +1))
    1.61 +  (is (instance? Integer 1))
    1.62 +  (is (instance? Integer +0))
    1.63 +  (is (instance? Integer 0))
    1.64 +  (is (instance? Integer -0))
    1.65 +  (is (instance? Integer -1))
    1.66 +  (is (instance? Integer -2147483648))
    1.67 +
    1.68 +  ; Read Long
    1.69 +  (is (instance? Long 2147483648))
    1.70 +  (is (instance? Long -2147483649))
    1.71 +  (is (instance? Long 9223372036854775807))
    1.72 +  (is (instance? Long -9223372036854775808))
    1.73 +
    1.74 +  ;; Numeric constants of different types don't wash out. Regression fixed in
    1.75 +  ;; r1157. Previously the compiler saw 0 and 0.0 as the same constant and
    1.76 +  ;; caused the sequence to be built of Doubles.
    1.77 +  (let [x 0.0]
    1.78 +    (let [sequence (loop [i 0 l '()]
    1.79 +                     (if (< i 5)
    1.80 +                       (recur (inc i) (conj l i))
    1.81 +                       l))]
    1.82 +      (is (= [4 3 2 1 0] sequence))
    1.83 +      (is (every? #(instance? Integer %)
    1.84 +                  sequence))))
    1.85 +
    1.86 +  ; Read BigInteger
    1.87 +  (is (instance? BigInteger 9223372036854775808))
    1.88 +  (is (instance? BigInteger -9223372036854775809))
    1.89 +  (is (instance? BigInteger 10000000000000000000000000000000000000000000000000))
    1.90 +  (is (instance? BigInteger -10000000000000000000000000000000000000000000000000))
    1.91 +
    1.92 +  ; Read Double
    1.93 +  (is (instance? Double +1.0e+1))
    1.94 +  (is (instance? Double +1.e+1))
    1.95 +  (is (instance? Double +1e+1))
    1.96 +
    1.97 +  (is (instance? Double +1.0e1))
    1.98 +  (is (instance? Double +1.e1))
    1.99 +  (is (instance? Double +1e1))
   1.100 +
   1.101 +  (is (instance? Double +1.0e-1))
   1.102 +  (is (instance? Double +1.e-1))
   1.103 +  (is (instance? Double +1e-1))
   1.104 +
   1.105 +  (is (instance? Double 1.0e+1))
   1.106 +  (is (instance? Double 1.e+1))
   1.107 +  (is (instance? Double 1e+1))
   1.108 +
   1.109 +  (is (instance? Double 1.0e1))
   1.110 +  (is (instance? Double 1.e1))
   1.111 +  (is (instance? Double 1e1))
   1.112 +
   1.113 +  (is (instance? Double 1.0e-1))
   1.114 +  (is (instance? Double 1.e-1))
   1.115 +  (is (instance? Double 1e-1))
   1.116 +
   1.117 +  (is (instance? Double -1.0e+1))
   1.118 +  (is (instance? Double -1.e+1))
   1.119 +  (is (instance? Double -1e+1))
   1.120 +
   1.121 +  (is (instance? Double -1.0e1))
   1.122 +  (is (instance? Double -1.e1))
   1.123 +  (is (instance? Double -1e1))
   1.124 +
   1.125 +  (is (instance? Double -1.0e-1))
   1.126 +  (is (instance? Double -1.e-1))
   1.127 +  (is (instance? Double -1e-1))
   1.128 +
   1.129 +  (is (instance? Double +1.0))
   1.130 +  (is (instance? Double +1.))
   1.131 +
   1.132 +  (is (instance? Double 1.0))
   1.133 +  (is (instance? Double 1.))
   1.134 +
   1.135 +  (is (instance? Double +0.0))
   1.136 +  (is (instance? Double +0.))
   1.137 +
   1.138 +  (is (instance? Double 0.0))
   1.139 +  (is (instance? Double 0.))
   1.140 +
   1.141 +  (is (instance? Double -0.0))
   1.142 +  (is (instance? Double -0.))
   1.143 +
   1.144 +  (is (instance? Double -1.0))
   1.145 +  (is (instance? Double -1.))
   1.146 +
   1.147 +  ; Read BigDecimal
   1.148 +  (is (instance? BigDecimal 9223372036854775808M))
   1.149 +  (is (instance? BigDecimal -9223372036854775809M))
   1.150 +  (is (instance? BigDecimal 2147483647M))
   1.151 +  (is (instance? BigDecimal +1M))
   1.152 +  (is (instance? BigDecimal 1M))
   1.153 +  (is (instance? BigDecimal +0M))
   1.154 +  (is (instance? BigDecimal 0M))
   1.155 +  (is (instance? BigDecimal -0M))
   1.156 +  (is (instance? BigDecimal -1M))
   1.157 +  (is (instance? BigDecimal -2147483648M))
   1.158 +
   1.159 +  (is (instance? BigDecimal +1.0e+1M))
   1.160 +  (is (instance? BigDecimal +1.e+1M))
   1.161 +  (is (instance? BigDecimal +1e+1M))
   1.162 +
   1.163 +  (is (instance? BigDecimal +1.0e1M))
   1.164 +  (is (instance? BigDecimal +1.e1M))
   1.165 +  (is (instance? BigDecimal +1e1M))
   1.166 +
   1.167 +  (is (instance? BigDecimal +1.0e-1M))
   1.168 +  (is (instance? BigDecimal +1.e-1M))
   1.169 +  (is (instance? BigDecimal +1e-1M))
   1.170 +
   1.171 +  (is (instance? BigDecimal 1.0e+1M))
   1.172 +  (is (instance? BigDecimal 1.e+1M))
   1.173 +  (is (instance? BigDecimal 1e+1M))
   1.174 +
   1.175 +  (is (instance? BigDecimal 1.0e1M))
   1.176 +  (is (instance? BigDecimal 1.e1M))
   1.177 +  (is (instance? BigDecimal 1e1M))
   1.178 +
   1.179 +  (is (instance? BigDecimal 1.0e-1M))
   1.180 +  (is (instance? BigDecimal 1.e-1M))
   1.181 +  (is (instance? BigDecimal 1e-1M))
   1.182 +
   1.183 +  (is (instance? BigDecimal -1.0e+1M))
   1.184 +  (is (instance? BigDecimal -1.e+1M))
   1.185 +  (is (instance? BigDecimal -1e+1M))
   1.186 +
   1.187 +  (is (instance? BigDecimal -1.0e1M))
   1.188 +  (is (instance? BigDecimal -1.e1M))
   1.189 +  (is (instance? BigDecimal -1e1M))
   1.190 +
   1.191 +  (is (instance? BigDecimal -1.0e-1M))
   1.192 +  (is (instance? BigDecimal -1.e-1M))
   1.193 +  (is (instance? BigDecimal -1e-1M))
   1.194 +
   1.195 +  (is (instance? BigDecimal +1.0M))
   1.196 +  (is (instance? BigDecimal +1.M))
   1.197 +
   1.198 +  (is (instance? BigDecimal 1.0M))
   1.199 +  (is (instance? BigDecimal 1.M))
   1.200 +
   1.201 +  (is (instance? BigDecimal +0.0M))
   1.202 +  (is (instance? BigDecimal +0.M))
   1.203 +
   1.204 +  (is (instance? BigDecimal 0.0M))
   1.205 +  (is (instance? BigDecimal 0.M))
   1.206 +
   1.207 +  (is (instance? BigDecimal -0.0M))
   1.208 +  (is (instance? BigDecimal -0.M))
   1.209 +
   1.210 +  (is (instance? BigDecimal -1.0M))
   1.211 +  (is (instance? BigDecimal -1.M))
   1.212 +)
   1.213 +
   1.214 +;; Characters
   1.215 +
   1.216 +(deftest t-Characters)
   1.217 +
   1.218 +;; nil
   1.219 +
   1.220 +(deftest t-nil)
   1.221 +
   1.222 +;; Booleans
   1.223 +
   1.224 +(deftest t-Booleans)
   1.225 +
   1.226 +;; Keywords
   1.227 +
   1.228 +(deftest t-Keywords
   1.229 +  (is (= :abc (keyword "abc")))
   1.230 +  (is (= :abc (keyword 'abc)))
   1.231 +  (is (= :*+!-_? (keyword "*+!-_?")))
   1.232 +  (is (= :abc:def:ghi (keyword "abc:def:ghi")))
   1.233 +  (is (= :abc/def (keyword "abc" "def")))
   1.234 +  (is (= :abc/def (keyword 'abc/def)))
   1.235 +  (is (= :abc.def/ghi (keyword "abc.def" "ghi")))
   1.236 +  (is (= :abc/def.ghi (keyword "abc" "def.ghi")))
   1.237 +  (is (= :abc:def/ghi:jkl.mno (keyword "abc:def" "ghi:jkl.mno")))
   1.238 +  (is (instance? clojure.lang.Keyword :alphabet))
   1.239 +  )
   1.240 +
   1.241 +(deftest reading-keywords
   1.242 +  (are [x y] (= x (read-string y))
   1.243 +       :foo ":foo"
   1.244 +       :foo/bar ":foo/bar"
   1.245 +       :user/foo "::foo")
   1.246 +  (are [err msg form] (thrown-with-msg? err msg (read-string form))
   1.247 +       Exception #"Invalid token: foo:" "foo:"
   1.248 +       Exception #"Invalid token: :bar/" ":bar/"
   1.249 +       Exception #"Invalid token: ::does.not/exist" "::does.not/exist"))
   1.250 +;; Lists
   1.251 +
   1.252 +(deftest t-Lists)
   1.253 +
   1.254 +;; Vectors
   1.255 +
   1.256 +(deftest t-Vectors)
   1.257 +
   1.258 +;; Maps
   1.259 +
   1.260 +(deftest t-Maps)
   1.261 +
   1.262 +;; Sets
   1.263 +
   1.264 +(deftest t-Sets)
   1.265 +
   1.266 +;; Macro characters
   1.267 +
   1.268 +;; Quote (')
   1.269 +
   1.270 +(deftest t-Quote)
   1.271 +
   1.272 +;; Character (\)
   1.273 +
   1.274 +(deftest t-Character)
   1.275 +
   1.276 +;; Comment (;)
   1.277 +
   1.278 +(deftest t-Comment)
   1.279 +
   1.280 +;; Meta (^)
   1.281 +
   1.282 +(deftest t-Meta)
   1.283 +
   1.284 +;; Deref (@)
   1.285 +
   1.286 +(deftest t-Deref)
   1.287 +
   1.288 +;; Dispatch (#)
   1.289 +
   1.290 +;; #{} - see Sets above
   1.291 +
   1.292 +;; Regex patterns (#"pattern")
   1.293 +
   1.294 +(deftest t-Regex)
   1.295 +
   1.296 +;; Metadata (#^)
   1.297 +
   1.298 +(deftest t-Metadata)
   1.299 +
   1.300 +;; Var-quote (#')
   1.301 +
   1.302 +(deftest t-Var-quote)
   1.303 +
   1.304 +;; Anonymous function literal (#())
   1.305 +
   1.306 +(deftest t-Anonymouns-function-literal)
   1.307 +
   1.308 +;; Syntax-quote (`, note, the "backquote" character), Unquote (~) and
   1.309 +;; Unquote-splicing (~@)
   1.310 +
   1.311 +(deftest t-Syntax-quote
   1.312 +  (are [x y] (= x y)
   1.313 +      `() ()    ; was NPE before SVN r1337
   1.314 +  ))
   1.315 +
   1.316 +;; (read)
   1.317 +;; (read stream)
   1.318 +;; (read stream eof-is-error)
   1.319 +;; (read stream eof-is-error eof-value)
   1.320 +;; (read stream eof-is-error eof-value is-recursive)
   1.321 +
   1.322 +(deftest t-read)