Math.t.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  22. function _squareBigger(uint256 value, uint256 ref) private pure returns (bool) {
  23. (bool noOverflow, uint256 square) = SafeMath.tryMul(value, value);
  24. return !noOverflow || square > ref;
  25. }
  26. function _squareSmaller(uint256 value, uint256 ref) private pure returns (bool) {
  27. return value * value < ref;
  28. }
  29. // LOG2
  30. function testLog2(uint256 input, uint8 r) public {
  31. Math.Rounding rounding = _asRounding(r);
  32. uint256 result = Math.log2(input, rounding);
  33. if (input == 0) {
  34. assertEq(result, 0);
  35. } else if (_powerOf2Bigger(result, input)) {
  36. assertTrue(rounding == Math.Rounding.Up);
  37. assertTrue(_powerOf2Smaller(result - 1, input));
  38. } else if (_powerOf2Smaller(result, input)) {
  39. assertFalse(rounding == Math.Rounding.Up);
  40. assertTrue(_powerOf2Bigger(result + 1, input));
  41. }
  42. }
  43. function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  44. return value >= 256 || 2**value > ref; // 2**256 overflows uint256
  45. }
  46. function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  47. return 2**value < ref;
  48. }
  49. // LOG10
  50. function testLog10(uint256 input, uint8 r) public {
  51. Math.Rounding rounding = _asRounding(r);
  52. uint256 result = Math.log10(input, rounding);
  53. if (input == 0) {
  54. assertEq(result, 0);
  55. } else if (_powerOf10Bigger(result, input)) {
  56. assertTrue(rounding == Math.Rounding.Up);
  57. assertTrue(_powerOf10Smaller(result - 1, input));
  58. } else if (_powerOf10Smaller(result, input)) {
  59. assertFalse(rounding == Math.Rounding.Up);
  60. assertTrue(_powerOf10Bigger(result + 1, input));
  61. }
  62. }
  63. function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  64. return value >= 78 || 10**value > ref; // 10**78 overflows uint256
  65. }
  66. function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  67. return 10**value < ref;
  68. }
  69. // LOG256
  70. function testLog256(uint256 input, uint8 r) public {
  71. Math.Rounding rounding = _asRounding(r);
  72. uint256 result = Math.log256(input, rounding);
  73. if (input == 0) {
  74. assertEq(result, 0);
  75. } else if (_powerOf256Bigger(result, input)) {
  76. assertTrue(rounding == Math.Rounding.Up);
  77. assertTrue(_powerOf256Smaller(result - 1, input));
  78. } else if (_powerOf256Smaller(result, input)) {
  79. assertFalse(rounding == Math.Rounding.Up);
  80. assertTrue(_powerOf256Bigger(result + 1, input));
  81. }
  82. }
  83. function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
  84. return value >= 32 || 256**value > ref; // 256**32 overflows uint256
  85. }
  86. function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
  87. return 256**value < ref;
  88. }
  89. // Helpers
  90. function _asRounding(uint8 r) private returns (Math.Rounding) {
  91. vm.assume(r < uint8(type(Math.Rounding).max));
  92. return Math.Rounding(r);
  93. }
  94. }