Branchless ternary, min and max methods (#4976)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
Lohann Paterno Coutinho Ferreira
2024-04-23 13:08:08 +01:00
committed by GitHub
parent 60afc99053
commit 4032b42694
5 changed files with 66 additions and 7 deletions

View File

@ -7,6 +7,16 @@ import {Test, stdError} from "forge-std/Test.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
contract MathTest is Test {
function testSelect(bool f, uint256 a, uint256 b) public {
assertEq(Math.ternary(f, a, b), f ? a : b);
}
// MIN & MAX
function testMinMax(uint256 a, uint256 b) public {
assertEq(Math.min(a, b), a < b ? a : b);
assertEq(Math.max(a, b), a > b ? a : b);
}
// CEILDIV
function testCeilDiv(uint256 a, uint256 b) public {
vm.assume(b > 0);