Remove code in preparation for v5.0 (#4258)
Co-authored-by: Ernesto García <ernestognw@gmail.com> Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -5,6 +5,21 @@ const { Rounding } = require('../../helpers/enums.js');
|
||||
|
||||
const Math = artifacts.require('$Math');
|
||||
|
||||
function expectStruct(value, expected) {
|
||||
for (const key in expected) {
|
||||
if (BN.isBN(value[key])) {
|
||||
expect(value[key]).to.be.bignumber.equal(expected[key]);
|
||||
} else {
|
||||
expect(value[key]).to.be.equal(expected[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function testCommutativeIterable(fn, lhs, rhs, expected, ...extra) {
|
||||
expectStruct(await fn(lhs, rhs, ...extra), expected);
|
||||
expectStruct(await fn(rhs, lhs, ...extra), expected);
|
||||
}
|
||||
|
||||
contract('Math', function () {
|
||||
const min = new BN('1234');
|
||||
const max = new BN('5678');
|
||||
@ -15,6 +30,130 @@ contract('Math', function () {
|
||||
this.math = await Math.new();
|
||||
});
|
||||
|
||||
describe('tryAdd', function () {
|
||||
it('adds correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
await testCommutativeIterable(this.math.$tryAdd, a, b, [true, a.add(b)]);
|
||||
});
|
||||
|
||||
it('reverts on addition overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('1');
|
||||
|
||||
await testCommutativeIterable(this.math.$tryAdd, a, b, [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('trySub', function () {
|
||||
it('subtracts correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expectStruct(await this.math.$trySub(a, b), [true, a.sub(b)]);
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$trySub(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryMul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutativeIterable(this.math.$tryMul, a, b, [true, a.mul(b)]);
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutativeIterable(this.math.$tryMul, a, b, [true, a.mul(b)]);
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('2');
|
||||
|
||||
await testCommutativeIterable(this.math.$tryMul, a, b, [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryDiv', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.math.$tryDiv(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryMod', function () {
|
||||
describe('modulos correctly', async function () {
|
||||
it('when the dividend is smaller than the divisor', async function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.math.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts with a 0 divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.math.$tryMod(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('max', function () {
|
||||
it('is correctly detected in first argument position', async function () {
|
||||
expect(await this.math.$max(max, min)).to.be.bignumber.equal(max);
|
||||
|
||||
Reference in New Issue
Block a user