44
55To create test functions, add a ` #[test] ` attribute like this:
66
7- ~~~
7+ ~~~ test_harness
88fn return_two() -> int {
99 2
1010}
@@ -37,7 +37,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3737Rust has built in support for simple unit testing. Functions can be
3838marked as unit tests using the ` test ` attribute.
3939
40- ~~~
40+ ~~~ test_harness
4141#[test]
4242fn return_none_if_empty() {
4343 // ... test code ...
@@ -55,7 +55,7 @@ other (`assert_eq`, ...) means, then the test fails.
5555When compiling a crate with the ` --test ` flag ` --cfg test ` is also
5656implied, so that tests can be conditionally compiled.
5757
58- ~~~
58+ ~~~ test_harness
5959#[cfg(test)]
6060mod tests {
6161 #[test]
@@ -80,11 +80,11 @@ Tests that are intended to fail can be annotated with the
8080task to fail then the test will be counted as successful; otherwise it
8181will be counted as a failure. For example:
8282
83- ~~~
83+ ~~~ test_harness
8484#[test]
8585#[should_fail]
8686fn test_out_of_bounds_failure() {
87- let v: [int] = [];
87+ let v: & [int] = [];
8888 v[0];
8989}
9090~~~
@@ -204,26 +204,22 @@ amount.
204204
205205For example:
206206
207- ~~~
208- # #![allow(unused_imports)]
207+ ~~~ test_harness
209208extern crate test;
210209
211- use std::slice;
212210use test::Bencher;
213211
214212#[bench]
215213fn bench_sum_1024_ints(b: &mut Bencher) {
216- let v = slice ::from_fn(1024, |n| n);
217- b.iter(|| { v.iter().fold(0, |old, new| old + *new);} );
214+ let v = Vec ::from_fn(1024, |n| n);
215+ b.iter(|| v.iter().fold(0, |old, new| old + *new));
218216}
219217
220218#[bench]
221219fn initialise_a_vector(b: &mut Bencher) {
222- b.iter(|| {slice ::from_elem(1024, 0u64);} );
220+ b.iter(|| Vec ::from_elem(1024, 0u64));
223221 b.bytes = 1024 * 8;
224222}
225-
226- # fn main() {}
227223~~~
228224
229225The benchmark runner will calibrate measurement of the benchmark
@@ -266,19 +262,16 @@ benchmarking what one expects. For example, the compiler might
266262recognize that some calculation has no external effects and remove
267263it entirely.
268264
269- ~~~
270- # #![allow(unused_imports)]
265+ ~~~ test_harness
271266extern crate test;
272267use test::Bencher;
273268
274269#[bench]
275270fn bench_xor_1000_ints(b: &mut Bencher) {
276271 b.iter(|| {
277- range(0, 1000).fold(0, |old, new| old ^ new);
278- });
272+ range(0, 1000).fold(0, |old, new| old ^ new);
273+ });
279274}
280-
281- # fn main() {}
282275~~~
283276
284277gives the following results
@@ -297,8 +290,11 @@ cannot remove the computation entirely. This could be done for the
297290example above by adjusting the ` bh.iter ` call to
298291
299292~~~
300- # struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let bh = X;
301- bh.iter(|| range(0, 1000).fold(0, |old, new| old ^ new))
293+ # struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
294+ b.iter(|| {
295+ // note lack of `;` (could also use an explicit `return`).
296+ range(0, 1000).fold(0, |old, new| old ^ new)
297+ });
302298~~~
303299
304300Or, the other option is to call the generic ` test::black_box `
@@ -309,10 +305,10 @@ forces it to consider any argument as used.
309305extern crate test;
310306
311307# fn main() {
312- # struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let bh = X;
313- bh .iter(|| {
314- test::black_box(range(0, 1000).fold(0, |old, new| old ^ new));
315- });
308+ # struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
309+ b .iter(|| {
310+ test::black_box(range(0, 1000).fold(0, |old, new| old ^ new));
311+ });
316312# }
317313~~~
318314
0 commit comments