Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
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. | |
8 | |
9 ; Author: Frantisek Sodomka | |
10 | |
11 ;; | |
12 ;; Created 1/29/2009 | |
13 | |
14 (ns clojure.test-clojure.logic | |
15 (:use clojure.test | |
16 [clojure.test-clojure.helpers :only (exception)])) | |
17 | |
18 | |
19 ;; *** Tests *** | |
20 | |
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 | |
27 | |
28 (if false :t) nil | |
29 (if false :t :f) :f | |
30 (if false (exception) :f) :f | |
31 | |
32 (if nil :t) nil | |
33 (if nil :t :f) :f | |
34 (if nil (exception) :f) :f ) | |
35 | |
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) | |
46 | |
47 0/2 | |
48 "" | |
49 #"" | |
50 (symbol "") | |
51 | |
52 () | |
53 [] | |
54 {} | |
55 #{} | |
56 (into-array []) ) | |
57 | |
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) | |
68 | |
69 2/3 | |
70 \a | |
71 "abc" | |
72 #"a*b" | |
73 'abc | |
74 :kw | |
75 | |
76 '(1 2) | |
77 [1 2] | |
78 {:a 1 :b 2} | |
79 #{1 2} | |
80 (into-array [1 2]) | |
81 | |
82 (new java.util.Date) )) | |
83 | |
84 | |
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 | |
90 | |
91 (butlast [1]) :yes | |
92 | |
93 (seq nil) :yes | |
94 (seq []) :yes | |
95 | |
96 (sequence nil) :no | |
97 (sequence []) :no | |
98 | |
99 (lazy-seq nil) :no | |
100 (lazy-seq []) :no | |
101 | |
102 (filter #(> % 10) [1 2 3]) :no | |
103 (map identity []) :no | |
104 (apply concat []) :no | |
105 | |
106 (concat) :no | |
107 (concat []) :no | |
108 | |
109 (reverse nil) :no | |
110 (reverse []) :no | |
111 | |
112 (sort nil) :no | |
113 (sort []) :no )) | |
114 | |
115 | |
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 | |
122 | |
123 (and true nil) nil | |
124 (and true false) false | |
125 | |
126 (and 1 true :kw 'abc "abc") "abc" | |
127 | |
128 (and 1 true :kw nil 'abc "abc") nil | |
129 (and 1 true :kw nil (exception) 'abc "abc") nil | |
130 | |
131 (and 1 true :kw 'abc "abc" false) false | |
132 (and 1 true :kw 'abc "abc" false (exception)) false )) | |
133 | |
134 | |
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 | |
141 | |
142 (or nil false true) true | |
143 (or nil false 1 2) 1 | |
144 (or nil false "abc" :kw) "abc" | |
145 | |
146 (or false nil) nil | |
147 (or nil false) false | |
148 (or nil nil nil false) false | |
149 | |
150 (or nil true false) true | |
151 (or nil true (exception) false) true | |
152 (or nil false "abc" (exception)) "abc" )) | |
153 | |
154 | |
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 | |
162 | |
163 ; numbers | |
164 0 | |
165 0.0 | |
166 42 | |
167 1.2 | |
168 0/2 | |
169 2/3 | |
170 | |
171 ; characters | |
172 \space | |
173 \tab | |
174 \a | |
175 | |
176 ; strings | |
177 "" | |
178 "abc" | |
179 | |
180 ; regexes | |
181 #"" | |
182 #"a*b" | |
183 | |
184 ; symbols | |
185 (symbol "") | |
186 'abc | |
187 | |
188 ; keywords | |
189 :kw | |
190 | |
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]) | |
202 | |
203 ; Java objects | |
204 (new java.util.Date) )) | |
205 |