aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/test_clojure/clojure_set.clj
blob: 3e698406166dfba3c033fc8092f6ac8c16e0be3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;  Copyright (c) Frantisek Sodomka. All rights reserved.  The use and
;;  distribution terms for this software are covered by the Eclipse Public
;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;;  be found in the file epl-v10.html at the root of this distribution.  By
;;  using this software in any fashion, you are agreeing to be bound by the
;;  terms of this license.  You must not remove this notice, or any other,
;;  from this software.

(ns clojure.contrib.test-clojure.clojure-set
  (:use clojure.contrib.test-is)
  (:require [clojure.set :as set]))


(deftest test-union
  (are [x y] (= x y)
      (set/union) #{}

      ; identity
      (set/union #{}) #{}
      (set/union #{1}) #{1}
      (set/union #{1 2 3}) #{1 2 3}

      ; 2 sets, at least one is empty
      (set/union #{} #{}) #{}
      (set/union #{} #{1}) #{1}
      (set/union #{} #{1 2 3}) #{1 2 3}
      (set/union #{1} #{}) #{1}
      (set/union #{1 2 3} #{}) #{1 2 3}

      ; 2 sets
      (set/union #{1} #{2}) #{1 2}
      (set/union #{1} #{1 2}) #{1 2}
      (set/union #{2} #{1 2}) #{1 2}
      (set/union #{1 2} #{3}) #{1 2 3}
      (set/union #{1 2} #{2 3}) #{1 2 3}

      ; 3 sets, some are empty
      (set/union #{} #{} #{}) #{}
      (set/union #{1} #{} #{}) #{1}
      (set/union #{} #{1} #{}) #{1}
      (set/union #{} #{} #{1}) #{1}
      (set/union #{1 2} #{2 3} #{}) #{1 2 3}

      ; 3 sets
      (set/union #{1 2} #{3 4} #{5 6}) #{1 2 3 4 5 6}
      (set/union #{1 2} #{2 3} #{1 3 4}) #{1 2 3 4}

      ; different data types
      (set/union #{1 2} #{:a :b} #{nil} #{false true} #{\c "abc"} #{[] [1 2]}
        #{{} {:a 1}} #{#{} #{1 2}})
          #{1 2 :a :b nil false true \c "abc" [] [1 2] {} {:a 1} #{} #{1 2}}

      ; different types of sets
      (set/union (hash-set) (hash-set 1 2) (hash-set 2 3))
          (hash-set 1 2 3)
      (set/union (sorted-set) (sorted-set 1 2) (sorted-set 2 3))
          (sorted-set 1 2 3)
      (set/union (hash-set) (hash-set 1 2) (hash-set 2 3)
        (sorted-set) (sorted-set 4 5) (sorted-set 5 6))
          (hash-set 1 2 3 4 5 6)  ; also equals (sorted-set 1 2 3 4 5 6)
))


(deftest test-intersection
  ; at least one argument is needed
  (is (thrown? IllegalArgumentException (set/intersection)))
  
  (are [x y] (= x y)
      ; identity
      (set/intersection #{}) #{}
      (set/intersection #{1}) #{1}
      (set/intersection #{1 2 3}) #{1 2 3}
      
      ; 2 sets, at least one is empty
      (set/intersection #{} #{}) #{}
      (set/intersection #{} #{1}) #{}
      (set/intersection #{} #{1 2 3}) #{}
      (set/intersection #{1} #{}) #{}
      (set/intersection #{1 2 3} #{}) #{}

      ; 2 sets
      (set/intersection #{1 2} #{1 2}) #{1 2}
      (set/intersection #{1 2} #{3 4}) #{}
      (set/intersection #{1 2} #{1}) #{1}
      (set/intersection #{1 2} #{2}) #{2}
      (set/intersection #{1 2 4} #{2 3 4 5}) #{2 4}

      ; 3 sets, some are empty
      (set/intersection #{} #{} #{}) #{}
      (set/intersection #{1} #{} #{}) #{}
      (set/intersection #{1} #{1} #{}) #{}
      (set/intersection #{1} #{} #{1}) #{}
      (set/intersection #{1 2} #{2 3} #{}) #{}

      ; 3 sets
      (set/intersection #{1 2} #{2 3} #{5 2}) #{2}
      (set/intersection #{1 2 3} #{1 3 4} #{1 3}) #{1 3}
      (set/intersection #{1 2 3} #{3 4 5} #{8 2 3}) #{3}

      ; different types of sets
      (set/intersection (hash-set 1 2) (hash-set 2 3)) #{2}
      (set/intersection (sorted-set 1 2) (sorted-set 2 3)) #{2}
      (set/intersection
        (hash-set 1 2) (hash-set 2 3)
        (sorted-set 1 2) (sorted-set 2 3)) #{2} ))


; difference
;
; select
; project
; rename-keys
; rename
; index
; map-invert
; join