@@ -5,26 +5,26 @@ import "../src/Test.sol";
55
66contract StdUtilsTest is Test {
77 function testBound () public {
8- assertEq (bound (5 , 0 , 4 ), 0 );
9- assertEq (bound (0 , 69 , 69 ), 69 );
10- assertEq (bound (0 , 68 , 69 ), 68 );
11- assertEq (bound (10 , 150 , 190 ), 174 );
12- assertEq (bound (300 , 2800 , 3200 ), 3107 );
13- assertEq (bound (9999 , 1337 , 6666 ), 4669 );
8+ assertEq (bound (uint256 ( 5 ) , 0 , 4 ), 0 );
9+ assertEq (bound (uint256 ( 0 ) , 69 , 69 ), 69 );
10+ assertEq (bound (uint256 ( 0 ) , 68 , 69 ), 68 );
11+ assertEq (bound (uint256 ( 10 ) , 150 , 190 ), 174 );
12+ assertEq (bound (uint256 ( 300 ) , 2800 , 3200 ), 3107 );
13+ assertEq (bound (uint256 ( 9999 ) , 1337 , 6666 ), 4669 );
1414 }
1515
1616 function testBound_WithinRange () public {
17- assertEq (bound (51 , 50 , 150 ), 51 );
18- assertEq (bound (51 , 50 , 150 ), bound (bound (51 , 50 , 150 ), 50 , 150 ));
19- assertEq (bound (149 , 50 , 150 ), 149 );
20- assertEq (bound (149 , 50 , 150 ), bound (bound (149 , 50 , 150 ), 50 , 150 ));
17+ assertEq (bound (uint256 ( 51 ) , 50 , 150 ), 51 );
18+ assertEq (bound (uint256 ( 51 ) , 50 , 150 ), bound (bound (uint256 ( 51 ) , 50 , 150 ), 50 , 150 ));
19+ assertEq (bound (uint256 ( 149 ) , 50 , 150 ), 149 );
20+ assertEq (bound (uint256 ( 149 ) , 50 , 150 ), bound (bound (uint256 ( 149 ) , 50 , 150 ), 50 , 150 ));
2121 }
2222
2323 function testBound_EdgeCoverage () public {
24- assertEq (bound (0 , 50 , 150 ), 50 );
25- assertEq (bound (1 , 50 , 150 ), 51 );
26- assertEq (bound (2 , 50 , 150 ), 52 );
27- assertEq (bound (3 , 50 , 150 ), 53 );
24+ assertEq (bound (uint256 ( 0 ) , 50 , 150 ), 50 );
25+ assertEq (bound (uint256 ( 1 ) , 50 , 150 ), 51 );
26+ assertEq (bound (uint256 ( 2 ) , 50 , 150 ), 52 );
27+ assertEq (bound (uint256 ( 3 ) , 50 , 150 ), 53 );
2828 assertEq (bound (type (uint256 ).max, 50 , 150 ), 150 );
2929 assertEq (bound (type (uint256 ).max - 1 , 50 , 150 ), 149 );
3030 assertEq (bound (type (uint256 ).max - 2 , 50 , 150 ), 148 );
@@ -65,7 +65,7 @@ contract StdUtilsTest is Test {
6565
6666 function testCannotBoundMaxLessThanMin () public {
6767 vm.expectRevert (bytes ("StdUtils bound(uint256,uint256,uint256): Max is less than min. " ));
68- bound (5 , 100 , 10 );
68+ bound (uint256 ( 5 ) , 100 , 10 );
6969 }
7070
7171 function testCannotBoundMaxLessThanMin (uint256 num , uint256 min , uint256 max ) public {
@@ -74,6 +74,90 @@ contract StdUtilsTest is Test {
7474 bound (num, min, max);
7575 }
7676
77+ function testBoundInt () public {
78+ assertEq (bound (- 3 , 0 , 4 ), 2 );
79+ assertEq (bound (0 , - 69 , - 69 ), - 69 );
80+ assertEq (bound (0 , - 69 , - 68 ), - 68 );
81+ assertEq (bound (- 10 , 150 , 190 ), 154 );
82+ assertEq (bound (- 300 , 2800 , 3200 ), 2908 );
83+ assertEq (bound (9999 , - 1337 , 6666 ), 1995 );
84+ }
85+
86+ function testBoundInt_WithinRange () public {
87+ assertEq (bound (51 , - 50 , 150 ), 51 );
88+ assertEq (bound (51 , - 50 , 150 ), bound (bound (51 , - 50 , 150 ), - 50 , 150 ));
89+ assertEq (bound (149 , - 50 , 150 ), 149 );
90+ assertEq (bound (149 , - 50 , 150 ), bound (bound (149 , - 50 , 150 ), - 50 , 150 ));
91+ }
92+
93+ function testBoundInt_EdgeCoverage () public {
94+ assertEq (bound (type (int256 ).min, - 50 , 150 ), - 50 );
95+ assertEq (bound (type (int256 ).min + 1 , - 50 , 150 ), - 49 );
96+ assertEq (bound (type (int256 ).min + 2 , - 50 , 150 ), - 48 );
97+ assertEq (bound (type (int256 ).min + 3 , - 50 , 150 ), - 47 );
98+ assertEq (bound (type (int256 ).min, 10 , 150 ), 10 );
99+ assertEq (bound (type (int256 ).min + 1 , 10 , 150 ), 11 );
100+ assertEq (bound (type (int256 ).min + 2 , 10 , 150 ), 12 );
101+ assertEq (bound (type (int256 ).min + 3 , 10 , 150 ), 13 );
102+
103+ assertEq (bound (type (int256 ).max, - 50 , 150 ), 150 );
104+ assertEq (bound (type (int256 ).max - 1 , - 50 , 150 ), 149 );
105+ assertEq (bound (type (int256 ).max - 2 , - 50 , 150 ), 148 );
106+ assertEq (bound (type (int256 ).max - 3 , - 50 , 150 ), 147 );
107+ assertEq (bound (type (int256 ).max, - 50 , - 10 ), - 10 );
108+ assertEq (bound (type (int256 ).max - 1 , - 50 , - 10 ), - 11 );
109+ assertEq (bound (type (int256 ).max - 2 , - 50 , - 10 ), - 12 );
110+ assertEq (bound (type (int256 ).max - 3 , - 50 , - 10 ), - 13 );
111+ }
112+
113+ function testBoundInt_DistributionIsEven (int256 min , uint256 size ) public {
114+ size = size % 100 + 1 ;
115+ min = bound (min, - int256 (size / 2 ), int256 (size - size / 2 ));
116+ int256 max = min + int256 (size) - 1 ;
117+ int256 result;
118+
119+ for (uint256 i = 1 ; i <= size * 4 ; ++ i) {
120+ // x > max
121+ result = bound (max + int256 (i), min, max);
122+ assertEq (result, min + int256 ((i - 1 ) % size));
123+ // x < min
124+ result = bound (min - int256 (i), min, max);
125+ assertEq (result, max - int256 ((i - 1 ) % size));
126+ }
127+ }
128+
129+ function testBoundInt (int256 num , int256 min , int256 max ) public {
130+ if (min > max) (min, max) = (max, min);
131+
132+ int256 result = bound (num, min, max);
133+
134+ assertGe (result, min);
135+ assertLe (result, max);
136+ assertEq (result, bound (result, min, max));
137+ if (num >= min && num <= max) assertEq (result, num);
138+ }
139+
140+ function testBoundIntInt256Max () public {
141+ assertEq (bound (0 , type (int256 ).max - 1 , type (int256 ).max), type (int256 ).max - 1 );
142+ assertEq (bound (1 , type (int256 ).max - 1 , type (int256 ).max), type (int256 ).max);
143+ }
144+
145+ function testBoundIntInt256Min () public {
146+ assertEq (bound (0 , type (int256 ).min, type (int256 ).min + 1 ), type (int256 ).min);
147+ assertEq (bound (1 , type (int256 ).min, type (int256 ).min + 1 ), type (int256 ).min + 1 );
148+ }
149+
150+ function testCannotBoundIntMaxLessThanMin () public {
151+ vm.expectRevert (bytes ("StdUtils bound(int256,int256,int256): Max is less than min. " ));
152+ bound (- 5 , 100 , 10 );
153+ }
154+
155+ function testCannotBoundIntMaxLessThanMin (int256 num , int256 min , int256 max ) public {
156+ vm.assume (min > max);
157+ vm.expectRevert (bytes ("StdUtils bound(int256,int256,int256): Max is less than min. " ));
158+ bound (num, min, max);
159+ }
160+
77161 function testGenerateCreateAddress () external {
78162 address deployer = 0x6C9FC64A53c1b71FB3f9Af64d1ae3A4931A5f4E9 ;
79163 uint256 nonce = 14 ;
0 commit comments