forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomCheatcodes.t.sol
More file actions
106 lines (83 loc) · 3.17 KB
/
RandomCheatcodes.t.sol
File metadata and controls
106 lines (83 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract RandomCheatcodesTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
int128 constant min = -170141183460469231731687303715884105728;
int128 constant max = 170141183460469231731687303715884105727;
function test_int128() public {
vm._expectCheatcodeRevert("vm.randomInt: number of bits cannot exceed 256");
int256 val = vm.randomInt(type(uint256).max);
val = vm.randomInt(128);
assertGe(val, min);
assertLe(val, max);
}
/// forge-config: default.allow_internal_expect_revert = true
function testReverttIf_int128() public {
int256 val = vm.randomInt(128);
vm.expectRevert("Error: a > b not satisfied [int]");
require(val > max, "Error: a > b not satisfied [int]");
}
function test_address() public {
address fresh_address = vm.randomAddress();
assert(fresh_address != address(this));
assert(fresh_address != address(vm));
}
function test_randomUintLimit() public {
vm._expectCheatcodeRevert("vm.randomUint: number of bits cannot exceed 256");
uint256 val = vm.randomUint(type(uint256).max);
}
function test_randomUints(uint256 x) public {
x = vm.randomUint(0, 256);
uint256 freshUint = vm.randomUint(x);
assert(0 <= freshUint);
if (x == 256) {
assert(freshUint <= type(uint256).max);
} else {
assert(freshUint <= 2 ** x - 1);
}
}
function test_randomSymbolicWord() public {
uint256 freshUint192 = vm.randomUint(192);
assert(0 <= freshUint192);
assert(freshUint192 <= type(uint192).max);
}
}
contract RandomBytesTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
bytes1 local_byte;
bytes local_bytes;
function manip_symbolic_bytes(bytes memory b) public {
uint256 middle = b.length / 2;
b[middle] = hex"aa";
}
function test_symbolic_bytes_revert() public {
vm._expectCheatcodeRevert();
bytes memory val = vm.randomBytes(type(uint256).max);
}
function test_symbolic_bytes_1() public {
uint256 length = uint256(vm.randomUint(1, type(uint8).max));
bytes memory fresh_bytes = vm.randomBytes(length);
uint256 index = uint256(vm.randomUint(1));
local_byte = fresh_bytes[index];
assertEq(fresh_bytes[index], local_byte);
}
function test_symbolic_bytes_2() public {
uint256 length = uint256(vm.randomUint(1, type(uint8).max));
bytes memory fresh_bytes = vm.randomBytes(length);
local_bytes = fresh_bytes;
assertEq(fresh_bytes, local_bytes);
}
function test_symbolic_bytes_3() public {
uint256 length = uint256(vm.randomUint(1, type(uint8).max));
bytes memory fresh_bytes = vm.randomBytes(length);
manip_symbolic_bytes(fresh_bytes);
assertEq(hex"aa", fresh_bytes[length / 2]);
}
function test_symbolic_bytes_length(uint8 l) public {
vm.assume(0 < l);
bytes memory fresh_bytes = vm.randomBytes(l);
assertEq(fresh_bytes.length, l);
}
}