-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedPointMathLibTest.sol
92 lines (68 loc) · 2.46 KB
/
FixedPointMathLibTest.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
pragma solidity 0.8.19;
import {FixedPointMathLib} from "./FixedPointMathLib.sol";
import "./helper.sol";
import "./IVM.sol";
// Run with medusa fuzz --target contracts/FixedPointMathLibTest.sol --deployment-order FixedPointMathLibTest
contract FixedPointMathLibTest is PropertiesAsserts{
event Debug(uint256);
IVM vm = IVM(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
// The following is an example of invariant
// It test that if z = x / y, then z <= x
// For any x and y greater than 1 unit
function testDivWadDown(uint256 x, uint256 y) public{
// We work with a decimals of 18
uint decimals = 10**18;
// Ensure x and y are greater than 1
x = clampGte(x, decimals);
y = clampGte(y, decimals);
// compute z = x / y
uint z = FixedPointMathLib.divWadDown(x, y);
// Ensure that z <= x
assertLte(z, x, "Z should be less or equal to X");
}
/**
* check for all the things that was in signedMathWad
*/
// x*y=y*x
function testrPow(uint256 x,uint256 y,uint256 a) public{
// uint decimals = 10**18;
x = clampBetween(x, 0,10**18);
y = clampBetween(y,0,10**18);
a= clampBetween(y,0,10**18);
//ensure a>0
// a = clampGte(a, 0);
uint256 scalar = 10**18;
uint256 t=x;
if(x<y){
x=y;
y=t;
}
if(x==y){
x=x+1;
}
if(a>0){
uint256 resxa= FixedPointMathLib.rpow(x,a,scalar);
uint256 resya= FixedPointMathLib.rpow(y,a,scalar);
// for m<n
assertLte(resya,resxa,"x^a > y^a failed");
}else if(a<0){
uint256 resxa= FixedPointMathLib.rpow(x,a,scalar);
uint256 resya= FixedPointMathLib.rpow(y,a,scalar);
// for m<n
assertLte(resxa,resya,"x^a < y^a failed");
}
}
// sqrt(x)= rpow(x,1/2,scalar)
function testSqrtToRpow(uint256 x) public{
uint decimals = 10**18;
// Ensure x and y are greater than 1
x = clampGte(x, decimals);
uint256 n=(10**18)/2;
uint256 scalar= 10**18;
// uint256 a= FixedPointMathLib.sqrt(100*(10**18));
// emit Debug(a);
// uint256 b= FixedPointMathLib.rpow(100*(10**18),(10**18)/2,scalar);
// emit Debug(b);
//assertEq(FixedPointMathLib.sqrt(x),FixedPointMathLib.rpow(x,n,scalar),"sqrt(x) != rpow(x,1/2,scalar)");
}
}