-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for most
random
cheatcodes (#877)
* Support `random*` cheatcodes except for `randomInt` for signed ints * `randomUInt` signature fix * Add remaining `random*` cheatcodes * Add `random*` tests to end-to-end tests * Fix capitalization in `randomUInt` * Fix `bytes` rules, fix `randomInt` test typo * Fix `uint8` signature * Update `randomBytes` implementation, leave passing tests for the first PR * Add `test_randomUint_Range` * Add `randomUint(uint256,uint256)` implementation * Fix implementation for `randomUint256Range` * Fix `test_randomUint_192` name in `end-to-end-prove-all` * Minor cleanup in `cheatcodes.md` * Add a test for `vm.randomUint()` * Fix `test_randomUint_Range(uint256,uint256)` signature * Fix whitespaces in rules one Co-authored-by: Andrei Văcaru <[email protected]> * Fix whitespaces in rules two Co-authored-by: Andrei Văcaru <[email protected]> * Rename `randomUintWidth` rule, add descriptions for other rules --------- Co-authored-by: Andrei Văcaru <[email protected]>
- Loading branch information
1 parent
75bcce3
commit 5f496c3
Showing
3 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
|
||
contract RandomVarTest is Test { | ||
uint256 constant length_limit = 72; | ||
|
||
function test_randomBool() public view { | ||
bool freshBool = vm.randomBool(); | ||
vm.assume(freshBool); | ||
} | ||
|
||
function test_randomAddress() public { | ||
address freshAddress = vm.randomAddress(); | ||
assertNotEq(freshAddress, address(this)); | ||
assertNotEq(freshAddress, address(vm)); | ||
} | ||
|
||
function test_randomBytes_length(uint256 len) public view { | ||
vm.assume(0 < len); | ||
vm.assume(len <= length_limit); | ||
bytes memory freshBytes = vm.randomBytes(len); | ||
assertEq(freshBytes.length, len); | ||
} | ||
|
||
function test_randomBytes4_length() public view { | ||
bytes4 freshBytes = vm.randomBytes4(); | ||
assertEq(freshBytes.length, 4); | ||
} | ||
|
||
function test_randomBytes8_length() public view { | ||
bytes8 freshBytes = vm.randomBytes8(); | ||
assertEq(freshBytes.length, 8); | ||
} | ||
|
||
function test_randomUint_192() public { | ||
uint256 randomUint192 = vm.randomUint(192); | ||
|
||
assert(0 <= randomUint192); | ||
assert(randomUint192 <= type(uint192).max); | ||
} | ||
|
||
function test_randomUint_Range(uint256 min, uint256 max) public { | ||
vm.assume(max >= min); | ||
uint256 rand = vm.randomUint(min, max); | ||
assertTrue(rand >= min, "rand >= min"); | ||
assertTrue(rand <= max, "rand <= max"); | ||
} | ||
|
||
function test_randomUint() public { | ||
uint256 rand = vm.randomUint(); | ||
assertTrue(rand >= type(uint256).min); | ||
assertTrue(rand <= type(uint256).max); | ||
} | ||
} |