Skip to content
Open
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
13 changes: 12 additions & 1 deletion plotters/src/data/quartiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl Quartiles {
/// ```
pub fn new<T: Into<f64> + 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);
Expand Down Expand Up @@ -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() {
Expand Down