diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/clojure_set.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,206 @@
     1.4 +;   Copyright (c) Rich Hickey. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +;; Author: Frantisek Sodomka
    1.13 +
    1.14 +
    1.15 +(ns clojure.test-clojure.clojure-set
    1.16 +  (:use clojure.test)
    1.17 +  (:require [clojure.set :as set]))
    1.18 +
    1.19 +(deftest test-union
    1.20 +  (are [x y] (= x y)
    1.21 +      (set/union) #{}
    1.22 +
    1.23 +      ; identity
    1.24 +      (set/union #{}) #{}
    1.25 +      (set/union #{1}) #{1}
    1.26 +      (set/union #{1 2 3}) #{1 2 3}
    1.27 +
    1.28 +      ; 2 sets, at least one is empty
    1.29 +      (set/union #{} #{}) #{}
    1.30 +      (set/union #{} #{1}) #{1}
    1.31 +      (set/union #{} #{1 2 3}) #{1 2 3}
    1.32 +      (set/union #{1} #{}) #{1}
    1.33 +      (set/union #{1 2 3} #{}) #{1 2 3}
    1.34 +
    1.35 +      ; 2 sets
    1.36 +      (set/union #{1} #{2}) #{1 2}
    1.37 +      (set/union #{1} #{1 2}) #{1 2}
    1.38 +      (set/union #{2} #{1 2}) #{1 2}
    1.39 +      (set/union #{1 2} #{3}) #{1 2 3}
    1.40 +      (set/union #{1 2} #{2 3}) #{1 2 3}
    1.41 +
    1.42 +      ; 3 sets, some are empty
    1.43 +      (set/union #{} #{} #{}) #{}
    1.44 +      (set/union #{1} #{} #{}) #{1}
    1.45 +      (set/union #{} #{1} #{}) #{1}
    1.46 +      (set/union #{} #{} #{1}) #{1}
    1.47 +      (set/union #{1 2} #{2 3} #{}) #{1 2 3}
    1.48 +
    1.49 +      ; 3 sets
    1.50 +      (set/union #{1 2} #{3 4} #{5 6}) #{1 2 3 4 5 6}
    1.51 +      (set/union #{1 2} #{2 3} #{1 3 4}) #{1 2 3 4}
    1.52 +
    1.53 +      ; different data types
    1.54 +      (set/union #{1 2} #{:a :b} #{nil} #{false true} #{\c "abc"} #{[] [1 2]}
    1.55 +        #{{} {:a 1}} #{#{} #{1 2}})
    1.56 +          #{1 2 :a :b nil false true \c "abc" [] [1 2] {} {:a 1} #{} #{1 2}}
    1.57 +
    1.58 +      ; different types of sets
    1.59 +      (set/union (hash-set) (hash-set 1 2) (hash-set 2 3))
    1.60 +          (hash-set 1 2 3)
    1.61 +      (set/union (sorted-set) (sorted-set 1 2) (sorted-set 2 3))
    1.62 +          (sorted-set 1 2 3)
    1.63 +      (set/union (hash-set) (hash-set 1 2) (hash-set 2 3)
    1.64 +        (sorted-set) (sorted-set 4 5) (sorted-set 5 6))
    1.65 +          (hash-set 1 2 3 4 5 6)  ; also equals (sorted-set 1 2 3 4 5 6)
    1.66 +))
    1.67 +
    1.68 +(deftest test-intersection
    1.69 +  ; at least one argument is needed
    1.70 +  (is (thrown? IllegalArgumentException (set/intersection)))
    1.71 +  
    1.72 +  (are [x y] (= x y)
    1.73 +      ; identity
    1.74 +      (set/intersection #{}) #{}
    1.75 +      (set/intersection #{1}) #{1}
    1.76 +      (set/intersection #{1 2 3}) #{1 2 3}
    1.77 +      
    1.78 +      ; 2 sets, at least one is empty
    1.79 +      (set/intersection #{} #{}) #{}
    1.80 +      (set/intersection #{} #{1}) #{}
    1.81 +      (set/intersection #{} #{1 2 3}) #{}
    1.82 +      (set/intersection #{1} #{}) #{}
    1.83 +      (set/intersection #{1 2 3} #{}) #{}
    1.84 +
    1.85 +      ; 2 sets
    1.86 +      (set/intersection #{1 2} #{1 2}) #{1 2}
    1.87 +      (set/intersection #{1 2} #{3 4}) #{}
    1.88 +      (set/intersection #{1 2} #{1}) #{1}
    1.89 +      (set/intersection #{1 2} #{2}) #{2}
    1.90 +      (set/intersection #{1 2 4} #{2 3 4 5}) #{2 4}
    1.91 +
    1.92 +      ; 3 sets, some are empty
    1.93 +      (set/intersection #{} #{} #{}) #{}
    1.94 +      (set/intersection #{1} #{} #{}) #{}
    1.95 +      (set/intersection #{1} #{1} #{}) #{}
    1.96 +      (set/intersection #{1} #{} #{1}) #{}
    1.97 +      (set/intersection #{1 2} #{2 3} #{}) #{}
    1.98 +
    1.99 +      ; 3 sets
   1.100 +      (set/intersection #{1 2} #{2 3} #{5 2}) #{2}
   1.101 +      (set/intersection #{1 2 3} #{1 3 4} #{1 3}) #{1 3}
   1.102 +      (set/intersection #{1 2 3} #{3 4 5} #{8 2 3}) #{3}
   1.103 +
   1.104 +      ; different types of sets
   1.105 +      (set/intersection (hash-set 1 2) (hash-set 2 3)) #{2}
   1.106 +      (set/intersection (sorted-set 1 2) (sorted-set 2 3)) #{2}
   1.107 +      (set/intersection
   1.108 +        (hash-set 1 2) (hash-set 2 3)
   1.109 +        (sorted-set 1 2) (sorted-set 2 3)) #{2} ))
   1.110 +
   1.111 +(deftest test-difference
   1.112 +  (are [x y] (= x y)
   1.113 +      ; identity
   1.114 +      (set/difference #{}) #{}
   1.115 +      (set/difference #{1}) #{1}
   1.116 +      (set/difference #{1 2 3}) #{1 2 3}
   1.117 +
   1.118 +      ; 2 sets
   1.119 +      (set/difference #{1 2} #{1 2}) #{}
   1.120 +      (set/difference #{1 2} #{3 4}) #{1 2}
   1.121 +      (set/difference #{1 2} #{1}) #{2}
   1.122 +      (set/difference #{1 2} #{2}) #{1}
   1.123 +      (set/difference #{1 2 4} #{2 3 4 5}) #{1}
   1.124 +
   1.125 +       ; 3 sets
   1.126 +      (set/difference #{1 2} #{2 3} #{5 2}) #{1}
   1.127 +      (set/difference #{1 2 3} #{1 3 4} #{1 3}) #{2}
   1.128 +      (set/difference #{1 2 3} #{3 4 5} #{8 2 3}) #{1} ))
   1.129 +
   1.130 +(deftest test-select
   1.131 +  (are [x y] (= x y)
   1.132 +    (set/select integer? #{}) #{}
   1.133 +    (set/select integer? #{1 2}) #{1 2}
   1.134 +    (set/select integer? #{1 2 :a :b :c}) #{1 2}
   1.135 +    (set/select integer? #{:a :b :c}) #{}) )
   1.136 +
   1.137 +(def compositions
   1.138 +  #{{:name "Art of the Fugue" :composer "J. S. Bach"}
   1.139 +    {:name "Musical Offering" :composer "J. S. Bach"}
   1.140 +    {:name "Requiem" :composer "Giuseppe Verdi"}
   1.141 +    {:name "Requiem" :composer "W. A. Mozart"}})
   1.142 +
   1.143 +(deftest test-project
   1.144 +  (are [x y] (= x y)
   1.145 +    (set/project compositions [:name]) #{{:name "Art of the Fugue"}
   1.146 +                                         {:name "Requiem"}
   1.147 +                                         {:name "Musical Offering"}}
   1.148 +    (set/project compositions [:composer]) #{{:composer "W. A. Mozart"}
   1.149 +                                             {:composer "Giuseppe Verdi"}
   1.150 +                                             {:composer "J. S. Bach"}}
   1.151 +    (set/project compositions [:year]) #{{}}
   1.152 +    (set/project #{{}} [:name]) #{{}} ))
   1.153 +
   1.154 +(deftest test-rename
   1.155 +  (are [x y] (= x y)
   1.156 +    (set/rename compositions {:name :title}) #{{:title "Art of the Fugue" :composer "J. S. Bach"}
   1.157 +                                               {:title "Musical Offering" :composer "J. S. Bach"}
   1.158 +                                               {:title "Requiem" :composer "Giuseppe Verdi"}
   1.159 +                                               {:title "Requiem" :composer "W. A. Mozart"}}
   1.160 +    (set/rename compositions {:year :decade}) #{{:name "Art of the Fugue" :composer "J. S. Bach"}
   1.161 +                                                {:name "Musical Offering" :composer "J. S. Bach"}
   1.162 +                                                {:name "Requiem" :composer "Giuseppe Verdi"}
   1.163 +                                                {:name "Requiem" :composer "W. A. Mozart"}}
   1.164 +    (set/rename #{{}} {:year :decade}) #{{}}))
   1.165 +
   1.166 +(deftest test-rename-keys
   1.167 +  (are [x y] (= x y)
   1.168 +    (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
   1.169 +    ))
   1.170 +
   1.171 +(deftest test-index
   1.172 +  (are [x y] (= x y)
   1.173 +    (set/index  #{{:c 2} {:b 1} {:a 1 :b 2}} [:b]) {{:b 2} #{{:a 1 :b 2}}, {:b 1} #{{:b 1}} {} #{{:c 2}}}
   1.174 +  ))
   1.175 +
   1.176 +(deftest test-join
   1.177 +  (are [x y] (= x y)
   1.178 +    (set/join compositions compositions) compositions
   1.179 +    (set/join compositions #{{:name "Art of the Fugue" :genre "Classical"}})
   1.180 +                           #{{:name "Art of the Fugue" :composer "J. S. Bach" :genre "Classical"}}
   1.181 +    ))
   1.182 +
   1.183 +(deftest test-map-invert
   1.184 +  (are [x y] (= x y)
   1.185 +       (set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b}))
   1.186 +
   1.187 +(deftest test-subset?
   1.188 +  (are [sub super] (set/subset? sub super)
   1.189 +       #{} #{}
   1.190 +       #{} #{1}
   1.191 +       #{1} #{1}
   1.192 +       #{1 2} #{1 2}
   1.193 +       #{1 2} #{1 2 42})
   1.194 +  (are [notsub super] (not (set/subset? notsub super))
   1.195 +       #{1} #{}
   1.196 +       #{2} #{1}
   1.197 +       #{1 3} #{1}))
   1.198 +
   1.199 +(deftest test-superset?
   1.200 +  (are [super sub] (set/superset? super sub)
   1.201 +       #{} #{}
   1.202 +       #{1} #{}
   1.203 +       #{1} #{1}
   1.204 +       #{1 2} #{1 2}
   1.205 +       #{1 2 42} #{1 2})
   1.206 +  (are [notsuper sub] (not (set/superset? notsuper sub))
   1.207 +       #{} #{1}
   1.208 +       #{2} #{1}
   1.209 +       #{1} #{1 3}))