/ test / utils / MoreMath / TestMoreMath.t.sol
TestMoreMath.t.sol
 1  pragma solidity >=0.6.0;
 2  
 3  import "truffle/Assert.sol";
 4  import "../../../contracts/utils/MoreMath.sol";
 5  
 6  contract TestMoreMath {
 7  
 8      function testPowAndMultiply() public {
 9  
10          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1), 1, "powAndMultiply f=1");
11          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1e1), 22, "powAndMultiply f=1e1");
12          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1e2), 256, "powAndMultiply f=1e2");
13          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1e3), 2592, "powAndMultiply f=1e3");
14          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1e4), 25937, "powAndMultiply f=1e4");
15          Assert.equal(MoreMath.powAndMultiply(11, 10, 10, 1e12), 2593742460100, "powAndMultiply f=1e12");
16      }
17  
18      function testPow() public {
19          
20          Assert.equal(MoreMath.pow(2, 0), 1, "pow 2,0");
21          Assert.equal(MoreMath.pow(2, 1), 2, "pow 2,1");
22          Assert.equal(MoreMath.pow(2, 2), 4, "pow 2,2");
23          Assert.equal(MoreMath.pow(2, 3), 8, "pow 2,3");
24          Assert.equal(MoreMath.pow(5, 10), 9765625, "pow 5,10");
25      }
26  
27      function testPowDecimal() public {
28          
29          uint b = 1e9;
30          Assert.equal(MoreMath.powDecimal(2e9, 15e8, b), 2823125404, "pow 2^1.50");
31          Assert.equal(MoreMath.powDecimal(2e9, 50e7, b), 1411562702, "pow 2^0.50");
32          Assert.equal(MoreMath.powDecimal(2e9, 77e7, b), 1703641918, "pow 2^0.77");
33          Assert.equal(MoreMath.powDecimal(2e9, 30e7, b), 1229973362, "pow 2^0.30");
34          Assert.equal(MoreMath.powDecimal(2e9, 3e7, b),  1020119746, "pow 2^0.03");
35          Assert.equal(MoreMath.powDecimal(1024e9, 10e7, b), 1998000721, "pow 1024^0.1");
36          Assert.equal(MoreMath.powDecimal(89021e9, 6612e5, b), 1871857777773, "pow 89021^0.6612");
37      }
38  
39      function testSqrtAndMultiply() public {
40          
41          Assert.equal(MoreMath.sqrtAndMultiply(1, 5), 5, "sqrtAndMultiply 1, 5");
42          Assert.equal(MoreMath.sqrtAndMultiply(9, 3), 9, "sqrtAndMultiply 9, 3");
43          Assert.equal(MoreMath.sqrtAndMultiply(2, 1e10), 14142135620, "sqrtAndMultiply 2, 1e10");
44          Assert.equal(MoreMath.sqrtAndMultiply(653628690, 10), 255661, "sqrtAndMultiply 653628690, 10");
45      }
46  
47      function testSqrt() public {
48          
49          Assert.equal(MoreMath.sqrt(1), 1, "sqrt 1");
50          Assert.equal(MoreMath.sqrt(9), 3, "sqrt 9");
51          Assert.equal(MoreMath.sqrt(16), 4, "sqrt 16");
52          Assert.equal(MoreMath.sqrt(653628690), 25566, "sqrt 653628690");
53      }
54  
55      function testStd() public {
56  
57          int[] memory a = new int[](8);
58          a[0] = 10e10;
59          a[1] = 12e10;
60          a[2] = 23e10;
61          a[3] = 23e10;
62          a[4] = 16e10;
63          a[5] = 23e10;
64          a[6] = 21e10;
65          a[7] = 16e10;
66          Assert.equal(MoreMath.std(a), 48989794855, "std");
67      }
68  
69      function testToString() public {
70  
71          Assert.equal(MoreMath.toString(0), "0", "0");
72          Assert.equal(MoreMath.toString(1), "1", "1");
73          Assert.equal(MoreMath.toString(123), "123", "123");
74          Assert.equal(MoreMath.toString(107680546035), "107680546035", "107680546035");
75          Assert.equal(MoreMath.toString(1e9), "1e9", "1e9");
76          Assert.equal(MoreMath.toString(1 ether), "1e18", "1 ether");
77          Assert.equal(MoreMath.toString(550e8), "55e9", "55e9");
78      }
79  }