-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLibDecimalFloatImplementation.div.t.sol
More file actions
132 lines (113 loc) · 4.51 KB
/
Copy pathLibDecimalFloatImplementation.div.t.sol
File metadata and controls
132 lines (113 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;
import {Test} from "forge-std/Test.sol";
import {
LibDecimalFloatImplementation,
EXPONENT_MIN,
EXPONENT_MAX
} from "src/lib/implementation/LibDecimalFloatImplementation.sol";
import {THREES, ONES} from "../../../lib/LibCommonResults.sol";
contract LibDecimalFloatImplementationDivTest is Test {
function checkDiv(
int256 signedCoefficientA,
int256 exponentA,
int256 signedCoefficientB,
int256 exponentB,
int256 signedCoefficientC,
int256 exponentC
) internal pure {
(int256 signedCoefficient, int256 exponent) =
LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB);
assertEq(signedCoefficient, signedCoefficientC, "coefficient");
assertEq(exponent, exponentC, "exponent");
}
/// 1 / 3 gas by parts 10
function testDiv1Over3Gas10() external pure {
(int256 c, int256 e) = LibDecimalFloatImplementation.div(1, 0, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
(c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37);
}
/// 1 / 3
function testDiv1Over3() external pure {
checkDiv(1, 0, 3, 0, THREES, -76);
}
/// - 1 / 3
function testDivNegative1Over3() external pure {
checkDiv(-1, 0, 3, 0, -THREES, -76);
}
/// 1 / 3 gas
function testDiv1Over3Gas0() external pure {
(int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.div(1e37, -37, 3e37, -37);
(signedCoefficient, exponent);
}
/// 1e18 / 3
function testDiv1e18Over3() external pure {
checkDiv(1e18, 0, 3, 0, THREES, -58);
}
/// 10,0 / 1e38,-37 == 1
function testDivTenOverOOMs() external pure {
checkDiv(10, 0, 1e38, -37, 1e76, -76);
}
/// 1e38,-37 / 2,0 == 5
function testDivOOMsOverTen() external pure {
checkDiv(1e38, -37, 2, 0, 5e75, -75);
}
/// 5e37,-37 / 2e37,-37 == 2.5
function testDivOOMs5and2() external pure {
checkDiv(5e37, -37, 2e37, -37, 2.5e76, -76);
}
/// (1 / 9) / (1 / 3) == 0.333..
function testDiv1Over9Over1Over3() external pure {
// 1 / 9
(int256 signedCoefficientA, int256 exponentA) = LibDecimalFloatImplementation.div(1, 0, 9, 0);
assertEq(signedCoefficientA, ONES);
assertEq(exponentA, -76);
// 1 / 3
(int256 signedCoefficientB, int256 exponentB) = LibDecimalFloatImplementation.div(1, 0, 3, 0);
assertEq(signedCoefficientB, THREES);
assertEq(exponentB, -76);
// (1 / 9) / (1 / 3)
(int256 signedCoefficient, int256 exponent) =
LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB);
assertEq(signedCoefficient, THREES);
assertEq(exponent, -76);
// (1 / 3) / (1 / 9) == 3
(signedCoefficient, exponent) =
LibDecimalFloatImplementation.div(signedCoefficientB, exponentB, signedCoefficientA, exponentA);
assertEq(signedCoefficient, 3e76);
assertEq(exponent, -76);
}
/// forge-config: default.fuzz.runs = 100
function testUnnormalizedThreesDiv0(int256 exponentA, int256 exponentB) external pure {
exponentA = bound(exponentA, EXPONENT_MIN / 2, EXPONENT_MAX / 2);
exponentB = bound(exponentB, EXPONENT_MIN / 2, EXPONENT_MAX / 2);
int256 d = 3;
int256 di = 0;
while (true) {
int256 i = 1;
int256 j = -76 - di;
while (true) {
// want to see full precision on the THREES regardless of the
// scale of the numerator and denominator.
checkDiv(i, exponentA, d, exponentB, THREES, exponentA - exponentB + j);
if (i == 1e76) {
break;
}
i *= 10;
++j;
}
if (d == 3e76) {
break;
}
d *= 10;
++di;
}
}
}