Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed panic when NaN is passed #28

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> {
let scaled: Vec<Point> = points
.iter()
Expand Down