Update docs
This commit is contained in:
@ -1,12 +1,15 @@
|
||||
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { MAX_UINT256 } = constants;
|
||||
const { Rounding } = require('../../helpers/enums.js');
|
||||
|
||||
const MathMock = artifacts.require('MathMock');
|
||||
|
||||
contract('Math', function (accounts) {
|
||||
const min = new BN('1234');
|
||||
const max = new BN('5678');
|
||||
const MAX_UINT256_SUB1 = MAX_UINT256.sub(new BN('1'));
|
||||
const MAX_UINT256_SUB2 = MAX_UINT256.sub(new BN('2'));
|
||||
|
||||
beforeEach(async function () {
|
||||
this.math = await MathMock.new();
|
||||
@ -85,4 +88,132 @@ contract('Math', function (accounts) {
|
||||
expect(await this.math.ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
|
||||
});
|
||||
});
|
||||
|
||||
describe('muldiv', function () {
|
||||
it('divide by 0', async function () {
|
||||
await expectRevert.unspecified(this.math.mulDiv(1, 1, 0, Rounding.Down));
|
||||
});
|
||||
|
||||
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');
|
||||
expect(await this.math.mulDiv('3', '5', '5', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
});
|
||||
|
||||
it('large values', async function () {
|
||||
expect(await this.math.mulDiv(
|
||||
new BN('42'),
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Down,
|
||||
)).to.be.bignumber.equal(new BN('41'));
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
new BN('17'),
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
Rounding.Down,
|
||||
)).to.be.bignumber.equal(new BN('17'));
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Down,
|
||||
)).to.be.bignumber.equal(MAX_UINT256_SUB2);
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256,
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Down,
|
||||
)).to.be.bignumber.equal(MAX_UINT256_SUB1);
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
Rounding.Down,
|
||||
)).to.be.bignumber.equal(MAX_UINT256);
|
||||
});
|
||||
});
|
||||
|
||||
describe('does round up', async function () {
|
||||
it('small values', async function () {
|
||||
expect(await this.math.mulDiv('3', '4', '5', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.mulDiv('3', '5', '5', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
});
|
||||
|
||||
it('large values', async function () {
|
||||
expect(await this.math.mulDiv(
|
||||
new BN('42'),
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Up,
|
||||
)).to.be.bignumber.equal(new BN('42'));
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
new BN('17'),
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
Rounding.Up,
|
||||
)).to.be.bignumber.equal(new BN('17'));
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Up,
|
||||
)).to.be.bignumber.equal(MAX_UINT256_SUB1);
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256,
|
||||
MAX_UINT256_SUB1,
|
||||
MAX_UINT256,
|
||||
Rounding.Up,
|
||||
)).to.be.bignumber.equal(MAX_UINT256_SUB1);
|
||||
|
||||
expect(await this.math.mulDiv(
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
MAX_UINT256,
|
||||
Rounding.Up,
|
||||
)).to.be.bignumber.equal(MAX_UINT256);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('sqrt', function () {
|
||||
it('rounds down', async function () {
|
||||
expect(await this.math.sqrt(new BN('0'), Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.sqrt(new BN('1'), Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.sqrt(new BN('2'), Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.sqrt(new BN('3'), Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.sqrt(new BN('4'), Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.sqrt(new BN('144'), Rounding.Down)).to.be.bignumber.equal('12');
|
||||
expect(await this.math.sqrt(new BN('999999'), Rounding.Down)).to.be.bignumber.equal('999');
|
||||
expect(await this.math.sqrt(new BN('1000000'), Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.sqrt(new BN('1000001'), Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.sqrt(new BN('1002000'), Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.sqrt(new BN('1002001'), Rounding.Down)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.sqrt(MAX_UINT256, Rounding.Down))
|
||||
.to.be.bignumber.equal('340282366920938463463374607431768211455');
|
||||
});
|
||||
|
||||
it('rounds up', async function () {
|
||||
expect(await this.math.sqrt(new BN('0'), Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.sqrt(new BN('1'), Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.sqrt(new BN('2'), Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.sqrt(new BN('3'), Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.sqrt(new BN('4'), Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.sqrt(new BN('144'), Rounding.Up)).to.be.bignumber.equal('12');
|
||||
expect(await this.math.sqrt(new BN('999999'), Rounding.Up)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.sqrt(new BN('1000000'), Rounding.Up)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.sqrt(new BN('1000001'), Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.sqrt(new BN('1002000'), Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.sqrt(new BN('1002001'), Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.sqrt(MAX_UINT256, Rounding.Up))
|
||||
.to.be.bignumber.equal('340282366920938463463374607431768211456');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
const { range } = require('../../../scripts/helpers');
|
||||
|
||||
const SafeCastMock = artifacts.require('SafeCastMock');
|
||||
|
||||
@ -41,7 +41,7 @@ contract('SafeCast', async (accounts) => {
|
||||
});
|
||||
}
|
||||
|
||||
[8, 16, 32, 64, 96, 128, 224].forEach(bits => testToUint(bits));
|
||||
range(8, 256, 8).forEach(bits => testToUint(bits));
|
||||
|
||||
describe('toUint256', () => {
|
||||
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
|
||||
@ -129,7 +129,7 @@ contract('SafeCast', async (accounts) => {
|
||||
});
|
||||
}
|
||||
|
||||
[8, 16, 32, 64, 128].forEach(bits => testToInt(bits));
|
||||
range(8, 256, 8).forEach(bits => testToInt(bits));
|
||||
|
||||
describe('toInt256', () => {
|
||||
const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
|
||||
|
||||
Reference in New Issue
Block a user