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