view src/clojure/test_clojure/logic.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/29/2009
14 (ns clojure.test-clojure.logic
15 (:use clojure.test
16 [clojure.test-clojure.helpers :only (exception)]))
19 ;; *** Tests ***
21 (deftest test-if
22 ; true/false/nil
23 (are [x y] (= x y)
24 (if true :t) :t
25 (if true :t :f) :t
26 (if true :t (exception)) :t
28 (if false :t) nil
29 (if false :t :f) :f
30 (if false (exception) :f) :f
32 (if nil :t) nil
33 (if nil :t :f) :f
34 (if nil (exception) :f) :f )
36 ; zero/empty is true
37 (are [x] (= (if x :t :f) :t)
38 (byte 0)
39 (short 0)
40 (int 0)
41 (long 0)
42 (bigint 0)
43 (float 0)
44 (double 0)
45 (bigdec 0)
47 0/2
48 ""
49 #""
50 (symbol "")
52 ()
53 []
54 {}
55 #{}
56 (into-array []) )
58 ; anything except nil/false is true
59 (are [x] (= (if x :t :f) :t)
60 (byte 2)
61 (short 2)
62 (int 2)
63 (long 2)
64 (bigint 2)
65 (float 2)
66 (double 2)
67 (bigdec 2)
69 2/3
70 \a
71 "abc"
72 #"a*b"
73 'abc
74 :kw
76 '(1 2)
77 [1 2]
78 {:a 1 :b 2}
79 #{1 2}
80 (into-array [1 2])
82 (new java.util.Date) ))
85 (deftest test-nil-punning
86 (are [x y] (= (if x :no :yes) y)
87 (first []) :yes
88 (next [1]) :yes
89 (rest [1]) :no
91 (butlast [1]) :yes
93 (seq nil) :yes
94 (seq []) :yes
96 (sequence nil) :no
97 (sequence []) :no
99 (lazy-seq nil) :no
100 (lazy-seq []) :no
102 (filter #(> % 10) [1 2 3]) :no
103 (map identity []) :no
104 (apply concat []) :no
106 (concat) :no
107 (concat []) :no
109 (reverse nil) :no
110 (reverse []) :no
112 (sort nil) :no
113 (sort []) :no ))
116 (deftest test-and
117 (are [x y] (= x y)
118 (and) true
119 (and true) true
120 (and nil) nil
121 (and false) false
123 (and true nil) nil
124 (and true false) false
126 (and 1 true :kw 'abc "abc") "abc"
128 (and 1 true :kw nil 'abc "abc") nil
129 (and 1 true :kw nil (exception) 'abc "abc") nil
131 (and 1 true :kw 'abc "abc" false) false
132 (and 1 true :kw 'abc "abc" false (exception)) false ))
135 (deftest test-or
136 (are [x y] (= x y)
137 (or) nil
138 (or true) true
139 (or nil) nil
140 (or false) false
142 (or nil false true) true
143 (or nil false 1 2) 1
144 (or nil false "abc" :kw) "abc"
146 (or false nil) nil
147 (or nil false) false
148 (or nil nil nil false) false
150 (or nil true false) true
151 (or nil true (exception) false) true
152 (or nil false "abc" (exception)) "abc" ))
155 (deftest test-not
156 (is (thrown? IllegalArgumentException (not)))
157 (are [x] (= (not x) true)
158 nil
159 false )
160 (are [x] (= (not x) false)
161 true
163 ; numbers
164 0
165 0.0
166 42
167 1.2
168 0/2
169 2/3
171 ; characters
172 \space
173 \tab
174 \a
176 ; strings
177 ""
178 "abc"
180 ; regexes
181 #""
182 #"a*b"
184 ; symbols
185 (symbol "")
186 'abc
188 ; keywords
189 :kw
191 ; collections/arrays
192 ()
193 '(1 2)
194 []
195 [1 2]
196 {}
197 {:a 1 :b 2}
198 #{}
199 #{1 2}
200 (into-array [])
201 (into-array [1 2])
203 ; Java objects
204 (new java.util.Date) ))