forked from bluss/qc.rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shrink.rs
226 lines (202 loc) · 5.92 KB
/
shrink.rs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// vim: sts=4 sw=4 et
use lazy::Lazy;
use super::std;
use std::hashmap::HashMap;
/**
The Shrink trait is used when trying to reduce a testcase to a minimal testcase.
Shrink should generate "simpler" values, the simplest first.
*/
pub trait Shrink {
fn shrink(&self) -> Lazy<Self> {
Lazy::new()
}
}
impl Shrink for () {}
impl Shrink for bool {}
impl Shrink for char {}
impl Shrink for f32 {}
impl Shrink for f64 {}
impl Shrink for i8 {}
impl Shrink for int {}
fn mpowers_of_two<T: Num + Ord>(n: T) -> ~[T] {
/* generate ~[0, n/2, n - n/4, n - n/8, n - n/16, .., n - 1] */
use std::num::One;
let mut ret = ~[std::num::Zero::zero()];
let one:T = One::one();
let two = one + one;
let mut div = one + one;
/* check for end or overflow */
while div < n && div >= two{
let next = n/div;
ret.push(n - next);
div = div * two;
}
ret
}
macro_rules! shrink_uint(
($x:expr) => (match $x {
0 => ~[],
1 => ~[0],
2 => ~[0, 1],
n @ 3 .. 8 => ~[n-3, n-2, n-1],
n => mpowers_of_two(n),
})
)
impl Shrink for u8 {
fn shrink(&self) -> Lazy<u8> { Lazy::new_from(shrink_uint!(*self)) }
}
impl Shrink for uint {
fn shrink(&self) -> Lazy<uint> { Lazy::new_from(shrink_uint!(*self)) }
}
/* type out the (A, B) tuple case as we can save half the .clone() calls */
impl<A: Send + Clone + Shrink, B: Send + Clone + Shrink> Shrink for (A, B) {
fn shrink(&self) -> Lazy<(A, B)> {
match self {
&(ref A, ref B) => {
let mut L = Lazy::new();
L.push_map_env(A.shrink(), B.clone(), |s, b| (s, b.clone()));
L.push_map_env(B.shrink(), A.clone(), |s, a| (a.clone(), s));
L
}
}
}
}
macro_rules! shrink_tuple(
($P:pat : $($T:ident),+ -> $($S:expr),+) => (
impl<$($T: Send + Clone + Shrink),+> Shrink for ($($T),+) {
fn shrink(&self) -> Lazy<($($T),+)> {
Lazy::create( |L| {
match self {
&($(ref $T),+) => {
$(
L.push_map_env($T.shrink(), self.clone(), |s, t| {
let $P = t.clone();
$S
});
)+
}
}
})
}
}
)
)
shrink_tuple!(
(a, b, c) : A, B, C ->
(s, b, c),
(a, s, c),
(a, b, s))
shrink_tuple!(
(a, b, c, d) : A, B, C, D ->
(s, b, c, d),
(a, s, c, d),
(a, b, s, d),
(a, b, c, s))
shrink_tuple!(
(a, b, c, d, e) : A, B, C, D, E ->
(s, b, c, d, e),
(a, s, c, d, e),
(a, b, s, d, e),
(a, b, c, s, e),
(a, b, c, d, s))
shrink_tuple!(
(a, b, c, d, e, f) : A, B, C, D, E, F ->
(s, b, c, d, e, f),
(a, s, c, d, e, f),
(a, b, s, d, e, f),
(a, b, c, s, e, f),
(a, b, c, d, s, f),
(a, b, c, d, e, s))
impl<T: Send + Clone + Shrink> Shrink for Option<T> {
fn shrink(&self) -> Lazy<Option<T>> {
Lazy::create( |L| {
match *self {
None => {}
Some(ref x) => {
L.push(None);
L.push_map(x.shrink(), |y| Some(y));
}
}
})
}
}
impl<T: Send + Clone + Shrink, U: Send + Clone + Shrink> Shrink for Result<T, U> {
fn shrink(&self) -> Lazy<Result<T, U>> {
Lazy::create( |L| {
match *self {
Ok(ref x) => L.push_map(x.shrink(), |y| Ok(y)),
Err(ref x) => L.push_map(x.shrink(), |y| Err(y)),
}
})
}
}
impl<T: Send + Shrink> Shrink for ~T {
fn shrink(&self) -> Lazy<~T> {
Lazy::create( |L| {
L.push_map((**self).shrink(), |u| ~u);
})
}
}
impl<T: 'static + Send + Shrink> Shrink for @T {
fn shrink(&self) -> Lazy<@T> {
Lazy::create( |L| {
L.push_map((**self).shrink(), |u| @u);
})
}
}
impl Shrink for ~str {
fn shrink(&self) -> Lazy<~str> {
Lazy::create( |L| {
if self.len() > 0 {
let v = self.chars().collect::<~[char]>();
L.push_map(v.shrink(), |v| std::str::from_chars(v));
}
})
}
}
impl<T: Send + Clone + Shrink> Shrink for ~[T] {
fn shrink(&self) -> Lazy<~[T]> {
let mut L = Lazy::new();
if self.len() == 0 {
return L;
}
L.push(~[]);
do L.push_thunk(self.clone()) |L, v| {
if v.len() > 2 {
let mid = v.len()/2;
L.push(v.iter().take(mid).map(|x| x.clone()).collect());
L.push(v.iter().skip(mid).map(|x| x.clone()).collect());
}
do L.push_thunk(v) |L, v| {
for index in range(0, v.len()) {
/* remove one at a time */
do L.push_thunk((index, v.clone())) |L, (index, v)| {
let mut v1 = v.clone();
v1.remove(index);
L.push(v1);
/* shrink one at a time */
do L.push_thunk((index, v)) |L, (index, v)| {
L.push_map_env(v[index].shrink(), (index, v), |selt, &(ref index, ref v)| {
let mut v1 = v.clone();
v1[*index] = selt;
v1
});
}
}
}
}
}
L
}
}
impl<K: Eq + Hash + Clone + Shrink + Send,
V: Clone + Shrink + Send> Shrink for HashMap<K, V> {
fn shrink(&self) -> Lazy<HashMap<K, V>> {
Lazy::create( |L| {
if self.len() > 0 {
let v = self.clone().move_iter().collect::<~[(K, V)]>();
L.push_map(v.shrink(), |v| v.move_iter().collect());
}
})
}
}