From ac8a8d602778df3d12c64720981293517aa169ea Mon Sep 17 00:00:00 2001 From: Erwin Kroon <123574+ekroon@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:55:15 +0100 Subject: [PATCH] fix: filter out NaN values The implementation would panic before in AlgorithmicIndexer --- src/lib.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1eaced4..c3c2de2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,6 +80,9 @@ impl<'a> StringSpark<'a> { let mut max: Option<&f64> = self.max.as_ref(); if min.is_none() || max.is_none() { for v in data { + if v.is_nan() { + continue; + } if let Some(m) = min { if v < m { min = Some(v); @@ -104,7 +107,9 @@ impl<'a> StringSpark<'a> { } else { let indexer = self.build_indexer.build_indexer(*min, *max, self.ticks); data.iter().for_each(|v| { - result.push(self.ticks[indexer.index(*v)]); + if !v.is_nan() { + result.push(self.ticks[indexer.index(*v)]); + } }); } } @@ -156,6 +161,22 @@ mod tests { assert_eq!(spark.spark(&[1.0, 2.0, 3.0]), "abc"); } + #[test] + fn test_nan() { + let spark = StringSpark::default(); + assert_eq!(spark.spark(&[f64::NAN, 1.0, 2.0, f64::NAN, 3.0]), "▁▅█"); + } + + #[ignore] + #[test] + fn test_infinite() { + let spark = StringSpark::default(); + assert_eq!( + spark.spark(&[f64::NEG_INFINITY, 0.0, f64::INFINITY,]), + "▁▅█" + ); + } + #[test] fn test_spark_fn() { assert_eq!(spark(&[1.0, 2.0, 3.0]), "▁▅█");