Math.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { MAX_UINT256 } = constants;
  4. const { Rounding } = require('../../helpers/enums.js');
  5. const Math = artifacts.require('$Math');
  6. contract('Math', function () {
  7. const min = new BN('1234');
  8. const max = new BN('5678');
  9. const MAX_UINT256_SUB1 = MAX_UINT256.sub(new BN('1'));
  10. const MAX_UINT256_SUB2 = MAX_UINT256.sub(new BN('2'));
  11. beforeEach(async function () {
  12. this.math = await Math.new();
  13. });
  14. describe('max', function () {
  15. it('is correctly detected in first argument position', async function () {
  16. expect(await this.math.$max(max, min)).to.be.bignumber.equal(max);
  17. });
  18. it('is correctly detected in second argument position', async function () {
  19. expect(await this.math.$max(min, max)).to.be.bignumber.equal(max);
  20. });
  21. });
  22. describe('min', function () {
  23. it('is correctly detected in first argument position', async function () {
  24. expect(await this.math.$min(min, max)).to.be.bignumber.equal(min);
  25. });
  26. it('is correctly detected in second argument position', async function () {
  27. expect(await this.math.$min(max, min)).to.be.bignumber.equal(min);
  28. });
  29. });
  30. describe('average', function () {
  31. function bnAverage(a, b) {
  32. return a.add(b).divn(2);
  33. }
  34. it('is correctly calculated with two odd numbers', async function () {
  35. const a = new BN('57417');
  36. const b = new BN('95431');
  37. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  38. });
  39. it('is correctly calculated with two even numbers', async function () {
  40. const a = new BN('42304');
  41. const b = new BN('84346');
  42. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  43. });
  44. it('is correctly calculated with one even and one odd number', async function () {
  45. const a = new BN('57417');
  46. const b = new BN('84346');
  47. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  48. });
  49. it('is correctly calculated with two max uint256 numbers', async function () {
  50. const a = MAX_UINT256;
  51. expect(await this.math.$average(a, a)).to.be.bignumber.equal(bnAverage(a, a));
  52. });
  53. });
  54. describe('ceilDiv', function () {
  55. it('does not round up on exact division', async function () {
  56. const a = new BN('10');
  57. const b = new BN('5');
  58. expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('2');
  59. });
  60. it('rounds up on division with remainders', async function () {
  61. const a = new BN('42');
  62. const b = new BN('13');
  63. expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('4');
  64. });
  65. it('does not overflow', async function () {
  66. const b = new BN('2');
  67. const result = new BN('1').shln(255);
  68. expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(result);
  69. });
  70. it('correctly computes max uint256 divided by 1', async function () {
  71. const b = new BN('1');
  72. expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
  73. });
  74. });
  75. describe('muldiv', function () {
  76. it('divide by 0', async function () {
  77. await expectRevert.unspecified(this.math.$mulDiv(1, 1, 0, Rounding.Down));
  78. });
  79. describe('does round down', async function () {
  80. it('small values', async function () {
  81. expect(await this.math.$mulDiv('3', '4', '5', Rounding.Down)).to.be.bignumber.equal('2');
  82. expect(await this.math.$mulDiv('3', '5', '5', Rounding.Down)).to.be.bignumber.equal('3');
  83. });
  84. it('large values', async function () {
  85. expect(
  86. await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  87. ).to.be.bignumber.equal(new BN('41'));
  88. expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  89. new BN('17'),
  90. );
  91. expect(
  92. await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  93. ).to.be.bignumber.equal(MAX_UINT256_SUB2);
  94. expect(
  95. await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  96. ).to.be.bignumber.equal(MAX_UINT256_SUB1);
  97. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  98. MAX_UINT256,
  99. );
  100. });
  101. });
  102. describe('does round up', async function () {
  103. it('small values', async function () {
  104. expect(await this.math.$mulDiv('3', '4', '5', Rounding.Up)).to.be.bignumber.equal('3');
  105. expect(await this.math.$mulDiv('3', '5', '5', Rounding.Up)).to.be.bignumber.equal('3');
  106. });
  107. it('large values', async function () {
  108. expect(await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  109. new BN('42'),
  110. );
  111. expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  112. new BN('17'),
  113. );
  114. expect(
  115. await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up),
  116. ).to.be.bignumber.equal(MAX_UINT256_SUB1);
  117. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  118. MAX_UINT256_SUB1,
  119. );
  120. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  121. MAX_UINT256,
  122. );
  123. });
  124. });
  125. });
  126. describe('sqrt', function () {
  127. it('rounds down', async function () {
  128. expect(await this.math.$sqrt('0', Rounding.Down)).to.be.bignumber.equal('0');
  129. expect(await this.math.$sqrt('1', Rounding.Down)).to.be.bignumber.equal('1');
  130. expect(await this.math.$sqrt('2', Rounding.Down)).to.be.bignumber.equal('1');
  131. expect(await this.math.$sqrt('3', Rounding.Down)).to.be.bignumber.equal('1');
  132. expect(await this.math.$sqrt('4', Rounding.Down)).to.be.bignumber.equal('2');
  133. expect(await this.math.$sqrt('144', Rounding.Down)).to.be.bignumber.equal('12');
  134. expect(await this.math.$sqrt('999999', Rounding.Down)).to.be.bignumber.equal('999');
  135. expect(await this.math.$sqrt('1000000', Rounding.Down)).to.be.bignumber.equal('1000');
  136. expect(await this.math.$sqrt('1000001', Rounding.Down)).to.be.bignumber.equal('1000');
  137. expect(await this.math.$sqrt('1002000', Rounding.Down)).to.be.bignumber.equal('1000');
  138. expect(await this.math.$sqrt('1002001', Rounding.Down)).to.be.bignumber.equal('1001');
  139. expect(await this.math.$sqrt(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  140. '340282366920938463463374607431768211455',
  141. );
  142. });
  143. it('rounds up', async function () {
  144. expect(await this.math.$sqrt('0', Rounding.Up)).to.be.bignumber.equal('0');
  145. expect(await this.math.$sqrt('1', Rounding.Up)).to.be.bignumber.equal('1');
  146. expect(await this.math.$sqrt('2', Rounding.Up)).to.be.bignumber.equal('2');
  147. expect(await this.math.$sqrt('3', Rounding.Up)).to.be.bignumber.equal('2');
  148. expect(await this.math.$sqrt('4', Rounding.Up)).to.be.bignumber.equal('2');
  149. expect(await this.math.$sqrt('144', Rounding.Up)).to.be.bignumber.equal('12');
  150. expect(await this.math.$sqrt('999999', Rounding.Up)).to.be.bignumber.equal('1000');
  151. expect(await this.math.$sqrt('1000000', Rounding.Up)).to.be.bignumber.equal('1000');
  152. expect(await this.math.$sqrt('1000001', Rounding.Up)).to.be.bignumber.equal('1001');
  153. expect(await this.math.$sqrt('1002000', Rounding.Up)).to.be.bignumber.equal('1001');
  154. expect(await this.math.$sqrt('1002001', Rounding.Up)).to.be.bignumber.equal('1001');
  155. expect(await this.math.$sqrt(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  156. '340282366920938463463374607431768211456',
  157. );
  158. });
  159. });
  160. describe('log', function () {
  161. describe('log2', function () {
  162. it('rounds down', async function () {
  163. // For some reason calling .$log2() directly fails
  164. expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Down)).to.be.bignumber.equal('0');
  165. expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Down)).to.be.bignumber.equal('0');
  166. expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Down)).to.be.bignumber.equal('1');
  167. expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Down)).to.be.bignumber.equal('1');
  168. expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Down)).to.be.bignumber.equal('2');
  169. expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Down)).to.be.bignumber.equal('2');
  170. expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Down)).to.be.bignumber.equal('2');
  171. expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Down)).to.be.bignumber.equal('2');
  172. expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Down)).to.be.bignumber.equal('3');
  173. expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Down)).to.be.bignumber.equal('3');
  174. expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  175. '255',
  176. );
  177. });
  178. it('rounds up', async function () {
  179. // For some reason calling .$log2() directly fails
  180. expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Up)).to.be.bignumber.equal('0');
  181. expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Up)).to.be.bignumber.equal('0');
  182. expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Up)).to.be.bignumber.equal('1');
  183. expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Up)).to.be.bignumber.equal('2');
  184. expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Up)).to.be.bignumber.equal('2');
  185. expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Up)).to.be.bignumber.equal('3');
  186. expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Up)).to.be.bignumber.equal('3');
  187. expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Up)).to.be.bignumber.equal('3');
  188. expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Up)).to.be.bignumber.equal('3');
  189. expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Up)).to.be.bignumber.equal('4');
  190. expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('256');
  191. });
  192. });
  193. describe('log10', function () {
  194. it('rounds down', async function () {
  195. expect(await this.math.$log10('0', Rounding.Down)).to.be.bignumber.equal('0');
  196. expect(await this.math.$log10('1', Rounding.Down)).to.be.bignumber.equal('0');
  197. expect(await this.math.$log10('2', Rounding.Down)).to.be.bignumber.equal('0');
  198. expect(await this.math.$log10('9', Rounding.Down)).to.be.bignumber.equal('0');
  199. expect(await this.math.$log10('10', Rounding.Down)).to.be.bignumber.equal('1');
  200. expect(await this.math.$log10('11', Rounding.Down)).to.be.bignumber.equal('1');
  201. expect(await this.math.$log10('99', Rounding.Down)).to.be.bignumber.equal('1');
  202. expect(await this.math.$log10('100', Rounding.Down)).to.be.bignumber.equal('2');
  203. expect(await this.math.$log10('101', Rounding.Down)).to.be.bignumber.equal('2');
  204. expect(await this.math.$log10('999', Rounding.Down)).to.be.bignumber.equal('2');
  205. expect(await this.math.$log10('1000', Rounding.Down)).to.be.bignumber.equal('3');
  206. expect(await this.math.$log10('1001', Rounding.Down)).to.be.bignumber.equal('3');
  207. expect(await this.math.$log10(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('77');
  208. });
  209. it('rounds up', async function () {
  210. expect(await this.math.$log10('0', Rounding.Up)).to.be.bignumber.equal('0');
  211. expect(await this.math.$log10('1', Rounding.Up)).to.be.bignumber.equal('0');
  212. expect(await this.math.$log10('2', Rounding.Up)).to.be.bignumber.equal('1');
  213. expect(await this.math.$log10('9', Rounding.Up)).to.be.bignumber.equal('1');
  214. expect(await this.math.$log10('10', Rounding.Up)).to.be.bignumber.equal('1');
  215. expect(await this.math.$log10('11', Rounding.Up)).to.be.bignumber.equal('2');
  216. expect(await this.math.$log10('99', Rounding.Up)).to.be.bignumber.equal('2');
  217. expect(await this.math.$log10('100', Rounding.Up)).to.be.bignumber.equal('2');
  218. expect(await this.math.$log10('101', Rounding.Up)).to.be.bignumber.equal('3');
  219. expect(await this.math.$log10('999', Rounding.Up)).to.be.bignumber.equal('3');
  220. expect(await this.math.$log10('1000', Rounding.Up)).to.be.bignumber.equal('3');
  221. expect(await this.math.$log10('1001', Rounding.Up)).to.be.bignumber.equal('4');
  222. expect(await this.math.$log10(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('78');
  223. });
  224. });
  225. describe('log256', function () {
  226. it('rounds down', async function () {
  227. expect(await this.math.$log256('0', Rounding.Down)).to.be.bignumber.equal('0');
  228. expect(await this.math.$log256('1', Rounding.Down)).to.be.bignumber.equal('0');
  229. expect(await this.math.$log256('2', Rounding.Down)).to.be.bignumber.equal('0');
  230. expect(await this.math.$log256('255', Rounding.Down)).to.be.bignumber.equal('0');
  231. expect(await this.math.$log256('256', Rounding.Down)).to.be.bignumber.equal('1');
  232. expect(await this.math.$log256('257', Rounding.Down)).to.be.bignumber.equal('1');
  233. expect(await this.math.$log256('65535', Rounding.Down)).to.be.bignumber.equal('1');
  234. expect(await this.math.$log256('65536', Rounding.Down)).to.be.bignumber.equal('2');
  235. expect(await this.math.$log256('65537', Rounding.Down)).to.be.bignumber.equal('2');
  236. expect(await this.math.$log256(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('31');
  237. });
  238. it('rounds up', async function () {
  239. expect(await this.math.$log256('0', Rounding.Up)).to.be.bignumber.equal('0');
  240. expect(await this.math.$log256('1', Rounding.Up)).to.be.bignumber.equal('0');
  241. expect(await this.math.$log256('2', Rounding.Up)).to.be.bignumber.equal('1');
  242. expect(await this.math.$log256('255', Rounding.Up)).to.be.bignumber.equal('1');
  243. expect(await this.math.$log256('256', Rounding.Up)).to.be.bignumber.equal('1');
  244. expect(await this.math.$log256('257', Rounding.Up)).to.be.bignumber.equal('2');
  245. expect(await this.math.$log256('65535', Rounding.Up)).to.be.bignumber.equal('2');
  246. expect(await this.math.$log256('65536', Rounding.Up)).to.be.bignumber.equal('2');
  247. expect(await this.math.$log256('65537', Rounding.Up)).to.be.bignumber.equal('3');
  248. expect(await this.math.$log256(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('32');
  249. });
  250. });
  251. });
  252. });