auto merge of #16961 : huonw/rust/bitv-twiddle, r=alexcrichton

bitv: add larger tests, better benchmarks & remove dead code.

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.
This commit is contained in:
bors 2014-09-04 01:11:04 +00:00
commit d59d97cbec
1 changed files with 29 additions and 22 deletions

View File

@ -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
@ -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;
@ -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]
@ -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]
@ -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(|| {
@ -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(|| {
@ -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(|| {
@ -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(|| {