use boostvoronoi::prelude::*;

mod common;
use common::almost_equal;
use std::io::{BufReader, Cursor};

type I = i64;

#[test]
fn stress_test_14() -> Result<(), BvError> {
    let output = {
        let input = r#"0
3
180 217 436 -48
561 725 -777 -922
-120 -155 443 558
"#;
        let br = BufReader::new(Cursor::new(input));
        let (points, segments) = read_boost_input_buffer::<I, _>(br)?;
        Builder::<I>::default()
            .with_vertices(points.iter())?
            .with_segments(segments.iter())?
            .build()?
    };
    assert_eq!(output.cells().len(), 9);
    let cell = output.cells()[0];
    assert_eq!(cell.id(), 0);
    let (_source_index, _cat) = 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);
    let (_source_index, _cat) = cell.source_index_2();
    assert_eq!(_cat, SourceCategory::Segment);
    assert!(!cell.is_degenerate());
    assert!(!cell.contains_point());
    assert!(cell.contains_segment());
    let cell = output.cells()[2];
    assert_eq!(cell.id(), 2);
    let (_source_index, _cat) = cell.source_index_2();
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[3];
    assert_eq!(cell.id(), 3);
    let (_source_index, _cat) = cell.source_index_2();
    assert_eq!(_cat, SourceCategory::Segment);
    assert!(!cell.is_degenerate());
    assert!(!cell.contains_point());
    assert!(cell.contains_segment());
    let cell = output.cells()[4];
    assert_eq!(cell.id(), 4);
    let (_source_index, _cat) = cell.source_index_2();
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[5];
    assert_eq!(cell.id(), 5);
    let (_source_index, _cat) = cell.source_index_2();
    assert_eq!(_cat, SourceCategory::Segment);
    assert!(!cell.is_degenerate());
    assert!(!cell.contains_point());
    assert!(cell.contains_segment());
    let cell = output.cells()[6];
    assert_eq!(cell.id(), 6);
    let (_source_index, _cat) = cell.source_index_2();
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[7];
    assert_eq!(cell.id(), 7);
    let (_source_index, _cat) = cell.source_index_2();
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    let cell = output.cells()[8];
    assert_eq!(cell.id(), 8);
    let (_source_index, _cat) = cell.source_index_2();
    assert!(!cell.is_degenerate());
    assert!(cell.contains_point());
    assert!(!cell.contains_segment());
    assert_eq!(output.vertices().len(), 12);
    assert_eq!(output.edges().len(), 40);
    let v = output.vertices()[0];
    assert!(almost_equal(v.x(), -130.3255115, v.y(), -146.8467560));
    assert_eq!(v.get_incident_edge().unwrap(), 7);
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 176.8832875, v.y(), 213.9891381));
    assert_eq!(v.get_incident_edge().unwrap(), 13);
    let v = output.vertices()[2];
    assert!(almost_equal(v.x(), 184.0803011, v.y(), 220.9417248));
    assert_eq!(v.get_incident_edge().unwrap(), 15);
    let v = output.vertices()[3];
    assert!(almost_equal(v.x(), 437.6183729, v.y(), 562.2494474));
    assert_eq!(v.get_incident_edge().unwrap(), 21);
    let v = output.vertices()[4];
    assert!(almost_equal(v.x(), 201.2781778, v.y(), -274.7501377));
    assert_eq!(v.get_incident_edge().unwrap(), 23);
    let v = output.vertices()[5];
    assert!(almost_equal(v.x(), 219.9553002, v.y(), -423.4359523));
    assert_eq!(v.get_incident_edge().unwrap(), 27);
    let v = output.vertices()[6];
    assert!(almost_equal(v.x(), 277.2534081, v.y(), -721.1719151));
    assert_eq!(v.get_incident_edge().unwrap(), 29);
    let v = output.vertices()[7];
    assert!(almost_equal(v.x(), 743.2903644, v.y(), 248.8540879));
    assert_eq!(v.get_incident_edge().unwrap(), 31);
    let v = output.vertices()[8];
    assert!(almost_equal(v.x(), 832.4769949, v.y(), 250.4606618));
    assert_eq!(v.get_incident_edge().unwrap(), 33);
    let v = output.vertices()[9];
    assert!(almost_equal(v.x(), 1038.6074092, v.y(), 248.0796174));
    assert_eq!(v.get_incident_edge().unwrap(), 35);
    let v = output.vertices()[10];
    assert!(almost_equal(v.x(), 1170.5279195, v.y(), 229.8279561));
    assert_eq!(v.get_incident_edge().unwrap(), 37);
    let v = output.vertices()[11];
    assert!(almost_equal(v.x(), 1445.0261704, v.y(), -2727.1433005));
    assert_eq!(v.get_incident_edge().unwrap(), 39);
    Ok(())
}
