Math.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const { BN, constants } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { MAX_UINT256 } = constants;
  4. const MathMock = artifacts.require('MathMock');
  5. contract('Math', function (accounts) {
  6. const min = new BN('1234');
  7. const max = new BN('5678');
  8. beforeEach(async function () {
  9. this.math = await MathMock.new();
  10. });
  11. describe('max', function () {
  12. it('is correctly detected in first argument position', async function () {
  13. expect(await this.math.max(max, min)).to.be.bignumber.equal(max);
  14. });
  15. it('is correctly detected in second argument position', async function () {
  16. expect(await this.math.max(min, max)).to.be.bignumber.equal(max);
  17. });
  18. });
  19. describe('min', function () {
  20. it('is correctly detected in first argument position', async function () {
  21. expect(await this.math.min(min, max)).to.be.bignumber.equal(min);
  22. });
  23. it('is correctly detected in second argument position', async function () {
  24. expect(await this.math.min(max, min)).to.be.bignumber.equal(min);
  25. });
  26. });
  27. describe('average', function () {
  28. function bnAverage (a, b) {
  29. return a.add(b).divn(2);
  30. }
  31. it('is correctly calculated with two odd numbers', async function () {
  32. const a = new BN('57417');
  33. const b = new BN('95431');
  34. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  35. });
  36. it('is correctly calculated with two even numbers', async function () {
  37. const a = new BN('42304');
  38. const b = new BN('84346');
  39. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  40. });
  41. it('is correctly calculated with one even and one odd number', async function () {
  42. const a = new BN('57417');
  43. const b = new BN('84346');
  44. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  45. });
  46. });
  47. describe('ceilDiv', function () {
  48. it('does not round up on exact division', async function () {
  49. const a = new BN('10');
  50. const b = new BN('5');
  51. expect(await this.math.ceilDiv(a, b)).to.be.bignumber.equal('2');
  52. });
  53. it('rounds up on division with remainders', async function () {
  54. const a = new BN('42');
  55. const b = new BN('13');
  56. expect(await this.math.ceilDiv(a, b)).to.be.bignumber.equal('4');
  57. });
  58. it('does not overflow', async function () {
  59. const b = new BN('2');
  60. const result = new BN('1').shln(255);
  61. expect(await this.math.ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(result);
  62. });
  63. it('correctly computes max uint256 divided by 1', async function () {
  64. const b = new BN('1');
  65. expect(await this.math.ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
  66. });
  67. });
  68. });