Skip to content

Commit

Permalink
fix: mean overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 14, 2024
1 parent 4ae60e5 commit 386bd74
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion crates/common/src/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn mean(values: &[u64]) -> u64 {
return 0;
}

values.iter().sum::<u64>() / (values.len() as u64)
(values.iter().map(|x| *x as u128).sum::<u128>() / values.len() as u128) as u64
}

/// Returns the median of a _sorted_ slice.
Expand Down Expand Up @@ -79,6 +79,12 @@ mod tests {
assert_eq!(m, 3);
}

#[test]
fn calc_mean_overflow() {
let m = mean(&[0, 1, 2, u32::MAX as u64, 3, u16::MAX as u64, u64::MAX, 6]);
assert_eq!(m, 2305843009750573057);
}

#[test]
fn calc_median_empty() {
let m = median_sorted(&[]);
Expand Down
6 changes: 3 additions & 3 deletions crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ impl MultiContractRunnerBuilder {
deployable_contracts.insert(id.clone(), (abi.clone(), bytecode, libs_to_deploy));
}

linked_contract.get_deployed_bytecode_bytes().map(|b| b.into_owned()).and_then(
|bytes| known_contracts.insert(id.clone(), (abi.clone(), bytes.to_vec())),
);
if let Some(bytes) = linked_contract.get_deployed_bytecode_bytes() {
known_contracts.insert(id.clone(), (abi.clone(), bytes.to_vec()));
}
}

let execution_info = known_contracts.flatten();
Expand Down

0 comments on commit 386bd74

Please sign in to comment.