annotate src/clojure/test_clojure/sequences.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
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 ; Author: Frantisek Sodomka
rlm@10 10 ; Contributors: Stuart Halloway
rlm@10 11
rlm@10 12 (ns clojure.test-clojure.sequences
rlm@10 13 (:use clojure.test))
rlm@10 14
rlm@10 15 ;; *** Tests ***
rlm@10 16
rlm@10 17 ; TODO:
rlm@10 18 ; apply, map, filter, remove
rlm@10 19 ; and more...
rlm@10 20
rlm@10 21 (deftest test-reduce-from-chunked-into-unchunked
rlm@10 22 (= [1 2 \a \b] (into [] (concat [1 2] "ab"))))
rlm@10 23
rlm@10 24 (deftest test-reduce
rlm@10 25 (let [int+ (fn [a b] (+ (int a) (int b)))
rlm@10 26 arange (range 100) ;; enough to cross nodes
rlm@10 27 avec (into [] arange)
rlm@10 28 alist (into () arange)
rlm@10 29 obj-array (into-array arange)
rlm@10 30 int-array (into-array Integer/TYPE arange)
rlm@10 31 long-array (into-array Long/TYPE arange)
rlm@10 32 float-array (into-array Float/TYPE arange)
rlm@10 33 char-array (into-array Character/TYPE (map char arange))
rlm@10 34 double-array (into-array Double/TYPE arange)
rlm@10 35 byte-array (into-array Byte/TYPE (map byte arange))
rlm@10 36 int-vec (into (vector-of :int) arange)
rlm@10 37 long-vec (into (vector-of :long) arange)
rlm@10 38 float-vec (into (vector-of :float) arange)
rlm@10 39 char-vec (into (vector-of :char) (map char arange))
rlm@10 40 double-vec (into (vector-of :double) arange)
rlm@10 41 byte-vec (into (vector-of :byte) (map byte arange))
rlm@10 42 all-true (into-array Boolean/TYPE (repeat 10 true))]
rlm@10 43 (is (= 4950
rlm@10 44 (reduce + arange)
rlm@10 45 (reduce + avec)
rlm@10 46 (reduce + alist)
rlm@10 47 (reduce + obj-array)
rlm@10 48 (reduce + int-array)
rlm@10 49 (reduce + long-array)
rlm@10 50 (reduce + float-array)
rlm@10 51 (reduce int+ char-array)
rlm@10 52 (reduce + double-array)
rlm@10 53 (reduce int+ byte-array)
rlm@10 54 (reduce + int-vec)
rlm@10 55 (reduce + long-vec)
rlm@10 56 (reduce + float-vec)
rlm@10 57 (reduce int+ char-vec)
rlm@10 58 (reduce + double-vec)
rlm@10 59 (reduce int+ byte-vec)))
rlm@10 60 (is (= 4951
rlm@10 61 (reduce + 1 arange)
rlm@10 62 (reduce + 1 avec)
rlm@10 63 (reduce + 1 alist)
rlm@10 64 (reduce + 1 obj-array)
rlm@10 65 (reduce + 1 int-array)
rlm@10 66 (reduce + 1 long-array)
rlm@10 67 (reduce + 1 float-array)
rlm@10 68 (reduce int+ 1 char-array)
rlm@10 69 (reduce + 1 double-array)
rlm@10 70 (reduce int+ 1 byte-array)
rlm@10 71 (reduce + 1 int-vec)
rlm@10 72 (reduce + 1 long-vec)
rlm@10 73 (reduce + 1 float-vec)
rlm@10 74 (reduce int+ 1 char-vec)
rlm@10 75 (reduce + 1 double-vec)
rlm@10 76 (reduce int+ 1 byte-vec)))
rlm@10 77 (is (= true
rlm@10 78 (reduce #(and %1 %2) all-true)
rlm@10 79 (reduce #(and %1 %2) true all-true)))))
rlm@10 80
rlm@10 81 (deftest test-equality
rlm@10 82 ; lazy sequences
rlm@10 83 (are [x y] (= x y)
rlm@10 84 ; fixed SVN 1288 - LazySeq and EmptyList equals/equiv
rlm@10 85 ; http://groups.google.com/group/clojure/browse_frm/thread/286d807be9cae2a5#
rlm@10 86 (map inc nil) ()
rlm@10 87 (map inc ()) ()
rlm@10 88 (map inc []) ()
rlm@10 89 (map inc #{}) ()
rlm@10 90 (map inc {}) () ))
rlm@10 91
rlm@10 92
rlm@10 93 (deftest test-lazy-seq
rlm@10 94 (are [x] (seq? x)
rlm@10 95 (lazy-seq nil)
rlm@10 96 (lazy-seq [])
rlm@10 97 (lazy-seq [1 2]))
rlm@10 98
rlm@10 99 (are [x y] (= x y)
rlm@10 100 (lazy-seq nil) ()
rlm@10 101 (lazy-seq [nil]) '(nil)
rlm@10 102
rlm@10 103 (lazy-seq ()) ()
rlm@10 104 (lazy-seq []) ()
rlm@10 105 (lazy-seq #{}) ()
rlm@10 106 (lazy-seq {}) ()
rlm@10 107 (lazy-seq "") ()
rlm@10 108 (lazy-seq (into-array [])) ()
rlm@10 109
rlm@10 110 (lazy-seq (list 1 2)) '(1 2)
rlm@10 111 (lazy-seq [1 2]) '(1 2)
rlm@10 112 (lazy-seq (sorted-set 1 2)) '(1 2)
rlm@10 113 (lazy-seq (sorted-map :a 1 :b 2)) '([:a 1] [:b 2])
rlm@10 114 (lazy-seq "abc") '(\a \b \c)
rlm@10 115 (lazy-seq (into-array [1 2])) '(1 2) ))
rlm@10 116
rlm@10 117
rlm@10 118 (deftest test-seq
rlm@10 119 (is (not (seq? (seq []))))
rlm@10 120 (is (seq? (seq [1 2])))
rlm@10 121
rlm@10 122 (are [x y] (= x y)
rlm@10 123 (seq nil) nil
rlm@10 124 (seq [nil]) '(nil)
rlm@10 125
rlm@10 126 (seq ()) nil
rlm@10 127 (seq []) nil
rlm@10 128 (seq #{}) nil
rlm@10 129 (seq {}) nil
rlm@10 130 (seq "") nil
rlm@10 131 (seq (into-array [])) nil
rlm@10 132
rlm@10 133 (seq (list 1 2)) '(1 2)
rlm@10 134 (seq [1 2]) '(1 2)
rlm@10 135 (seq (sorted-set 1 2)) '(1 2)
rlm@10 136 (seq (sorted-map :a 1 :b 2)) '([:a 1] [:b 2])
rlm@10 137 (seq "abc") '(\a \b \c)
rlm@10 138 (seq (into-array [1 2])) '(1 2) ))
rlm@10 139
rlm@10 140
rlm@10 141 (deftest test-cons
rlm@10 142 (is (thrown? IllegalArgumentException (cons 1 2)))
rlm@10 143 (are [x y] (= x y)
rlm@10 144 (cons 1 nil) '(1)
rlm@10 145 (cons nil nil) '(nil)
rlm@10 146
rlm@10 147 (cons \a nil) '(\a)
rlm@10 148 (cons \a "") '(\a)
rlm@10 149 (cons \a "bc") '(\a \b \c)
rlm@10 150
rlm@10 151 (cons 1 ()) '(1)
rlm@10 152 (cons 1 '(2 3)) '(1 2 3)
rlm@10 153
rlm@10 154 (cons 1 []) [1]
rlm@10 155 (cons 1 [2 3]) [1 2 3]
rlm@10 156
rlm@10 157 (cons 1 #{}) '(1)
rlm@10 158 (cons 1 (sorted-set 2 3)) '(1 2 3)
rlm@10 159
rlm@10 160 (cons 1 (into-array [])) '(1)
rlm@10 161 (cons 1 (into-array [2 3])) '(1 2 3) ))
rlm@10 162
rlm@10 163
rlm@10 164 (deftest test-empty
rlm@10 165 (are [x y] (and (= (empty x) y)
rlm@10 166 (= (class (empty x)) (class y)))
rlm@10 167 nil nil
rlm@10 168
rlm@10 169 () ()
rlm@10 170 '(1 2) ()
rlm@10 171
rlm@10 172 [] []
rlm@10 173 [1 2] []
rlm@10 174
rlm@10 175 {} {}
rlm@10 176 {:a 1 :b 2} {}
rlm@10 177
rlm@10 178 (sorted-map) (sorted-map)
rlm@10 179 (sorted-map :a 1 :b 2) (sorted-map)
rlm@10 180
rlm@10 181 #{} #{}
rlm@10 182 #{1 2} #{}
rlm@10 183
rlm@10 184 (sorted-set) (sorted-set)
rlm@10 185 (sorted-set 1 2) (sorted-set)
rlm@10 186
rlm@10 187 (seq ()) nil ; (seq ()) => nil
rlm@10 188 (seq '(1 2)) ()
rlm@10 189
rlm@10 190 (seq []) nil ; (seq []) => nil
rlm@10 191 (seq [1 2]) ()
rlm@10 192
rlm@10 193 (seq "") nil ; (seq "") => nil
rlm@10 194 (seq "ab") ()
rlm@10 195
rlm@10 196 (lazy-seq ()) ()
rlm@10 197 (lazy-seq '(1 2)) ()
rlm@10 198
rlm@10 199 (lazy-seq []) ()
rlm@10 200 (lazy-seq [1 2]) ()
rlm@10 201
rlm@10 202 ; non-coll, non-seq => nil
rlm@10 203 42 nil
rlm@10 204 1.2 nil
rlm@10 205 "abc" nil ))
rlm@10 206
rlm@10 207 ;Tests that the comparator is preservered
rlm@10 208 ;The first element should be the same in each set if preserved.
rlm@10 209 (deftest test-empty-sorted
rlm@10 210 (let [inv-compare (comp - compare)]
rlm@10 211 (are [x y] (= (first (into (empty x) x))
rlm@10 212 (first y))
rlm@10 213 (sorted-set 1 2 3) (sorted-set 1 2 3)
rlm@10 214 (sorted-set-by inv-compare 1 2 3) (sorted-set-by inv-compare 1 2 3)
rlm@10 215
rlm@10 216 (sorted-map 1 :a 2 :b 3 :c) (sorted-map 1 :a 2 :b 3 :c)
rlm@10 217 (sorted-map-by inv-compare 1 :a 2 :b 3 :c) (sorted-map-by inv-compare 1 :a 2 :b 3 :c))))
rlm@10 218
rlm@10 219
rlm@10 220 (deftest test-not-empty
rlm@10 221 ; empty coll/seq => nil
rlm@10 222 (are [x] (= (not-empty x) nil)
rlm@10 223 ()
rlm@10 224 []
rlm@10 225 {}
rlm@10 226 #{}
rlm@10 227 (seq ())
rlm@10 228 (seq [])
rlm@10 229 (lazy-seq ())
rlm@10 230 (lazy-seq []) )
rlm@10 231
rlm@10 232 ; non-empty coll/seq => identity
rlm@10 233 (are [x] (and (= (not-empty x) x)
rlm@10 234 (= (class (not-empty x)) (class x)))
rlm@10 235 '(1 2)
rlm@10 236 [1 2]
rlm@10 237 {:a 1}
rlm@10 238 #{1 2}
rlm@10 239 (seq '(1 2))
rlm@10 240 (seq [1 2])
rlm@10 241 (lazy-seq '(1 2))
rlm@10 242 (lazy-seq [1 2]) ))
rlm@10 243
rlm@10 244
rlm@10 245 (deftest test-first
rlm@10 246 (is (thrown? IllegalArgumentException (first)))
rlm@10 247 (is (thrown? IllegalArgumentException (first true)))
rlm@10 248 (is (thrown? IllegalArgumentException (first false)))
rlm@10 249 (is (thrown? IllegalArgumentException (first 1)))
rlm@10 250 (is (thrown? IllegalArgumentException (first 1 2)))
rlm@10 251 (is (thrown? IllegalArgumentException (first \a)))
rlm@10 252 (is (thrown? IllegalArgumentException (first 's)))
rlm@10 253 (is (thrown? IllegalArgumentException (first :k)))
rlm@10 254 (are [x y] (= x y)
rlm@10 255 (first nil) nil
rlm@10 256
rlm@10 257 ; string
rlm@10 258 (first "") nil
rlm@10 259 (first "a") \a
rlm@10 260 (first "abc") \a
rlm@10 261
rlm@10 262 ; list
rlm@10 263 (first ()) nil
rlm@10 264 (first '(1)) 1
rlm@10 265 (first '(1 2 3)) 1
rlm@10 266
rlm@10 267 (first '(nil)) nil
rlm@10 268 (first '(1 nil)) 1
rlm@10 269 (first '(nil 2)) nil
rlm@10 270 (first '(())) ()
rlm@10 271 (first '(() nil)) ()
rlm@10 272 (first '(() 2 nil)) ()
rlm@10 273
rlm@10 274 ; vector
rlm@10 275 (first []) nil
rlm@10 276 (first [1]) 1
rlm@10 277 (first [1 2 3]) 1
rlm@10 278
rlm@10 279 (first [nil]) nil
rlm@10 280 (first [1 nil]) 1
rlm@10 281 (first [nil 2]) nil
rlm@10 282 (first [[]]) []
rlm@10 283 (first [[] nil]) []
rlm@10 284 (first [[] 2 nil]) []
rlm@10 285
rlm@10 286 ; set
rlm@10 287 (first #{}) nil
rlm@10 288 (first #{1}) 1
rlm@10 289 (first (sorted-set 1 2 3)) 1
rlm@10 290
rlm@10 291 (first #{nil}) nil
rlm@10 292 (first (sorted-set 1 nil)) nil
rlm@10 293 (first (sorted-set nil 2)) nil
rlm@10 294 (first #{#{}}) #{}
rlm@10 295 (first (sorted-set #{} nil)) nil
rlm@10 296 ;(first (sorted-set #{} 2 nil)) nil
rlm@10 297
rlm@10 298 ; map
rlm@10 299 (first {}) nil
rlm@10 300 (first (sorted-map :a 1)) '(:a 1)
rlm@10 301 (first (sorted-map :a 1 :b 2 :c 3)) '(:a 1)
rlm@10 302
rlm@10 303 ; array
rlm@10 304 (first (into-array [])) nil
rlm@10 305 (first (into-array [1])) 1
rlm@10 306 (first (into-array [1 2 3])) 1
rlm@10 307 (first (to-array [nil])) nil
rlm@10 308 (first (to-array [1 nil])) 1
rlm@10 309 (first (to-array [nil 2])) nil ))
rlm@10 310
rlm@10 311
rlm@10 312 (deftest test-next
rlm@10 313 (is (thrown? IllegalArgumentException (next)))
rlm@10 314 (is (thrown? IllegalArgumentException (next true)))
rlm@10 315 (is (thrown? IllegalArgumentException (next false)))
rlm@10 316 (is (thrown? IllegalArgumentException (next 1)))
rlm@10 317 (is (thrown? IllegalArgumentException (next 1 2)))
rlm@10 318 (is (thrown? IllegalArgumentException (next \a)))
rlm@10 319 (is (thrown? IllegalArgumentException (next 's)))
rlm@10 320 (is (thrown? IllegalArgumentException (next :k)))
rlm@10 321 (are [x y] (= x y)
rlm@10 322 (next nil) nil
rlm@10 323
rlm@10 324 ; string
rlm@10 325 (next "") nil
rlm@10 326 (next "a") nil
rlm@10 327 (next "abc") '(\b \c)
rlm@10 328
rlm@10 329 ; list
rlm@10 330 (next ()) nil
rlm@10 331 (next '(1)) nil
rlm@10 332 (next '(1 2 3)) '(2 3)
rlm@10 333
rlm@10 334 (next '(nil)) nil
rlm@10 335 (next '(1 nil)) '(nil)
rlm@10 336 (next '(1 ())) '(())
rlm@10 337 (next '(nil 2)) '(2)
rlm@10 338 (next '(())) nil
rlm@10 339 (next '(() nil)) '(nil)
rlm@10 340 (next '(() 2 nil)) '(2 nil)
rlm@10 341
rlm@10 342 ; vector
rlm@10 343 (next []) nil
rlm@10 344 (next [1]) nil
rlm@10 345 (next [1 2 3]) [2 3]
rlm@10 346
rlm@10 347 (next [nil]) nil
rlm@10 348 (next [1 nil]) [nil]
rlm@10 349 (next [1 []]) [[]]
rlm@10 350 (next [nil 2]) [2]
rlm@10 351 (next [[]]) nil
rlm@10 352 (next [[] nil]) [nil]
rlm@10 353 (next [[] 2 nil]) [2 nil]
rlm@10 354
rlm@10 355 ; set
rlm@10 356 (next #{}) nil
rlm@10 357 (next #{1}) nil
rlm@10 358 (next (sorted-set 1 2 3)) '(2 3)
rlm@10 359
rlm@10 360 (next #{nil}) nil
rlm@10 361 (next (sorted-set 1 nil)) '(1)
rlm@10 362 (next (sorted-set nil 2)) '(2)
rlm@10 363 (next #{#{}}) nil
rlm@10 364 (next (sorted-set #{} nil)) '(#{})
rlm@10 365 ;(next (sorted-set #{} 2 nil)) #{}
rlm@10 366
rlm@10 367 ; map
rlm@10 368 (next {}) nil
rlm@10 369 (next (sorted-map :a 1)) nil
rlm@10 370 (next (sorted-map :a 1 :b 2 :c 3)) '((:b 2) (:c 3))
rlm@10 371
rlm@10 372 ; array
rlm@10 373 (next (into-array [])) nil
rlm@10 374 (next (into-array [1])) nil
rlm@10 375 (next (into-array [1 2 3])) '(2 3)
rlm@10 376
rlm@10 377 (next (to-array [nil])) nil
rlm@10 378 (next (to-array [1 nil])) '(nil)
rlm@10 379 ;(next (to-array [1 (into-array [])])) (list (into-array []))
rlm@10 380 (next (to-array [nil 2])) '(2)
rlm@10 381 (next (to-array [(into-array [])])) nil
rlm@10 382 (next (to-array [(into-array []) nil])) '(nil)
rlm@10 383 (next (to-array [(into-array []) 2 nil])) '(2 nil) ))
rlm@10 384
rlm@10 385
rlm@10 386 (deftest test-last
rlm@10 387 (are [x y] (= x y)
rlm@10 388 (last nil) nil
rlm@10 389
rlm@10 390 ; list
rlm@10 391 (last ()) nil
rlm@10 392 (last '(1)) 1
rlm@10 393 (last '(1 2 3)) 3
rlm@10 394
rlm@10 395 (last '(nil)) nil
rlm@10 396 (last '(1 nil)) nil
rlm@10 397 (last '(nil 2)) 2
rlm@10 398 (last '(())) ()
rlm@10 399 (last '(() nil)) nil
rlm@10 400 (last '(() 2 nil)) nil
rlm@10 401
rlm@10 402 ; vector
rlm@10 403 (last []) nil
rlm@10 404 (last [1]) 1
rlm@10 405 (last [1 2 3]) 3
rlm@10 406
rlm@10 407 (last [nil]) nil
rlm@10 408 (last [1 nil]) nil
rlm@10 409 (last [nil 2]) 2
rlm@10 410 (last [[]]) []
rlm@10 411 (last [[] nil]) nil
rlm@10 412 (last [[] 2 nil]) nil
rlm@10 413
rlm@10 414 ; set
rlm@10 415 (last #{}) nil
rlm@10 416 (last #{1}) 1
rlm@10 417 (last (sorted-set 1 2 3)) 3
rlm@10 418
rlm@10 419 (last #{nil}) nil
rlm@10 420 (last (sorted-set 1 nil)) 1
rlm@10 421 (last (sorted-set nil 2)) 2
rlm@10 422 (last #{#{}}) #{}
rlm@10 423 (last (sorted-set #{} nil)) #{}
rlm@10 424 ;(last (sorted-set #{} 2 nil)) nil
rlm@10 425
rlm@10 426 ; map
rlm@10 427 (last {}) nil
rlm@10 428 (last (sorted-map :a 1)) [:a 1]
rlm@10 429 (last (sorted-map :a 1 :b 2 :c 3)) [:c 3]
rlm@10 430
rlm@10 431 ; string
rlm@10 432 (last "") nil
rlm@10 433 (last "a") \a
rlm@10 434 (last "abc") \c
rlm@10 435
rlm@10 436 ; array
rlm@10 437 (last (into-array [])) nil
rlm@10 438 (last (into-array [1])) 1
rlm@10 439 (last (into-array [1 2 3])) 3
rlm@10 440 (last (to-array [nil])) nil
rlm@10 441 (last (to-array [1 nil])) nil
rlm@10 442 (last (to-array [nil 2])) 2 ))
rlm@10 443
rlm@10 444
rlm@10 445 ;; (ffirst coll) = (first (first coll))
rlm@10 446 ;;
rlm@10 447 (deftest test-ffirst
rlm@10 448 (is (thrown? IllegalArgumentException (ffirst)))
rlm@10 449 (are [x y] (= x y)
rlm@10 450 (ffirst nil) nil
rlm@10 451
rlm@10 452 (ffirst ()) nil
rlm@10 453 (ffirst '((1 2) (3 4))) 1
rlm@10 454
rlm@10 455 (ffirst []) nil
rlm@10 456 (ffirst [[1 2] [3 4]]) 1
rlm@10 457
rlm@10 458 (ffirst {}) nil
rlm@10 459 (ffirst {:a 1}) :a
rlm@10 460
rlm@10 461 (ffirst #{}) nil
rlm@10 462 (ffirst #{[1 2]}) 1 ))
rlm@10 463
rlm@10 464
rlm@10 465 ;; (fnext coll) = (first (next coll)) = (second coll)
rlm@10 466 ;;
rlm@10 467 (deftest test-fnext
rlm@10 468 (is (thrown? IllegalArgumentException (fnext)))
rlm@10 469 (are [x y] (= x y)
rlm@10 470 (fnext nil) nil
rlm@10 471
rlm@10 472 (fnext ()) nil
rlm@10 473 (fnext '(1)) nil
rlm@10 474 (fnext '(1 2 3 4)) 2
rlm@10 475
rlm@10 476 (fnext []) nil
rlm@10 477 (fnext [1]) nil
rlm@10 478 (fnext [1 2 3 4]) 2
rlm@10 479
rlm@10 480 (fnext {}) nil
rlm@10 481 (fnext (sorted-map :a 1)) nil
rlm@10 482 (fnext (sorted-map :a 1 :b 2)) [:b 2]
rlm@10 483
rlm@10 484 (fnext #{}) nil
rlm@10 485 (fnext #{1}) nil
rlm@10 486 (fnext (sorted-set 1 2 3 4)) 2 ))
rlm@10 487
rlm@10 488
rlm@10 489 ;; (nfirst coll) = (next (first coll))
rlm@10 490 ;;
rlm@10 491 (deftest test-nfirst
rlm@10 492 (is (thrown? IllegalArgumentException (nfirst)))
rlm@10 493 (are [x y] (= x y)
rlm@10 494 (nfirst nil) nil
rlm@10 495
rlm@10 496 (nfirst ()) nil
rlm@10 497 (nfirst '((1 2 3) (4 5 6))) '(2 3)
rlm@10 498
rlm@10 499 (nfirst []) nil
rlm@10 500 (nfirst [[1 2 3] [4 5 6]]) '(2 3)
rlm@10 501
rlm@10 502 (nfirst {}) nil
rlm@10 503 (nfirst {:a 1}) '(1)
rlm@10 504
rlm@10 505 (nfirst #{}) nil
rlm@10 506 (nfirst #{[1 2]}) '(2) ))
rlm@10 507
rlm@10 508
rlm@10 509 ;; (nnext coll) = (next (next coll))
rlm@10 510 ;;
rlm@10 511 (deftest test-nnext
rlm@10 512 (is (thrown? IllegalArgumentException (nnext)))
rlm@10 513 (are [x y] (= x y)
rlm@10 514 (nnext nil) nil
rlm@10 515
rlm@10 516 (nnext ()) nil
rlm@10 517 (nnext '(1)) nil
rlm@10 518 (nnext '(1 2)) nil
rlm@10 519 (nnext '(1 2 3 4)) '(3 4)
rlm@10 520
rlm@10 521 (nnext []) nil
rlm@10 522 (nnext [1]) nil
rlm@10 523 (nnext [1 2]) nil
rlm@10 524 (nnext [1 2 3 4]) '(3 4)
rlm@10 525
rlm@10 526 (nnext {}) nil
rlm@10 527 (nnext (sorted-map :a 1)) nil
rlm@10 528 (nnext (sorted-map :a 1 :b 2)) nil
rlm@10 529 (nnext (sorted-map :a 1 :b 2 :c 3 :d 4)) '([:c 3] [:d 4])
rlm@10 530
rlm@10 531 (nnext #{}) nil
rlm@10 532 (nnext #{1}) nil
rlm@10 533 (nnext (sorted-set 1 2)) nil
rlm@10 534 (nnext (sorted-set 1 2 3 4)) '(3 4) ))
rlm@10 535
rlm@10 536
rlm@10 537 (deftest test-nth
rlm@10 538 ; maps, sets are not supported
rlm@10 539 (is (thrown? UnsupportedOperationException (nth {} 0)))
rlm@10 540 (is (thrown? UnsupportedOperationException (nth {:a 1 :b 2} 0)))
rlm@10 541 (is (thrown? UnsupportedOperationException (nth #{} 0)))
rlm@10 542 (is (thrown? UnsupportedOperationException (nth #{1 2 3} 0)))
rlm@10 543
rlm@10 544 ; out of bounds
rlm@10 545 (is (thrown? IndexOutOfBoundsException (nth '() 0)))
rlm@10 546 (is (thrown? IndexOutOfBoundsException (nth '(1 2 3) 5)))
rlm@10 547 (is (thrown? IndexOutOfBoundsException (nth '() -1)))
rlm@10 548 (is (thrown? IndexOutOfBoundsException (nth '(1 2 3) -1)))
rlm@10 549
rlm@10 550 (is (thrown? IndexOutOfBoundsException (nth [] 0)))
rlm@10 551 (is (thrown? IndexOutOfBoundsException (nth [1 2 3] 5)))
rlm@10 552 (is (thrown? IndexOutOfBoundsException (nth [] -1)))
rlm@10 553 (is (thrown? IndexOutOfBoundsException (nth [1 2 3] -1))) ; ???
rlm@10 554
rlm@10 555 (is (thrown? IndexOutOfBoundsException (nth (into-array []) 0)))
rlm@10 556 (is (thrown? IndexOutOfBoundsException (nth (into-array [1 2 3]) 5)))
rlm@10 557 (is (thrown? IndexOutOfBoundsException (nth (into-array []) -1)))
rlm@10 558 (is (thrown? IndexOutOfBoundsException (nth (into-array [1 2 3]) -1)))
rlm@10 559
rlm@10 560 (is (thrown? StringIndexOutOfBoundsException (nth "" 0)))
rlm@10 561 (is (thrown? StringIndexOutOfBoundsException (nth "abc" 5)))
rlm@10 562 (is (thrown? StringIndexOutOfBoundsException (nth "" -1)))
rlm@10 563 (is (thrown? StringIndexOutOfBoundsException (nth "abc" -1)))
rlm@10 564
rlm@10 565 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. []) 0)))
rlm@10 566 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) 5)))
rlm@10 567 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. []) -1))) ; ???
rlm@10 568 (is (thrown? IndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) -1))) ; ???
rlm@10 569
rlm@10 570 (are [x y] (= x y)
rlm@10 571 (nth '(1) 0) 1
rlm@10 572 (nth '(1 2 3) 0) 1
rlm@10 573 (nth '(1 2 3 4 5) 1) 2
rlm@10 574 (nth '(1 2 3 4 5) 4) 5
rlm@10 575 (nth '(1 2 3) 5 :not-found) :not-found
rlm@10 576
rlm@10 577 (nth [1] 0) 1
rlm@10 578 (nth [1 2 3] 0) 1
rlm@10 579 (nth [1 2 3 4 5] 1) 2
rlm@10 580 (nth [1 2 3 4 5] 4) 5
rlm@10 581 (nth [1 2 3] 5 :not-found) :not-found
rlm@10 582
rlm@10 583 (nth (into-array [1]) 0) 1
rlm@10 584 (nth (into-array [1 2 3]) 0) 1
rlm@10 585 (nth (into-array [1 2 3 4 5]) 1) 2
rlm@10 586 (nth (into-array [1 2 3 4 5]) 4) 5
rlm@10 587 (nth (into-array [1 2 3]) 5 :not-found) :not-found
rlm@10 588
rlm@10 589 (nth "a" 0) \a
rlm@10 590 (nth "abc" 0) \a
rlm@10 591 (nth "abcde" 1) \b
rlm@10 592 (nth "abcde" 4) \e
rlm@10 593 (nth "abc" 5 :not-found) :not-found
rlm@10 594
rlm@10 595 (nth (java.util.ArrayList. [1]) 0) 1
rlm@10 596 (nth (java.util.ArrayList. [1 2 3]) 0) 1
rlm@10 597 (nth (java.util.ArrayList. [1 2 3 4 5]) 1) 2
rlm@10 598 (nth (java.util.ArrayList. [1 2 3 4 5]) 4) 5
rlm@10 599 (nth (java.util.ArrayList. [1 2 3]) 5 :not-found) :not-found )
rlm@10 600
rlm@10 601 ; regex Matchers
rlm@10 602 (let [m (re-matcher #"(a)(b)" "ababaa")]
rlm@10 603 (re-find m) ; => ["ab" "a" "b"]
rlm@10 604 (are [x y] (= x y)
rlm@10 605 (nth m 0) "ab"
rlm@10 606 (nth m 1) "a"
rlm@10 607 (nth m 2) "b"
rlm@10 608 (nth m 3 :not-found) :not-found
rlm@10 609 (nth m -1 :not-found) :not-found )
rlm@10 610 (is (thrown? IndexOutOfBoundsException (nth m 3)))
rlm@10 611 (is (thrown? IndexOutOfBoundsException (nth m -1))))
rlm@10 612
rlm@10 613 (let [m (re-matcher #"c" "ababaa")]
rlm@10 614 (re-find m) ; => nil
rlm@10 615 (are [x y] (= x y)
rlm@10 616 (nth m 0 :not-found) :not-found
rlm@10 617 (nth m 2 :not-found) :not-found
rlm@10 618 (nth m -1 :not-found) :not-found )
rlm@10 619 (is (thrown? IllegalStateException (nth m 0)))
rlm@10 620 (is (thrown? IllegalStateException (nth m 2)))
rlm@10 621 (is (thrown? IllegalStateException (nth m -1)))))
rlm@10 622
rlm@10 623
rlm@10 624 ; distinct was broken for nil & false:
rlm@10 625 ; fixed in rev 1278:
rlm@10 626 ; http://code.google.com/p/clojure/source/detail?r=1278
rlm@10 627 ;
rlm@10 628 (deftest test-distinct
rlm@10 629 (are [x y] (= x y)
rlm@10 630 (distinct ()) ()
rlm@10 631 (distinct '(1)) '(1)
rlm@10 632 (distinct '(1 2 3)) '(1 2 3)
rlm@10 633 (distinct '(1 2 3 1 1 1)) '(1 2 3)
rlm@10 634 (distinct '(1 1 1 2)) '(1 2)
rlm@10 635 (distinct '(1 2 1 2)) '(1 2)
rlm@10 636
rlm@10 637 (distinct []) ()
rlm@10 638 (distinct [1]) '(1)
rlm@10 639 (distinct [1 2 3]) '(1 2 3)
rlm@10 640 (distinct [1 2 3 1 2 2 1 1]) '(1 2 3)
rlm@10 641 (distinct [1 1 1 2]) '(1 2)
rlm@10 642 (distinct [1 2 1 2]) '(1 2)
rlm@10 643
rlm@10 644 (distinct "") ()
rlm@10 645 (distinct "a") '(\a)
rlm@10 646 (distinct "abc") '(\a \b \c)
rlm@10 647 (distinct "abcabab") '(\a \b \c)
rlm@10 648 (distinct "aaab") '(\a \b)
rlm@10 649 (distinct "abab") '(\a \b) )
rlm@10 650
rlm@10 651 (are [x] (= (distinct [x x]) [x])
rlm@10 652 nil
rlm@10 653 false true
rlm@10 654 0 42
rlm@10 655 0.0 3.14
rlm@10 656 2/3
rlm@10 657 0M 1M
rlm@10 658 \c
rlm@10 659 "" "abc"
rlm@10 660 'sym
rlm@10 661 :kw
rlm@10 662 () '(1 2)
rlm@10 663 [] [1 2]
rlm@10 664 {} {:a 1 :b 2}
rlm@10 665 #{} #{1 2} ))
rlm@10 666
rlm@10 667
rlm@10 668 (deftest test-interpose
rlm@10 669 (are [x y] (= x y)
rlm@10 670 (interpose 0 []) ()
rlm@10 671 (interpose 0 [1]) '(1)
rlm@10 672 (interpose 0 [1 2]) '(1 0 2)
rlm@10 673 (interpose 0 [1 2 3]) '(1 0 2 0 3) ))
rlm@10 674
rlm@10 675
rlm@10 676 (deftest test-interleave
rlm@10 677 (are [x y] (= x y)
rlm@10 678 (interleave [1 2] [3 4]) '(1 3 2 4)
rlm@10 679
rlm@10 680 (interleave [1] [3 4]) '(1 3)
rlm@10 681 (interleave [1 2] [3]) '(1 3)
rlm@10 682
rlm@10 683 (interleave [] [3 4]) ()
rlm@10 684 (interleave [1 2] []) ()
rlm@10 685 (interleave [] []) () ))
rlm@10 686
rlm@10 687
rlm@10 688 (deftest test-zipmap
rlm@10 689 (are [x y] (= x y)
rlm@10 690 (zipmap [:a :b] [1 2]) {:a 1 :b 2}
rlm@10 691
rlm@10 692 (zipmap [:a] [1 2]) {:a 1}
rlm@10 693 (zipmap [:a :b] [1]) {:a 1}
rlm@10 694
rlm@10 695 (zipmap [] [1 2]) {}
rlm@10 696 (zipmap [:a :b] []) {}
rlm@10 697 (zipmap [] []) {} ))
rlm@10 698
rlm@10 699
rlm@10 700 (deftest test-concat
rlm@10 701 (are [x y] (= x y)
rlm@10 702 (concat) ()
rlm@10 703
rlm@10 704 (concat []) ()
rlm@10 705 (concat [1 2]) '(1 2)
rlm@10 706
rlm@10 707 (concat [1 2] [3 4]) '(1 2 3 4)
rlm@10 708 (concat [] [3 4]) '(3 4)
rlm@10 709 (concat [1 2] []) '(1 2)
rlm@10 710 (concat [] []) ()
rlm@10 711
rlm@10 712 (concat [1 2] [3 4] [5 6]) '(1 2 3 4 5 6) ))
rlm@10 713
rlm@10 714
rlm@10 715 (deftest test-cycle
rlm@10 716 (are [x y] (= x y)
rlm@10 717 (cycle []) ()
rlm@10 718
rlm@10 719 (take 3 (cycle [1])) '(1 1 1)
rlm@10 720 (take 5 (cycle [1 2 3])) '(1 2 3 1 2)
rlm@10 721
rlm@10 722 (take 3 (cycle [nil])) '(nil nil nil) ))
rlm@10 723
rlm@10 724
rlm@10 725 (deftest test-partition
rlm@10 726 (are [x y] (= x y)
rlm@10 727 (partition 2 [1 2 3]) '((1 2))
rlm@10 728 (partition 2 [1 2 3 4]) '((1 2) (3 4))
rlm@10 729 (partition 2 []) ()
rlm@10 730
rlm@10 731 (partition 2 3 [1 2 3 4 5 6 7]) '((1 2) (4 5))
rlm@10 732 (partition 2 3 [1 2 3 4 5 6 7 8]) '((1 2) (4 5) (7 8))
rlm@10 733 (partition 2 3 []) ()
rlm@10 734
rlm@10 735 (partition 1 []) ()
rlm@10 736 (partition 1 [1 2 3]) '((1) (2) (3))
rlm@10 737
rlm@10 738 (partition 5 [1 2 3]) ()
rlm@10 739
rlm@10 740 ; (partition 0 [1 2 3]) (repeat nil) ; infinite sequence of nil
rlm@10 741 (partition -1 [1 2 3]) ()
rlm@10 742 (partition -2 [1 2 3]) () ))
rlm@10 743
rlm@10 744
rlm@10 745 (deftest test-reverse
rlm@10 746 (are [x y] (= x y)
rlm@10 747 (reverse nil) () ; since SVN 1294
rlm@10 748 (reverse []) ()
rlm@10 749 (reverse [1]) '(1)
rlm@10 750 (reverse [1 2 3]) '(3 2 1) ))
rlm@10 751
rlm@10 752
rlm@10 753 (deftest test-take
rlm@10 754 (are [x y] (= x y)
rlm@10 755 (take 1 [1 2 3 4 5]) '(1)
rlm@10 756 (take 3 [1 2 3 4 5]) '(1 2 3)
rlm@10 757 (take 5 [1 2 3 4 5]) '(1 2 3 4 5)
rlm@10 758 (take 9 [1 2 3 4 5]) '(1 2 3 4 5)
rlm@10 759
rlm@10 760 (take 0 [1 2 3 4 5]) ()
rlm@10 761 (take -1 [1 2 3 4 5]) ()
rlm@10 762 (take -2 [1 2 3 4 5]) () ))
rlm@10 763
rlm@10 764
rlm@10 765 (deftest test-drop
rlm@10 766 (are [x y] (= x y)
rlm@10 767 (drop 1 [1 2 3 4 5]) '(2 3 4 5)
rlm@10 768 (drop 3 [1 2 3 4 5]) '(4 5)
rlm@10 769 (drop 5 [1 2 3 4 5]) ()
rlm@10 770 (drop 9 [1 2 3 4 5]) ()
rlm@10 771
rlm@10 772 (drop 0 [1 2 3 4 5]) '(1 2 3 4 5)
rlm@10 773 (drop -1 [1 2 3 4 5]) '(1 2 3 4 5)
rlm@10 774 (drop -2 [1 2 3 4 5]) '(1 2 3 4 5) ))
rlm@10 775
rlm@10 776
rlm@10 777 (deftest test-take-nth
rlm@10 778 (are [x y] (= x y)
rlm@10 779 (take-nth 1 [1 2 3 4 5]) '(1 2 3 4 5)
rlm@10 780 (take-nth 2 [1 2 3 4 5]) '(1 3 5)
rlm@10 781 (take-nth 3 [1 2 3 4 5]) '(1 4)
rlm@10 782 (take-nth 4 [1 2 3 4 5]) '(1 5)
rlm@10 783 (take-nth 5 [1 2 3 4 5]) '(1)
rlm@10 784 (take-nth 9 [1 2 3 4 5]) '(1)
rlm@10 785
rlm@10 786 ; infinite seq of 1s = (repeat 1)
rlm@10 787 ;(take-nth 0 [1 2 3 4 5])
rlm@10 788 ;(take-nth -1 [1 2 3 4 5])
rlm@10 789 ;(take-nth -2 [1 2 3 4 5])
rlm@10 790 ))
rlm@10 791
rlm@10 792
rlm@10 793 (deftest test-take-while
rlm@10 794 (are [x y] (= x y)
rlm@10 795 (take-while pos? []) ()
rlm@10 796 (take-while pos? [1 2 3 4]) '(1 2 3 4)
rlm@10 797 (take-while pos? [1 2 3 -1]) '(1 2 3)
rlm@10 798 (take-while pos? [1 -1 2 3]) '(1)
rlm@10 799 (take-while pos? [-1 1 2 3]) ()
rlm@10 800 (take-while pos? [-1 -2 -3]) () ))
rlm@10 801
rlm@10 802
rlm@10 803 (deftest test-drop-while
rlm@10 804 (are [x y] (= x y)
rlm@10 805 (drop-while pos? []) ()
rlm@10 806 (drop-while pos? [1 2 3 4]) ()
rlm@10 807 (drop-while pos? [1 2 3 -1]) '(-1)
rlm@10 808 (drop-while pos? [1 -1 2 3]) '(-1 2 3)
rlm@10 809 (drop-while pos? [-1 1 2 3]) '(-1 1 2 3)
rlm@10 810 (drop-while pos? [-1 -2 -3]) '(-1 -2 -3) ))
rlm@10 811
rlm@10 812
rlm@10 813 (deftest test-butlast
rlm@10 814 (are [x y] (= x y)
rlm@10 815 (butlast []) nil
rlm@10 816 (butlast [1]) nil
rlm@10 817 (butlast [1 2 3]) '(1 2) ))
rlm@10 818
rlm@10 819
rlm@10 820 (deftest test-drop-last
rlm@10 821 (are [x y] (= x y)
rlm@10 822 ; as butlast
rlm@10 823 (drop-last []) ()
rlm@10 824 (drop-last [1]) ()
rlm@10 825 (drop-last [1 2 3]) '(1 2)
rlm@10 826
rlm@10 827 ; as butlast, but lazy
rlm@10 828 (drop-last 1 []) ()
rlm@10 829 (drop-last 1 [1]) ()
rlm@10 830 (drop-last 1 [1 2 3]) '(1 2)
rlm@10 831
rlm@10 832 (drop-last 2 []) ()
rlm@10 833 (drop-last 2 [1]) ()
rlm@10 834 (drop-last 2 [1 2 3]) '(1)
rlm@10 835
rlm@10 836 (drop-last 5 []) ()
rlm@10 837 (drop-last 5 [1]) ()
rlm@10 838 (drop-last 5 [1 2 3]) ()
rlm@10 839
rlm@10 840 (drop-last 0 []) ()
rlm@10 841 (drop-last 0 [1]) '(1)
rlm@10 842 (drop-last 0 [1 2 3]) '(1 2 3)
rlm@10 843
rlm@10 844 (drop-last -1 []) ()
rlm@10 845 (drop-last -1 [1]) '(1)
rlm@10 846 (drop-last -1 [1 2 3]) '(1 2 3)
rlm@10 847
rlm@10 848 (drop-last -2 []) ()
rlm@10 849 (drop-last -2 [1]) '(1)
rlm@10 850 (drop-last -2 [1 2 3]) '(1 2 3) ))
rlm@10 851
rlm@10 852
rlm@10 853 (deftest test-split-at
rlm@10 854 (is (vector? (split-at 2 [])))
rlm@10 855 (is (vector? (split-at 2 [1 2 3])))
rlm@10 856
rlm@10 857 (are [x y] (= x y)
rlm@10 858 (split-at 2 []) [() ()]
rlm@10 859 (split-at 2 [1 2 3 4 5]) [(list 1 2) (list 3 4 5)]
rlm@10 860
rlm@10 861 (split-at 5 [1 2 3]) [(list 1 2 3) ()]
rlm@10 862 (split-at 0 [1 2 3]) [() (list 1 2 3)]
rlm@10 863 (split-at -1 [1 2 3]) [() (list 1 2 3)]
rlm@10 864 (split-at -5 [1 2 3]) [() (list 1 2 3)] ))
rlm@10 865
rlm@10 866
rlm@10 867 (deftest test-split-with
rlm@10 868 (is (vector? (split-with pos? [])))
rlm@10 869 (is (vector? (split-with pos? [1 2 -1 0 3 4])))
rlm@10 870
rlm@10 871 (are [x y] (= x y)
rlm@10 872 (split-with pos? []) [() ()]
rlm@10 873 (split-with pos? [1 2 -1 0 3 4]) [(list 1 2) (list -1 0 3 4)]
rlm@10 874
rlm@10 875 (split-with pos? [-1 2 3 4 5]) [() (list -1 2 3 4 5)]
rlm@10 876 (split-with number? [1 -2 "abc" \x]) [(list 1 -2) (list "abc" \x)] ))
rlm@10 877
rlm@10 878
rlm@10 879 (deftest test-repeat
rlm@10 880 (is (thrown? IllegalArgumentException (repeat)))
rlm@10 881
rlm@10 882 ; infinite sequence => use take
rlm@10 883 (are [x y] (= x y)
rlm@10 884 (take 0 (repeat 7)) ()
rlm@10 885 (take 1 (repeat 7)) '(7)
rlm@10 886 (take 2 (repeat 7)) '(7 7)
rlm@10 887 (take 5 (repeat 7)) '(7 7 7 7 7) )
rlm@10 888
rlm@10 889 ; limited sequence
rlm@10 890 (are [x y] (= x y)
rlm@10 891 (repeat 0 7) ()
rlm@10 892 (repeat 1 7) '(7)
rlm@10 893 (repeat 2 7) '(7 7)
rlm@10 894 (repeat 5 7) '(7 7 7 7 7)
rlm@10 895
rlm@10 896 (repeat -1 7) ()
rlm@10 897 (repeat -3 7) () )
rlm@10 898
rlm@10 899 ; test different data types
rlm@10 900 (are [x] (= (repeat 3 x) (list x x x))
rlm@10 901 nil
rlm@10 902 false true
rlm@10 903 0 42
rlm@10 904 0.0 3.14
rlm@10 905 2/3
rlm@10 906 0M 1M
rlm@10 907 \c
rlm@10 908 "" "abc"
rlm@10 909 'sym
rlm@10 910 :kw
rlm@10 911 () '(1 2)
rlm@10 912 [] [1 2]
rlm@10 913 {} {:a 1 :b 2}
rlm@10 914 #{} #{1 2} ))
rlm@10 915
rlm@10 916
rlm@10 917 (deftest test-range
rlm@10 918 (are [x y] (= x y)
rlm@10 919 (range 0) () ; exclusive end!
rlm@10 920 (range 1) '(0)
rlm@10 921 (range 5) '(0 1 2 3 4)
rlm@10 922
rlm@10 923 (range -1) ()
rlm@10 924 (range -3) ()
rlm@10 925
rlm@10 926 (range 2.5) '(0 1 2)
rlm@10 927 (range 7/3) '(0 1 2)
rlm@10 928
rlm@10 929 (range 0 3) '(0 1 2)
rlm@10 930 (range 0 1) '(0)
rlm@10 931 (range 0 0) ()
rlm@10 932 (range 0 -3) ()
rlm@10 933
rlm@10 934 (range 3 6) '(3 4 5)
rlm@10 935 (range 3 4) '(3)
rlm@10 936 (range 3 3) ()
rlm@10 937 (range 3 1) ()
rlm@10 938 (range 3 0) ()
rlm@10 939 (range 3 -2) ()
rlm@10 940
rlm@10 941 (range -2 5) '(-2 -1 0 1 2 3 4)
rlm@10 942 (range -2 0) '(-2 -1)
rlm@10 943 (range -2 -1) '(-2)
rlm@10 944 (range -2 -2) ()
rlm@10 945 (range -2 -5) ()
rlm@10 946
rlm@10 947 (range 3 9 0) ()
rlm@10 948 (range 3 9 1) '(3 4 5 6 7 8)
rlm@10 949 (range 3 9 2) '(3 5 7)
rlm@10 950 (range 3 9 3) '(3 6)
rlm@10 951 (range 3 9 10) '(3)
rlm@10 952 (range 3 9 -1) () ))
rlm@10 953
rlm@10 954
rlm@10 955 (deftest test-empty?
rlm@10 956 (are [x] (empty? x)
rlm@10 957 nil
rlm@10 958 ()
rlm@10 959 (lazy-seq nil) ; => ()
rlm@10 960 []
rlm@10 961 {}
rlm@10 962 #{}
rlm@10 963 ""
rlm@10 964 (into-array []) )
rlm@10 965
rlm@10 966 (are [x] (not (empty? x))
rlm@10 967 '(1 2)
rlm@10 968 (lazy-seq [1 2])
rlm@10 969 [1 2]
rlm@10 970 {:a 1 :b 2}
rlm@10 971 #{1 2}
rlm@10 972 "abc"
rlm@10 973 (into-array [1 2]) ))
rlm@10 974
rlm@10 975
rlm@10 976 (deftest test-every?
rlm@10 977 ; always true for nil or empty coll/seq
rlm@10 978 (are [x] (= (every? pos? x) true)
rlm@10 979 nil
rlm@10 980 () [] {} #{}
rlm@10 981 (lazy-seq [])
rlm@10 982 (into-array []) )
rlm@10 983
rlm@10 984 (are [x y] (= x y)
rlm@10 985 true (every? pos? [1])
rlm@10 986 true (every? pos? [1 2])
rlm@10 987 true (every? pos? [1 2 3 4 5])
rlm@10 988
rlm@10 989 false (every? pos? [-1])
rlm@10 990 false (every? pos? [-1 -2])
rlm@10 991 false (every? pos? [-1 -2 3])
rlm@10 992 false (every? pos? [-1 2])
rlm@10 993 false (every? pos? [1 -2])
rlm@10 994 false (every? pos? [1 2 -3])
rlm@10 995 false (every? pos? [1 2 -3 4]) )
rlm@10 996
rlm@10 997 (are [x y] (= x y)
rlm@10 998 true (every? #{:a} [:a :a])
rlm@10 999 ;! false (every? #{:a} [:a :b]) ; Issue 68: every? returns nil instead of false
rlm@10 1000 ;! false (every? #{:a} [:b :b]) ; http://code.google.com/p/clojure/issues/detail?id=68
rlm@10 1001 ))
rlm@10 1002
rlm@10 1003
rlm@10 1004 (deftest test-not-every?
rlm@10 1005 ; always false for nil or empty coll/seq
rlm@10 1006 (are [x] (= (not-every? pos? x) false)
rlm@10 1007 nil
rlm@10 1008 () [] {} #{}
rlm@10 1009 (lazy-seq [])
rlm@10 1010 (into-array []) )
rlm@10 1011
rlm@10 1012 (are [x y] (= x y)
rlm@10 1013 false (not-every? pos? [1])
rlm@10 1014 false (not-every? pos? [1 2])
rlm@10 1015 false (not-every? pos? [1 2 3 4 5])
rlm@10 1016
rlm@10 1017 true (not-every? pos? [-1])
rlm@10 1018 true (not-every? pos? [-1 -2])
rlm@10 1019 true (not-every? pos? [-1 -2 3])
rlm@10 1020 true (not-every? pos? [-1 2])
rlm@10 1021 true (not-every? pos? [1 -2])
rlm@10 1022 true (not-every? pos? [1 2 -3])
rlm@10 1023 true (not-every? pos? [1 2 -3 4]) )
rlm@10 1024
rlm@10 1025 (are [x y] (= x y)
rlm@10 1026 false (not-every? #{:a} [:a :a])
rlm@10 1027 true (not-every? #{:a} [:a :b])
rlm@10 1028 true (not-every? #{:a} [:b :b]) ))
rlm@10 1029
rlm@10 1030
rlm@10 1031 (deftest test-not-any?
rlm@10 1032 ; always true for nil or empty coll/seq
rlm@10 1033 (are [x] (= (not-any? pos? x) true)
rlm@10 1034 nil
rlm@10 1035 () [] {} #{}
rlm@10 1036 (lazy-seq [])
rlm@10 1037 (into-array []) )
rlm@10 1038
rlm@10 1039 (are [x y] (= x y)
rlm@10 1040 false (not-any? pos? [1])
rlm@10 1041 false (not-any? pos? [1 2])
rlm@10 1042 false (not-any? pos? [1 2 3 4 5])
rlm@10 1043
rlm@10 1044 true (not-any? pos? [-1])
rlm@10 1045 true (not-any? pos? [-1 -2])
rlm@10 1046
rlm@10 1047 false (not-any? pos? [-1 -2 3])
rlm@10 1048 false (not-any? pos? [-1 2])
rlm@10 1049 false (not-any? pos? [1 -2])
rlm@10 1050 false (not-any? pos? [1 2 -3])
rlm@10 1051 false (not-any? pos? [1 2 -3 4]) )
rlm@10 1052
rlm@10 1053 (are [x y] (= x y)
rlm@10 1054 false (not-any? #{:a} [:a :a])
rlm@10 1055 false (not-any? #{:a} [:a :b])
rlm@10 1056 true (not-any? #{:a} [:b :b]) ))
rlm@10 1057
rlm@10 1058
rlm@10 1059 (deftest test-some
rlm@10 1060 ;; always nil for nil or empty coll/seq
rlm@10 1061 (are [x] (= (some pos? x) nil)
rlm@10 1062 nil
rlm@10 1063 () [] {} #{}
rlm@10 1064 (lazy-seq [])
rlm@10 1065 (into-array []))
rlm@10 1066
rlm@10 1067 (are [x y] (= x y)
rlm@10 1068 nil (some nil nil)
rlm@10 1069
rlm@10 1070 true (some pos? [1])
rlm@10 1071 true (some pos? [1 2])
rlm@10 1072
rlm@10 1073 nil (some pos? [-1])
rlm@10 1074 nil (some pos? [-1 -2])
rlm@10 1075 true (some pos? [-1 2])
rlm@10 1076 true (some pos? [1 -2])
rlm@10 1077
rlm@10 1078 :a (some #{:a} [:a :a])
rlm@10 1079 :a (some #{:a} [:b :a])
rlm@10 1080 nil (some #{:a} [:b :b])
rlm@10 1081
rlm@10 1082 :a (some #{:a} '(:a :b))
rlm@10 1083 :a (some #{:a} #{:a :b})
rlm@10 1084 ))
rlm@10 1085
rlm@10 1086 (deftest test-flatten-present
rlm@10 1087 (are [expected nested-val] (= (flatten nested-val) expected)
rlm@10 1088 ;simple literals
rlm@10 1089 [] nil
rlm@10 1090 [] 1
rlm@10 1091 [] 'test
rlm@10 1092 [] :keyword
rlm@10 1093 [] 1/2
rlm@10 1094 [] #"[\r\n]"
rlm@10 1095 [] true
rlm@10 1096 [] false
rlm@10 1097 ;vectors
rlm@10 1098 [1 2 3 4 5] [[1 2] [3 4 [5]]]
rlm@10 1099 [1 2 3 4 5] [1 2 3 4 5]
rlm@10 1100 [#{1 2} 3 4 5] [#{1 2} 3 4 5]
rlm@10 1101 ;sets
rlm@10 1102 [] #{}
rlm@10 1103 [] #{#{1 2} 3 4 5}
rlm@10 1104 [] #{1 2 3 4 5}
rlm@10 1105 [] #{#{1 2} 3 4 5}
rlm@10 1106 ;lists
rlm@10 1107 [] '()
rlm@10 1108 [1 2 3 4 5] `(1 2 3 4 5)
rlm@10 1109 ;maps
rlm@10 1110 [] {:a 1 :b 2}
rlm@10 1111 [:a 1 :b 2] (seq {:a 1 :b 2})
rlm@10 1112 [] {[:a :b] 1 :c 2}
rlm@10 1113 [:a :b 1 :c 2] (seq {[:a :b] 1 :c 2})
rlm@10 1114 [:a 1 2 :b 3] (seq {:a [1 2] :b 3})
rlm@10 1115 ;Strings
rlm@10 1116 [] "12345"
rlm@10 1117 [\1 \2 \3 \4 \5] (seq "12345")
rlm@10 1118 ;fns
rlm@10 1119 [] count
rlm@10 1120 [count even? odd?] [count even? odd?]))
rlm@10 1121
rlm@10 1122 (deftest test-group-by
rlm@10 1123 (is (= (group-by even? [1 2 3 4 5])
rlm@10 1124 {false [1 3 5], true [2 4]})))
rlm@10 1125
rlm@10 1126 (deftest test-partition-by
rlm@10 1127 (are [test-seq] (= (partition-by (comp even? count) test-seq)
rlm@10 1128 [["a"] ["bb" "cccc" "dd"] ["eee" "f"] ["" "hh"]])
rlm@10 1129 ["a" "bb" "cccc" "dd" "eee" "f" "" "hh"]
rlm@10 1130 '("a" "bb" "cccc" "dd" "eee" "f" "" "hh"))
rlm@10 1131 (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm")
rlm@10 1132 [[\a] [\b \c \d] [\e] [\f \g \h] [\i] [\j \k \l \m]])))
rlm@10 1133
rlm@10 1134 (deftest test-frequencies
rlm@10 1135 (are [expected test-seq] (= (frequencies test-seq) expected)
rlm@10 1136 {\p 2, \s 4, \i 4, \m 1} "mississippi"
rlm@10 1137 {1 4 2 2 3 1} [1 1 1 1 2 2 3]
rlm@10 1138 {1 4 2 2 3 1} '(1 1 1 1 2 2 3)))
rlm@10 1139
rlm@10 1140 (deftest test-reductions
rlm@10 1141 (is (= (reductions + nil)
rlm@10 1142 [0]))
rlm@10 1143 (is (= (reductions + [1 2 3 4 5])
rlm@10 1144 [1 3 6 10 15]))
rlm@10 1145 (is (= (reductions + 10 [1 2 3 4 5])
rlm@10 1146 [10 11 13 16 20 25])))
rlm@10 1147
rlm@10 1148 (deftest test-rand-nth-invariants
rlm@10 1149 (let [elt (rand-nth [:a :b :c :d])]
rlm@10 1150 (is (#{:a :b :c :d} elt))))
rlm@10 1151
rlm@10 1152 (deftest test-partition-all
rlm@10 1153 (is (= (partition-all 4 [1 2 3 4 5 6 7 8 9])
rlm@10 1154 [[1 2 3 4] [5 6 7 8] [9]]))
rlm@10 1155 (is (= (partition-all 4 2 [1 2 3 4 5 6 7 8 9])
rlm@10 1156 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))
rlm@10 1157
rlm@10 1158 (deftest test-shuffle-invariants
rlm@10 1159 (is (= (count (shuffle [1 2 3 4])) 4))
rlm@10 1160 (let [shuffled-seq (shuffle [1 2 3 4])]
rlm@10 1161 (is (every? #{1 2 3 4} shuffled-seq))))
rlm@10 1162