New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix mean on empty arrays #2612
Fix mean on empty arrays #2612
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 😄 let's add a small test to ensure we don't break it?
also welcome to github, thanks for submitting your first pr ever to foundry 🥳
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks,
smol style nit
common/src/calc.rs
Outdated
let len = values.len(); | ||
if len > 0 { | ||
values.iter().copied().fold(U256::zero(), |sum, val| sum + val.into()) / len | ||
} else { | ||
U256::zero() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer
if values.is_empty() {
return U256::zero()
}
here
Ahah, I'm learning Rust |
@Rubilmax the tests are in the Lines 38 to 64 in 50d73f7
The More in the Rust book: https://doc.rust-lang.org/book/ch11-01-writing-tests.html Edit: You can basically just copy one of the tests and pass in an empty array |
Thank you, done! |
* Fix mean on empty arrays * Update code style * Add empty slice tests
Motivation
The gas report of my test suite is panicking when providing
FOUNDRY_FUZZ_RUNS=0
, because it's computing the mean of an empty array (&func.calls
)Solution
Check if the array is empty in
calc::mean