foundry
foundry copied to clipboard
Ability to get sorted arrays when fuzzing
Component
Forge
Describe the feature you would like
As discussed in https://github.com/foundry-rs/foundry/discussions/3948, my protocol has some business logic that is dependent upon the end user sending a sorted array as a function argument.
I've been bashing my head against the wall trying to fuzz the input arrays in a way that is scalable (tests don't take ages to run), to no fruition.
I pulled together an implementation of the quicksort algorithm in Solidity based on the sources I found on the Internet. The issue is that sorting a fuzzed arrays slows down test run times.
It would be nice to be able to instruct Foundry to sort the array on our behalf. I'm thinking of something like this (echo-ing the proposal I left in https://github.com/foundry-rs/foundry/issues/4085):
/// @custom:sort-array
function test_Something(uint256[] memory arr) external {
// ...
}
That is, whenever the custom NatSpec tag above is specified, Foundry would provide the user with a sorted array arr
, every time. It would be cheaper to let Foundry do this in Rust rather than sorting the array in memory in Solidity.
As an alternative to the above, we could consider using a specific prefix for the array variable names:
function test_Something(uint256[] memory sortedArr) external {
// ...
}
The idea being, every time Foundry sees a function param that is an array type and its name starts with sorted
, it would automatically sort the array.
Sorted arrays should be a common enough use case for a feature like this to be worth considering and implementing.