view src/clojure/test_clojure/predicates.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: Frantisek Sodomka
11 ;;
12 ;; Created 1/28/2009
14 (ns clojure.test-clojure.predicates
15 (:use clojure.test))
18 ;; *** Type predicates ***
20 (def myvar 42)
22 (def sample-data {
23 :nil nil
25 :bool-true true
26 :bool-false false
28 :byte (byte 7)
29 :short (short 7)
30 :int (int 7)
31 :long (long 7)
32 :bigint (bigint 7)
33 :float (float 7)
34 :double (double 7)
35 :bigdec (bigdec 7)
37 :ratio 2/3
39 :character \a
40 :symbol 'abc
41 :keyword :kw
43 :empty-string ""
44 :empty-regex #""
45 :empty-list ()
46 :empty-lazy-seq (lazy-seq nil)
47 :empty-vector []
48 :empty-map {}
49 :empty-set #{}
50 :empty-array (into-array [])
52 :string "abc"
53 :regex #"a*b"
54 :list '(1 2 3)
55 :lazy-seq (lazy-seq [1 2 3])
56 :vector [1 2 3]
57 :map {:a 1 :b 2 :c 3}
58 :set #{1 2 3}
59 :array (into-array [1 2 3])
61 :fn (fn [x] (* 2 x))
63 :class java.util.Date
64 :object (new java.util.Date)
66 :var (var myvar)
67 :delay (delay (+ 1 2))
68 })
71 (def type-preds {
72 nil? [:nil]
74 true? [:bool-true]
75 false? [:bool-false]
76 ; boolean?
78 integer? [:byte :short :int :long :bigint]
79 float? [:float :double]
80 decimal? [:bigdec]
81 ratio? [:ratio]
82 rational? [:byte :short :int :long :bigint :ratio :bigdec]
83 number? [:byte :short :int :long :bigint :ratio :bigdec :float :double]
85 ; character?
86 symbol? [:symbol]
87 keyword? [:keyword]
89 string? [:empty-string :string]
90 ; regex?
92 list? [:empty-list :list]
93 vector? [:empty-vector :vector]
94 map? [:empty-map :map]
95 set? [:empty-set :set]
97 coll? [:empty-list :list
98 :empty-lazy-seq :lazy-seq
99 :empty-vector :vector
100 :empty-map :map
101 :empty-set :set]
103 seq? [:empty-list :list
104 :empty-lazy-seq :lazy-seq]
105 ; array?
107 fn? [:fn]
108 ifn? [:fn
109 :empty-vector :vector :empty-map :map :empty-set :set
110 :keyword :symbol :var]
112 class? [:class]
113 var? [:var]
114 delay? [:delay]
115 })
118 ;; Test all type predicates against all data types
119 ;;
120 (defn- get-fn-name [f]
121 (str
122 (apply str (nthnext (first (.split (str f) "_"))
123 (count "clojure.core$")))
124 "?"))
126 (deftest test-type-preds
127 (doseq [tp type-preds]
128 (doseq [dt sample-data]
129 (if (some #(= % (first dt)) (second tp))
130 (is ((first tp) (second dt))
131 (pr-str (list (get-fn-name (first tp)) (second dt))))
132 (is (not ((first tp) (second dt)))
133 (pr-str (list 'not (list (get-fn-name (first tp)) (second dt)))))))))
136 ;; Additional tests:
137 ;; http://groups.google.com/group/clojure/browse_thread/thread/537761a06edb4b06/bfd4f0705b746a38
138 ;;
139 (deftest test-string?-more
140 (are [x] (not (string? x))
141 (new java.lang.StringBuilder "abc")
142 (new java.lang.StringBuffer "xyz")))