-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignedWadMathTest.sol
163 lines (142 loc) · 5.09 KB
/
SignedWadMathTest.sol
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
pragma solidity 0.8.19;
import {SignedWadMath} from "./SignedWadMath.sol";
import "./helper.sol";
import "./IVM.sol";
// Run with medusa fuzz --target contracts/SignedWadMathTest.sol --deployment-order SignedWadMathTest
contract SignedWadMathTest is PropertiesAsserts{
/**
* list of invariants i want to check here
*
* toWadUnsafe
* 1. doesn't revert on overflow and give negative values after
* going out of uint256 max limit
*
* 2. check for precision stuffs like bkchodi of fixed point math
* fromDaysWadUnsafe
*
* 1. result will always greater than input param
* 2. check everytime from seconds to days and from that days to second is correct
*
*
* unsafeWadMul & unsafeWadDiv
* x*y= y*x = y*y*x*(1/y)= ((x*y)^n)/((x*y)^n-1
* x*1=1*x
* x*0=0=0*x
* 1/(x*y)= (1/x)*(1/y)
* x/y = x*(1/y)
* if x>1 & y>1 then always x*y>1
* xy is a hyperbolic curve so fix x and vary y and make sure getting
* hyperbolic curve
*
* wadMul & wadDiv but they will be reverting
* same operation as above
*
* wadpow
*
* if a>b then always a^x > b^x for x>0
* if a>b then always a^x < b^x for x<0
*
*
*
* exponential ang log should be inverse to each other
*
* unsafeDiv
*
* if x>y => x/y >1
*
* There is no point to perform these test again in fixedPointMath . Can be done by copy paste
*/
event Debug(uint256);
event Debug(int256);
IVM vm = IVM(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
// The following is an example of invariant
// It test that if x < 10**18
// Then x <= uint(toWadUnsafe(x))
function testtoWadUnsafe(uint256 x) public{
x = clampLte(x, 10**18);
int256 y = SignedWadMath.toWadUnsafe(x);
emit Debug(y);
// Ensure that x <= uint(y)
assertLte(x, uint(y), "X should be less or equal to Y");
}
// for given input seconds
// numeric value of second>min>day
function testSecMinDay(uint256 x) public{
x = clampLte(x, 10**18);
int256 y = SignedWadMath.toDaysWadUnsafe(x);
assertEq(x,SignedWadMath.fromDaysWadUnsafe(y),"minutesToDays!=DaysToMinuues");
}
//xy=yx
function testunsafeWadMul(int256 x,int256 y) public{
x = clampBetween(x, 1,10**18);
y=clampBetween(y,1,10**18);
assertEq(SignedWadMath.unsafeWadMul(x,y),SignedWadMath.unsafeWadMul(y,x),"xy != yx");
}
// x / y = x(1/y)
function testunsafeWadMul1(int256 x,int256 y) public{
x = clampBetween(x, 0,10**18);
emit Debug(x);
y=clampBetween(y,1,10**18);
emit Debug(y);
int256 resDiv= SignedWadMath.unsafeWadDiv(1,y);
emit Debug(resDiv);
assertEq(SignedWadMath.unsafeWadDiv(x,y),SignedWadMath.unsafeWadMul(x,resDiv),"x / y != x(1/y)");
}
// x>y => x/y>1
function testunsafeWadMul2(int256 x, int256 y) public {
x = clampBetween(x, 1,10**18);
y = clampBetween(y,1,10**18);
if(x>=y){
// Ensure that x <= uint(y)
assertLte(1, uint(SignedWadMath.unsafeWadDiv(x,y)), "x>y but x/y>!1");
}else {
// Ensure that x <= uint(y)
assertLte(1, uint(SignedWadMath.unsafeWadDiv(y,x)), "y>x but y/x>!1");
}
}
// 1/(x*y)= (1/x)*(1/y)
function testunsafeWadDiv(int256 x, int256 y) public {
x = clampBetween(x, 1,10**18);
y = clampBetween(y,1,10**18);
int256 Div1x= SignedWadMath.unsafeWadDiv(1,x);
int256 Div1y= SignedWadMath.unsafeWadDiv(1,y);
int Mul1x1y= SignedWadMath.unsafeWadMul(Div1x,Div1y);
int256 Mulxy= SignedWadMath.unsafeWadMul(x,y);
assertEq(SignedWadMath.unsafeWadDiv(1,Mulxy),Mul1x1y,"1/(x*y)!= (1/x)*(1/y)");
}
// if a>b then always a^x > b^x for x>0
// if a>b then always a^x < b^x for x<0
//x = 505456470057136353;, y = 505456461792312955; prb math is throwing error for this x&y
//checked here this lib is good
function testwadPow(int256 x,int256 y,int256 a) public {
x = clampBetween(x, 0,10**18);
y = clampBetween(y,0,10**18);
a= clampBetween(y,-10**18,10**18);
int256 t=x;
if(x<y){
x=y;
y=t;
}
if(x==y){
x=x+1;
}
if(a>0){
int256 resxa= SignedWadMath.wadPow(x,a);
int256 resya= SignedWadMath.wadPow(y,a);
// for m<n
assertLte(resya,resxa,"x^a > y^a failed");
}else if(a<0){
int256 resxa= SignedWadMath.wadPow(x,a);
int256 resya= SignedWadMath.wadPow(y,a);
// for m<n
assertLte(resxa,resya,"x^a < y^a failed");
}
}
// i think again error due to precision loss of 1 wei as said by josselin
function totestLogAndExp(int256 x) public{
x = clampBetween(x, -10**18,10**18);
int256 resExp= SignedWadMath.wadExp(x);
int256 resLog= SignedWadMath.wadLn(resExp);
assertEq(x,resLog,"x!=log(exp(x))");
}
}