Skip to content

Commit

Permalink
fix: filter out NaN values
Browse files Browse the repository at this point in the history
The implementation would panic before in AlgorithmicIndexer
  • Loading branch information
ekroon committed Jan 5, 2023
1 parent 799994d commit ac8a8d6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)]);
}
});
}
}
Expand Down Expand Up @@ -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]), "▁▅█");
Expand Down

0 comments on commit ac8a8d6

Please sign in to comment.