Math.t.sol 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "forge-std/Test.sol";
  4. import "../../../contracts/utils/math/Math.sol";
  5. import "../../../contracts/utils/math/SafeMath.sol";
  6. contract MathTest is Test {
  7. // SQRT
  8. function testSqrt(uint256 input, uint8 r) public {
  9. Math.Rounding rounding = _asRounding(r);
  10. uint256 result = Math.sqrt(input, rounding);
  11. // square of result is bigger than input
  12. if (_squareBigger(result, input)) {
  13. assertTrue(rounding == Math.Rounding.Up);
  14. assertTrue(_squareSmaller(result - 1, input));
  15. }
  16. // square of result is smaller than input
  17. else if (_squareSmaller(result, input)) {
  18. assertFalse(rounding == Math.Rounding.Up);
  19. assertTrue(_squareBigger(result + 1, input));
  20. }
  21. // input is perfect square
  22. else {
  23. assertEq(result * result, input);
  24. }
  25. }
  26. function _squareBigger(uint256 value, uint256 ref) private pure returns (bool) {
  27. (bool noOverflow, uint256 square) = SafeMath.tryMul(value, value);
  28. return !noOverflow || square > ref;
  29. }
  30. function _squareSmaller(uint256 value, uint256 ref) private pure returns (bool) {
  31. return value * value < ref;
  32. }
  33. // LOG2
  34. function testLog2(uint256 input, uint8 r) public {
  35. Math.Rounding rounding = _asRounding(r);
  36. uint256 result = Math.log2(input, rounding);
  37. if (input == 0) {
  38. assertEq(result, 0);
  39. } else if (_powerOf2Bigger(result, input)) {
  40. assertTrue(rounding == Math.Rounding.Up);
  41. assertTrue(_powerOf2Smaller(result - 1, input));
  42. } else if (_powerOf2Smaller(result, input)) {
  43. assertFalse(rounding == Math.Rounding.Up);
  44. assertTrue(_powerOf2Bigger(result + 1, input));
  45. } else {
  46. assertEq(2**result, input);
  47. }
  48. }
  49. function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  50. return value >= 256 || 2**value > ref; // 2**256 overflows uint256
  51. }
  52. function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  53. return 2**value < ref;
  54. }
  55. // LOG10
  56. function testLog10(uint256 input, uint8 r) public {
  57. Math.Rounding rounding = _asRounding(r);
  58. uint256 result = Math.log10(input, rounding);
  59. if (input == 0) {
  60. assertEq(result, 0);
  61. } else if (_powerOf10Bigger(result, input)) {
  62. assertTrue(rounding == Math.Rounding.Up);
  63. assertTrue(_powerOf10Smaller(result - 1, input));
  64. } else if (_powerOf10Smaller(result, input)) {
  65. assertFalse(rounding == Math.Rounding.Up);
  66. assertTrue(_powerOf10Bigger(result + 1, input));
  67. } else {
  68. assertEq(10**result, input);
  69. }
  70. }
  71. function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  72. return value >= 78 || 10**value > ref; // 10**78 overflows uint256
  73. }
  74. function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  75. return 10**value < ref;
  76. }
  77. // LOG256
  78. function testLog256(uint256 input, uint8 r) public {
  79. Math.Rounding rounding = _asRounding(r);
  80. uint256 result = Math.log256(input, rounding);
  81. if (input == 0) {
  82. assertEq(result, 0);
  83. } else if (_powerOf256Bigger(result, input)) {
  84. assertTrue(rounding == Math.Rounding.Up);
  85. assertTrue(_powerOf256Smaller(result - 1, input));
  86. } else if (_powerOf256Smaller(result, input)) {
  87. assertFalse(rounding == Math.Rounding.Up);
  88. assertTrue(_powerOf256Bigger(result + 1, input));
  89. } else {
  90. assertEq(256**result, input);
  91. }
  92. }
  93. function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  94. return value >= 32 || 256**value > ref; // 256**32 overflows uint256
  95. }
  96. function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  97. return 256**value < ref;
  98. }
  99. // Helpers
  100. function _asRounding(uint8 r) private returns (Math.Rounding) {
  101. vm.assume(r < uint8(type(Math.Rounding).max));
  102. return Math.Rounding(r);
  103. }
  104. }