4
4
5
5
To create test functions, add a ` #[test] ` attribute like this:
6
6
7
- ~~~
7
+ ~~~ test_harness
8
8
fn return_two() -> int {
9
9
2
10
10
}
@@ -37,7 +37,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
37
37
Rust has built in support for simple unit testing. Functions can be
38
38
marked as unit tests using the ` test ` attribute.
39
39
40
- ~~~
40
+ ~~~ test_harness
41
41
#[test]
42
42
fn return_none_if_empty() {
43
43
// ... test code ...
@@ -55,7 +55,7 @@ other (`assert_eq`, ...) means, then the test fails.
55
55
When compiling a crate with the ` --test ` flag ` --cfg test ` is also
56
56
implied, so that tests can be conditionally compiled.
57
57
58
- ~~~
58
+ ~~~ test_harness
59
59
#[cfg(test)]
60
60
mod tests {
61
61
#[test]
@@ -80,11 +80,11 @@ Tests that are intended to fail can be annotated with the
80
80
task to fail then the test will be counted as successful; otherwise it
81
81
will be counted as a failure. For example:
82
82
83
- ~~~
83
+ ~~~ test_harness
84
84
#[test]
85
85
#[should_fail]
86
86
fn test_out_of_bounds_failure() {
87
- let v: [int] = [];
87
+ let v: & [int] = [];
88
88
v[0];
89
89
}
90
90
~~~
@@ -204,26 +204,22 @@ amount.
204
204
205
205
For example:
206
206
207
- ~~~
208
- # #![allow(unused_imports)]
207
+ ~~~ test_harness
209
208
extern crate test;
210
209
211
- use std::slice;
212
210
use test::Bencher;
213
211
214
212
#[bench]
215
213
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));
218
216
}
219
217
220
218
#[bench]
221
219
fn initialise_a_vector(b: &mut Bencher) {
222
- b.iter(|| {slice ::from_elem(1024, 0u64);} );
220
+ b.iter(|| Vec ::from_elem(1024, 0u64));
223
221
b.bytes = 1024 * 8;
224
222
}
225
-
226
- # fn main() {}
227
223
~~~
228
224
229
225
The benchmark runner will calibrate measurement of the benchmark
@@ -266,19 +262,16 @@ benchmarking what one expects. For example, the compiler might
266
262
recognize that some calculation has no external effects and remove
267
263
it entirely.
268
264
269
- ~~~
270
- # #![allow(unused_imports)]
265
+ ~~~ test_harness
271
266
extern crate test;
272
267
use test::Bencher;
273
268
274
269
#[bench]
275
270
fn bench_xor_1000_ints(b: &mut Bencher) {
276
271
b.iter(|| {
277
- range(0, 1000).fold(0, |old, new| old ^ new);
278
- });
272
+ range(0, 1000).fold(0, |old, new| old ^ new);
273
+ });
279
274
}
280
-
281
- # fn main() {}
282
275
~~~
283
276
284
277
gives the following results
@@ -297,8 +290,11 @@ cannot remove the computation entirely. This could be done for the
297
290
example above by adjusting the ` bh.iter ` call to
298
291
299
292
~~~
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
+ });
302
298
~~~
303
299
304
300
Or, the other option is to call the generic ` test::black_box `
@@ -309,10 +305,10 @@ forces it to consider any argument as used.
309
305
extern crate test;
310
306
311
307
# 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
+ });
316
312
# }
317
313
~~~
318
314
0 commit comments