Skip to content

Commit

Permalink
Fixed panic when NaN is passed (#28)
Browse files Browse the repository at this point in the history
* Fixed panic when NaN is passed

* Fixed format
  • Loading branch information
AndriBaal committed Mar 20, 2023
1 parent 81e58c6 commit 4e17b93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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

0 comments on commit 4e17b93

Please sign in to comment.