Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
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