Mercurial > lasercutter
view 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 source
1 ; Copyright (c) Rich Hickey. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; 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 by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ;; Author: Frantisek Sodomka12 (ns clojure.test-clojure.clojure-set13 (:use clojure.test)14 (:require [clojure.set :as set]))16 (deftest test-union17 (are [x y] (= x y)18 (set/union) #{}20 ; identity21 (set/union #{}) #{}22 (set/union #{1}) #{1}23 (set/union #{1 2 3}) #{1 2 3}25 ; 2 sets, at least one is empty26 (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}32 ; 2 sets33 (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}39 ; 3 sets, some are empty40 (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}46 ; 3 sets47 (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}50 ; different data types51 (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}}55 ; different types of sets56 (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 ))65 (deftest test-intersection66 ; at least one argument is needed67 (is (thrown? IllegalArgumentException (set/intersection)))69 (are [x y] (= x y)70 ; identity71 (set/intersection #{}) #{}72 (set/intersection #{1}) #{1}73 (set/intersection #{1 2 3}) #{1 2 3}75 ; 2 sets, at least one is empty76 (set/intersection #{} #{}) #{}77 (set/intersection #{} #{1}) #{}78 (set/intersection #{} #{1 2 3}) #{}79 (set/intersection #{1} #{}) #{}80 (set/intersection #{1 2 3} #{}) #{}82 ; 2 sets83 (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}89 ; 3 sets, some are empty90 (set/intersection #{} #{} #{}) #{}91 (set/intersection #{1} #{} #{}) #{}92 (set/intersection #{1} #{1} #{}) #{}93 (set/intersection #{1} #{} #{1}) #{}94 (set/intersection #{1 2} #{2 3} #{}) #{}96 ; 3 sets97 (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}101 ; different types of sets102 (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/intersection105 (hash-set 1 2) (hash-set 2 3)106 (sorted-set 1 2) (sorted-set 2 3)) #{2} ))108 (deftest test-difference109 (are [x y] (= x y)110 ; identity111 (set/difference #{}) #{}112 (set/difference #{1}) #{1}113 (set/difference #{1 2 3}) #{1 2 3}115 ; 2 sets116 (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}122 ; 3 sets123 (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} ))127 (deftest test-select128 (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}) #{}) )134 (def compositions135 #{{: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"}})140 (deftest test-project141 (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]) #{{}} ))151 (deftest test-rename152 (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}) #{{}}))163 (deftest test-rename-keys164 (are [x y] (= x y)165 (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}166 ))168 (deftest test-index169 (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 ))173 (deftest test-join174 (are [x y] (= x y)175 (set/join compositions compositions) compositions176 (set/join compositions #{{:name "Art of the Fugue" :genre "Classical"}})177 #{{:name "Art of the Fugue" :composer "J. S. Bach" :genre "Classical"}}178 ))180 (deftest test-map-invert181 (are [x y] (= x y)182 (set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b}))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}))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}))