use boostvoronoi::prelude::*;

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

type I = i64;

#[test]
fn stress_test_13() -> Result<(), BvError> {
    let output = {
        let input = r#"0
3
503 -633 600 -981
664 -702 688 -800
-658 584 880 -850
"#;
        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!(!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_eq!(_cat, SourceCategory::Segment);
    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(), -142.9265766, v.y(), -813.0427527));
    assert_eq!(v.get_incident_edge().unwrap(), 7);
    let v = output.vertices()[1];
    assert!(almost_equal(v.x(), 554.1342703, v.y(), -618.7470568));
    assert_eq!(v.get_incident_edge().unwrap(), 11);
    let v = output.vertices()[2];
    assert!(almost_equal(v.x(), 597.6624688, v.y(), -718.2459260));
    assert_eq!(v.get_incident_edge().unwrap(), 17);
    let v = output.vertices()[3];
    assert!(almost_equal(v.x(), 593.1177536, v.y(), -682.8898519));
    assert_eq!(v.get_incident_edge().unwrap(), 19);
    let v = output.vertices()[4];
    assert!(almost_equal(v.x(), -1282.7701855, v.y(), -1505.7951379));
    assert_eq!(v.get_incident_edge().unwrap(), 21);
    let v = output.vertices()[5];
    assert!(almost_equal(v.x(), 623.2141768, v.y(), -815.8659159));
    assert_eq!(v.get_incident_edge().unwrap(), 25);
    let v = output.vertices()[6];
    assert!(almost_equal(v.x(), 684.6559989, v.y(), -696.9413880));
    assert_eq!(v.get_incident_edge().unwrap(), 27);
    let v = output.vertices()[7];
    assert!(almost_equal(v.x(), 737.9114534, v.y(), -787.7767869));
    assert_eq!(v.get_incident_edge().unwrap(), 29);
    let v = output.vertices()[8];
    assert!(almost_equal(v.x(), 746.2790312, v.y(), -940.2268218));
    assert_eq!(v.get_incident_edge().unwrap(), 33);
    let v = output.vertices()[9];
    assert!(almost_equal(v.x(), 750.8438609, v.y(), -942.4461865));
    assert_eq!(v.get_incident_edge().unwrap(), 35);
    let v = output.vertices()[10];
    assert!(almost_equal(v.x(), 766.3723644, v.y(), -971.8684125));
    assert_eq!(v.get_incident_edge().unwrap(), 37);
    let v = output.vertices()[11];
    assert!(almost_equal(v.x(), -5452.0336668, v.y(), -4557.7181168));
    assert_eq!(v.get_incident_edge().unwrap(), 39);
    Ok(())
}
