-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forge): Implement vm.Sleep (#5519)
* implement vm sleep * forge fmt * remove println
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "./Vm.sol"; | ||
|
||
contract SleepTest is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
function testSleep() public { | ||
uint256 milliseconds = 1234; | ||
|
||
string[] memory inputs = new string[](2); | ||
inputs[0] = "date"; | ||
// OS X does not support precision more than 1 second | ||
inputs[1] = "+%s000"; | ||
|
||
bytes memory res = vm.ffi(inputs); | ||
uint256 start = vm.parseUint(string(res)); | ||
|
||
vm.sleep(milliseconds); | ||
|
||
res = vm.ffi(inputs); | ||
uint256 end = vm.parseUint(string(res)); | ||
|
||
// Limit precision to 1000 ms | ||
assertGe(end - start, milliseconds / 1000 * 1000, "sleep failed"); | ||
} | ||
|
||
/// forge-config: default.fuzz.runs = 10 | ||
function testSleepFuzzed(uint256 _milliseconds) public { | ||
// Limit sleep time to 2 seconds to decrease test time | ||
uint256 milliseconds = _milliseconds % 2000; | ||
|
||
string[] memory inputs = new string[](2); | ||
inputs[0] = "date"; | ||
// OS X does not support precision more than 1 second | ||
inputs[1] = "+%s000"; | ||
|
||
bytes memory res = vm.ffi(inputs); | ||
uint256 start = vm.parseUint(string(res)); | ||
|
||
vm.sleep(milliseconds); | ||
|
||
res = vm.ffi(inputs); | ||
uint256 end = vm.parseUint(string(res)); | ||
|
||
// Limit precision to 1000 ms | ||
assertGe(end - start, milliseconds / 1000 * 1000, "sleep failed"); | ||
} | ||
} |
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