#![forbid(unsafe_code)]
use alloc::collections::BTreeMap;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use crate::dialect::Dialect;
fn ln_approx(x: f64) -> f64 {
if x <= 0.0 {
return f64::NEG_INFINITY;
}
let bits = x.to_bits();
let exp = ((bits >> 52) & 0x7FF) as i64 - 1023;
let mantissa = f64::from_bits((bits & 0x000F_FFFF_FFFF_FFFF) | 0x3FF0_0000_0000_0000);
let t = (mantissa - 1.0) / (mantissa + 1.0);
let t2 = t * t;
let ln_m = 2.0
* t
* (1.0
+ t2 * (1.0 / 3.0
+ t2 * (1.0 / 5.0
+ t2 * (1.0 / 7.0 + t2 * (1.0 / 9.0 + t2 * (1.0 / 11.0 + t2 / 13.0))))));
(exp as f64) * core::f64::consts::LN_2 + ln_m
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Topology {
FullyConnected,
Ring,
Random(f64),
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipConfig {
pub transmission_probability: f64,
pub max_rounds: u32,
pub topology: Topology,
}
impl Default for GossipConfig {
fn default() -> Self {
Self {
transmission_probability: 0.8,
max_rounds: 100,
topology: Topology::FullyConnected,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct Lcg {
seed: u64,
}
impl Lcg {
fn new(seed: u64) -> Self {
Self { seed }
}
fn next_f64(&mut self) -> f64 {
let m: u64 = 1 << 31;
self.seed = (self.seed.wrapping_mul(1_103_515_245).wrapping_add(12345)) % m;
self.seed as f64 / m as f64
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PropagationState {
pub dialect: Dialect,
pub infected: BTreeSet<String>,
pub susceptible: BTreeSet<String>,
pub round: u32,
pub transmission_prob: f64,
}
impl PropagationState {
pub fn coverage(&self) -> f64 {
let total = self.infected.len() + self.susceptible.len();
if total == 0 {
1.0
} else {
self.infected.len() as f64 / total as f64
}
}
pub fn is_complete(&self) -> bool {
self.susceptible.is_empty()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipStats {
pub total_rounds: u64,
pub successful_propagations: u64,
pub failed_propagations: u64,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SimulationResult {
pub dialect_name: String,
pub rounds: u32,
pub final_coverage: f64,
pub successful: bool,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipNetwork {
agents: BTreeMap<String, BTreeSet<String>>,
topology: BTreeMap<String, BTreeSet<String>>,
active_propagations: BTreeMap<String, PropagationState>,
stats: GossipStats,
config: GossipConfig,
rng: Lcg,
}
impl GossipNetwork {
pub fn new(config: GossipConfig, seed: u64) -> Self {
Self {
agents: BTreeMap::new(),
topology: BTreeMap::new(),
active_propagations: BTreeMap::new(),
stats: GossipStats::default(),
config,
rng: Lcg::new(seed),
}
}
pub fn with_defaults() -> Self {
Self::new(GossipConfig::default(), 1_234_567)
}
pub fn add_agent(&mut self, agent_id: impl Into<String>) -> bool {
let id = agent_id.into();
if self.agents.contains_key(&id) {
return false;
}
self.agents.insert(id.clone(), BTreeSet::new());
self.topology.insert(id.clone(), BTreeSet::new());
self.update_topology(&id);
true
}
pub fn remove_agent(&mut self, agent_id: &str) -> bool {
if self.agents.remove(agent_id).is_none() {
return false;
}
self.topology.remove(agent_id);
for neighbors in self.topology.values_mut() {
neighbors.remove(agent_id);
}
for prop in self.active_propagations.values_mut() {
prop.infected.remove(agent_id);
prop.susceptible.remove(agent_id);
}
true
}
pub fn agent_ids(&self) -> Vec<&str> {
self.agents.keys().map(|s| s.as_str()).collect()
}
pub fn agent_count(&self) -> usize {
self.agents.len()
}
pub fn neighbors(&self, agent_id: &str) -> Option<Vec<&str>> {
self.topology
.get(agent_id)
.map(|s| s.iter().map(|n| n.as_str()).collect())
}
fn update_topology(&mut self, new_agent_id: &str) {
match &self.config.topology {
Topology::FullyConnected => {
let all_ids: Vec<String> = self.agents.keys().cloned().collect();
for id in &all_ids {
if id != new_agent_id {
self.topology
.get_mut(new_agent_id)
.unwrap()
.insert(id.clone());
self.topology
.get_mut(id.as_str())
.unwrap()
.insert(String::from(new_agent_id));
}
}
}
Topology::Ring => {
let all_ids: Vec<String> = self.agents.keys().cloned().collect();
if all_ids.len() > 1 {
let others: Vec<&String> = all_ids
.iter()
.filter(|id| id.as_str() != new_agent_id)
.collect();
let count = others.len().min(2);
for id in &others[..count] {
self.topology
.get_mut(new_agent_id)
.unwrap()
.insert((*id).clone());
self.topology
.get_mut(id.as_str())
.unwrap()
.insert(String::from(new_agent_id));
}
}
}
Topology::Random(p) => {
let p = *p;
let all_ids: Vec<String> = self.agents.keys().cloned().collect();
for id in &all_ids {
if id.as_str() != new_agent_id && self.rng.next_f64() < p {
self.topology
.get_mut(new_agent_id)
.unwrap()
.insert(id.clone());
self.topology
.get_mut(id.as_str())
.unwrap()
.insert(String::from(new_agent_id));
}
}
}
}
}
pub fn announce_dialect(&mut self, dialect: Dialect, initial_agent_id: &str) -> Option<String> {
if !self.agents.contains_key(initial_agent_id) {
return None;
}
if self.active_propagations.contains_key(&dialect.name) {
return None;
}
let dialect_name = dialect.name.clone();
self.agents
.get_mut(initial_agent_id)
.unwrap()
.insert(dialect_name.clone());
let mut infected = BTreeSet::new();
infected.insert(String::from(initial_agent_id));
let susceptible: BTreeSet<String> = self
.agents
.keys()
.filter(|id| id.as_str() != initial_agent_id)
.cloned()
.collect();
let transmission_prob = self.config.transmission_probability;
self.active_propagations.insert(
dialect_name.clone(),
PropagationState {
dialect,
infected,
susceptible,
round: 0,
transmission_prob,
},
);
#[cfg(feature = "tracing")]
tracing::event!(
tracing::Level::INFO,
dialect = %dialect_name,
initial_agent = initial_agent_id,
network_size = self.agents.len(),
"gossip_announce_dialect"
);
Some(dialect_name)
}
pub fn step(&mut self) -> Vec<(String, Vec<String>)> {
let dialect_names: Vec<String> = self.active_propagations.keys().cloned().collect();
let mut results = Vec::new();
for dialect_name in &dialect_names {
let newly_infected = self.step_propagation(dialect_name);
if !newly_infected.is_empty() {
results.push((dialect_name.clone(), newly_infected));
}
}
self.cleanup_completed_propagations();
self.stats.total_rounds += 1;
#[cfg(feature = "tracing")]
for (name, prop) in &self.active_propagations {
tracing::event!(
tracing::Level::INFO,
dialect = %name,
round = prop.round,
coverage = prop.coverage(),
infected = prop.infected.len(),
susceptible = prop.susceptible.len(),
"gossip_round_convergence"
);
}
results
}
fn step_propagation(&mut self, dialect_name: &str) -> Vec<String> {
let prop = match self.active_propagations.get(dialect_name) {
Some(p) => p,
None => return Vec::new(),
};
let infected: Vec<String> = prop.infected.iter().cloned().collect();
let transmission_prob = prop.transmission_prob;
let mut newly_infected = Vec::new();
for infected_id in &infected {
let neighbor_ids: Vec<String> = self
.topology
.get(infected_id.as_str())
.map(|s| s.iter().cloned().collect())
.unwrap_or_default();
let prop = self.active_propagations.get(dialect_name).unwrap();
for neighbor_id in &neighbor_ids {
if prop.susceptible.contains(neighbor_id) && self.rng.next_f64() < transmission_prob
{
newly_infected.push(neighbor_id.clone());
}
}
}
if let Some(prop) = self.active_propagations.get_mut(dialect_name) {
for id in &newly_infected {
prop.infected.insert(id.clone());
prop.susceptible.remove(id);
if let Some(agent_dialects) = self.agents.get_mut(id) {
agent_dialects.insert(dialect_name.to_string());
}
}
prop.round += 1;
}
newly_infected
}
fn cleanup_completed_propagations(&mut self) {
let max_rounds = self.config.max_rounds;
let to_remove: Vec<(String, bool)> = self
.active_propagations
.iter()
.filter_map(|(name, prop)| {
if prop.susceptible.is_empty() {
Some((name.clone(), true))
} else if prop.round >= max_rounds {
Some((name.clone(), false))
} else {
None
}
})
.collect();
for (name, success) in to_remove {
self.active_propagations.remove(&name);
if success {
self.stats.successful_propagations += 1;
} else {
self.stats.failed_propagations += 1;
}
}
}
pub fn receive_gossip(&mut self, agent_id: &str, dialect: &Dialect) -> Result<(), Vec<String>> {
let mut violations = Vec::new();
if !crate::r1::verify_r1_dialect(dialect) {
violations.extend(crate::r1::r1_violations(dialect));
}
if !crate::r2::verify_r2(dialect) {
violations.push(alloc::format!(
"R2 violation: invalid resource bounds in '{}'",
dialect.name
));
}
if !crate::r3::verify_r3(dialect) {
violations.extend(
crate::r3::r3_violations(dialect)
.into_iter()
.map(|v| alloc::format!("R3 violation: {}", v)),
);
}
if !violations.is_empty() {
return Err(violations);
}
if let Some(agent_dialects) = self.agents.get_mut(agent_id) {
agent_dialects.insert(dialect.name.clone());
}
if let Some(prop) = self.active_propagations.get_mut(&dialect.name) {
prop.susceptible.remove(agent_id);
prop.infected.insert(String::from(agent_id));
}
Ok(())
}
pub fn select_peers(&mut self, agent_id: &str, k: usize) -> Vec<String> {
let neighbors: Vec<String> = match self.topology.get(agent_id) {
Some(n) => n.iter().cloned().collect(),
None => return Vec::new(),
};
if neighbors.len() <= k {
return neighbors;
}
let mut shuffled = neighbors;
let len = shuffled.len();
for i in (1..len).rev() {
let r = self.rng.next_f64();
let j = (r * (i + 1) as f64) as usize;
let j = j.min(i); shuffled.swap(i, j);
}
shuffled.truncate(k);
shuffled
}
pub fn estimate_convergence_time(&self) -> u32 {
let n = self.agents.len();
if n <= 1 {
return 0;
}
let p = self.config.transmission_probability;
let rounds = ln_approx(n as f64) / ln_approx(1.0 + p);
rounds as u32 + 1
}
pub fn is_connected(&self) -> bool {
if self.agents.is_empty() {
return true;
}
let start = match self.agents.keys().next() {
Some(id) => id.clone(),
None => return true,
};
let mut visited = BTreeSet::new();
let mut queue = alloc::vec![start];
while let Some(current) = queue.pop() {
if !visited.insert(current.clone()) {
continue;
}
if let Some(neighbors) = self.topology.get(¤t) {
for neighbor in neighbors {
if !visited.contains(neighbor) {
queue.push(neighbor.clone());
}
}
}
}
visited.len() == self.agents.len()
}
pub fn simulate_propagation(
&mut self,
dialect: Dialect,
initial_agent_id: &str,
) -> Option<SimulationResult> {
let dialect_name = self.announce_dialect(dialect, initial_agent_id)?;
let mut rounds = 0u32;
while self.active_propagations.contains_key(&dialect_name) {
self.step();
rounds += 1;
}
let successful = self
.agents
.values()
.all(|dialects| dialects.contains(&dialect_name));
Some(SimulationResult {
dialect_name,
rounds,
final_coverage: if successful { 1.0 } else { 0.0 },
successful,
})
}
pub fn config(&self) -> &GossipConfig {
&self.config
}
pub fn stats(&self) -> &GossipStats {
&self.stats
}
pub fn active_propagations(&self) -> &BTreeMap<String, PropagationState> {
&self.active_propagations
}
pub fn propagation_stats(&self, dialect_name: &str) -> Option<&PropagationState> {
self.active_propagations.get(dialect_name)
}
pub fn agent_has_dialect(&self, agent_id: &str, dialect_name: &str) -> bool {
self.agents
.get(agent_id)
.map(|d| d.contains(dialect_name))
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dialect::{PerformativeDef, ResourceBounds};
use crate::sexpr::{Atom, SExpr};
use alloc::string::String;
fn test_dialect(name: &str) -> Dialect {
Dialect {
types: Default::default(),
roles: Vec::new(),
state: None,
state_bounds: None,
causal_locality: Default::default(),
name: String::from(name),
extends: Vec::new(),
author: None,
performatives: vec![PerformativeDef {
role: None,
name: alloc::format!("{}-action", name),
params: Vec::new(),
template: SExpr::List(alloc::vec![
SExpr::Atom(Atom::Symbol(String::from("effect"))),
SExpr::Atom(Atom::Symbol(alloc::format!("{}-effect", name))),
]),
}],
resources: ResourceBounds {
max_depth: 8,
max_expansion_size: 512,
},
examples: Vec::new(),
signature: None,
hash: None,
protocol: None,
causal_protocol: None,
shapes: Vec::new(),
}
}
#[test]
fn new_network_is_empty() {
let net = GossipNetwork::with_defaults();
assert_eq!(net.agent_count(), 0);
assert!(net.agent_ids().is_empty());
assert!(net.active_propagations().is_empty());
}
#[test]
fn default_config() {
let config = GossipConfig::default();
assert!((config.transmission_probability - 0.8).abs() < f64::EPSILON);
assert_eq!(config.max_rounds, 100);
assert_eq!(config.topology, Topology::FullyConnected);
}
#[test]
fn add_agent() {
let mut net = GossipNetwork::with_defaults();
assert!(net.add_agent("alice"));
assert!(net.add_agent("bob"));
assert!(!net.add_agent("alice")); assert_eq!(net.agent_count(), 2);
}
#[test]
fn remove_agent() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
net.add_agent("bob");
assert!(net.remove_agent("alice"));
assert!(!net.remove_agent("alice")); assert_eq!(net.agent_count(), 1);
}
#[test]
fn remove_agent_updates_topology() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
net.add_agent("bob");
net.add_agent("carol");
assert_eq!(net.neighbors("alice").unwrap().len(), 2);
net.remove_agent("bob");
let neighbors = net.neighbors("alice").unwrap();
assert_eq!(neighbors.len(), 1);
assert_eq!(neighbors[0], "carol");
}
#[test]
fn fully_connected_topology() {
let mut net = GossipNetwork::with_defaults();
for name in &["a", "b", "c", "d"] {
net.add_agent(*name);
}
for name in &["a", "b", "c", "d"] {
assert_eq!(net.neighbors(name).unwrap().len(), 3);
}
}
#[test]
fn ring_topology() {
let config = GossipConfig {
topology: Topology::Ring,
..GossipConfig::default()
};
let mut net = GossipNetwork::new(config, 42);
for name in &["a", "b", "c", "d", "e"] {
net.add_agent(*name);
}
for name in &["a", "b", "c", "d", "e"] {
let n = net.neighbors(name).unwrap().len();
assert!(n <= 4, "ring neighbor count {} > 4 for {}", n, name);
assert!(n >= 1, "ring neighbor count {} < 1 for {}", n, name);
}
}
#[test]
fn is_connected_empty_network() {
let net = GossipNetwork::with_defaults();
assert!(net.is_connected());
}
#[test]
fn is_connected_fully_connected() {
let mut net = GossipNetwork::with_defaults();
for name in &["a", "b", "c"] {
net.add_agent(*name);
}
assert!(net.is_connected());
}
#[test]
fn announce_dialect_creates_propagation() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
net.add_agent("bob");
let result = net.announce_dialect(test_dialect("test"), "alice");
assert_eq!(result, Some(String::from("test")));
assert_eq!(net.active_propagations().len(), 1);
let prop = net.propagation_stats("test").unwrap();
assert_eq!(prop.infected.len(), 1);
assert!(prop.infected.contains("alice"));
assert_eq!(prop.susceptible.len(), 1);
assert!(prop.susceptible.contains("bob"));
assert_eq!(prop.round, 0);
}
#[test]
fn announce_dialect_unknown_agent() {
let mut net = GossipNetwork::with_defaults();
let result = net.announce_dialect(test_dialect("test"), "nonexistent");
assert!(result.is_none());
}
#[test]
fn announce_dialect_duplicate() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
net.announce_dialect(test_dialect("test"), "alice");
let result = net.announce_dialect(test_dialect("test"), "alice");
assert!(result.is_none());
}
#[test]
fn step_propagates_dialect() {
let config = GossipConfig {
transmission_probability: 1.0, ..GossipConfig::default()
};
let mut net = GossipNetwork::new(config, 42);
for name in &["a", "b", "c"] {
net.add_agent(*name);
}
net.announce_dialect(test_dialect("test"), "a");
let results = net.step();
assert!(!results.is_empty());
assert!(net.active_propagations().is_empty()); assert_eq!(net.stats().successful_propagations, 1);
}
#[test]
fn step_respects_max_rounds() {
let config = GossipConfig {
transmission_probability: 0.0, max_rounds: 3,
..GossipConfig::default()
};
let mut net = GossipNetwork::new(config, 42);
net.add_agent("a");
net.add_agent("b");
net.announce_dialect(test_dialect("test"), "a");
for _ in 0..4 {
net.step();
}
assert!(net.active_propagations().is_empty());
assert_eq!(net.stats().failed_propagations, 1);
}
#[test]
fn estimate_convergence_single_agent() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
assert_eq!(net.estimate_convergence_time(), 0);
}
#[test]
fn estimate_convergence_log_n() {
let mut net = GossipNetwork::with_defaults();
for i in 0..100 {
net.add_agent(alloc::format!("agent-{}", i));
}
let estimate = net.estimate_convergence_time();
assert!(estimate > 0);
assert!(
estimate <= 10,
"estimate {} too high for 100 agents",
estimate
);
}
#[test]
fn estimate_convergence_empty_network() {
let net = GossipNetwork::with_defaults();
assert_eq!(net.estimate_convergence_time(), 0);
}
#[test]
fn receive_gossip_valid_dialect() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
let d = test_dialect("ext");
assert!(net.receive_gossip("alice", &d).is_ok());
assert!(net.agent_has_dialect("alice", "ext"));
}
#[test]
fn receive_gossip_rejects_r3_violation() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
let bad = Dialect {
types: Default::default(),
roles: Vec::new(),
state: None,
state_bounds: None,
causal_locality: Default::default(),
name: String::from("bad"),
extends: Vec::new(),
author: None,
performatives: vec![PerformativeDef {
role: None,
name: String::from("tell"), params: Vec::new(),
template: SExpr::Atom(Atom::Symbol(String::from("x"))),
}],
resources: ResourceBounds {
max_depth: 8,
max_expansion_size: 512,
},
examples: Vec::new(),
signature: None,
hash: None,
protocol: None,
causal_protocol: None,
shapes: Vec::new(),
};
let result = net.receive_gossip("alice", &bad);
assert!(result.is_err());
assert!(!net.agent_has_dialect("alice", "bad"));
}
#[test]
fn receive_gossip_rejects_r2_violation() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("alice");
let bad = Dialect {
types: Default::default(),
roles: Vec::new(),
state: None,
state_bounds: None,
causal_locality: Default::default(),
name: String::from("bad-bounds"),
extends: Vec::new(),
author: None,
performatives: Vec::new(),
resources: ResourceBounds {
max_depth: 100, max_expansion_size: 512,
},
examples: Vec::new(),
signature: None,
hash: None,
protocol: None,
causal_protocol: None,
shapes: Vec::new(),
};
let result = net.receive_gossip("alice", &bad);
assert!(result.is_err());
}
#[test]
fn select_peers_returns_subset() {
let mut net = GossipNetwork::with_defaults();
for name in &["a", "b", "c", "d", "e"] {
net.add_agent(*name);
}
let peers = net.select_peers("a", 2);
assert_eq!(peers.len(), 2);
let neighbors = net.neighbors("a").unwrap();
for peer in &peers {
assert!(neighbors.contains(&peer.as_str()));
}
}
#[test]
fn select_peers_all_when_k_large() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("a");
net.add_agent("b");
let peers = net.select_peers("a", 10);
assert_eq!(peers.len(), 1); }
#[test]
fn select_peers_unknown_agent() {
let mut net = GossipNetwork::with_defaults();
let peers = net.select_peers("nonexistent", 3);
assert!(peers.is_empty());
}
#[test]
fn simulate_propagation_converges() {
let config = GossipConfig {
transmission_probability: 1.0,
..GossipConfig::default()
};
let mut net = GossipNetwork::new(config, 42);
for i in 0..10 {
net.add_agent(alloc::format!("agent-{}", i));
}
let result = net
.simulate_propagation(test_dialect("test"), "agent-0")
.unwrap();
assert!(result.successful);
assert!((result.final_coverage - 1.0).abs() < f64::EPSILON);
assert_eq!(result.rounds, 1);
}
#[test]
fn simulate_propagation_with_realistic_probability() {
let mut net = GossipNetwork::new(GossipConfig::default(), 42);
for i in 0..20 {
net.add_agent(alloc::format!("agent-{}", i));
}
let result = net
.simulate_propagation(test_dialect("epidemic"), "agent-0")
.unwrap();
assert!(result.successful);
assert!(
result.rounds < 20,
"took {} rounds for 20 agents",
result.rounds
);
}
#[test]
fn simulate_propagation_unknown_agent() {
let mut net = GossipNetwork::with_defaults();
let result = net.simulate_propagation(test_dialect("test"), "nonexistent");
assert!(result.is_none());
}
#[test]
fn propagation_coverage() {
let mut state = PropagationState {
dialect: test_dialect("test"),
infected: BTreeSet::new(),
susceptible: BTreeSet::new(),
round: 0,
transmission_prob: 0.8,
};
assert!((state.coverage() - 1.0).abs() < f64::EPSILON);
assert!(state.is_complete());
state.infected.insert(String::from("a"));
state.susceptible.insert(String::from("b"));
state.susceptible.insert(String::from("c"));
assert!((state.coverage() - 1.0 / 3.0).abs() < 0.01);
assert!(!state.is_complete());
}
#[test]
fn deterministic_with_same_seed() {
let run = |seed: u64| -> SimulationResult {
let mut net = GossipNetwork::new(GossipConfig::default(), seed);
for i in 0..15 {
net.add_agent(alloc::format!("agent-{}", i));
}
net.simulate_propagation(test_dialect("det-test"), "agent-0")
.unwrap()
};
let r1 = run(9999);
let r2 = run(9999);
assert_eq!(r1.rounds, r2.rounds);
assert_eq!(r1.successful, r2.successful);
}
#[test]
fn stats_track_success_and_failure() {
let config = GossipConfig {
transmission_probability: 1.0,
max_rounds: 100,
..GossipConfig::default()
};
let mut net = GossipNetwork::new(config, 42);
net.add_agent("a");
net.add_agent("b");
net.simulate_propagation(test_dialect("good"), "a");
assert_eq!(net.stats().successful_propagations, 1);
let config2 = GossipConfig {
transmission_probability: 0.0,
max_rounds: 2,
..GossipConfig::default()
};
let mut net2 = GossipNetwork::new(config2, 42);
net2.add_agent("a");
net2.add_agent("b");
net2.simulate_propagation(test_dialect("fail"), "a");
assert_eq!(net2.stats().failed_propagations, 1);
}
#[test]
fn lcg_produces_values_in_range() {
let mut rng = Lcg::new(12345);
for _ in 0..1000 {
let v = rng.next_f64();
assert!((0.0..1.0).contains(&v), "LCG value {} out of range", v);
}
}
#[test]
fn lcg_deterministic() {
let mut rng1 = Lcg::new(42);
let mut rng2 = Lcg::new(42);
for _ in 0..100 {
assert_eq!(rng1.next_f64().to_bits(), rng2.next_f64().to_bits());
}
}
#[test]
fn remove_agent_during_propagation() {
let mut net = GossipNetwork::with_defaults();
net.add_agent("a");
net.add_agent("b");
net.add_agent("c");
net.announce_dialect(test_dialect("test"), "a");
net.remove_agent("b");
let prop = net.propagation_stats("test").unwrap();
assert!(!prop.susceptible.contains("b"));
assert_eq!(prop.susceptible.len(), 1);
}
}