#[cfg(test)]
mod tests {
use causal_hub::{
datasets::{MissingMechanism, MissingType},
labels, map,
models::{DiGraph, Graph},
random::{Random, RngMissingMechanism},
set,
types::{Error, ErrorKind, Result},
};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256PlusPlus;
#[test]
fn new() {
let labels = labels!("X", "Y");
let pr = map![(0, set![1])];
let mechanism = MissingMechanism::new(labels, pr);
assert!(mechanism.is_ok());
}
#[test]
fn new_out_of_bounds_key() {
let labels = labels!("X", "Y");
let pr = map![(2, set![1])];
let mechanism = MissingMechanism::new(labels, pr);
assert!(matches!(
mechanism,
Err(Error {
kind: ErrorKind::IndexOutOfBounds(2),
..
})
));
}
#[test]
fn new_out_of_bounds_value() {
let labels = labels!("X", "Y");
let pr = map![(0, set![2])];
let mechanism = MissingMechanism::new(labels, pr);
assert!(matches!(
mechanism,
Err(Error {
kind: ErrorKind::IndexOutOfBounds(2),
..
})
));
}
#[test]
fn random_mcar() -> Result<()> {
let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
let labels = labels!["X", "Y", "Z"];
let mut graph = DiGraph::empty(labels)?;
graph.add_edge(0, 1)?;
graph.add_edge(2, 1)?;
let mut sampler = RngMissingMechanism::new(&mut rng, &graph, MissingType::MCAR, 0.5)?;
let pr = sampler.random()?;
assert_eq!(pr.len(), 2);
for v in pr.values() {
assert!(v.is_empty());
}
Ok(())
}
#[test]
fn random_mar() -> Result<()> {
let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
let labels = labels!["X", "Y", "Z"];
let mut graph = DiGraph::empty(labels)?;
graph.add_edge(0, 1)?; graph.add_edge(2, 1)?;
let mut sampler = RngMissingMechanism::new(&mut rng, &graph, MissingType::MAR, 0.5)?;
let pr = sampler.random()?;
assert_eq!(pr.len(), 2);
for (x, pa) in pr {
assert!(!pa.is_empty());
for z in pa {
assert_ne!(x, z);
}
}
Ok(())
}
#[test]
fn random_mnar() -> Result<()> {
let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
let labels = labels!["X", "Y", "Z"];
let mut graph = DiGraph::empty(labels)?;
graph.add_edge(0, 1)?;
graph.add_edge(2, 1)?;
let mut sampler = RngMissingMechanism::new(&mut rng, &graph, MissingType::MNAR, 0.5)?;
let pr = sampler.random()?;
assert_eq!(pr.len(), 2);
Ok(())
}
}