Skip to content

Finalize story for libtest #20603

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
TARGET_CRATES := libc std flate arena term \
serialize getopts collections test rand \
log regex graphviz core rbml alloc \
unicode
unicode rustc_bench
RUSTC_CRATES := rustc rustc_typeck rustc_borrowck rustc_resolve rustc_driver \
rustc_trans rustc_back rustc_llvm
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
Expand Down Expand Up @@ -90,11 +90,13 @@ DEPS_term := std log
DEPS_getopts := std
DEPS_collections := core alloc unicode
DEPS_num := std
DEPS_test := std getopts serialize rbml term regex native:rust_test_helpers
DEPS_test := std getopts serialize rbml term regex native:rust_test_helpers \
rustc_bench
DEPS_rand := core
DEPS_log := std regex
DEPS_regex := std
DEPS_fmt_macros = std
DEPS_rustc_bench := std

TOOL_DEPS_compiletest := test getopts
TOOL_DEPS_rustdoc := rustdoc
Expand Down
23 changes: 11 additions & 12 deletions src/doc/guide-testing.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% The Rust Testing Guide

> Program testing can be a very effective way to show the presence of bugs, but
> it is hopelessly inadequate for showing their absence.
> it is hopelessly inadequate for showing their absence.
>
> Edsger W. Dijkstra, "The Humble Programmer" (1972)

Expand Down Expand Up @@ -310,7 +310,7 @@ extern crate adder;
#[test]
fn it_works() {
assert_eq(4, adder::add_two(2));
}
}
```

This looks similar to our previous tests, but slightly different. We now have
Expand Down Expand Up @@ -442,7 +442,7 @@ code. Let's make our `src/lib.rs` look like this (comments elided):
```{rust,ignore}
#![feature(globs)]

extern crate test;
extern crate rustc_bench;

pub fn add_two(a: i32) -> i32 {
a + 2
Expand All @@ -451,7 +451,7 @@ pub fn add_two(a: i32) -> i32 {
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
use rustc_bench::Bencher;

#[test]
fn it_works() {
Expand Down Expand Up @@ -512,8 +512,8 @@ compiler might recognize that some calculation has no external effects and
remove it entirely.

```{rust,ignore}
extern crate test;
use test::Bencher;
extern crate rustc_bench;
use rustc_bench::Bencher;

#[bench]
fn bench_xor_1000_ints(b: &mut Bencher) {
Expand Down Expand Up @@ -547,20 +547,19 @@ b.iter(|| {
});
```

Or, the other option is to call the generic `test::black_box` function, which
is an opaque "black box" to the optimizer and so forces it to consider any
argument as used.
Or, the other option is to call the generic `rustc_bench::black_box` function,
which is an opaque "black box" to the optimizer and so forces it to consider
any argument as used.

```rust
extern crate test;

extern crate rustc_bench;
# fn main() {
# struct X;
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
b.iter(|| {
let mut n = 1000_u32;

test::black_box(&mut n); // pretend to modify `n`
rustc_bench::black_box(&mut n); // pretend to modify `n`

range(0, n).fold(0, |a, b| a ^ b)
})
Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ mod imp {

#[cfg(test)]
mod test {
extern crate test;
use self::test::Bencher;
extern crate rustc_bench;
use self::rustc_bench::Bencher;
use core::ptr::PtrExt;
use heap;

Expand Down
4 changes: 2 additions & 2 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ impl<T> Drop for TypedArena<T> {

#[cfg(test)]
mod tests {
extern crate test;
use self::test::Bencher;
extern crate rustc_bench;
use self::rustc_bench::Bencher;
use super::{Arena, TypedArena};

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use prelude::*;
use std::rand;
use std::rand::Rng;
use test::{Bencher, black_box};
use rustc_bench::{Bencher, black_box};

pub fn insert_rand_n<M, I, R>(n: uint,
map: &mut M,
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,7 @@ mod bitv_bench {
use std::rand;
use std::rand::Rng;
use std::u32;
use test::{Bencher, black_box};
use rustc_bench::{Bencher, black_box};

use super::Bitv;

Expand All @@ -2526,7 +2526,7 @@ mod bitv_bench {
for _ in range(0u, 100) {
bitv |= 1 << ((r.next_u32() as uint) % u32::BITS);
}
black_box(&bitv)
black_box(&bitv);
});
}

Expand All @@ -2538,7 +2538,7 @@ mod bitv_bench {
for _ in range(0u, 100) {
bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
}
black_box(&bitv)
black_box(&bitv);
});
}

Expand Down Expand Up @@ -3002,7 +3002,7 @@ mod bitv_set_bench {
use std::rand;
use std::rand::Rng;
use std::u32;
use test::{Bencher, black_box};
use rustc_bench::{Bencher, black_box};

use super::{Bitv, BitvSet};

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ mod test {
mod bench {
use prelude::*;
use std::rand::{weak_rng, Rng};
use test::{Bencher, black_box};
use rustc_bench::{Bencher, black_box};

use super::BTreeMap;
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
Expand Down
21 changes: 10 additions & 11 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ mod tests {
use std::rand;
use std::hash;
use std::thread::Thread;
use test::Bencher;
use test;
use rustc_bench::Bencher;

use super::{DList, Node};

Expand Down Expand Up @@ -1101,31 +1100,31 @@ mod tests {
}

#[bench]
fn bench_collect_into(b: &mut test::Bencher) {
fn bench_collect_into(b: &mut Bencher) {
let v = &[0i; 64];
b.iter(|| {
let _: DList<int> = v.iter().map(|x| *x).collect();
})
}

#[bench]
fn bench_push_front(b: &mut test::Bencher) {
fn bench_push_front(b: &mut Bencher) {
let mut m: DList<int> = DList::new();
b.iter(|| {
m.push_front(0);
})
}

#[bench]
fn bench_push_back(b: &mut test::Bencher) {
fn bench_push_back(b: &mut Bencher) {
let mut m: DList<int> = DList::new();
b.iter(|| {
m.push_back(0);
})
}

#[bench]
fn bench_push_back_pop_back(b: &mut test::Bencher) {
fn bench_push_back_pop_back(b: &mut Bencher) {
let mut m: DList<int> = DList::new();
b.iter(|| {
m.push_back(0);
Expand All @@ -1134,7 +1133,7 @@ mod tests {
}

#[bench]
fn bench_push_front_pop_front(b: &mut test::Bencher) {
fn bench_push_front_pop_front(b: &mut Bencher) {
let mut m: DList<int> = DList::new();
b.iter(|| {
m.push_front(0);
Expand All @@ -1143,31 +1142,31 @@ mod tests {
}

#[bench]
fn bench_iter(b: &mut test::Bencher) {
fn bench_iter(b: &mut Bencher) {
let v = &[0i; 128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().count() == 128);
})
}
#[bench]
fn bench_iter_mut(b: &mut test::Bencher) {
fn bench_iter_mut(b: &mut Bencher) {
let v = &[0i; 128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter_mut().count() == 128);
})
}
#[bench]
fn bench_iter_rev(b: &mut test::Bencher) {
fn bench_iter_rev(b: &mut Bencher) {
let v = &[0i; 128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().rev().count() == 128);
})
}
#[bench]
fn bench_iter_mut_rev(b: &mut test::Bencher) {
fn bench_iter_mut_rev(b: &mut Bencher) {
let v = &[0i; 128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern crate core;
extern crate unicode;
extern crate alloc;

#[cfg(test)] extern crate test;
#[cfg(test)] extern crate rustc_bench;
#[cfg(test)] #[macro_use] extern crate std;
#[cfg(test)] #[macro_use] extern crate log;

Expand Down
31 changes: 15 additions & 16 deletions src/libcollections/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,7 @@ mod tests {
use core::iter;
use std::fmt::Show;
use std::hash;
use test::Bencher;
use test;
use rustc_bench::{Bencher, black_box};

use super::RingBuf;

Expand Down Expand Up @@ -1750,15 +1749,15 @@ mod tests {
}

#[bench]
fn bench_new(b: &mut test::Bencher) {
fn bench_new(b: &mut Bencher) {
b.iter(|| {
let ring: RingBuf<u64> = RingBuf::new();
test::black_box(ring);
black_box(ring);
})
}

#[bench]
fn bench_push_back_100(b: &mut test::Bencher) {
fn bench_push_back_100(b: &mut Bencher) {
let mut deq = RingBuf::with_capacity(101);
b.iter(|| {
for i in range(0i, 100) {
Expand All @@ -1770,7 +1769,7 @@ mod tests {
}

#[bench]
fn bench_push_front_100(b: &mut test::Bencher) {
fn bench_push_front_100(b: &mut Bencher) {
let mut deq = RingBuf::with_capacity(101);
b.iter(|| {
for i in range(0i, 100) {
Expand All @@ -1782,65 +1781,65 @@ mod tests {
}

#[bench]
fn bench_pop_back_100(b: &mut test::Bencher) {
fn bench_pop_back_100(b: &mut Bencher) {
let mut deq: RingBuf<int> = RingBuf::with_capacity(101);

b.iter(|| {
deq.head = 100;
deq.tail = 0;
while !deq.is_empty() {
test::black_box(deq.pop_back());
black_box(deq.pop_back());
}
})
}

#[bench]
fn bench_pop_front_100(b: &mut test::Bencher) {
fn bench_pop_front_100(b: &mut Bencher) {
let mut deq: RingBuf<int> = RingBuf::with_capacity(101);

b.iter(|| {
deq.head = 100;
deq.tail = 0;
while !deq.is_empty() {
test::black_box(deq.pop_front());
black_box(deq.pop_front());
}
})
}

#[bench]
fn bench_grow_1025(b: &mut test::Bencher) {
fn bench_grow_1025(b: &mut Bencher) {
b.iter(|| {
let mut deq = RingBuf::new();
for i in range(0i, 1025) {
deq.push_front(i);
}
test::black_box(deq);
black_box(deq);
})
}

#[bench]
fn bench_iter_1000(b: &mut test::Bencher) {
fn bench_iter_1000(b: &mut Bencher) {
let ring: RingBuf<int> = range(0i, 1000).collect();

b.iter(|| {
let mut sum = 0;
for &i in ring.iter() {
sum += i;
}
test::black_box(sum);
black_box(sum);
})
}

#[bench]
fn bench_mut_iter_1000(b: &mut test::Bencher) {
fn bench_mut_iter_1000(b: &mut Bencher) {
let mut ring: RingBuf<int> = range(0i, 1000).collect();

b.iter(|| {
let mut sum = 0;
for i in ring.iter_mut() {
sum += *i;
}
test::black_box(sum);
black_box(sum);
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2730,7 +2730,7 @@ mod bench {
use core::ptr;
use core::iter::repeat;
use std::rand::{weak_rng, Rng};
use test::{Bencher, black_box};
use rustc_bench::{Bencher, black_box};

#[bench]
fn iterator(b: &mut Bencher) {
Expand Down
Loading