Skip to content

Commit

Permalink
Fix dividing by zero in Speedtest:avg if array is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Aug 9, 2024
1 parent bcfc9f0 commit 6b76103
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions coverage_point_calculator/src/speedtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ impl Speedtest {
}

pub fn avg(speedtests: &[Self]) -> Self {
if speedtests.is_empty() {
return Self {
upload_speed: BytesPs::new(0),
download_speed: BytesPs::new(0),
latency_millis: 0,
timestamp: Utc::now(),
};
}
let mut download = 0;
let mut upload = 0;
let mut latency = 0;
Expand Down Expand Up @@ -195,6 +203,14 @@ mod tests {
assert_eq!(Fail, SpeedtestTier::from_latency(101));
}

#[test]
fn speedtest_avg_should_not_panic() {
let avg = Speedtest::avg(&vec![]);
assert_eq!(avg.upload_speed, BytesPs(0));
assert_eq!(avg.download_speed, BytesPs(0));
assert_eq!(avg.latency_millis, 0);
}

#[test]
fn minimum_required_speedtests_provided_for_multiplier_above_zero() {
let speedtest = Speedtest {
Expand Down

0 comments on commit 6b76103

Please sign in to comment.