Mercurial > lasercutter
comparison src/clojure/test_clojure/clojure_set.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 (ns clojure.test-clojure.clojure-set | |
13 (:use clojure.test) | |
14 (:require [clojure.set :as set])) | |
15 | |
16 (deftest test-union | |
17 (are [x y] (= x y) | |
18 (set/union) #{} | |
19 | |
20 ; identity | |
21 (set/union #{}) #{} | |
22 (set/union #{1}) #{1} | |
23 (set/union #{1 2 3}) #{1 2 3} | |
24 | |
25 ; 2 sets, at least one is empty | |
26 (set/union #{} #{}) #{} | |
27 (set/union #{} #{1}) #{1} | |
28 (set/union #{} #{1 2 3}) #{1 2 3} | |
29 (set/union #{1} #{}) #{1} | |
30 (set/union #{1 2 3} #{}) #{1 2 3} | |
31 | |
32 ; 2 sets | |
33 (set/union #{1} #{2}) #{1 2} | |
34 (set/union #{1} #{1 2}) #{1 2} | |
35 (set/union #{2} #{1 2}) #{1 2} | |
36 (set/union #{1 2} #{3}) #{1 2 3} | |
37 (set/union #{1 2} #{2 3}) #{1 2 3} | |
38 | |
39 ; 3 sets, some are empty | |
40 (set/union #{} #{} #{}) #{} | |
41 (set/union #{1} #{} #{}) #{1} | |
42 (set/union #{} #{1} #{}) #{1} | |
43 (set/union #{} #{} #{1}) #{1} | |
44 (set/union #{1 2} #{2 3} #{}) #{1 2 3} | |
45 | |
46 ; 3 sets | |
47 (set/union #{1 2} #{3 4} #{5 6}) #{1 2 3 4 5 6} | |
48 (set/union #{1 2} #{2 3} #{1 3 4}) #{1 2 3 4} | |
49 | |
50 ; different data types | |
51 (set/union #{1 2} #{:a :b} #{nil} #{false true} #{\c "abc"} #{[] [1 2]} | |
52 #{{} {:a 1}} #{#{} #{1 2}}) | |
53 #{1 2 :a :b nil false true \c "abc" [] [1 2] {} {:a 1} #{} #{1 2}} | |
54 | |
55 ; different types of sets | |
56 (set/union (hash-set) (hash-set 1 2) (hash-set 2 3)) | |
57 (hash-set 1 2 3) | |
58 (set/union (sorted-set) (sorted-set 1 2) (sorted-set 2 3)) | |
59 (sorted-set 1 2 3) | |
60 (set/union (hash-set) (hash-set 1 2) (hash-set 2 3) | |
61 (sorted-set) (sorted-set 4 5) (sorted-set 5 6)) | |
62 (hash-set 1 2 3 4 5 6) ; also equals (sorted-set 1 2 3 4 5 6) | |
63 )) | |
64 | |
65 (deftest test-intersection | |
66 ; at least one argument is needed | |
67 (is (thrown? IllegalArgumentException (set/intersection))) | |
68 | |
69 (are [x y] (= x y) | |
70 ; identity | |
71 (set/intersection #{}) #{} | |
72 (set/intersection #{1}) #{1} | |
73 (set/intersection #{1 2 3}) #{1 2 3} | |
74 | |
75 ; 2 sets, at least one is empty | |
76 (set/intersection #{} #{}) #{} | |
77 (set/intersection #{} #{1}) #{} | |
78 (set/intersection #{} #{1 2 3}) #{} | |
79 (set/intersection #{1} #{}) #{} | |
80 (set/intersection #{1 2 3} #{}) #{} | |
81 | |
82 ; 2 sets | |
83 (set/intersection #{1 2} #{1 2}) #{1 2} | |
84 (set/intersection #{1 2} #{3 4}) #{} | |
85 (set/intersection #{1 2} #{1}) #{1} | |
86 (set/intersection #{1 2} #{2}) #{2} | |
87 (set/intersection #{1 2 4} #{2 3 4 5}) #{2 4} | |
88 | |
89 ; 3 sets, some are empty | |
90 (set/intersection #{} #{} #{}) #{} | |
91 (set/intersection #{1} #{} #{}) #{} | |
92 (set/intersection #{1} #{1} #{}) #{} | |
93 (set/intersection #{1} #{} #{1}) #{} | |
94 (set/intersection #{1 2} #{2 3} #{}) #{} | |
95 | |
96 ; 3 sets | |
97 (set/intersection #{1 2} #{2 3} #{5 2}) #{2} | |
98 (set/intersection #{1 2 3} #{1 3 4} #{1 3}) #{1 3} | |
99 (set/intersection #{1 2 3} #{3 4 5} #{8 2 3}) #{3} | |
100 | |
101 ; different types of sets | |
102 (set/intersection (hash-set 1 2) (hash-set 2 3)) #{2} | |
103 (set/intersection (sorted-set 1 2) (sorted-set 2 3)) #{2} | |
104 (set/intersection | |
105 (hash-set 1 2) (hash-set 2 3) | |
106 (sorted-set 1 2) (sorted-set 2 3)) #{2} )) | |
107 | |
108 (deftest test-difference | |
109 (are [x y] (= x y) | |
110 ; identity | |
111 (set/difference #{}) #{} | |
112 (set/difference #{1}) #{1} | |
113 (set/difference #{1 2 3}) #{1 2 3} | |
114 | |
115 ; 2 sets | |
116 (set/difference #{1 2} #{1 2}) #{} | |
117 (set/difference #{1 2} #{3 4}) #{1 2} | |
118 (set/difference #{1 2} #{1}) #{2} | |
119 (set/difference #{1 2} #{2}) #{1} | |
120 (set/difference #{1 2 4} #{2 3 4 5}) #{1} | |
121 | |
122 ; 3 sets | |
123 (set/difference #{1 2} #{2 3} #{5 2}) #{1} | |
124 (set/difference #{1 2 3} #{1 3 4} #{1 3}) #{2} | |
125 (set/difference #{1 2 3} #{3 4 5} #{8 2 3}) #{1} )) | |
126 | |
127 (deftest test-select | |
128 (are [x y] (= x y) | |
129 (set/select integer? #{}) #{} | |
130 (set/select integer? #{1 2}) #{1 2} | |
131 (set/select integer? #{1 2 :a :b :c}) #{1 2} | |
132 (set/select integer? #{:a :b :c}) #{}) ) | |
133 | |
134 (def compositions | |
135 #{{:name "Art of the Fugue" :composer "J. S. Bach"} | |
136 {:name "Musical Offering" :composer "J. S. Bach"} | |
137 {:name "Requiem" :composer "Giuseppe Verdi"} | |
138 {:name "Requiem" :composer "W. A. Mozart"}}) | |
139 | |
140 (deftest test-project | |
141 (are [x y] (= x y) | |
142 (set/project compositions [:name]) #{{:name "Art of the Fugue"} | |
143 {:name "Requiem"} | |
144 {:name "Musical Offering"}} | |
145 (set/project compositions [:composer]) #{{:composer "W. A. Mozart"} | |
146 {:composer "Giuseppe Verdi"} | |
147 {:composer "J. S. Bach"}} | |
148 (set/project compositions [:year]) #{{}} | |
149 (set/project #{{}} [:name]) #{{}} )) | |
150 | |
151 (deftest test-rename | |
152 (are [x y] (= x y) | |
153 (set/rename compositions {:name :title}) #{{:title "Art of the Fugue" :composer "J. S. Bach"} | |
154 {:title "Musical Offering" :composer "J. S. Bach"} | |
155 {:title "Requiem" :composer "Giuseppe Verdi"} | |
156 {:title "Requiem" :composer "W. A. Mozart"}} | |
157 (set/rename compositions {:year :decade}) #{{:name "Art of the Fugue" :composer "J. S. Bach"} | |
158 {:name "Musical Offering" :composer "J. S. Bach"} | |
159 {:name "Requiem" :composer "Giuseppe Verdi"} | |
160 {:name "Requiem" :composer "W. A. Mozart"}} | |
161 (set/rename #{{}} {:year :decade}) #{{}})) | |
162 | |
163 (deftest test-rename-keys | |
164 (are [x y] (= x y) | |
165 (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"} | |
166 )) | |
167 | |
168 (deftest test-index | |
169 (are [x y] (= x y) | |
170 (set/index #{{:c 2} {:b 1} {:a 1 :b 2}} [:b]) {{:b 2} #{{:a 1 :b 2}}, {:b 1} #{{:b 1}} {} #{{:c 2}}} | |
171 )) | |
172 | |
173 (deftest test-join | |
174 (are [x y] (= x y) | |
175 (set/join compositions compositions) compositions | |
176 (set/join compositions #{{:name "Art of the Fugue" :genre "Classical"}}) | |
177 #{{:name "Art of the Fugue" :composer "J. S. Bach" :genre "Classical"}} | |
178 )) | |
179 | |
180 (deftest test-map-invert | |
181 (are [x y] (= x y) | |
182 (set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b})) | |
183 | |
184 (deftest test-subset? | |
185 (are [sub super] (set/subset? sub super) | |
186 #{} #{} | |
187 #{} #{1} | |
188 #{1} #{1} | |
189 #{1 2} #{1 2} | |
190 #{1 2} #{1 2 42}) | |
191 (are [notsub super] (not (set/subset? notsub super)) | |
192 #{1} #{} | |
193 #{2} #{1} | |
194 #{1 3} #{1})) | |
195 | |
196 (deftest test-superset? | |
197 (are [super sub] (set/superset? super sub) | |
198 #{} #{} | |
199 #{1} #{} | |
200 #{1} #{1} | |
201 #{1 2} #{1 2} | |
202 #{1 2 42} #{1 2}) | |
203 (are [notsuper sub] (not (set/superset? notsuper sub)) | |
204 #{} #{1} | |
205 #{2} #{1} | |
206 #{1} #{1 3})) |