impl ward condition for max view ward (#106)

* impl ward condition for max view ward
This commit is contained in:
Al Liu 2023-03-27 18:45:50 +08:00 committed by GitHub
parent 92ef9e5a77
commit ae16e8583b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 8 deletions

View File

@ -6,13 +6,18 @@ use serde::Deserialize;
/// the set threshold. /// the set threshold.
#[derive(Debug, Deserialize, Copy, Clone)] #[derive(Debug, Deserialize, Copy, Clone)]
pub struct MaxViewWard { pub struct MaxViewWard {
_max_view: usize, max_view: usize,
} }
impl<N: Node> SimulationWard<N> for MaxViewWard { impl<N: Node> SimulationWard<N> for MaxViewWard {
type SimulationState = SimulationState<N>; type SimulationState = SimulationState<N>;
fn analyze(&mut self, _state: &Self::SimulationState) -> bool { fn analyze(&mut self, state: &Self::SimulationState) -> bool {
true // TODO: implement using simulation state !state
.nodes
.read()
.expect("simulations: MaxViewWard panic when requiring a read lock")
.iter()
.any(|n| n.current_view() >= self.max_view)
} }
} }
@ -31,7 +36,7 @@ mod test {
type Settings = (); type Settings = ();
type State = Self; type State = Self;
fn new<R: Rng>(rng: &mut R, id: NodeId, settings: Self::Settings) -> Self { fn new<R: Rng>(_rng: &mut R, id: NodeId, _settings: Self::Settings) -> Self {
id.inner() id.inner()
} }
@ -51,13 +56,15 @@ mod test {
self.add_assign(1); self.add_assign(1);
} }
} }
let mut ttf = MaxViewWard { _max_view: 10 }; let mut ttf = MaxViewWard { max_view: 10 };
let mut cond = false;
let node = 11; let node = 9;
let mut state = SimulationState { let state = SimulationState {
nodes: Arc::new(RwLock::new(vec![node])), nodes: Arc::new(RwLock::new(vec![node])),
}; };
assert!(ttf.analyze(&state)); assert!(ttf.analyze(&state));
state.nodes.write().unwrap().push(11);
assert!(!ttf.analyze(&state));
} }
} }