Skip to content

Commit

Permalink
bitv: add larger tests, better benchmarks & remove dead code.
Browse files Browse the repository at this point in the history
There were no tests for iteration etc. with more than 5 elements,
i.e. not even going beyond a single word. This situation is rectified.

Also, the only benchmarks for `set` were with a constant bit value,
which was not indicative of every situation, due to inlining & branch
removal. This adds a benchmark at the other end of the spectrum: random
input.
  • Loading branch information
huonw committed Sep 3, 2014
1 parent 5dfb7a6 commit 5c81918
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/libcollections/bitv.rs
Expand Up @@ -95,20 +95,6 @@ fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<
static TRUE: bool = true;
static FALSE: bool = false;

#[deriving(Clone)]
struct SmallBitv {
/// only the lowest nbits of this value are used. the rest is undefined.
bits: uint
}

#[deriving(Clone)]
struct BigBitv {
storage: Vec<uint>
}

#[deriving(Clone)]
enum BitvVariant { Big(BigBitv), Small(SmallBitv) }

/// The bitvector type.
///
/// # Example
Expand Down Expand Up @@ -1653,6 +1639,7 @@ impl<'a> Iterator<uint> for TwoBitPositions<'a> {
#[cfg(test)]
mod tests {
use std::prelude::*;
use std::iter::range_step;
use std::uint;
use std::rand;
use std::rand::Rng;
Expand Down Expand Up @@ -2046,12 +2033,14 @@ mod tests {

#[test]
fn test_bitv_iterator() {
let bools = [true, false, true, true];
let bools = vec![true, false, true, true];
let bitv: Bitv = bools.iter().map(|n| *n).collect();

for (act, &ex) in bitv.iter().zip(bools.iter()) {
assert_eq!(ex, act);
}
assert_eq!(bitv.iter().collect::<Vec<bool>>(), bools)

let long = Vec::from_fn(10000, |i| i % 2 == 0);
let bitv: Bitv = long.iter().map(|n| *n).collect();
assert_eq!(bitv.iter().collect::<Vec<bool>>(), long)
}

#[test]
Expand All @@ -2061,6 +2050,12 @@ mod tests {

let idxs: Vec<uint> = bitv.iter().collect();
assert_eq!(idxs, vec!(0, 2, 3));

let long: BitvSet = range(0u, 10000).map(|n| n % 2 == 0).collect();
let real = range_step(0, 10000, 2).collect::<Vec<uint>>();

let idxs: Vec<uint> = long.iter().collect();
assert_eq!(idxs, real);
}

#[test]
Expand Down Expand Up @@ -2574,7 +2569,7 @@ mod tests {
}

#[bench]
fn bench_bitv_big(b: &mut Bencher) {
fn bench_bitv_set_big_fixed(b: &mut Bencher) {
let mut r = rng();
let mut bitv = Bitv::with_capacity(BENCH_BITS, false);
b.iter(|| {
Expand All @@ -2586,7 +2581,19 @@ mod tests {
}

#[bench]
fn bench_bitv_small(b: &mut Bencher) {
fn bench_bitv_set_big_variable(b: &mut Bencher) {
let mut r = rng();
let mut bitv = Bitv::with_capacity(BENCH_BITS, false);
b.iter(|| {
for i in range(0u, 100) {
bitv.set((r.next_u32() as uint) % BENCH_BITS, r.gen());
}
&bitv
})
}

#[bench]
fn bench_bitv_set_small(b: &mut Bencher) {
let mut r = rng();
let mut bitv = Bitv::with_capacity(uint::BITS, false);
b.iter(|| {
Expand All @@ -2598,7 +2605,7 @@ mod tests {
}

#[bench]
fn bench_bitv_set_small(b: &mut Bencher) {
fn bench_bitvset_small(b: &mut Bencher) {
let mut r = rng();
let mut bitv = BitvSet::new();
b.iter(|| {
Expand All @@ -2610,7 +2617,7 @@ mod tests {
}

#[bench]
fn bench_bitv_set_big(b: &mut Bencher) {
fn bench_bitvset_big(b: &mut Bencher) {
let mut r = rng();
let mut bitv = BitvSet::new();
b.iter(|| {
Expand Down

5 comments on commit 5c81918

@bors
Copy link
Contributor

@bors bors commented on 5c81918 Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@5c81918

@bors
Copy link
Contributor

@bors bors commented on 5c81918 Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/bitv-twiddle = 5c81918 into auto

@bors
Copy link
Contributor

@bors bors commented on 5c81918 Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/bitv-twiddle = 5c81918 merged ok, testing candidate = d59d97c

@bors
Copy link
Contributor

@bors bors commented on 5c81918 Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d59d97c

Please sign in to comment.