Skip to content

Commit

Permalink
Give reduction-type tests in Bitv more natural names
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriks committed Apr 17, 2014
1 parent 9f3fd93 commit 2334202
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions src/libcollections/bitv.rs
Expand Up @@ -97,12 +97,12 @@ impl SmallBitv {
pub fn set_all(&mut self) { self.bits = !0; }

#[inline]
pub fn is_true(&self, nbits: uint) -> bool {
pub fn all(&self, nbits: uint) -> bool {
small_mask(nbits) & !self.bits == 0
}

#[inline]
pub fn is_false(&self, nbits: uint) -> bool {
pub fn none(&self, nbits: uint) -> bool {
small_mask(nbits) & self.bits == 0
}

Expand Down Expand Up @@ -412,9 +412,9 @@ impl Bitv {

/// Returns `true` if all bits are 1
#[inline]
pub fn is_true(&self) -> bool {
pub fn all(&self) -> bool {
match self.rep {
Small(ref b) => b.is_true(self.nbits),
Small(ref b) => b.all(self.nbits),
_ => {
for i in self.iter() { if !i { return false; } }
true
Expand All @@ -433,16 +433,22 @@ impl Bitv {
}

/// Returns `true` if all bits are 0
pub fn is_false(&self) -> bool {
pub fn none(&self) -> bool {
match self.rep {
Small(ref b) => b.is_false(self.nbits),
Small(ref b) => b.none(self.nbits),
Big(_) => {
for i in self.iter() { if i { return false; } }
true
}
}
}

#[inline]
/// Returns `true` if any bit is 1
pub fn any(&self) -> bool {
!self.none()
}

pub fn init_to_vec(&self, i: uint) -> uint {
return if self.get(i) { 1 } else { 0 };
}
Expand Down Expand Up @@ -1551,6 +1557,51 @@ mod tests {
assert!(b.contains(&1000));
}

#[test]
fn test_small_bitv_tests() {
let v = from_bytes([0]);
assert!(!v.all());
assert!(!v.any());
assert!(v.none());

let v = from_bytes([0b00010100]);
assert!(!v.all());
assert!(v.any());
assert!(!v.none());

let v = from_bytes([0xFF]);
assert!(v.all());
assert!(v.any());
assert!(!v.none());
}

#[test]
fn test_big_bitv_tests() {
let v = from_bytes([ // 88 bits
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0]);
assert!(!v.all());
assert!(!v.any());
assert!(v.none());

let v = from_bytes([ // 88 bits
0, 0, 0b00010100, 0,
0, 0, 0, 0b00110100,
0, 0, 0]);
assert!(!v.all());
assert!(v.any());
assert!(!v.none());

let v = from_bytes([ // 88 bits
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF]);
assert!(v.all());
assert!(v.any());
assert!(!v.none());
}

fn rng() -> rand::IsaacRng {
let seed = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
rand::SeedableRng::from_seed(seed)
Expand Down

0 comments on commit 2334202

Please sign in to comment.