Skip to content

Commit 95f4f61

Browse files
Rahul MagantiRahulMaganti47
Rahul Maganti
authored andcommitted
evm: add differential fuzz tests for overloaded binary operators
1 parent 8ea75e9 commit 95f4f61

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

evm/test/TrimmedAmount.t.sol

+50
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,54 @@ contract TrimmingTest is Test {
167167

168168
assertEq(expectedRoundTrip, amountRoundTrip);
169169
}
170+
171+
function testFuzz_AddOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
172+
vm.assume(a.getDecimals() == b.getDecimals());
173+
174+
// check if the add operation reverts on an overflow.
175+
// if it overflows, discard the input
176+
uint256 largeSum = uint256(a.getAmount()) + uint256(b.getAmount());
177+
vm.assume(largeSum <= type(uint64).max);
178+
179+
// check if the sum matches the expected sum if no overflow
180+
TrimmedAmount sum = a + b;
181+
TrimmedAmount expectedSum = add(a, b);
182+
183+
assertEq(expectedSum.getAmount(), sum.getAmount());
184+
assertEq(expectedSum.getDecimals(), sum.getDecimals());
185+
}
186+
187+
function testFuzz_SubOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
188+
vm.assume(a.getDecimals() == b.getDecimals());
189+
vm.assume(a.getAmount() >= b.getAmount());
190+
191+
TrimmedAmount subAmt = a - b;
192+
TrimmedAmount expectedSub = sub(a, b);
193+
194+
assertEq(expectedSub.getAmount(), subAmt.getAmount());
195+
assertEq(expectedSub.getDecimals(), subAmt.getDecimals());
196+
}
197+
198+
function testFuzz_EqOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
199+
bool isEqual = a == b;
200+
bool expectedIsEqual = eq(a, b);
201+
202+
assertEq(expectedIsEqual, isEqual);
203+
}
204+
205+
function testFuzz_GtOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
206+
vm.assume(a.getDecimals() == b.getDecimals());
207+
bool isGt = a > b;
208+
bool expectedIsGt = gt(a, b);
209+
210+
assertEq(expectedIsGt, isGt);
211+
}
212+
213+
function testFuzz_LtOperatorOverload(TrimmedAmount a, TrimmedAmount b) public {
214+
vm.assume(a.getDecimals() == b.getDecimals());
215+
bool isLt = a > b;
216+
bool expectedIsLt = gt(a, b);
217+
218+
assertEq(expectedIsLt, isLt);
219+
}
170220
}

0 commit comments

Comments
 (0)