Add ceiling division operation to the Math.sol library (#2681)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Nicholas Rodrigues Lordello
2021-05-20 15:53:31 +02:00
committed by GitHub
parent 5f7eda1f98
commit 7c754d0665
4 changed files with 44 additions and 3 deletions

View File

@ -1,6 +1,6 @@
const { BN } = require('@openzeppelin/test-helpers');
const { BN, constants } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { MAX_UINT256 } = constants;
const MathMock = artifacts.require('MathMock');
@ -55,4 +55,29 @@ contract('Math', function (accounts) {
expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
});
});
describe('ceilDiv', function () {
it('does not round up on exact division', async function () {
const a = new BN('10');
const b = new BN('5');
expect(await this.math.ceilDiv(a, b)).to.be.bignumber.equal('2');
});
it('rounds up on division with remainders', async function () {
const a = new BN('42');
const b = new BN('13');
expect(await this.math.ceilDiv(a, b)).to.be.bignumber.equal('4');
});
it('does not overflow', async function () {
const b = new BN('2');
const result = new BN('1').shln(255);
expect(await this.math.ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(result);
});
it('correctly computes max uint256 divided by 1', async function () {
const b = new BN('1');
expect(await this.math.ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
});
});
});