rustc: Add EnumSet tests
This commit is contained in:
parent
dc2ca9d883
commit
78520867b9
@ -45,10 +45,6 @@ pub impl<E:CLike> EnumSet<E> {
|
||||
self.bits |= bit(e);
|
||||
}
|
||||
|
||||
fn plus(&self, e: E) -> EnumSet<E> {
|
||||
EnumSet {bits: self.bits | bit(e)}
|
||||
}
|
||||
|
||||
fn contains_elem(&self, e: E) -> bool {
|
||||
(self.bits & bit(e)) != 0
|
||||
}
|
||||
@ -86,3 +82,152 @@ impl<E:CLike> core::BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
|
||||
EnumSet {bits: self.bits & e.bits}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use core;
|
||||
use core::iter;
|
||||
use util::enum_set::*;
|
||||
|
||||
#[deriving(Eq)]
|
||||
enum Foo {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
impl CLike for Foo {
|
||||
pub fn to_uint(&self) -> uint {
|
||||
*self as uint
|
||||
}
|
||||
|
||||
pub fn from_uint(v: uint) -> Foo {
|
||||
unsafe { cast::transmute(v) }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty() {
|
||||
let e: EnumSet<Foo> = EnumSet::empty();
|
||||
assert!(e.is_empty());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// intersect
|
||||
|
||||
#[test]
|
||||
fn test_two_empties_do_not_intersect() {
|
||||
let e1: EnumSet<Foo> = EnumSet::empty();
|
||||
let e2: EnumSet<Foo> = EnumSet::empty();
|
||||
assert!(!e1.intersects(e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_does_not_intersect_with_full() {
|
||||
let e1: EnumSet<Foo> = EnumSet::empty();
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
||||
e2.add(A);
|
||||
e2.add(B);
|
||||
e2.add(C);
|
||||
|
||||
assert!(!e1.intersects(e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disjoint_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
e1.add(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
||||
e2.add(B);
|
||||
|
||||
assert!(!e1.intersects(e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlapping_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
e1.add(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
||||
e2.add(A);
|
||||
e2.add(B);
|
||||
|
||||
assert!(e1.intersects(e2));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// contains and contains_elem
|
||||
|
||||
#[test]
|
||||
fn test_contains() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
e1.add(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
||||
e2.add(A);
|
||||
e2.add(B);
|
||||
|
||||
assert!(!e1.contains(e2));
|
||||
assert!(e2.contains(e1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_contains_elem() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
e1.add(A);
|
||||
assert!(e1.contains_elem(A));
|
||||
assert!(!e1.contains_elem(B));
|
||||
assert!(!e1.contains_elem(C));
|
||||
|
||||
e1.add(A);
|
||||
e1.add(B);
|
||||
assert!(e1.contains_elem(A));
|
||||
assert!(e1.contains_elem(B));
|
||||
assert!(!e1.contains_elem(C));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// each
|
||||
|
||||
#[test]
|
||||
fn test_each() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
|
||||
assert_eq!(~[], iter::to_vec(|f| e1.each(f)))
|
||||
|
||||
e1.add(A);
|
||||
assert_eq!(~[A], iter::to_vec(|f| e1.each(f)))
|
||||
|
||||
e1.add(C);
|
||||
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
|
||||
|
||||
e1.add(C);
|
||||
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
|
||||
|
||||
e1.add(B);
|
||||
assert_eq!(~[A,B,C], iter::to_vec(|f| e1.each(f)))
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// operators
|
||||
|
||||
#[test]
|
||||
fn test_operators() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
||||
e1.add(A);
|
||||
e1.add(C);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
||||
e2.add(B);
|
||||
e2.add(C);
|
||||
|
||||
let e_union = e1 | e2;
|
||||
assert_eq!(~[A,B,C], iter::to_vec(|f| e_union.each(f)))
|
||||
|
||||
let e_intersection = e1 & e2;
|
||||
assert_eq!(~[C], iter::to_vec(|f| e_intersection.each(f)))
|
||||
|
||||
let e_subtract = e1 - e2;
|
||||
assert_eq!(~[A], iter::to_vec(|f| e_subtract.each(f)))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user