use dyntri_cdt2d::monte_carlo::fixed_volume::Simulation;
use rand::SeedableRng;
use rand_xoshiro::Xoshiro128StarStar;
use test_log::test;

fn generate_cdt() -> Simulation {
    let mut rng = Xoshiro128StarStar::seed_from_u64(42);
    let mut simulation = Simulation::default(1000, 10);
    for _ in 0..10_000 {
        simulation.step(&mut rng);
    }
    simulation
}

#[test]
fn test_simulation() {
    generate_cdt();
}

macro_rules! test_ok {
    ($res:expr) => {
        $res.unwrap_or_else(|e| panic!("Test failed with error: {e}"))
    };
}

#[test]
fn test_triangulation_construction_flat() {
    let simulation = Simulation::default(1000, 10);
    let triangulation = simulation.construct_triangulation();
    assert_eq!(simulation.ntime(), triangulation.ntime);
    assert_eq!(simulation.ntriangles(), triangulation.num_simplices());

    test_ok!(triangulation.check_biconnectivity());
    test_ok!(triangulation.check_vertex_consistency_ordered());
    test_ok!(triangulation.check_simplex_counts());
    test_ok!(triangulation.check_neighbour_absolute_ordering());
    test_ok!(triangulation.check_simplex_kinds());
}

#[test]
fn test_triangulation_construction() {
    let simulation = generate_cdt();
    let triangulation = simulation.construct_triangulation();
    assert_eq!(simulation.ntime(), triangulation.ntime);
    assert_eq!(simulation.ntriangles(), triangulation.num_simplices());

    test_ok!(triangulation.check_biconnectivity());
    test_ok!(triangulation.check_vertex_consistency_ordered());
    test_ok!(triangulation.check_simplex_counts());
    test_ok!(triangulation.check_neighbour_absolute_ordering());
    test_ok!(triangulation.check_simplex_kinds());
}
