@@ -45,10 +45,6 @@ pub impl<E:CLike> EnumSet<E> {
45
45
self . bits |= bit ( e) ;
46
46
}
47
47
48
- fn plus ( & self , e : E ) -> EnumSet < E > {
49
- EnumSet { bits : self . bits | bit ( e) }
50
- }
51
-
52
48
fn contains_elem ( & self , e : E ) -> bool {
53
49
( self . bits & bit ( e) ) != 0
54
50
}
@@ -86,3 +82,152 @@ impl<E:CLike> core::BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
86
82
EnumSet { bits : self . bits & e. bits }
87
83
}
88
84
}
85
+
86
+ #[ cfg( test) ]
87
+ mod test {
88
+ use core;
89
+ use core:: iter;
90
+ use util:: enum_set:: * ;
91
+
92
+ #[ deriving( Eq ) ]
93
+ enum Foo {
94
+ A , B , C
95
+ }
96
+
97
+ impl CLike for Foo {
98
+ pub fn to_uint ( & self ) -> uint {
99
+ * self as uint
100
+ }
101
+
102
+ pub fn from_uint ( v : uint ) -> Foo {
103
+ unsafe { cast:: transmute ( v) }
104
+ }
105
+ }
106
+
107
+ #[ test]
108
+ fn test_empty ( ) {
109
+ let e: EnumSet < Foo > = EnumSet :: empty ( ) ;
110
+ assert ! ( e. is_empty( ) ) ;
111
+ }
112
+
113
+ ///////////////////////////////////////////////////////////////////////////
114
+ // intersect
115
+
116
+ #[ test]
117
+ fn test_two_empties_do_not_intersect ( ) {
118
+ let e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
119
+ let e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
120
+ assert ! ( !e1. intersects( e2) ) ;
121
+ }
122
+
123
+ #[ test]
124
+ fn test_empty_does_not_intersect_with_full ( ) {
125
+ let e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
126
+
127
+ let mut e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
128
+ e2. add ( A ) ;
129
+ e2. add ( B ) ;
130
+ e2. add ( C ) ;
131
+
132
+ assert ! ( !e1. intersects( e2) ) ;
133
+ }
134
+
135
+ #[ test]
136
+ fn test_disjoint_intersects ( ) {
137
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
138
+ e1. add ( A ) ;
139
+
140
+ let mut e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
141
+ e2. add ( B ) ;
142
+
143
+ assert ! ( !e1. intersects( e2) ) ;
144
+ }
145
+
146
+ #[ test]
147
+ fn test_overlapping_intersects ( ) {
148
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
149
+ e1. add ( A ) ;
150
+
151
+ let mut e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
152
+ e2. add ( A ) ;
153
+ e2. add ( B ) ;
154
+
155
+ assert ! ( e1. intersects( e2) ) ;
156
+ }
157
+
158
+ ///////////////////////////////////////////////////////////////////////////
159
+ // contains and contains_elem
160
+
161
+ #[ test]
162
+ fn test_contains ( ) {
163
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
164
+ e1. add ( A ) ;
165
+
166
+ let mut e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
167
+ e2. add ( A ) ;
168
+ e2. add ( B ) ;
169
+
170
+ assert ! ( !e1. contains( e2) ) ;
171
+ assert ! ( e2. contains( e1) ) ;
172
+ }
173
+
174
+ #[ test]
175
+ fn test_contains_elem ( ) {
176
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
177
+ e1. add ( A ) ;
178
+ assert ! ( e1. contains_elem( A ) ) ;
179
+ assert ! ( !e1. contains_elem( B ) ) ;
180
+ assert ! ( !e1. contains_elem( C ) ) ;
181
+
182
+ e1. add ( A ) ;
183
+ e1. add ( B ) ;
184
+ assert ! ( e1. contains_elem( A ) ) ;
185
+ assert ! ( e1. contains_elem( B ) ) ;
186
+ assert ! ( !e1. contains_elem( C ) ) ;
187
+ }
188
+
189
+ ///////////////////////////////////////////////////////////////////////////
190
+ // each
191
+
192
+ #[ test]
193
+ fn test_each ( ) {
194
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
195
+
196
+ assert_eq ! ( ~[ ] , iter:: to_vec( |f| e1. each( f) ) )
197
+
198
+ e1. add ( A ) ;
199
+ assert_eq ! ( ~[ A ] , iter:: to_vec( |f| e1. each( f) ) )
200
+
201
+ e1. add ( C ) ;
202
+ assert_eq ! ( ~[ A , C ] , iter:: to_vec( |f| e1. each( f) ) )
203
+
204
+ e1. add ( C ) ;
205
+ assert_eq ! ( ~[ A , C ] , iter:: to_vec( |f| e1. each( f) ) )
206
+
207
+ e1. add ( B ) ;
208
+ assert_eq ! ( ~[ A , B , C ] , iter:: to_vec( |f| e1. each( f) ) )
209
+ }
210
+
211
+ ///////////////////////////////////////////////////////////////////////////
212
+ // operators
213
+
214
+ #[ test]
215
+ fn test_operators ( ) {
216
+ let mut e1: EnumSet < Foo > = EnumSet :: empty ( ) ;
217
+ e1. add ( A ) ;
218
+ e1. add ( C ) ;
219
+
220
+ let mut e2: EnumSet < Foo > = EnumSet :: empty ( ) ;
221
+ e2. add ( B ) ;
222
+ e2. add ( C ) ;
223
+
224
+ let e_union = e1 | e2;
225
+ assert_eq ! ( ~[ A , B , C ] , iter:: to_vec( |f| e_union. each( f) ) )
226
+
227
+ let e_intersection = e1 & e2;
228
+ assert_eq ! ( ~[ C ] , iter:: to_vec( |f| e_intersection. each( f) ) )
229
+
230
+ let e_subtract = e1 - e2;
231
+ assert_eq ! ( ~[ A ] , iter:: to_vec( |f| e_subtract. each( f) ) )
232
+ }
233
+ }
0 commit comments