Repository navigation
Expand file tree
/
Copy pathpredicates.clj
More file actions
194 lines (157 loc) · 6.26 KB
/
Copy pathpredicates.clj
File metadata and controls
194 lines (157 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
; Author: Frantisek Sodomka
;;
;; Created 1/28/2009
(ns clojure.test-clojure.predicates
(:use clojure.test))
;; *** Type predicates ***
(def myvar 42)
(def sample-data {
:nil nil
:bool-true true
:bool-false false
:byte (byte 7)
:short (short 7)
:int (int 7)
:long (long 7)
:bigint (bigint 7)
:float (float 7)
:double (double 7)
:bigdec (bigdec 7)
:ratio 2/3
:character \a
:symbol 'abc
:keyword :kw
:empty-string ""
:empty-regex #""
:empty-list ()
:empty-lazy-seq (lazy-seq nil)
:empty-vector []
:empty-map {}
:empty-set #{}
:empty-array (into-array [])
:string "abc"
:regex #"a*b"
:list '(1 2 3)
:lazy-seq (lazy-seq [1 2 3])
:vector [1 2 3]
:map {:a 1 :b 2 :c 3}
:set #{1 2 3}
:array (into-array [1 2 3])
:fn (fn [x] (* 2 x))
:class java.util.Date
:object (new java.util.Date)
:var (var myvar)
:delay (delay (+ 1 2))
})
(def type-preds {
nil? [:nil]
true? [:bool-true]
false? [:bool-false]
; boolean?
integer? [:byte :short :int :long :bigint]
float? [:float :double]
decimal? [:bigdec]
ratio? [:ratio]
rational? [:byte :short :int :long :bigint :ratio :bigdec]
number? [:byte :short :int :long :bigint :ratio :bigdec :float :double]
; character?
symbol? [:symbol]
keyword? [:keyword]
string? [:empty-string :string]
; regex?
list? [:empty-list :list]
vector? [:empty-vector :vector]
map? [:empty-map :map]
set? [:empty-set :set]
coll? [:empty-list :list
:empty-lazy-seq :lazy-seq
:empty-vector :vector
:empty-map :map
:empty-set :set]
seq? [:empty-list :list
:empty-lazy-seq :lazy-seq]
; array?
fn? [:fn]
ifn? [:fn
:empty-vector :vector :empty-map :map :empty-set :set
:keyword :symbol :var]
class? [:class]
var? [:var]
delay? [:delay]
})
;; Test all type predicates against all data types
;;
(defn- get-fn-name [f]
(str
(apply str (nthnext (first (.split (str f) "_"))
(count "clojure.core$")))
"?"))
(deftest test-type-preds
(doseq [tp type-preds]
(doseq [dt sample-data]
(if (some #(= % (first dt)) (second tp))
(is ((first tp) (second dt))
(pr-str (list (get-fn-name (first tp)) (second dt))))
(is (not ((first tp) (second dt)))
(pr-str (list 'not (list (get-fn-name (first tp)) (second dt)))))))))
;; Additional tests:
;; http://groups.google.com/group/clojure/browse_thread/thread/537761a06edb4b06/bfd4f0705b746a38
;;
(deftest test-string?-more
(are [x] (not (string? x))
(new java.lang.StringBuilder "abc")
(new java.lang.StringBuffer "xyz")))
(def pred-val-table
(let [now (java.util.Date.)
uuid (java.util.UUID/randomUUID)
barray (byte-array 0)
uri (java.net.URI. "http://clojure.org")]
['
[identity int? pos-int? neg-int? nat-int? double? boolean? indexed? seqable? ident? uuid? decimal? inst? uri? bytes?]
[0 true false false true false false false false false false false false false false]
[1 true true false true false false false false false false false false false false]
[-1 true false true false false false false false false false false false false false]
[1.0 false false false false true false false false false false false false false false]
[true false false false false false true false false false false false false false false]
[[] false false false false false false true true false false false false false false]
[nil false false false false false false false true false false false false false false]
[{} false false false false false false false true false false false false false false]
[:foo false false false false false false false false true false false false false false]
['foo false false false false false false false false true false false false false false]
[0.0M false false false false false false false false false false true false false false]
[0N false false false false false false false false false false false false false false]
[uuid false false false false false false false false false true false false false false]
[uri false false false false false false false false false false false false true false]
[now false false false false false false false false false false false true false false]
[barray false false false false false false false true false false false false false true]]))
(deftest test-preds
(let [[preds & rows] pred-val-table]
(doseq [row rows]
(let [v (first row)]
(dotimes [i (count row)]
(is (= ((resolve (nth preds i)) v) (nth row i))
(pr-str (list (nth preds i) v))))))))
;; Special double predicates
(deftest test-double-preds
(is (NaN? ##NaN))
(is (NaN? (Double/parseDouble "NaN")))
(is (NaN? (Float/parseFloat "NaN")))
(is (NaN? Float/NaN))
(is (not (NaN? 5)))
(is (thrown? Throwable (NaN? nil)))
(is (thrown? Throwable (NaN? :xyz)))
(is (infinite? ##Inf))
(is (infinite? ##-Inf))
(is (infinite? Double/POSITIVE_INFINITY))
(is (infinite? Double/NEGATIVE_INFINITY))
(is (infinite? Float/POSITIVE_INFINITY))
(is (infinite? Float/NEGATIVE_INFINITY))
(is (thrown? Throwable (infinite? nil)))
(is (thrown? Throwable (infinite? :xyz))))