use approx::abs_diff_eq;
use boostvoronoi::prelude::*;

type I = i32;

pub fn almost_equal(x1: f64, x2: f64, y1: f64, y2: f64) -> bool {
    let epsilon = 0.001;
    abs_diff_eq!(x1, x2, epsilon = epsilon) && abs_diff_eq!(y1, y2, epsilon = epsilon)
}

#[test]
fn single_point_1() {
    let output = {
        let _v = [Point { x: 10, y: 11 }];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("single_point_1")
            .build()
            .expect("single_point_1")
    };

    assert_eq!(output.cells().len(), 1);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 0);
}

#[test]
fn two_points_1() -> Result<(), BvError> {
    let output = {
        let _v = [Point { x: 10, y: 11 }, Point { x: 1, y: 3 }];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("two_points_1")
            .build()
            .expect("two_points_1")
    };
    assert_eq!(output.cells().len(), 2);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 0);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 2);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 0);
    assert_eq!(output.edges().first().unwrap().prev()?, 0);
    let e = output.edges()[0];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);

    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 1);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 1);
    let e = output.edges()[1];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    Ok(())
}

#[test]
/// same as test two_points_2 but reversed order
fn two_points_2() -> Result<(), BvError> {
    let output = {
        let _v = [Point { x: 1, y: 3 }, Point { x: 10, y: 11 }];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("two_points_1")
            .build()
            .expect("two_points_1")
    };
    assert_eq!(output.cells().len(), 2);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 0);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 1);
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 2);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 0);
    assert_eq!(output.edges().first().unwrap().prev()?, 0);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 1);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 1);
    Ok(())
}

#[test]
fn two_points_3() -> Result<(), BvError> {
    let output = {
        let _v = [Point { x: 45, y: 1 }, Point { x: 8, y: 23 }];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("two_points_2")
            .build()
            .expect("two_points_2")
    };
    assert_eq!(output.cells().len(), 2);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 2);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 0);
    assert_eq!(output.edges().first().unwrap().prev()?, 0);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 1);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 1);
    Ok(())
}

#[test]
fn three_points_1() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 11 },
            Point { x: 1, y: 3 },
            Point { x: 5, y: 4 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("three_points_1")
            .build()
            .expect("three_points_1")
    };
    assert_eq!(output.cells().len(), 3);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 1);
    assert_eq!(output.edges().len(), 6);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 0.804348, v.y(), 12.2826));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 5);
    assert_eq!(output.edges().first().unwrap().prev()?, 5);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(1).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 2);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 2);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 1);
    assert!(output.edges().get(2).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 1);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(3).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 4);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 4);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 2);
    assert!(output.edges().get(4).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 3);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 3);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 0);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 0);
    Ok(())
}

#[test]
/// 3 co-linear sites (on x)
fn three_points_2() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 1, y: 8 },
            Point { x: 1, y: 3 },
            Point { x: 1, y: 2 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("three_points_2")
            .build()
            .expect("three_points_2")
    };
    assert_eq!(output.cells().len(), 3);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 4);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 0);
    assert_eq!(output.edges().first().unwrap().prev()?, 0);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 2);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 2);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 1);
    assert!(output.edges().get(2).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 1);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 3);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 3);
    Ok(())
}

/// 3 co-linear sites
#[test]
fn three_points_3() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 19, y: 8 },
            Point { x: 19, y: 1 },
            Point { x: 19, y: 20 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("three_points_3")
            .build()
            .expect("three_points_3")
    };
    assert_eq!(output.cells().len(), 3);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 0);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 2);
    assert_eq!(output.vertices().len(), 0);
    assert_eq!(output.edges().len(), 4);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 0);
    assert_eq!(output.edges().first().unwrap().prev()?, 0);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 2);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 2);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 1);
    assert!(output.edges().get(2).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 1);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 3);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 3);
    Ok(())
}

/// 2+1 co-linear sites
#[test]
fn three_points_4() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 10 },
            Point { x: 1, y: 1 },
            Point { x: 1, y: 6 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("three_points_4")
            .build()
            .expect("three_points_4")
    };
    assert_eq!(output.cells().len(), 3);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 1);
    assert_eq!(output.edges().len(), 6);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 7.5, v.y(), 3.5));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert_eq!(output.edges().first().unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 4);
    assert_eq!(output.edges().first().unwrap().prev()?, 4);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 2);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 2);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 1);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 5);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 5);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 0);
    assert!(output.edges().get(4).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 0);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 0);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 3);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 3);
    Ok(())
}

#[test]
fn four_points_1() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 8 },
            Point { x: 1, y: 3 },
            Point { x: 4, y: 2 },
            Point { x: 5, y: 6 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("four_points_1")
            .build()
            .expect("four_points_1")
    };
    assert_eq!(output.cells().len(), 4);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 3);
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    assert_eq!(cell.source_index(), 0);
    assert_eq!(output.vertices().len(), 2);
    assert_eq!(output.edges().len(), 10);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 3.11538, v.y(), 4.34615));
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 9.16667, v.y(), 2.83333));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 2);
    assert_eq!(output.edges().first().unwrap().prev()?, 2);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(1).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 8);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 4);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 0);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 0);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 5);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 6);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(4).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 1);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 8);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 6);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 3);
    assert_eq!(output.edges().get(6).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(6).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(6).unwrap().twin()?, 7);
    assert_eq!(output.edges().get(6).unwrap().next()?, 3);
    assert_eq!(output.edges().get(6).unwrap().prev()?, 5);
    assert_eq!(output.edges().get(7).unwrap().cell()?, 3);
    assert!(output.edges().get(7).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(7).unwrap().twin()?, 6);
    assert_eq!(output.edges().get(7).unwrap().next()?, 9);
    assert_eq!(output.edges().get(7).unwrap().prev()?, 9);
    assert_eq!(output.edges().get(8).unwrap().cell()?, 1);
    assert!(output.edges().get(8).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(8).unwrap().twin()?, 9);
    assert_eq!(output.edges().get(8).unwrap().next()?, 4);
    assert_eq!(output.edges().get(8).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(9).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(9).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(9).unwrap().twin()?, 8);
    assert_eq!(output.edges().get(9).unwrap().next()?, 7);
    assert_eq!(output.edges().get(9).unwrap().prev()?, 7);
    Ok(())
}

#[test]
fn four_points_2() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 18 },
            Point { x: 12, y: 3 },
            Point { x: 4, y: 21 },
            Point { x: 8, y: 62 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("four_points_2")
            .build()
            .expect("four_points_2")
    };
    assert_eq!(output.cells().len(), 4);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 3);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 0);
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    assert_eq!(cell.source_index(), 1);
    assert_eq!(output.vertices().len(), 3);
    assert_eq!(output.edges().len(), 12);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 1.89286, v.y(), 9.28571));
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 17.4419, v.y(), 40.3837));
    let v = output.vertices()[2];
    assert!(almost_equal(v.x(), 347.724, v.y(), 55.3966));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert_eq!(output.edges().first().unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 4);
    assert_eq!(output.edges().first().unwrap().prev()?, 2);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 9);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 11);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 0);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 4);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(3).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 7);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 8);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 0);
    assert!(output.edges().get(4).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 2);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 0);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 10);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 6);
    assert_eq!(output.edges().get(6).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(6).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(6).unwrap().twin()?, 7);
    assert_eq!(output.edges().get(6).unwrap().next()?, 5);
    assert_eq!(output.edges().get(6).unwrap().prev()?, 10);
    assert_eq!(output.edges().get(7).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(7).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(7).unwrap().twin()?, 6);
    assert_eq!(output.edges().get(7).unwrap().next()?, 8);
    assert_eq!(output.edges().get(7).unwrap().prev()?, 3);
    assert_eq!(output.edges().get(8).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(8).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(8).unwrap().twin()?, 9);
    assert_eq!(output.edges().get(8).unwrap().next()?, 3);
    assert_eq!(output.edges().get(8).unwrap().prev()?, 7);
    assert_eq!(output.edges().get(9).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(9).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(9).unwrap().twin()?, 8);
    assert_eq!(output.edges().get(9).unwrap().next()?, 11);
    assert_eq!(output.edges().get(9).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(10).unwrap().cell()?, 3);
    assert!(output.edges().get(10).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(10).unwrap().twin()?, 11);
    assert_eq!(output.edges().get(10).unwrap().next()?, 6);
    assert_eq!(output.edges().get(10).unwrap().prev()?, 5);
    assert_eq!(output.edges().get(11).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(11).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(11).unwrap().twin()?, 10);
    assert_eq!(output.edges().get(11).unwrap().next()?, 1);
    assert_eq!(output.edges().get(11).unwrap().prev()?, 9);
    Ok(())
}

#[test]
fn four_points_3() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 18 },
            Point { x: 12, y: 3 },
            Point { x: 4, y: 21 },
            Point { x: 8, y: 3 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("four_points_3")
            .build()
            .expect("four_points_3")
    };
    assert_eq!(output.cells().len(), 4);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 3);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 0);
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    assert_eq!(cell.source_index(), 1);
    assert_eq!(output.vertices().len(), 2);
    assert_eq!(output.edges().len(), 10);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 2.90625, v.y(), 11.3125));
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 10.0, v.y(), 10.3667));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 2);
    assert_eq!(output.edges().first().unwrap().prev()?, 2);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(1).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 4);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 6);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 0);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 0);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 7);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 9);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 1);
    assert!(output.edges().get(4).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 6);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 8);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 8);
    assert_eq!(output.edges().get(6).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(6).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(6).unwrap().twin()?, 7);
    assert_eq!(output.edges().get(6).unwrap().next()?, 1);
    assert_eq!(output.edges().get(6).unwrap().prev()?, 4);
    assert_eq!(output.edges().get(7).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(7).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(7).unwrap().twin()?, 6);
    assert_eq!(output.edges().get(7).unwrap().next()?, 9);
    assert_eq!(output.edges().get(7).unwrap().prev()?, 3);
    assert_eq!(output.edges().get(8).unwrap().cell()?, 3);
    assert!(output.edges().get(8).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(8).unwrap().twin()?, 9);
    assert_eq!(output.edges().get(8).unwrap().next()?, 5);
    assert_eq!(output.edges().get(8).unwrap().prev()?, 5);
    assert_eq!(output.edges().get(9).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(9).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(9).unwrap().twin()?, 8);
    assert_eq!(output.edges().get(9).unwrap().next()?, 3);
    assert_eq!(output.edges().get(9).unwrap().prev()?, 7);
    Ok(())
}

#[test]
fn five_points_1() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 8, y: 9 },
            Point { x: 2, y: 14 },
            Point { x: 1, y: 15 },
            Point { x: 4, y: 16 },
            Point { x: 9, y: 8 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("five_points_1")
            .build()
            .expect("five_points_1")
    };
    assert_eq!(output.cells().len(), 5);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 2);
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 1);
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 3);
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    assert_eq!(cell.source_index(), 0);
    let cell = output.cells()[4];
    assert_eq!(cell.id(), 4);
    assert_eq!(cell.source_index(), 4);
    assert_eq!(output.vertices().len(), 4);
    assert_eq!(output.edges().len(), 16);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 2.5, v.y(), 15.5));
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 5.68182, v.y(), 12.3182));
    let v = output.vertices()[2];
    assert!(almost_equal(v.x(), -27.5, v.y(), -27.5));
    let v = output.vertices()[3];
    assert!(almost_equal(v.x(), 21.1667, v.y(), 21.1667));
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert!(output.edges().first().unwrap().vertex0().is_none());
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 2);
    assert_eq!(output.edges().first().unwrap().prev()?, 2);
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(1).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 12);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 4);
    assert_eq!(output.edges().get(2).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 0);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 0);
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert!(output.edges().get(3).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 5);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 15);
    assert_eq!(output.edges().get(4).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(4).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 1);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 6);
    assert_eq!(output.edges().get(5).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 11);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 3);
    assert_eq!(output.edges().get(6).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(6).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(6).unwrap().twin()?, 7);
    assert_eq!(output.edges().get(6).unwrap().next()?, 4);
    assert_eq!(output.edges().get(6).unwrap().prev()?, 12);
    assert_eq!(output.edges().get(7).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(7).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(7).unwrap().twin()?, 6);
    assert_eq!(output.edges().get(7).unwrap().next()?, 8);
    assert_eq!(output.edges().get(7).unwrap().prev()?, 10);
    assert_eq!(output.edges().get(8).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(8).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(8).unwrap().twin()?, 9);
    assert_eq!(output.edges().get(8).unwrap().next()?, 10);
    assert_eq!(output.edges().get(8).unwrap().prev()?, 7);
    assert_eq!(output.edges().get(9).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(9).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(9).unwrap().twin()?, 8);
    assert_eq!(output.edges().get(9).unwrap().next()?, 13);
    assert_eq!(output.edges().get(9).unwrap().prev()?, 14);
    assert_eq!(output.edges().get(10).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(10).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(10).unwrap().twin()?, 11);
    assert_eq!(output.edges().get(10).unwrap().next()?, 7);
    assert_eq!(output.edges().get(10).unwrap().prev()?, 8);
    assert_eq!(output.edges().get(11).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(11).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(11).unwrap().twin()?, 10);
    assert_eq!(output.edges().get(11).unwrap().next()?, 15);
    assert_eq!(output.edges().get(11).unwrap().prev()?, 5);
    assert_eq!(output.edges().get(12).unwrap().cell()?, 1);
    assert!(output.edges().get(12).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(12).unwrap().twin()?, 13);
    assert_eq!(output.edges().get(12).unwrap().next()?, 6);
    assert_eq!(output.edges().get(12).unwrap().prev()?, 1);
    assert_eq!(output.edges().get(13).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(13).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(13).unwrap().twin()?, 12);
    assert_eq!(output.edges().get(13).unwrap().next()?, 14);
    assert_eq!(output.edges().get(13).unwrap().prev()?, 9);
    assert_eq!(output.edges().get(14).unwrap().cell()?, 4);
    assert!(output.edges().get(14).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(14).unwrap().twin()?, 15);
    assert_eq!(output.edges().get(14).unwrap().next()?, 9);
    assert_eq!(output.edges().get(14).unwrap().prev()?, 13);
    assert_eq!(output.edges().get(15).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(15).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(15).unwrap().twin()?, 14);
    assert_eq!(output.edges().get(15).unwrap().next()?, 3);
    assert_eq!(output.edges().get(15).unwrap().prev()?, 11);
    Ok(())
}

#[test]
fn eighth_points_1() -> Result<(), BvError> {
    let output = {
        let _v = [
            Point { x: 10, y: 16 },
            Point { x: 12, y: 3 },
            Point { x: 4, y: 12 },
            Point { x: 8, y: 10 },
            Point { x: 7, y: 18 },
            Point { x: 8, y: 9 },
            Point { x: 9, y: 8 },
            Point { x: 11, y: 11 },
        ];
        Builder::<I>::default()
            .with_vertices(_v.iter())
            .expect("eighth_points_1")
            .build()
            .expect("eighth_points_1")
    };
    assert_eq!(output.cells().len(), 8);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    assert_eq!(cell.source_index(), 2);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[1];
    assert_eq!(cell.id(), 1);
    assert_eq!(cell.source_index(), 4);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    assert_eq!(cell.source_index(), 5);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    assert_eq!(cell.source_index(), 3);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[4];
    assert_eq!(cell.id(), 4);
    assert_eq!(cell.source_index(), 6);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[5];
    assert_eq!(cell.id(), 5);
    assert_eq!(cell.source_index(), 0);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[6];
    assert_eq!(cell.id(), 6);
    assert_eq!(cell.source_index(), 7);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[7];
    assert_eq!(cell.id(), 7);
    assert_eq!(cell.source_index(), 1);
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    assert_eq!(output.vertices().len(), 9);
    assert_eq!(output.edges().len(), 32);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), 5.25, v.y(), 9.5));
    assert_eq!(v.get_incident_edge()?, 7);
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 6.75, v.y(), 14.375));
    assert_eq!(v.get_incident_edge()?, 13);
    let v = output.vertices()[2];
    assert!(almost_equal(v.x(), 7.28571, v.y(), 13.5714));
    assert_eq!(v.get_incident_edge()?, 15);
    let v = output.vertices()[3];
    assert!(almost_equal(v.x(), 9.5, v.y(), 9.5));
    assert_eq!(v.get_incident_edge()?, 19);
    let v = output.vertices()[4];
    assert!(almost_equal(v.x(), 9.78571, v.y(), 9.64286));
    assert_eq!(v.get_incident_edge()?, 21);
    let v = output.vertices()[5];
    assert!(almost_equal(v.x(), 8.625, v.y(), 13.125));
    assert_eq!(v.get_incident_edge()?, 23);
    let v = output.vertices()[6];
    assert!(almost_equal(v.x(), -2.0, v.y(), -2.0));
    assert_eq!(v.get_incident_edge()?, 27);
    let v = output.vertices()[7];
    assert!(almost_equal(v.x(), -4.75, v.y(), -3.83333));
    assert_eq!(v.get_incident_edge()?, 29);
    let v = output.vertices()[8];
    assert!(almost_equal(v.x(), 13.3947, v.y(), 7.23684));
    assert_eq!(v.get_incident_edge()?, 31);
    assert_eq!(output.edges().first().unwrap().cell()?, 0);
    assert_eq!(output.edges().first().unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().first().unwrap().twin()?, 1);
    assert_eq!(output.edges().first().unwrap().next()?, 28);
    assert_eq!(output.edges().first().unwrap().prev()?, 12);
    let e = output.edges()[0];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 13);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(1).unwrap().cell()?, 1);
    assert!(output.edges().get(1).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(1).unwrap().twin()?, 0);
    assert_eq!(output.edges().get(1).unwrap().next()?, 10);
    assert_eq!(output.edges().get(1).unwrap().prev()?, 10);
    let e = output.edges()[1];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 1);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 11);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(2).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(2).unwrap().vertex0().unwrap(), 7);
    assert_eq!(output.edges().get(2).unwrap().twin()?, 3);
    assert_eq!(output.edges().get(2).unwrap().next()?, 4);
    assert_eq!(output.edges().get(2).unwrap().prev()?, 28);
    let e = output.edges()[2];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 0);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 29);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(3).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(3).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(3).unwrap().twin()?, 2);
    assert_eq!(output.edges().get(3).unwrap().next()?, 26);
    assert_eq!(output.edges().get(3).unwrap().prev()?, 6);
    let e = output.edges()[3];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 7);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 7);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(4).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(4).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(4).unwrap().twin()?, 5);
    assert_eq!(output.edges().get(4).unwrap().next()?, 12);
    assert_eq!(output.edges().get(4).unwrap().prev()?, 2);
    let e = output.edges()[4];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 2);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 3);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(5).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(5).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(5).unwrap().twin()?, 4);
    assert_eq!(output.edges().get(5).unwrap().next()?, 7);
    assert_eq!(output.edges().get(5).unwrap().prev()?, 14);
    let e = output.edges()[5];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 0);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 15);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(6).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(6).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(6).unwrap().twin()?, 7);
    assert_eq!(output.edges().get(6).unwrap().next()?, 3);
    assert_eq!(output.edges().get(6).unwrap().prev()?, 8);
    let e = output.edges()[6];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 0);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 9);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(7).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(7).unwrap().vertex0().unwrap(), 0);
    assert_eq!(output.edges().get(7).unwrap().twin()?, 6);
    assert_eq!(output.edges().get(7).unwrap().next()?, 19);
    assert_eq!(output.edges().get(7).unwrap().prev()?, 5);
    let e = output.edges()[7];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 3);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 4);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(8).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(8).unwrap().vertex0().unwrap(), 6);
    assert_eq!(output.edges().get(8).unwrap().twin()?, 9);
    assert_eq!(output.edges().get(8).unwrap().next()?, 6);
    assert_eq!(output.edges().get(8).unwrap().prev()?, 26);
    let e = output.edges()[8];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 3);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 27);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(9).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(9).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(9).unwrap().twin()?, 8);
    assert_eq!(output.edges().get(9).unwrap().next()?, 24);
    assert_eq!(output.edges().get(9).unwrap().prev()?, 18);
    let e = output.edges()[9];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 6);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 19);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(10).unwrap().cell()?, 1);
    assert_eq!(output.edges().get(10).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(10).unwrap().twin()?, 11);
    assert_eq!(output.edges().get(10).unwrap().next()?, 1);
    assert_eq!(output.edges().get(10).unwrap().prev()?, 1);
    let e = output.edges()[10];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 0);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(11).unwrap().cell()?, 5);
    assert!(output.edges().get(11).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(11).unwrap().twin()?, 10);
    assert_eq!(output.edges().get(11).unwrap().next()?, 13);
    assert_eq!(output.edges().get(11).unwrap().prev()?, 23);
    let e = output.edges()[11];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 1);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 22);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(12).unwrap().cell()?, 0);
    assert_eq!(output.edges().get(12).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(12).unwrap().twin()?, 13);
    assert_eq!(output.edges().get(12).unwrap().next()?, 0);
    assert_eq!(output.edges().get(12).unwrap().prev()?, 4);
    let e = output.edges()[12];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 1);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 5);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(13).unwrap().cell()?, 5);
    assert_eq!(output.edges().get(13).unwrap().vertex0().unwrap(), 1);
    assert_eq!(output.edges().get(13).unwrap().twin()?, 12);
    assert_eq!(output.edges().get(13).unwrap().next()?, 15);
    assert_eq!(output.edges().get(13).unwrap().prev()?, 11);
    let e = output.edges()[13];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 2);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 10);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(14).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(14).unwrap().vertex0().unwrap(), 5);
    assert_eq!(output.edges().get(14).unwrap().twin()?, 15);
    assert_eq!(output.edges().get(14).unwrap().next()?, 5);
    assert_eq!(output.edges().get(14).unwrap().prev()?, 16);
    let e = output.edges()[14];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 2);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 17);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(15).unwrap().cell()?, 5);
    assert_eq!(output.edges().get(15).unwrap().vertex0().unwrap(), 2);
    assert_eq!(output.edges().get(15).unwrap().twin()?, 14);
    assert_eq!(output.edges().get(15).unwrap().next()?, 23);
    assert_eq!(output.edges().get(15).unwrap().prev()?, 13);
    let e = output.edges()[15];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 5);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 12);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(16).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(16).unwrap().vertex0().unwrap(), 4);
    assert_eq!(output.edges().get(16).unwrap().twin()?, 17);
    assert_eq!(output.edges().get(16).unwrap().next()?, 14);
    assert_eq!(output.edges().get(16).unwrap().prev()?, 19);
    let e = output.edges()[16];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 5);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 18);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(17).unwrap().cell()?, 6);
    assert_eq!(output.edges().get(17).unwrap().vertex0().unwrap(), 5);
    assert_eq!(output.edges().get(17).unwrap().twin()?, 16);
    assert_eq!(output.edges().get(17).unwrap().next()?, 21);
    assert_eq!(output.edges().get(17).unwrap().prev()?, 22);
    let e = output.edges()[17];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 4);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 23);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(18).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(18).unwrap().vertex0().unwrap(), 4);
    assert_eq!(output.edges().get(18).unwrap().twin()?, 19);
    assert_eq!(output.edges().get(18).unwrap().next()?, 9);
    assert_eq!(output.edges().get(18).unwrap().prev()?, 20);
    let e = output.edges()[18];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 3);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 21);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(19).unwrap().cell()?, 3);
    assert_eq!(output.edges().get(19).unwrap().vertex0().unwrap(), 3);
    assert_eq!(output.edges().get(19).unwrap().twin()?, 18);
    assert_eq!(output.edges().get(19).unwrap().next()?, 16);
    assert_eq!(output.edges().get(19).unwrap().prev()?, 7);
    let e = output.edges()[19];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 4);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 6);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(20).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(20).unwrap().vertex0().unwrap(), 8);
    assert_eq!(output.edges().get(20).unwrap().twin()?, 21);
    assert_eq!(output.edges().get(20).unwrap().next()?, 18);
    assert_eq!(output.edges().get(20).unwrap().prev()?, 24);
    let e = output.edges()[20];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 4);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 25);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(21).unwrap().cell()?, 6);
    assert_eq!(output.edges().get(21).unwrap().vertex0().unwrap(), 4);
    assert_eq!(output.edges().get(21).unwrap().twin()?, 20);
    assert_eq!(output.edges().get(21).unwrap().next()?, 31);
    assert_eq!(output.edges().get(21).unwrap().prev()?, 17);
    let e = output.edges()[21];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 8);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 16);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(22).unwrap().cell()?, 6);
    assert!(output.edges().get(22).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(22).unwrap().twin()?, 23);
    assert_eq!(output.edges().get(22).unwrap().next()?, 17);
    assert_eq!(output.edges().get(22).unwrap().prev()?, 31);
    let e = output.edges()[22];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 5);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 30);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(23).unwrap().cell()?, 5);
    assert_eq!(output.edges().get(23).unwrap().vertex0().unwrap(), 5);
    assert_eq!(output.edges().get(23).unwrap().twin()?, 22);
    assert_eq!(output.edges().get(23).unwrap().next()?, 11);
    assert_eq!(output.edges().get(23).unwrap().prev()?, 15);
    let e = output.edges()[23];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 14);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(24).unwrap().cell()?, 4);
    assert_eq!(output.edges().get(24).unwrap().vertex0().unwrap(), 6);
    assert_eq!(output.edges().get(24).unwrap().twin()?, 25);
    assert_eq!(output.edges().get(24).unwrap().next()?, 20);
    assert_eq!(output.edges().get(24).unwrap().prev()?, 9);
    let e = output.edges()[24];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 8);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 8);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(25).unwrap().cell()?, 7);
    assert_eq!(output.edges().get(25).unwrap().vertex0().unwrap(), 8);
    assert_eq!(output.edges().get(25).unwrap().twin()?, 24);
    assert_eq!(output.edges().get(25).unwrap().next()?, 27);
    assert_eq!(output.edges().get(25).unwrap().prev()?, 30);
    let e = output.edges()[25];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 6);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 31);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(26).unwrap().cell()?, 2);
    assert_eq!(output.edges().get(26).unwrap().vertex0().unwrap(), 7);
    assert_eq!(output.edges().get(26).unwrap().twin()?, 27);
    assert_eq!(output.edges().get(26).unwrap().next()?, 8);
    assert_eq!(output.edges().get(26).unwrap().prev()?, 3);
    let e = output.edges()[26];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 6);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 2);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(27).unwrap().cell()?, 7);
    assert_eq!(output.edges().get(27).unwrap().vertex0().unwrap(), 6);
    assert_eq!(output.edges().get(27).unwrap().twin()?, 26);
    assert_eq!(output.edges().get(27).unwrap().next()?, 29);
    assert_eq!(output.edges().get(27).unwrap().prev()?, 25);
    let e = output.edges()[27];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 7);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 24);
    assert!(output.edge_is_finite(e.id())?);
    assert!(!output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(28).unwrap().cell()?, 0);
    assert!(output.edges().get(28).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(28).unwrap().twin()?, 29);
    assert_eq!(output.edges().get(28).unwrap().next()?, 2);
    assert_eq!(output.edges().get(28).unwrap().prev()?, 0);
    let e = output.edges()[28];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 7);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 1);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(29).unwrap().cell()?, 7);
    assert_eq!(output.edges().get(29).unwrap().vertex0().unwrap(), 7);
    assert_eq!(output.edges().get(29).unwrap().twin()?, 28);
    assert_eq!(output.edges().get(29).unwrap().next()?, 30);
    assert_eq!(output.edges().get(29).unwrap().prev()?, 27);
    let e = output.edges()[29];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 26);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(30).unwrap().cell()?, 7);
    assert!(output.edges().get(30).unwrap().vertex0().is_none());
    assert_eq!(output.edges().get(30).unwrap().twin()?, 31);
    assert_eq!(output.edges().get(30).unwrap().next()?, 25);
    assert_eq!(output.edges().get(30).unwrap().prev()?, 29);
    let e = output.edges()[30];
    assert_eq!(output.edge_get_vertex1(e.id())?.unwrap(), 8);
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 28);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    assert_eq!(output.edges().get(31).unwrap().cell()?, 6);
    assert_eq!(output.edges().get(31).unwrap().vertex0().unwrap(), 8);
    assert_eq!(output.edges().get(31).unwrap().twin()?, 30);
    assert_eq!(output.edges().get(31).unwrap().next()?, 22);
    assert_eq!(output.edges().get(31).unwrap().prev()?, 21);
    let e = output.edges()[31];
    assert!(output.edge_get_vertex1(e.id())?.is_none());
    assert_eq!(output.edge_rot_next(e.id()).unwrap(), 20);
    assert!(!output.edge_is_finite(e.id())?);
    assert!(output.edge_is_infinite(e.id())?);
    assert!(e.is_linear());
    assert!(!e.is_curved());
    assert!(e.is_primary());
    assert!(!e.is_secondary());
    Ok(())
}
