|
| 1 | +// SPDX-License-Identifier: CAL |
| 2 | +pragma solidity =0.8.25; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | + |
| 6 | +import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFloatImplementation.sol"; |
| 7 | + |
| 8 | +contract LibDecimalFloatImplementationUnabsUnsignedMulOrDivLossyTest is Test { |
| 9 | + /// a and b are both not negative. |
| 10 | + function testUnabsUnsignedMulOrDivLossyPositive(uint256 a, uint256 b, uint256 c, int256 exponent) external pure { |
| 11 | + a = bound(a, 0, uint256(type(int256).max)); |
| 12 | + b = bound(b, 0, uint256(type(int256).max)); |
| 13 | + c = bound(c, 0, uint256(type(int256).max)); |
| 14 | + |
| 15 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 16 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(int256(a), int256(b), c, exponent); |
| 17 | + |
| 18 | + int256 expectedSignedCoefficient = int256(c); |
| 19 | + int256 expectedExponent = exponent; |
| 20 | + |
| 21 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient); |
| 22 | + assertEq(actualExponent, expectedExponent); |
| 23 | + } |
| 24 | + |
| 25 | + /// a and b are both not negative, c overflows int256. |
| 26 | + function testUnabsUnsignedMulOrDivLossyPositiveOverflow(uint256 a, uint256 b, uint256 c, int256 exponent) |
| 27 | + external |
| 28 | + pure |
| 29 | + { |
| 30 | + a = bound(a, 0, uint256(type(int256).max)); |
| 31 | + b = bound(b, 0, uint256(type(int256).max)); |
| 32 | + c = bound(c, uint256(type(int256).max) + 1, type(uint256).max); |
| 33 | + vm.assume(exponent != type(int256).max); // Prevent overflow in exponent. |
| 34 | + |
| 35 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 36 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(int256(a), int256(b), c, exponent); |
| 37 | + // Expect the result to be positive. |
| 38 | + int256 expectedSignedCoefficient = int256(c / 10); |
| 39 | + int256 expectedExponent = exponent + 1; |
| 40 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 41 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 42 | + } |
| 43 | + |
| 44 | + /// a and b are both negative. |
| 45 | + function testUnabsUnsignedMulOrDivLossyNegative(uint256 a, uint256 b, uint256 c, int256 exponent) external pure { |
| 46 | + a = bound(a, 1, uint256(type(int256).max)); |
| 47 | + b = bound(b, 1, uint256(type(int256).max)); |
| 48 | + c = bound(c, 0, uint256(type(int256).max)); |
| 49 | + |
| 50 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 51 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(-int256(a), -int256(b), c, exponent); |
| 52 | + |
| 53 | + int256 expectedSignedCoefficient = int256(c); |
| 54 | + int256 expectedExponent = exponent; |
| 55 | + |
| 56 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 57 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 58 | + } |
| 59 | + |
| 60 | + /// a and b are both negative, c overflows int256. |
| 61 | + function testUnabsUnsignedMulOrDivLossyNegativeOverflow(uint256 a, uint256 b, uint256 c, int256 exponent) |
| 62 | + external |
| 63 | + pure |
| 64 | + { |
| 65 | + a = bound(a, 1, uint256(type(int256).max)); |
| 66 | + b = bound(b, 1, uint256(type(int256).max)); |
| 67 | + c = bound(c, uint256(type(int256).max) + 1, type(uint256).max); |
| 68 | + vm.assume(exponent != type(int256).max); // Prevent overflow in exponent. |
| 69 | + |
| 70 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 71 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(-int256(a), -int256(b), c, exponent); |
| 72 | + |
| 73 | + // Expect the result to be negative. |
| 74 | + int256 expectedSignedCoefficient = int256(c / 10); |
| 75 | + int256 expectedExponent = exponent + 1; |
| 76 | + |
| 77 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 78 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 79 | + } |
| 80 | + |
| 81 | + /// a is negative, b is positive. |
| 82 | + function testUnabsUnsignedMulOrDivLossyMixedAB(uint256 a, uint256 b, uint256 c, int256 exponent) external pure { |
| 83 | + a = bound(a, 1, uint256(type(int256).max)); |
| 84 | + b = bound(b, 0, uint256(type(int256).max)); |
| 85 | + c = bound(c, 0, uint256(type(int256).max)); |
| 86 | + |
| 87 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 88 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(-int256(a), int256(b), c, exponent); |
| 89 | + |
| 90 | + // Expect the result to be negative. |
| 91 | + int256 expectedSignedCoefficient = -int256(c); |
| 92 | + int256 expectedExponent = exponent; |
| 93 | + |
| 94 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 95 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 96 | + } |
| 97 | + |
| 98 | + /// a is positive, b is negative. c overflows int256. |
| 99 | + function testUnabsUnsignedMulOrDivLossyMixedBA(uint256 a, uint256 b, uint256 c, int256 exponent) external pure { |
| 100 | + a = bound(a, 0, uint256(type(int256).max)); |
| 101 | + b = bound(b, 1, uint256(type(int256).max)); |
| 102 | + c = bound(c, uint256(type(int256).max) + 2, type(uint256).max); |
| 103 | + vm.assume(exponent != type(int256).max); // Prevent overflow in exponent. |
| 104 | + |
| 105 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 106 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(int256(a), -int256(b), c, exponent); |
| 107 | + |
| 108 | + // Expect the result to be negative. |
| 109 | + int256 expectedSignedCoefficient = -int256(c / 10); |
| 110 | + int256 expectedExponent = exponent + 1; |
| 111 | + |
| 112 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 113 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 114 | + } |
| 115 | + |
| 116 | + /// a is negative, b is positive, c overflows int256. |
| 117 | + function testUnabsUnsignedMulOrDivLossyMixedABOverflow(uint256 a, uint256 b, uint256 c, int256 exponent) |
| 118 | + external |
| 119 | + pure |
| 120 | + { |
| 121 | + a = bound(a, 1, uint256(type(int256).max)); |
| 122 | + b = bound(b, 0, uint256(type(int256).max)); |
| 123 | + c = bound(c, uint256(type(int256).max) + 2, type(uint256).max); |
| 124 | + vm.assume(exponent != type(int256).max); // Prevent overflow in exponent. |
| 125 | + |
| 126 | + (int256 actualSignedCoefficient, int256 actualExponent) = |
| 127 | + LibDecimalFloatImplementation.unabsUnsignedMulOrDivLossy(-int256(a), int256(b), c, exponent); |
| 128 | + |
| 129 | + // Expect the result to be negative. |
| 130 | + int256 expectedSignedCoefficient = -int256(c / 10); |
| 131 | + int256 expectedExponent = exponent + 1; |
| 132 | + |
| 133 | + assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signed coefficient mismatch"); |
| 134 | + assertEq(actualExponent, expectedExponent, "exponent mismatch"); |
| 135 | + } |
| 136 | +} |
0 commit comments