Skip to content

Commit 4a17592

Browse files
committed
---
yaml --- r: 140653 b: refs/heads/try2 c: 7852086 h: refs/heads/master i: 140651: 62d838b v: v3
1 parent 83153dc commit 4a17592

File tree

2 files changed

+150
-5
lines changed

2 files changed

+150
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: dc2ca9d8830a7a34242aa7722f545bc242813af3
8+
refs/heads/try2: 78520867b928ba33c557617895dd197c11361cbd
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustc/util/enum_set.rs

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ pub impl<E:CLike> EnumSet<E> {
4545
self.bits |= bit(e);
4646
}
4747

48-
fn plus(&self, e: E) -> EnumSet<E> {
49-
EnumSet {bits: self.bits | bit(e)}
50-
}
51-
5248
fn contains_elem(&self, e: E) -> bool {
5349
(self.bits & bit(e)) != 0
5450
}
@@ -86,3 +82,152 @@ impl<E:CLike> core::BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
8682
EnumSet {bits: self.bits & e.bits}
8783
}
8884
}
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

Comments
 (0)