From 9115a9e7f1080679a09f441a5cf0882336c8395b Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 27 Aug 2018 14:00:52 -0700 Subject: [PATCH] use run_until_done in more places --- headless/src/main.rs | 24 +++++++++---------- sim/src/init.rs | 16 ++++++++++--- sim/tests/completion.rs | 2 +- sim/tests/parking.rs | 52 ++++++++++++++++++++--------------------- 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/headless/src/main.rs b/headless/src/main.rs index c4b4cd73a9..d50ec89956 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -29,7 +29,7 @@ struct Flags { big_sim: bool, /// Scenario name for savestating - #[structopt(long = "scenario_name", default_value = "editor")] + #[structopt(long = "scenario_name", default_value = "headless")] scenario_name: String, } @@ -62,15 +62,15 @@ fn main() { None }; - let mut benchmark = sim.start_benchmark(); - loop { - sim.step(&map, &control_map); - if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { - let speed = sim.measure_speed(&mut benchmark); - println!("{0}, speed = {1:.2}x", sim.summary(), speed); - } - if Some(sim.time) == save_at { - sim.save(); - } - } + sim::init::run_until_done( + &mut sim, + &map, + &control_map, + |sim| { + if Some(sim.time) == save_at { + sim.save(); + } + }, + |_parked| {}, + ); } diff --git a/sim/src/init.rs b/sim/src/init.rs index 4138010f6d..604c8be227 100644 --- a/sim/src/init.rs +++ b/sim/src/init.rs @@ -1,7 +1,7 @@ use abstutil; use control::ControlMap; use map_model::{Edits, Map}; -use {Sim, Tick}; +use {ParkedCar, Sim, Tick}; // Convenience method to setup everything. pub fn load( @@ -45,14 +45,24 @@ pub fn big_spawn(sim: &mut Sim, map: &Map) { // TODO share the helpers for spawning specific parking spots and stuff? // TODO time limit and a callback for the step results? -pub fn run_until_done(sim: &mut Sim, map: &Map, control_map: &ControlMap) { +pub fn run_until_done( + sim: &mut Sim, + map: &Map, + control_map: &ControlMap, + sim_cb: CB1, + handle_step: CB2, +) where + CB1: Fn(&Sim), + CB2: Fn(Vec), +{ let mut benchmark = sim.start_benchmark(); loop { - sim.step(&map, &control_map); + handle_step(sim.step(&map, &control_map)); if sim.time.is_multiple_of(Tick::from_seconds(60)) { let speed = sim.measure_speed(&mut benchmark); println!("{0}, speed = {1:.2}x", sim.summary(), speed); } + sim_cb(sim); if sim.is_done() { break; } diff --git a/sim/tests/completion.rs b/sim/tests/completion.rs index 667a7bb167..45d3e01d74 100644 --- a/sim/tests/completion.rs +++ b/sim/tests/completion.rs @@ -12,7 +12,7 @@ fn aorta_model_completes() { Some(sim::Tick::from_seconds(30)), ); sim::init::small_spawn(&mut sim, &map); - sim::init::run_until_done(&mut sim, &map, &control_map); + sim::init::run_until_done(&mut sim, &map, &control_map, |_sim| {}, |_parked| {}); } // TODO other tests (not completion) to add: diff --git a/sim/tests/parking.rs b/sim/tests/parking.rs index 2bd63a2a4f..b6f4b973ed 100644 --- a/sim/tests/parking.rs +++ b/sim/tests/parking.rs @@ -20,19 +20,19 @@ fn park_on_goal_st() { sim.seed_specific_parked_cars(parking2, (5..8).collect()); sim.start_parked_car_with_goal(&map, car, driving2); - loop { - if let Some(p) = sim.step(&map, &control_map).first() { - assert_eq!(p.car, car); - assert_eq!(p.spot.lane, parking2); - assert_eq!(p.spot.idx, 4); - break; - } - if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { - println!("{}", sim.summary()); - } - // TODO time limit - } - println!("Expected conditions met at {}", sim.time); + sim::init::run_until_done( + &mut sim, + &map, + &control_map, + |_sim| {}, + |parked| { + if let Some(p) = parked.first() { + assert_eq!(p.car, car); + assert_eq!(p.spot.lane, parking2); + assert_eq!(p.spot.idx, 4); + } + }, + ); } #[test] @@ -48,19 +48,19 @@ fn wander_around_for_parking() { sim.seed_specific_parked_cars(parking2, (0..8).collect()); sim.start_parked_car_with_goal(&map, car, driving2); - loop { - if let Some(p) = sim.step(&map, &control_map).first() { - assert_eq!(p.car, car); - assert_eq!(p.spot.lane, parking1); - assert_eq!(p.spot.idx, 0); - break; - } - if sim.time.is_multiple_of(sim::Tick::from_seconds(60)) { - println!("{}", sim.summary()); - } - // TODO time limit - } - println!("Expected conditions met at {}", sim.time); + sim::init::run_until_done( + &mut sim, + &map, + &control_map, + |_sim| {}, + |parked| { + if let Some(p) = parked.first() { + assert_eq!(p.car, car); + assert_eq!(p.spot.lane, parking1); + assert_eq!(p.spot.idx, 0); + } + }, + ); } fn setup(