diff --git a/src/lib.rs b/src/lib.rs index 208ff34..e2cfdf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ extern crate std; extern crate alloc; use alloc::vec::Vec; -use core::{f64, fmt}; +use core::{cmp::Ordering, f64, fmt}; use robust::orient2d; /// Near-duplicate points (where both `x` and `y` only differ within this value) @@ -441,7 +441,7 @@ fn find_seed_triangle(points: &[Point]) -> Option<(usize, usize, usize)> { } fn sortf(f: &mut [(usize, f64)]) { - f.sort_unstable_by(|&(_, da), &(_, db)| da.partial_cmp(&db).unwrap()); + f.sort_unstable_by(|&(_, da), &(_, db)| da.partial_cmp(&db).unwrap_or(Ordering::Equal)); } /// Order collinear points by dx (or dy if all x are identical) and return the list as a hull diff --git a/tests/tests.rs b/tests/tests.rs index 684c290..dd711da 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -158,6 +158,25 @@ fn hull_collinear_issue24() { assert_eq!(t.hull, &[0, 3, 2, 1], "Invalid hull"); } +#[test] +/// The test ensures that even when an invalid sequence of points is passed, there is no panic. +/// In this test, the output does not matter as long as an output is returned. +fn invalid_nan_sequence() { + let points = vec![ + Point { x: -3.5, y: -1.5 }, + Point { + x: f64::NAN, + y: f64::NAN, + }, + Point { + x: f64::NAN, + y: f64::NAN, + }, + Point { x: -3.5, y: -1.5 }, + ]; + triangulate(&points); +} + fn scale_points(points: &[Point], scale: f64) -> Vec { let scaled: Vec = points .iter()