Math.test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. function expectStruct(value, expected) {
  7. for (const key in expected) {
  8. if (BN.isBN(value[key])) {
  9. expect(value[key]).to.be.bignumber.equal(expected[key]);
  10. } else {
  11. expect(value[key]).to.be.equal(expected[key]);
  12. }
  13. }
  14. }
  15. async function testCommutativeIterable(fn, lhs, rhs, expected, ...extra) {
  16. expectStruct(await fn(lhs, rhs, ...extra), expected);
  17. expectStruct(await fn(rhs, lhs, ...extra), expected);
  18. }
  19. contract('Math', function () {
  20. const min = new BN('1234');
  21. const max = new BN('5678');
  22. const MAX_UINT256_SUB1 = MAX_UINT256.sub(new BN('1'));
  23. const MAX_UINT256_SUB2 = MAX_UINT256.sub(new BN('2'));
  24. beforeEach(async function () {
  25. this.math = await Math.new();
  26. });
  27. describe('tryAdd', function () {
  28. it('adds correctly', async function () {
  29. const a = new BN('5678');
  30. const b = new BN('1234');
  31. await testCommutativeIterable(this.math.$tryAdd, a, b, [true, a.add(b)]);
  32. });
  33. it('reverts on addition overflow', async function () {
  34. const a = MAX_UINT256;
  35. const b = new BN('1');
  36. await testCommutativeIterable(this.math.$tryAdd, a, b, [false, '0']);
  37. });
  38. });
  39. describe('trySub', function () {
  40. it('subtracts correctly', async function () {
  41. const a = new BN('5678');
  42. const b = new BN('1234');
  43. expectStruct(await this.math.$trySub(a, b), [true, a.sub(b)]);
  44. });
  45. it('reverts if subtraction result would be negative', async function () {
  46. const a = new BN('1234');
  47. const b = new BN('5678');
  48. expectStruct(await this.math.$trySub(a, b), [false, '0']);
  49. });
  50. });
  51. describe('tryMul', function () {
  52. it('multiplies correctly', async function () {
  53. const a = new BN('1234');
  54. const b = new BN('5678');
  55. await testCommutativeIterable(this.math.$tryMul, a, b, [true, a.mul(b)]);
  56. });
  57. it('multiplies by zero correctly', async function () {
  58. const a = new BN('0');
  59. const b = new BN('5678');
  60. await testCommutativeIterable(this.math.$tryMul, a, b, [true, a.mul(b)]);
  61. });
  62. it('reverts on multiplication overflow', async function () {
  63. const a = MAX_UINT256;
  64. const b = new BN('2');
  65. await testCommutativeIterable(this.math.$tryMul, a, b, [false, '0']);
  66. });
  67. });
  68. describe('tryDiv', function () {
  69. it('divides correctly', async function () {
  70. const a = new BN('5678');
  71. const b = new BN('5678');
  72. expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
  73. });
  74. it('divides zero correctly', async function () {
  75. const a = new BN('0');
  76. const b = new BN('5678');
  77. expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
  78. });
  79. it('returns complete number result on non-even division', async function () {
  80. const a = new BN('7000');
  81. const b = new BN('5678');
  82. expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
  83. });
  84. it('reverts on division by zero', async function () {
  85. const a = new BN('5678');
  86. const b = new BN('0');
  87. expectStruct(await this.math.$tryDiv(a, b), [false, '0']);
  88. });
  89. });
  90. describe('tryMod', function () {
  91. describe('modulos correctly', async function () {
  92. it('when the dividend is smaller than the divisor', async function () {
  93. const a = new BN('284');
  94. const b = new BN('5678');
  95. expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
  96. });
  97. it('when the dividend is equal to the divisor', async function () {
  98. const a = new BN('5678');
  99. const b = new BN('5678');
  100. expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
  101. });
  102. it('when the dividend is larger than the divisor', async function () {
  103. const a = new BN('7000');
  104. const b = new BN('5678');
  105. expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
  106. });
  107. it('when the dividend is a multiple of the divisor', async function () {
  108. const a = new BN('17034'); // 17034 == 5678 * 3
  109. const b = new BN('5678');
  110. expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
  111. });
  112. });
  113. it('reverts with a 0 divisor', async function () {
  114. const a = new BN('5678');
  115. const b = new BN('0');
  116. expectStruct(await this.math.$tryMod(a, b), [false, '0']);
  117. });
  118. });
  119. describe('max', function () {
  120. it('is correctly detected in first argument position', async function () {
  121. expect(await this.math.$max(max, min)).to.be.bignumber.equal(max);
  122. });
  123. it('is correctly detected in second argument position', async function () {
  124. expect(await this.math.$max(min, max)).to.be.bignumber.equal(max);
  125. });
  126. });
  127. describe('min', function () {
  128. it('is correctly detected in first argument position', async function () {
  129. expect(await this.math.$min(min, max)).to.be.bignumber.equal(min);
  130. });
  131. it('is correctly detected in second argument position', async function () {
  132. expect(await this.math.$min(max, min)).to.be.bignumber.equal(min);
  133. });
  134. });
  135. describe('average', function () {
  136. function bnAverage(a, b) {
  137. return a.add(b).divn(2);
  138. }
  139. it('is correctly calculated with two odd numbers', async function () {
  140. const a = new BN('57417');
  141. const b = new BN('95431');
  142. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  143. });
  144. it('is correctly calculated with two even numbers', async function () {
  145. const a = new BN('42304');
  146. const b = new BN('84346');
  147. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  148. });
  149. it('is correctly calculated with one even and one odd number', async function () {
  150. const a = new BN('57417');
  151. const b = new BN('84346');
  152. expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  153. });
  154. it('is correctly calculated with two max uint256 numbers', async function () {
  155. const a = MAX_UINT256;
  156. expect(await this.math.$average(a, a)).to.be.bignumber.equal(bnAverage(a, a));
  157. });
  158. });
  159. describe('ceilDiv', function () {
  160. it('does not round up on exact division', async function () {
  161. const a = new BN('10');
  162. const b = new BN('5');
  163. expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('2');
  164. });
  165. it('rounds up on division with remainders', async function () {
  166. const a = new BN('42');
  167. const b = new BN('13');
  168. expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('4');
  169. });
  170. it('does not overflow', async function () {
  171. const b = new BN('2');
  172. const result = new BN('1').shln(255);
  173. expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(result);
  174. });
  175. it('correctly computes max uint256 divided by 1', async function () {
  176. const b = new BN('1');
  177. expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
  178. });
  179. });
  180. describe('muldiv', function () {
  181. it('divide by 0', async function () {
  182. await expectRevert.unspecified(this.math.$mulDiv(1, 1, 0, Rounding.Down));
  183. });
  184. describe('does round down', async function () {
  185. it('small values', async function () {
  186. expect(await this.math.$mulDiv('3', '4', '5', Rounding.Down)).to.be.bignumber.equal('2');
  187. expect(await this.math.$mulDiv('3', '5', '5', Rounding.Down)).to.be.bignumber.equal('3');
  188. });
  189. it('large values', async function () {
  190. expect(
  191. await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  192. ).to.be.bignumber.equal(new BN('41'));
  193. expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  194. new BN('17'),
  195. );
  196. expect(
  197. await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  198. ).to.be.bignumber.equal(MAX_UINT256_SUB2);
  199. expect(
  200. await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
  201. ).to.be.bignumber.equal(MAX_UINT256_SUB1);
  202. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  203. MAX_UINT256,
  204. );
  205. });
  206. });
  207. describe('does round up', async function () {
  208. it('small values', async function () {
  209. expect(await this.math.$mulDiv('3', '4', '5', Rounding.Up)).to.be.bignumber.equal('3');
  210. expect(await this.math.$mulDiv('3', '5', '5', Rounding.Up)).to.be.bignumber.equal('3');
  211. });
  212. it('large values', async function () {
  213. expect(await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  214. new BN('42'),
  215. );
  216. expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  217. new BN('17'),
  218. );
  219. expect(
  220. await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up),
  221. ).to.be.bignumber.equal(MAX_UINT256_SUB1);
  222. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  223. MAX_UINT256_SUB1,
  224. );
  225. expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  226. MAX_UINT256,
  227. );
  228. });
  229. });
  230. });
  231. describe('sqrt', function () {
  232. it('rounds down', async function () {
  233. expect(await this.math.$sqrt('0', Rounding.Down)).to.be.bignumber.equal('0');
  234. expect(await this.math.$sqrt('1', Rounding.Down)).to.be.bignumber.equal('1');
  235. expect(await this.math.$sqrt('2', Rounding.Down)).to.be.bignumber.equal('1');
  236. expect(await this.math.$sqrt('3', Rounding.Down)).to.be.bignumber.equal('1');
  237. expect(await this.math.$sqrt('4', Rounding.Down)).to.be.bignumber.equal('2');
  238. expect(await this.math.$sqrt('144', Rounding.Down)).to.be.bignumber.equal('12');
  239. expect(await this.math.$sqrt('999999', Rounding.Down)).to.be.bignumber.equal('999');
  240. expect(await this.math.$sqrt('1000000', Rounding.Down)).to.be.bignumber.equal('1000');
  241. expect(await this.math.$sqrt('1000001', Rounding.Down)).to.be.bignumber.equal('1000');
  242. expect(await this.math.$sqrt('1002000', Rounding.Down)).to.be.bignumber.equal('1000');
  243. expect(await this.math.$sqrt('1002001', Rounding.Down)).to.be.bignumber.equal('1001');
  244. expect(await this.math.$sqrt(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  245. '340282366920938463463374607431768211455',
  246. );
  247. });
  248. it('rounds up', async function () {
  249. expect(await this.math.$sqrt('0', Rounding.Up)).to.be.bignumber.equal('0');
  250. expect(await this.math.$sqrt('1', Rounding.Up)).to.be.bignumber.equal('1');
  251. expect(await this.math.$sqrt('2', Rounding.Up)).to.be.bignumber.equal('2');
  252. expect(await this.math.$sqrt('3', Rounding.Up)).to.be.bignumber.equal('2');
  253. expect(await this.math.$sqrt('4', Rounding.Up)).to.be.bignumber.equal('2');
  254. expect(await this.math.$sqrt('144', Rounding.Up)).to.be.bignumber.equal('12');
  255. expect(await this.math.$sqrt('999999', Rounding.Up)).to.be.bignumber.equal('1000');
  256. expect(await this.math.$sqrt('1000000', Rounding.Up)).to.be.bignumber.equal('1000');
  257. expect(await this.math.$sqrt('1000001', Rounding.Up)).to.be.bignumber.equal('1001');
  258. expect(await this.math.$sqrt('1002000', Rounding.Up)).to.be.bignumber.equal('1001');
  259. expect(await this.math.$sqrt('1002001', Rounding.Up)).to.be.bignumber.equal('1001');
  260. expect(await this.math.$sqrt(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
  261. '340282366920938463463374607431768211456',
  262. );
  263. });
  264. });
  265. describe('log', function () {
  266. describe('log2', function () {
  267. it('rounds down', async function () {
  268. // For some reason calling .$log2() directly fails
  269. expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Down)).to.be.bignumber.equal('0');
  270. expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Down)).to.be.bignumber.equal('0');
  271. expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Down)).to.be.bignumber.equal('1');
  272. expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Down)).to.be.bignumber.equal('1');
  273. expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Down)).to.be.bignumber.equal('2');
  274. expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Down)).to.be.bignumber.equal('2');
  275. expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Down)).to.be.bignumber.equal('2');
  276. expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Down)).to.be.bignumber.equal('2');
  277. expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Down)).to.be.bignumber.equal('3');
  278. expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Down)).to.be.bignumber.equal('3');
  279. expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
  280. '255',
  281. );
  282. });
  283. it('rounds up', async function () {
  284. // For some reason calling .$log2() directly fails
  285. expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Up)).to.be.bignumber.equal('0');
  286. expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Up)).to.be.bignumber.equal('0');
  287. expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Up)).to.be.bignumber.equal('1');
  288. expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Up)).to.be.bignumber.equal('2');
  289. expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Up)).to.be.bignumber.equal('2');
  290. expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Up)).to.be.bignumber.equal('3');
  291. expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Up)).to.be.bignumber.equal('3');
  292. expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Up)).to.be.bignumber.equal('3');
  293. expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Up)).to.be.bignumber.equal('3');
  294. expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Up)).to.be.bignumber.equal('4');
  295. expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('256');
  296. });
  297. });
  298. describe('log10', function () {
  299. it('rounds down', async function () {
  300. expect(await this.math.$log10('0', Rounding.Down)).to.be.bignumber.equal('0');
  301. expect(await this.math.$log10('1', Rounding.Down)).to.be.bignumber.equal('0');
  302. expect(await this.math.$log10('2', Rounding.Down)).to.be.bignumber.equal('0');
  303. expect(await this.math.$log10('9', Rounding.Down)).to.be.bignumber.equal('0');
  304. expect(await this.math.$log10('10', Rounding.Down)).to.be.bignumber.equal('1');
  305. expect(await this.math.$log10('11', Rounding.Down)).to.be.bignumber.equal('1');
  306. expect(await this.math.$log10('99', Rounding.Down)).to.be.bignumber.equal('1');
  307. expect(await this.math.$log10('100', Rounding.Down)).to.be.bignumber.equal('2');
  308. expect(await this.math.$log10('101', Rounding.Down)).to.be.bignumber.equal('2');
  309. expect(await this.math.$log10('999', Rounding.Down)).to.be.bignumber.equal('2');
  310. expect(await this.math.$log10('1000', Rounding.Down)).to.be.bignumber.equal('3');
  311. expect(await this.math.$log10('1001', Rounding.Down)).to.be.bignumber.equal('3');
  312. expect(await this.math.$log10(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('77');
  313. });
  314. it('rounds up', async function () {
  315. expect(await this.math.$log10('0', Rounding.Up)).to.be.bignumber.equal('0');
  316. expect(await this.math.$log10('1', Rounding.Up)).to.be.bignumber.equal('0');
  317. expect(await this.math.$log10('2', Rounding.Up)).to.be.bignumber.equal('1');
  318. expect(await this.math.$log10('9', Rounding.Up)).to.be.bignumber.equal('1');
  319. expect(await this.math.$log10('10', Rounding.Up)).to.be.bignumber.equal('1');
  320. expect(await this.math.$log10('11', Rounding.Up)).to.be.bignumber.equal('2');
  321. expect(await this.math.$log10('99', Rounding.Up)).to.be.bignumber.equal('2');
  322. expect(await this.math.$log10('100', Rounding.Up)).to.be.bignumber.equal('2');
  323. expect(await this.math.$log10('101', Rounding.Up)).to.be.bignumber.equal('3');
  324. expect(await this.math.$log10('999', Rounding.Up)).to.be.bignumber.equal('3');
  325. expect(await this.math.$log10('1000', Rounding.Up)).to.be.bignumber.equal('3');
  326. expect(await this.math.$log10('1001', Rounding.Up)).to.be.bignumber.equal('4');
  327. expect(await this.math.$log10(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('78');
  328. });
  329. });
  330. describe('log256', function () {
  331. it('rounds down', async function () {
  332. expect(await this.math.$log256('0', Rounding.Down)).to.be.bignumber.equal('0');
  333. expect(await this.math.$log256('1', Rounding.Down)).to.be.bignumber.equal('0');
  334. expect(await this.math.$log256('2', Rounding.Down)).to.be.bignumber.equal('0');
  335. expect(await this.math.$log256('255', Rounding.Down)).to.be.bignumber.equal('0');
  336. expect(await this.math.$log256('256', Rounding.Down)).to.be.bignumber.equal('1');
  337. expect(await this.math.$log256('257', Rounding.Down)).to.be.bignumber.equal('1');
  338. expect(await this.math.$log256('65535', Rounding.Down)).to.be.bignumber.equal('1');
  339. expect(await this.math.$log256('65536', Rounding.Down)).to.be.bignumber.equal('2');
  340. expect(await this.math.$log256('65537', Rounding.Down)).to.be.bignumber.equal('2');
  341. expect(await this.math.$log256(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('31');
  342. });
  343. it('rounds up', async function () {
  344. expect(await this.math.$log256('0', Rounding.Up)).to.be.bignumber.equal('0');
  345. expect(await this.math.$log256('1', Rounding.Up)).to.be.bignumber.equal('0');
  346. expect(await this.math.$log256('2', Rounding.Up)).to.be.bignumber.equal('1');
  347. expect(await this.math.$log256('255', Rounding.Up)).to.be.bignumber.equal('1');
  348. expect(await this.math.$log256('256', Rounding.Up)).to.be.bignumber.equal('1');
  349. expect(await this.math.$log256('257', Rounding.Up)).to.be.bignumber.equal('2');
  350. expect(await this.math.$log256('65535', Rounding.Up)).to.be.bignumber.equal('2');
  351. expect(await this.math.$log256('65536', Rounding.Up)).to.be.bignumber.equal('2');
  352. expect(await this.math.$log256('65537', Rounding.Up)).to.be.bignumber.equal('3');
  353. expect(await this.math.$log256(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('32');
  354. });
  355. });
  356. });
  357. });