Skip to content

Commit 4197b52

Browse files
♻️ Better bound implementation (#220)
Co-authored-by: t11s <[email protected]>
1 parent 196cfa4 commit 4197b52

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

.gas-snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Bytes32AddressLibTest:testFromLast20Bytes() (gas: 191)
1818
CREATE3Test:testDeployERC20() (gas: 852410)
1919
CREATE3Test:testFailDoubleDeployDifferentBytecode() (gas: 9079256848778914164)
2020
CREATE3Test:testFailDoubleDeploySameBytecode() (gas: 9079256848778906218)
21-
DSTestPlusTest:testBound() (gas: 16777)
21+
DSTestPlusTest:testBound() (gas: 14208)
2222
DSTestPlusTest:testBrutalizeMemory() (gas: 446)
2323
DSTestPlusTest:testFailBoundMinBiggerThanMax() (gas: 309)
2424
DSTestPlusTest:testRelApproxEqBothZeroesPasses() (gas: 413)

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"prettier-plugin-solidity": "^1.0.0-beta.13"
1616
},
1717
"scripts": {
18-
"lint": "prettier --write src/**/*.sol"
18+
"lint": "prettier --write **.sol"
1919
}
2020
}

src/test/DSTestPlus.t.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {DSTestPlus} from "./utils/DSTestPlus.sol";
55

66
contract DSTestPlusTest is DSTestPlus {
77
function testBound() public {
8-
assertEq(bound(5, 0, 4), 0);
98
assertEq(bound(0, 69, 69), 69);
109
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);
10+
assertEq(bound(5, 0, 4), 0);
11+
assertEq(bound(9999, 1337, 6666), 6006);
12+
assertEq(bound(0, type(uint256).max - 6, type(uint256).max), type(uint256).max - 6);
13+
assertEq(bound(6, type(uint256).max - 6, type(uint256).max), type(uint256).max);
1414
}
1515

1616
function testFailBoundMinBiggerThanMax() public {

src/test/utils/DSTestPlus.sol

+7-10
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,13 @@ contract DSTestPlus is DSTest {
141141

142142
uint256 size = max - min;
143143

144-
if (max != type(uint256).max) size++; // Make the max inclusive.
145-
if (size == 0) return min; // Using max would be equivalent as well.
146-
// Ensure max is inclusive in cases where x != 0 and max is at uint max.
147-
if (max == type(uint256).max && x != 0) x--; // Accounted for later.
148-
149-
if (x < min) x += size * (((min - x) / size) + 1);
150-
result = min + ((x - min) % size);
151-
152-
// Account for decrementing x to make max inclusive.
153-
if (max == type(uint256).max && x != 0) result++;
144+
if (size == 0) result = min;
145+
else if (size == type(uint256).max) result = x;
146+
else {
147+
++size; // Make max inclusive.
148+
uint256 mod = x % size;
149+
result = min + mod;
150+
}
154151

155152
emit log_named_uint("Bound Result", result);
156153
}

0 commit comments

Comments
 (0)