blob: 40a94c3709513da76603a6e28ed63dae3109655d (
plain)
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
195
196
197
198
199
200
201
202
|
;; Copyright (c) Frantisek Sodomka. 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.
;;
;; Created 1/29/2009
(ns clojure.contrib.test-clojure.logic
(:use clojure.contrib.test-is
[clojure.contrib.test-clojure.test-utils :only (exception)]))
;; *** Tests ***
(deftest test-if
; true/false/nil
(are (= _1 _2)
(if true :t) :t
(if true :t :f) :t
(if true :t (exception)) :t
(if false :t) nil
(if false :t :f) :f
(if false (exception) :f) :f
(if nil :t) nil
(if nil :t :f) :f
(if nil (exception) :f) :f )
; zero/empty is true
(are (= (if _ :t :f) :t)
(byte 0)
(short 0)
(int 0)
(long 0)
(bigint 0)
(float 0)
(double 0)
(bigdec 0)
0/2
""
#""
(symbol "")
()
[]
{}
#{}
(into-array []) )
; anything except nil/false is true
(are (= (if _ :t :f) :t)
(byte 2)
(short 2)
(int 2)
(long 2)
(bigint 2)
(float 2)
(double 2)
(bigdec 2)
2/3
\a
"abc"
#"a*b"
'abc
:kw
'(1 2)
[1 2]
{:a 1 :b 2}
#{1 2}
(into-array [1 2])
(new java.util.Date) ))
(deftest test-nil-punning
(are (= (if _1 :no :yes) _2)
(first []) :yes
(next [1]) :yes
(rest [1]) :no
(butlast [1]) :yes
(seq nil) :yes
(seq []) :yes
(sequence nil) :no
(sequence []) :no
(lazy-seq nil) :no
(lazy-seq []) :no
(filter #(> % 10) [1 2 3]) :no
(map identity []) :no
(apply concat []) :no
(concat) :no
(concat []) :no
(reverse nil) :no
(reverse []) :no
(sort nil) :no
(sort []) :no ))
(deftest test-and
(are (= _1 _2)
(and) true
(and true) true
(and nil) nil
(and false) false
(and true nil) nil
(and true false) false
(and 1 true :kw 'abc "abc") "abc"
(and 1 true :kw nil 'abc "abc") nil
(and 1 true :kw nil (exception) 'abc "abc") nil
(and 1 true :kw 'abc "abc" false) false
(and 1 true :kw 'abc "abc" false (exception)) false ))
(deftest test-or
(are (= _1 _2)
(or) nil
(or true) true
(or nil) nil
(or false) false
(or nil false true) true
(or nil false 1 2) 1
(or nil false "abc" :kw) "abc"
(or false nil) nil
(or nil false) false
(or nil nil nil false) false
(or nil true false) true
(or nil true (exception) false) true
(or nil false "abc" (exception)) "abc" ))
(deftest test-not
(is (thrown? IllegalArgumentException (not)))
(are (= (not _) true)
nil
false )
(are (= (not _) false)
true
; numbers
0
0.0
42
1.2
0/2
2/3
; characters
\space
\tab
\a
; strings
""
"abc"
; regexes
#""
#"a*b"
; symbols
(symbol "")
'abc
; keywords
:kw
; collections/arrays
()
'(1 2)
[]
[1 2]
{}
{:a 1 :b 2}
#{}
#{1 2}
(into-array [])
(into-array [1 2])
; Java objects
(new java.util.Date) ))
|