Change behavior of ceilDiv(0, 0) and improve test coverage (#4348)

This commit is contained in:
Ernesto García
2023-06-14 14:21:42 -06:00
committed by GitHub
parent ac5480e7ca
commit 2477534260
10 changed files with 178 additions and 2 deletions

View File

@ -2,6 +2,7 @@ const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { MAX_UINT256 } = constants;
const { Rounding } = require('../../helpers/enums.js');
const { expectRevertCustomError } = require('../../helpers/customError.js');
const Math = artifacts.require('$Math');
@ -204,6 +205,19 @@ contract('Math', function () {
});
describe('ceilDiv', function () {
it('reverts on zero division', async function () {
const a = new BN('2');
const b = new BN('0');
// It's unspecified because it's a low level 0 division error
await expectRevert.unspecified(this.math.$ceilDiv(a, b));
});
it('does not round up a zero result', async function () {
const a = new BN('0');
const b = new BN('2');
expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('0');
});
it('does not round up on exact division', async function () {
const a = new BN('10');
const b = new BN('5');
@ -233,6 +247,10 @@ contract('Math', function () {
await expectRevert.unspecified(this.math.$mulDiv(1, 1, 0, Rounding.Down));
});
it('reverts with result higher than 2 ^ 256', async function () {
await expectRevertCustomError(this.math.$mulDiv(5, MAX_UINT256, 2, Rounding.Down), 'MathOverflowedMulDiv', []);
});
describe('does round down', async function () {
it('small values', async function () {
expect(await this.math.$mulDiv('3', '4', '5', Rounding.Down)).to.be.bignumber.equal('2');