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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use alloc::boxed::Box;
use core::cell::RefCell;
use core::ops::Range;
use tinyrand::{Probability, Rand};
#[derive(Default)]
pub struct State {
next_u128_invocations: u64,
next_bool_invocations: u64,
next_lim_u128_invocations: u64,
}
impl State {
pub fn next_u128_invocations(&self) -> u64 {
self.next_u128_invocations
}
pub fn next_bool_invocations(&self) -> u64 {
self.next_bool_invocations
}
pub fn next_lim_u128_invocations(&self) -> u64 {
self.next_lim_u128_invocations
}
}
pub struct Surrogate<'a, 'd> {
state: &'a mut State,
next_u128_delegate: &'a mut Box<dyn FnMut(&State) -> u128 + 'd>,
}
impl Surrogate<'_, '_> {
pub fn state(&self) -> &State {
self.state
}
}
impl<'a, 'd> Rand for Surrogate<'a, 'd> {
fn next_u64(&mut self) -> u64 {
self.next_u128() as u64
}
fn next_u128(&mut self) -> u128 {
let r = (self.next_u128_delegate)(self.state);
self.state.next_u128_invocations += 1;
r
}
}
pub struct Mock<'d> {
state: State,
next_u128_delegate: Box<dyn FnMut(&State) -> u128 + 'd>,
next_bool_delegate: Box<dyn FnMut(Surrogate, Probability) -> bool + 'd>,
next_lim_u128_delegate: Box<dyn FnMut(Surrogate, u128) -> u128 + 'd>,
}
impl<'a> Default for Mock<'a> {
fn default() -> Self {
Self {
state: State::default(),
next_u128_delegate: Box::new(fixed(0)),
next_bool_delegate: Box::new(|mut surrogate, p| Rand::next_bool(&mut surrogate, p)),
next_lim_u128_delegate: Box::new(|mut surrogate, lim| {
Rand::next_lim_u128(&mut surrogate, lim)
}),
}
}
}
impl<'d> Mock<'d> {
#[must_use]
pub fn with_next_u128(mut self, delegate: impl FnMut(&State) -> u128 + 'd) -> Self {
self.next_u128_delegate = Box::new(delegate);
self
}
#[must_use]
pub fn with_next_bool(
mut self,
delegate: impl FnMut(Surrogate, Probability) -> bool + 'd,
) -> Self {
self.next_bool_delegate = Box::new(delegate);
self
}
#[must_use]
pub fn with_next_lim_u128(
mut self,
delegate: impl FnMut(Surrogate, u128) -> u128 + 'd,
) -> Self {
self.next_lim_u128_delegate = Box::new(delegate);
self
}
pub fn state(&self) -> &State {
&self.state
}
}
impl Rand for Mock<'_> {
fn next_u64(&mut self) -> u64 {
self.next_u128() as u64
}
fn next_u128(&mut self) -> u128 {
let next_u64_delegate = &mut self.next_u128_delegate;
let r = next_u64_delegate(&self.state);
self.state.next_u128_invocations += 1;
r
}
fn next_bool(&mut self, p: Probability) -> bool {
let surrogate = Surrogate {
next_u128_delegate: &mut self.next_u128_delegate,
state: &mut self.state,
};
let next_bool_delegate = &mut self.next_bool_delegate;
let r = next_bool_delegate(surrogate, p);
self.state.next_bool_invocations += 1;
r
}
fn next_lim_u16(&mut self, lim: u16) -> u16 {
self.next_lim_u128(u128::from(lim)) as u16
}
fn next_lim_u32(&mut self, lim: u32) -> u32 {
self.next_lim_u128(u128::from(lim)) as u32
}
fn next_lim_u64(&mut self, lim: u64) -> u64 {
self.next_lim_u128(u128::from(lim)) as u64
}
fn next_lim_u128(&mut self, lim: u128) -> u128 {
let surrogate = Surrogate {
next_u128_delegate: &mut self.next_u128_delegate,
state: &mut self.state,
};
let next_lim_delegate = &mut self.next_lim_u128_delegate;
let r = next_lim_delegate(surrogate, lim);
self.state.next_lim_u128_invocations += 1;
r
}
}
pub fn counter(range: Range<u128>) -> impl FnMut(&State) -> u128 {
let mut current = range.start;
move |_| {
let c = current;
let next = current + 1;
current = if next == range.end { range.start } else { next };
c
}
}
pub fn fixed(val: u128) -> impl FnMut(&State) -> u128 {
move |_| val
}
pub fn echo(cell: &RefCell<u128>) -> impl FnMut(&State) -> u128 + '_ {
move |_| *cell.borrow()
}
#[cfg(test)]
mod tests;