1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::fmt;
use std::sync::Arc;
use strategy::traits::*;
use test_runner::*;
pub struct Map<S, F> {
pub(super) source: S,
pub(super) fun: Arc<F>,
}
impl<S : fmt::Debug, F> fmt::Debug for Map<S, F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Map")
.field("source", &self.source)
.field("fun", &"<function>")
.finish()
}
}
impl<S : Clone, F> Clone for Map<S, F> {
fn clone(&self) -> Self {
Map {
source: self.source.clone(),
fun: self.fun.clone(),
}
}
}
impl<S : Strategy, O : fmt::Debug,
F : Fn (<S::Value as ValueTree>::Value) -> O>
Strategy for Map<S, F> {
type Value = Map<S::Value, F>;
fn new_value(&self, runner: &mut TestRunner)
-> Result<Self::Value, String> {
self.source.new_value(runner).map(
|v| Map { source: v, fun: self.fun.clone() })
}
}
impl<S : ValueTree, O : fmt::Debug, F : Fn (S::Value) -> O>
ValueTree for Map<S, F> {
type Value = O;
fn current(&self) -> O {
(self.fun)(self.source.current())
}
fn simplify(&mut self) -> bool {
self.source.simplify()
}
fn complicate(&mut self) -> bool {
self.source.complicate()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_map() {
TestRunner::new(Config::default())
.run(&(0..10).prop_map(|v| v * 2), |&v| {
assert!(0 == v % 2);
Ok(())
}).unwrap();
}
}