forked from bluss/qc.rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arbitrary.rs
199 lines (167 loc) · 4.23 KB
/
arbitrary.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
// vim: sts=4 sw=4 et
use super::std;
use super::std::rand::{Rand, Rng, task_rng};
use std::rand::distributions::exponential::Exp1;
use std::cell::RefCell;
use std::hashmap::{HashMap, HashSet};
/* Arbitrary */
/**
The Arbitrary trait can generate a randomly chosen value (with restrictions).
You can pass a size factor to allow specifying test size (sizes of vectors and
numbers).
*/
pub trait Arbitrary {
/**
arbitrary should return an arbitrary value of its type.
The value should be randomly chosen and its size should be scaled by the size
parameter.
*/
fn arbitrary(uint) -> Self;
}
/// Create an arbitrary value of type T
#[inline]
pub fn arbitrary<T: Arbitrary>(sz: uint) -> T {
Arbitrary::arbitrary(sz)
}
/// A wrapper type to reuse an existing Rand instance for the Arbitrary impl
#[deriving(IterBytes, Eq, Clone)]
pub struct Random<T>(T);
/// A small number >= 0.
#[deriving(Eq, Clone)]
pub struct SmallN(uint);
impl SmallN {
pub fn unwrap(self) -> uint {
let SmallN(v) = self;
v
}
}
fn small_n(size: uint) -> uint {
let f: std::rand::distributions::exponential::Exp1 = std::rand::random();
let Exp1(val) = f;
let n = (val * (size as f64)) as uint;
n.min(&(16 * size))
}
/* Helper: Iter */
#[deriving(Clone)]
struct Iter<T> {
count: uint,
size: uint,
}
fn arbiter<T>(sz: uint) -> Iter<T> {
Iter{count: small_n(sz), size: sz }
}
impl<T: Arbitrary> Iterator<T> for Iter<T> {
fn next(&mut self) -> Option<T> {
if self.count > 0 {
self.count -= 1;
Some(arbitrary(self.size))
} else { None }
}
fn size_hint(&self) -> (uint, Option<uint>) {
(self.count, Some(self.count))
}
}
macro_rules! arb_rand( ($T:ty) => (
impl Arbitrary for $T {
fn arbitrary(_: uint) -> $T {
std::rand::random()
}
}
)
)
macro_rules! arb_tuple( ($($T:ident),+ ) => (
impl<$($T: Arbitrary),+> Arbitrary for ($($T),+) {
fn arbitrary(sz: uint) -> ($($T),+) {
($(arbitrary::<$T>(sz)),+)
}
}
)
)
arb_rand!(i8)
//arb_rand!(u8)
arb_rand!(int)
arb_rand!(uint)
arb_rand!(f32)
arb_rand!(f64)
arb_rand!(bool)
arb_rand!(())
arb_tuple!(A, B)
arb_tuple!(A, B, C)
arb_tuple!(A, B, C, D)
arb_tuple!(A, B, C, D, E)
arb_tuple!(A, B, C, D, E, F)
arb_tuple!(A, B, C, D, E, F, G)
arb_tuple!(A, B, C, D, E, F, G, H)
impl<T: Rand> Arbitrary for Random<T> {
fn arbitrary(_: uint) -> Random<T> {
Random(std::rand::random())
}
}
impl<T: Arbitrary> Arbitrary for ~T {
#[inline]
fn arbitrary(sz: uint) -> ~T { ~arbitrary(sz) }
}
impl<T: 'static + Arbitrary> Arbitrary for @T {
#[inline]
fn arbitrary(sz: uint) -> @T { @arbitrary(sz) }
}
impl Arbitrary for u8 {
fn arbitrary(_: uint) -> u8 {
std::rand::random()
}
}
impl Arbitrary for char {
fn arbitrary(_: uint) -> char {
std::rand::random::<u8>() as char
}
}
impl Arbitrary for SmallN {
fn arbitrary(sz: uint) -> SmallN {
SmallN(small_n(sz))
}
}
impl<T: Arbitrary> Arbitrary for ~[T] {
fn arbitrary(sz: uint) -> ~[T] {
arbiter::<T>(sz).collect()
}
}
impl<T: Arbitrary> Arbitrary for Option<T> {
fn arbitrary(sz: uint) -> Option<T> {
if std::rand::random() {
Some(arbitrary(sz))
} else {
None
}
}
}
impl<T: Arbitrary, U: Arbitrary> Arbitrary for Result<T, U> {
fn arbitrary(sz: uint) -> Result<T, U> {
if std::rand::random() {
Ok(arbitrary(sz))
} else {
Err(arbitrary(sz))
}
}
}
impl Arbitrary for ~str {
fn arbitrary(sz: uint) -> ~str {
let mut rng = std::rand::task_rng();
let n = small_n(sz);
rng.gen_ascii_str(n)
}
}
impl <T: Arbitrary> Arbitrary for RefCell<Option<T>> {
fn arbitrary(sz: uint) -> RefCell<Option<T>> {
RefCell::new(arbitrary(sz))
}
}
impl<K: Eq + Hash + Arbitrary> Arbitrary for HashSet<K> {
fn arbitrary(sz: uint) -> HashSet<K> {
arbiter::<K>(sz).collect()
}
}
impl<K: Eq + Hash + Arbitrary, V: Arbitrary> Arbitrary for HashMap<K, V> {
fn arbitrary(sz: uint) -> HashMap<K, V> {
arbiter::<(K, V)>(sz).collect()
}
}