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