Skip to content

Commit ad8e8d6

Browse files
committed
---
yaml --- r: 152689 b: refs/heads/try2 c: cb6219f h: refs/heads/master i: 152687: 666649a v: v3
1 parent bd8300b commit ad8e8d6

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 11bdeea76734e1c756246373db23d5db29632ce8
8+
refs/heads/try2: cb6219f396c3f09188faad2377f0abdd8d4aba8d
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/doc/guide-testing.md

Lines changed: 21 additions & 25 deletions
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)