Skip to content

Commit cb6219f

Browse files
committed
testing guide: update to use test_harness & fix problems.
rustdoc now supports compiling things with `--test` so the examples in this guide can be compiled & tested properly (revealing a few issues & out-dated behaviours). Also, reword an example to be clearer, cc #12242.
1 parent 11bdeea commit cb6219f

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/doc/guide-testing.md

+21-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
To create test functions, add a `#[test]` attribute like this:
66

7-
~~~
7+
~~~test_harness
88
fn return_two() -> int {
99
2
1010
}
@@ -37,7 +37,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3737
Rust has built in support for simple unit testing. Functions can be
3838
marked as unit tests using the `test` attribute.
3939

40-
~~~
40+
~~~test_harness
4141
#[test]
4242
fn return_none_if_empty() {
4343
// ... test code ...
@@ -55,7 +55,7 @@ other (`assert_eq`, ...) means, then the test fails.
5555
When compiling a crate with the `--test` flag `--cfg test` is also
5656
implied, so that tests can be conditionally compiled.
5757

58-
~~~
58+
~~~test_harness
5959
#[cfg(test)]
6060
mod tests {
6161
#[test]
@@ -80,11 +80,11 @@ Tests that are intended to fail can be annotated with the
8080
task to fail then the test will be counted as successful; otherwise it
8181
will be counted as a failure. For example:
8282

83-
~~~
83+
~~~test_harness
8484
#[test]
8585
#[should_fail]
8686
fn test_out_of_bounds_failure() {
87-
let v: [int] = [];
87+
let v: &[int] = [];
8888
v[0];
8989
}
9090
~~~
@@ -204,26 +204,22 @@ amount.
204204

205205
For example:
206206

207-
~~~
208-
# #![allow(unused_imports)]
207+
~~~test_harness
209208
extern crate test;
210209
211-
use std::slice;
212210
use test::Bencher;
213211
214212
#[bench]
215213
fn 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]
221219
fn 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

229225
The benchmark runner will calibrate measurement of the benchmark
@@ -266,19 +262,16 @@ benchmarking what one expects. For example, the compiler might
266262
recognize that some calculation has no external effects and remove
267263
it entirely.
268264

269-
~~~
270-
# #![allow(unused_imports)]
265+
~~~test_harness
271266
extern crate test;
272267
use test::Bencher;
273268
274269
#[bench]
275270
fn 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

284277
gives the following results
@@ -297,8 +290,11 @@ cannot remove the computation entirely. This could be done for the
297290
example 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

304300
Or, the other option is to call the generic `test::black_box`
@@ -309,10 +305,10 @@ forces it to consider any argument as used.
309305
extern 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

Comments
 (0)