Skip to content

Commit bd51bfe

Browse files
committed
refactor: fix rabbit suggestions
Signed-off-by: Stevan A <stevana@users.noreply.github.com>
1 parent fcceb41 commit bd51bfe

3 files changed

Lines changed: 27 additions & 31 deletions

File tree

simulation/amaru-sim/src/simulator/generate.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,7 @@ pub fn generate_zip_with<A: Copy, B: Copy, C>(
386386
let xs = generator1(rng);
387387
let ys = generator2(rng);
388388
assert_eq!(xs.len(), ys.len());
389-
let mut zs = Vec::with_capacity(xs.len());
390-
391-
for i in 0..xs.len() {
392-
zs.push(f(xs[i], ys[i]));
393-
}
394-
zs
389+
xs.into_iter().zip(ys).map(|(x, y)| f(x, y)).collect()
395390
}
396391
}
397392

simulation/amaru-sim/src/simulator/shrink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn shrink<A: Debug + Clone, B: Debug>(
5454
number_of_shrinks += 1;
5555
last_error = result;
5656
input = complement;
57-
n = n.saturating_sub(1).max(2);
57+
n = n.max(2) - 1;
5858
some_complement_is_failing = true;
5959
break;
6060
}

simulation/amaru-sim/src/simulator/simulate.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,29 @@ impl<Msg> Drop for World<Msg> {
250250
}
251251
}
252252

253+
fn run_test<Msg: Debug + PartialEq + Clone, F: Fn() -> NodeHandle<Msg>>(
254+
number_of_nodes: u8,
255+
spawn: F,
256+
property: impl Fn(History<Msg>) -> Result<(), String>,
257+
) -> impl Fn(&[Reverse<Entry<Msg>>]) -> (History<Msg>, Result<(), String>) {
258+
move |entries| {
259+
let node_handles: Vec<_> = (1..=number_of_nodes)
260+
.map(|i| (format!("n{}", i), spawn()))
261+
.collect();
262+
263+
let mut world = World::new(entries.to_vec(), node_handles);
264+
265+
match world.run_world() {
266+
Ok(history) => {
267+
let history = History(history.to_vec());
268+
let result = property(history.clone());
269+
(history, result)
270+
}
271+
Err((reason, history)) => (History(history.to_vec()), Err(reason)),
272+
}
273+
}
274+
}
275+
253276
pub fn simulate<Msg, F>(
254277
config: SimulateConfig,
255278
spawn: F,
@@ -263,35 +286,13 @@ pub fn simulate<Msg, F>(
263286
{
264287
let mut rng = StdRng::seed_from_u64(config.seed);
265288

266-
fn test<Msg: Debug + PartialEq + Clone, F: Fn() -> NodeHandle<Msg>>(
267-
number_of_nodes: u8,
268-
spawn: F,
269-
property: impl Fn(History<Msg>) -> Result<(), String>,
270-
) -> impl Fn(&[Reverse<Entry<Msg>>]) -> (History<Msg>, Result<(), String>) {
271-
move |entries| {
272-
let node_handles: Vec<_> = (1..=number_of_nodes)
273-
.map(|i| (format!("n{}", i), spawn()))
274-
.collect();
275-
276-
let mut world = World::new(entries.to_vec(), node_handles);
277-
278-
match world.run_world() {
279-
Ok(history) => (
280-
History(history.to_vec()),
281-
property(History(history.to_vec())),
282-
),
283-
Err((reason, history)) => (History(history.to_vec()), Err(reason)),
284-
}
285-
}
286-
}
287-
288289
for test_number in 1..=config.number_of_tests {
289290
let entries: Vec<Reverse<Entry<Msg>>> = generator(&mut rng);
290291

291-
match test(config.number_of_nodes, &spawn, &property)(&entries) {
292+
match run_test(config.number_of_nodes, &spawn, &property)(&entries) {
292293
(_history, Err(reason)) => {
293294
let (shrunk_entries, (shrunk_history, result), number_of_shrinks) = shrink(
294-
test(config.number_of_nodes, &spawn, &property),
295+
run_test(config.number_of_nodes, &spawn, &property),
295296
entries,
296297
|result| result.1 == Err(reason.clone()),
297298
);

0 commit comments

Comments
 (0)