@@ -15,12 +15,12 @@ contract PercentageMathFunctions {
1515 return PercentageMath.percentDiv (x, y);
1616 }
1717
18- function percentAvg (
18+ function weightedAvg (
1919 uint256 x ,
2020 uint256 y ,
2121 uint256 percentage
2222 ) public pure returns (uint256 ) {
23- return PercentageMath.percentAvg (x, y, percentage);
23+ return PercentageMath.weightedAvg (x, y, percentage);
2424 }
2525}
2626
@@ -35,7 +35,7 @@ contract PercentageMathFunctionsRef {
3535 return PercentageMathRef.percentDiv (x, y);
3636 }
3737
38- function percentAvg (
38+ function weightedAvg (
3939 uint256 x ,
4040 uint256 y ,
4141 uint256 percentage
@@ -95,34 +95,34 @@ contract TestPercentageMath is Test {
9595
9696 vm.expectRevert ();
9797 PercentageMath.percentDiv (x, y);
98+ }
9899
99- function testPercentAvg (
100+ function testWeightedAvg (
100101 uint256 x ,
101102 uint256 y ,
102103 uint16 percentage
103104 ) public {
104105 vm.assume (percentage <= PERCENTAGE_FACTOR);
105- vm.assume (
106- percentage < PERCENTAGE_FACTOR && x <= MAX_UINT256_MINUS_HALF_PERCENTAGE / (PERCENTAGE_FACTOR - percentage)
107- );
108- vm.assume (percentage > 0 && y <= MAX_UINT256_MINUS_HALF_PERCENTAGE / percentage);
106+ if (percentage > 0 ) vm.assume (y <= MAX_UINT256_MINUS_HALF_PERCENTAGE / percentage);
107+ if (percentage < PERCENTAGE_FACTOR)
108+ vm.assume (x <= MAX_UINT256_MINUS_HALF_PERCENTAGE / (PERCENTAGE_FACTOR - percentage));
109109
110110 assertEq (
111- PercentageMath.percentAvg (x, y, percentage),
111+ PercentageMath.weightedAvg (x, y, percentage),
112112 PercentageMathRef.percentMul (x, PERCENTAGE_FACTOR - percentage) +
113113 PercentageMathRef.percentMul (y, percentage)
114114 );
115115 }
116116
117- function testPercentAvgRevertWhenPercentageTooHigh (
117+ function testWeightedAvgRevertWhenPercentageTooHigh (
118118 uint256 x ,
119119 uint256 y ,
120120 uint256 percentage
121121 ) public {
122122 vm.assume (percentage > PERCENTAGE_FACTOR);
123123
124124 vm.expectRevert (abi.encodeWithSignature ("PercentageTooHigh() " ));
125- PercentageMath.percentAvg (x, y, percentage);
125+ PercentageMath.weightedAvg (x, y, percentage);
126126 }
127127
128128 /// GAS COMPARISONS ///
@@ -138,7 +138,7 @@ contract TestPercentageMath is Test {
138138 }
139139
140140 function testGasPercentageAvg () public view {
141- math.percentAvg (1 ether, 2 ether, 5_000 );
142- mathRef.percentAvg (1 ether, 2 ether, 5_000 );
141+ math.weightedAvg (1 ether, 2 ether, 5_000 );
142+ mathRef.weightedAvg (1 ether, 2 ether, 5_000 );
143143 }
144144}
0 commit comments