diff src/clojure/test_clojure/other_functions.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/other_functions.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,86 @@
     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: Frantisek Sodomka
    1.13 +
    1.14 +
    1.15 +(ns clojure.test-clojure.other-functions
    1.16 +  (:use clojure.test))
    1.17 +
    1.18 +; http://clojure.org/other_functions
    1.19 +
    1.20 +; [= not= (tests in data_structures.clj and elsewhere)]
    1.21 +
    1.22 +
    1.23 +(deftest test-identity
    1.24 +  ; exactly 1 argument needed
    1.25 +  (is (thrown? IllegalArgumentException (identity)))
    1.26 +  (is (thrown? IllegalArgumentException (identity 1 2)))
    1.27 +
    1.28 +  (are [x] (= (identity x) x)
    1.29 +      nil
    1.30 +      false true
    1.31 +      0 42
    1.32 +      0.0 3.14
    1.33 +      2/3
    1.34 +      0M 1M
    1.35 +      \c
    1.36 +      "" "abc"
    1.37 +      'sym
    1.38 +      :kw
    1.39 +      () '(1 2)
    1.40 +      [] [1 2]
    1.41 +      {} {:a 1 :b 2}
    1.42 +      #{} #{1 2} )
    1.43 +
    1.44 +  ; evaluation
    1.45 +  (are [x y] (= (identity x) y)
    1.46 +      (+ 1 2) 3
    1.47 +      (> 5 0) true ))
    1.48 +
    1.49 +
    1.50 +(deftest test-name
    1.51 +  (are [x y] (= x (name y))
    1.52 +       "foo" :foo
    1.53 +       "bar" 'bar
    1.54 +       "quux" "quux"))
    1.55 +
    1.56 +(deftest test-fnil
    1.57 +  (let [f1 (fnil vector :a)
    1.58 +        f2 (fnil vector :a :b)
    1.59 +        f3 (fnil vector :a :b :c)]
    1.60 +    (are [result input] (= result [(apply f1 input) (apply f2 input) (apply f3 input)])
    1.61 +         [[1 2 3 4] [1 2 3 4] [1 2 3 4]]  [1 2 3 4]
    1.62 +         [[:a 2 3 4] [:a 2 3 4] [:a 2 3 4]] [nil 2 3 4]
    1.63 +         [[:a nil 3 4] [:a :b 3 4] [:a :b 3 4]] [nil nil 3 4]
    1.64 +         [[:a nil nil 4] [:a :b nil 4] [:a :b :c 4]] [nil nil nil 4]
    1.65 +         [[:a nil nil nil] [:a :b nil nil] [:a :b :c nil]] [nil nil nil nil]))
    1.66 +  (are [x y] (= x y)
    1.67 +       ((fnil + 0) nil 42) 42
    1.68 +       ((fnil conj []) nil 42) [42]
    1.69 +       (reduce #(update-in %1 [%2] (fnil inc 0)) {} 
    1.70 +               ["fun" "counting" "words" "fun"])
    1.71 +       {"words" 1, "counting" 1, "fun" 2}
    1.72 +       (reduce #(update-in %1 [(first %2)] (fnil conj []) (second %2)) {} 
    1.73 +               [[:a 1] [:a 2] [:b 3]])
    1.74 +       {:b [3], :a [1 2]}))
    1.75 +
    1.76 +; time assert comment doc
    1.77 +
    1.78 +; partial
    1.79 +; comp
    1.80 +; complement
    1.81 +; constantly
    1.82 +
    1.83 +; Printing
    1.84 +; pr prn print println newline
    1.85 +; pr-str prn-str print-str println-str [with-out-str (vars.clj)]
    1.86 +
    1.87 +; Regex Support
    1.88 +; re-matcher re-find re-matches re-groups re-seq
    1.89 +