-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathSignedWadMath.t.sol
More file actions
79 lines (61 loc) · 1.96 KB
/
SignedWadMath.t.sol
File metadata and controls
79 lines (61 loc) · 1.96 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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {wadMul, wadDiv, wadExp} from "../utils/SignedWadMath.sol";
contract SignedWadMathTest is DSTestPlus {
function testWadMul(
uint256 x,
uint256 y,
bool negX,
bool negY
) public {
x = bound(x, 0, 99999999999999e18);
y = bound(x, 0, 99999999999999e18);
int256 xPrime = negX ? -int256(x) : int256(x);
int256 yPrime = negY ? -int256(y) : int256(y);
assertEq(wadMul(xPrime, yPrime), (xPrime * yPrime) / 1e18);
}
function testWadExpZeroPoint() public {
assertEq(wadExp(-41446531673892822312), 1);
assertEq(wadExp(-41446531673892822313), 0);
}
function testFailWadMulEdgeCase() public pure {
int256 x = -1;
int256 y = type(int256).min;
wadMul(x, y);
}
function testFailWadMulEdgeCase2() public pure {
int256 x = type(int256).min;
int256 y = -1;
wadMul(x, y);
}
function testFailWadMulOverflow(int256 x, int256 y) public pure {
// Ignore cases where x * y does not overflow.
unchecked {
if ((x * y) / x == y) revert();
}
wadMul(x, y);
}
function testWadDiv(
uint256 x,
uint256 y,
bool negX,
bool negY
) public {
x = bound(x, 0, 99999999e18);
y = bound(x, 1, 99999999e18);
int256 xPrime = negX ? -int256(x) : int256(x);
int256 yPrime = negY ? -int256(y) : int256(y);
assertEq(wadDiv(xPrime, yPrime), (xPrime * 1e18) / yPrime);
}
function testFailWadDivOverflow(int256 x, int256 y) public pure {
// Ignore cases where x * WAD does not overflow or y is 0.
unchecked {
if (y == 0 || (x * 1e18) / 1e18 == x) revert();
}
wadDiv(x, y);
}
function testFailWadDivZeroDenominator(int256 x) public pure {
wadDiv(x, 0);
}
}