diff --git a/plotters/src/data/quartiles.rs b/plotters/src/data/quartiles.rs index 78613dad..0a16eaa8 100644 --- a/plotters/src/data/quartiles.rs +++ b/plotters/src/data/quartiles.rs @@ -45,7 +45,10 @@ impl Quartiles { /// ``` pub fn new + Copy + PartialOrd>(s: &[T]) -> Self { let mut s = s.to_owned(); - s.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap()); + // NaN has no total order, so `partial_cmp` returns `None` for it; a numeric + // data series (where NaN means a missing measurement) must not panic the + // whole plot. Sorting NaN as equal keeps the result defined. + s.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); let lower = Quartiles::percentile_of_sorted(&s, 25_f64); let median = Quartiles::percentile_of_sorted(&s, 50_f64); @@ -102,6 +105,14 @@ impl Quartiles { mod test { use super::*; + #[test] + fn test_nan_in_data_does_not_panic() { + // Regression: `partial_cmp(..).unwrap()` aborted the caller on a single NaN. + let q = Quartiles::new(&[1.0f64, f64::NAN, 2.0]); + let values = q.values(); + assert_eq!(values.len(), 5); + } + #[test] #[should_panic] fn test_empty_input() {