-
It is possible to fuzz arrays like this: function testFuzzArr(uint256[] memory arr) external {
// ...
} But I wonder if it is possible to have some advanced e.g. each array element should not be greater than X, or the array elements should be ordered in ascending order. I guess I could iterate over the array items and do a Does Foundry offer any support for this use case? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
This should be easy to write a helper method for, but you should use
This would be a bad assume condition, as the odds of getting a sorted array are small, especially as array length increases. For this, you should just sort the received array Also in general, I don't think the uint array fuzz strategy is as good as the regular uint strategy, you can see the arrays don't use foundry/evm/src/fuzz/strategies/param.rs Lines 25 to 34 in b78509f |
Beta Was this translation helpful? Give feedback.
-
To follow up on this topic .. I just bumped into another nasty issue. Consider the following use case: function testBar(uint256[] memory a, uint256[] memory b, uint256[] memory c) external {
// ...
} Where you want:
The naive way of enforcing this would be to use vm.assume(a.length != 0);
vm.assume(a.length == b.length && a.length == c.length); However, it doesn't take long to notice that this is not a good approach, because of the too many rejects issue. I got this error as soon as I implemented the assumptions above:
I tried increasing the One potential workaround is to fuzz the length of the array and then generate the array data based on some seed .. but this is not trivial and it involves writing a mini-fuzzer, basically. Another potential solution is to create one large array, and then create the three sub-arrays based off that large array. This does have the downside of (i) not allowing more than 256 / 3 = ~85 element arrays (256 is the maximum array length used by the fuzzer) and (ii) having to manually convert the types for the sub-arrays, if they are meant to have different types. @mds1, can you think of any other solution? Sorry to ping you but I'm stuck. |
Beta Was this translation helpful? Give feedback.
-
Following up here for posterity - I ended up doing something more creative.
Code snippet here: https://gist.github.com/PaulRBerg/23bb1e7b04a43aa5376974d9f7a6aa1c |
Beta Was this translation helpful? Give feedback.
Following up here for posterity - I ended up doing something more creative.
Code snippet here:
https://gist.github.com/PaulRBerg/23bb1e7b04a43aa5376974d9f7a6aa1c