Update docs

This commit is contained in:
github-actions
2023-05-09 19:56:30 +00:00
parent 47e38c7bda
commit 6ae39c4dc1
579 changed files with 30453 additions and 21485 deletions

View File

@ -3,9 +3,10 @@ const { MAX_UINT256 } = constants;
const { expect } = require('chai');
const SafeMathMock = artifacts.require('SafeMathMock');
const SafeMath = artifacts.require('$SafeMath');
const SafeMathMemoryCheck = artifacts.require('$SafeMathMemoryCheck');
function expectStruct (value, expected) {
function expectStruct(value, expected) {
for (const key in expected) {
if (BN.isBN(value[key])) {
expect(value[key]).to.be.bignumber.equal(expected[key]);
@ -15,17 +16,17 @@ function expectStruct (value, expected) {
}
}
contract('SafeMath', function (accounts) {
contract('SafeMath', function () {
beforeEach(async function () {
this.safeMath = await SafeMathMock.new();
this.safeMath = await SafeMath.new();
});
async function testCommutative (fn, lhs, rhs, expected, ...extra) {
async function testCommutative(fn, lhs, rhs, expected, ...extra) {
expect(await fn(lhs, rhs, ...extra)).to.be.bignumber.equal(expected);
expect(await fn(rhs, lhs, ...extra)).to.be.bignumber.equal(expected);
}
async function testFailsCommutative (fn, lhs, rhs, reason, ...extra) {
async function testFailsCommutative(fn, lhs, rhs, reason, ...extra) {
if (reason === undefined) {
await expectRevert.unspecified(fn(lhs, rhs, ...extra));
await expectRevert.unspecified(fn(rhs, lhs, ...extra));
@ -35,7 +36,7 @@ contract('SafeMath', function (accounts) {
}
}
async function testCommutativeIterable (fn, lhs, rhs, expected, ...extra) {
async function testCommutativeIterable(fn, lhs, rhs, expected, ...extra) {
expectStruct(await fn(lhs, rhs, ...extra), expected);
expectStruct(await fn(rhs, lhs, ...extra), expected);
}
@ -46,14 +47,14 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('1234');
testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: true, value: a.add(b) });
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [true, a.add(b)]);
});
it('reverts on addition overflow', async function () {
const a = MAX_UINT256;
const b = new BN('1');
testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: false, value: '0' });
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [false, '0']);
});
});
@ -62,14 +63,14 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('1234');
expectStruct(await this.safeMath.trySub(a, b), { flag: true, value: a.sub(b) });
expectStruct(await this.safeMath.$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.safeMath.trySub(a, b), { flag: false, value: '0' });
expectStruct(await this.safeMath.$trySub(a, b), [false, '0']);
});
});
@ -78,21 +79,21 @@ contract('SafeMath', function (accounts) {
const a = new BN('1234');
const b = new BN('5678');
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
testCommutativeIterable(this.safeMath.$tryMul, a, b, [true, a.mul(b)]);
});
it('multiplies by zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
testCommutativeIterable(this.safeMath.$tryMul, a, b, [true, a.mul(b)]);
});
it('reverts on multiplication overflow', async function () {
const a = MAX_UINT256;
const b = new BN('2');
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: false, value: '0' });
testCommutativeIterable(this.safeMath.$tryMul, a, b, [false, '0']);
});
});
@ -101,28 +102,28 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('5678');
expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
expectStruct(await this.safeMath.$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.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
expectStruct(await this.safeMath.$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.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
expectStruct(await this.safeMath.$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.safeMath.tryDiv(a, b), { flag: false, value: '0' });
expectStruct(await this.safeMath.$tryDiv(a, b), [false, '0']);
});
});
@ -132,28 +133,28 @@ contract('SafeMath', function (accounts) {
const a = new BN('284');
const b = new BN('5678');
expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
expectStruct(await this.safeMath.$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.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
expectStruct(await this.safeMath.$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.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
expectStruct(await this.safeMath.$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.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
expectStruct(await this.safeMath.$tryMod(a, b), [true, a.mod(b)]);
});
});
@ -161,7 +162,7 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('0');
expectStruct(await this.safeMath.tryMod(a, b), { flag: false, value: '0' });
expectStruct(await this.safeMath.$tryMod(a, b), [false, '0']);
});
});
});
@ -172,14 +173,14 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('1234');
await testCommutative(this.safeMath.doAdd, a, b, a.add(b));
await testCommutative(this.safeMath.$add, a, b, a.add(b));
});
it('reverts on addition overflow', async function () {
const a = MAX_UINT256;
const b = new BN('1');
await testFailsCommutative(this.safeMath.doAdd, a, b, undefined);
await testFailsCommutative(this.safeMath.$add, a, b, undefined);
});
});
@ -188,14 +189,14 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('1234');
expect(await this.safeMath.doSub(a, b)).to.be.bignumber.equal(a.sub(b));
expect(await this.safeMath.$sub(a, b)).to.be.bignumber.equal(a.sub(b));
});
it('reverts if subtraction result would be negative', async function () {
const a = new BN('1234');
const b = new BN('5678');
await expectRevert.unspecified(this.safeMath.doSub(a, b));
await expectRevert.unspecified(this.safeMath.$sub(a, b));
});
});
@ -204,21 +205,21 @@ contract('SafeMath', function (accounts) {
const a = new BN('1234');
const b = new BN('5678');
await testCommutative(this.safeMath.doMul, a, b, a.mul(b));
await testCommutative(this.safeMath.$mul, a, b, a.mul(b));
});
it('multiplies by zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
await testCommutative(this.safeMath.doMul, a, b, '0');
await testCommutative(this.safeMath.$mul, a, b, '0');
});
it('reverts on multiplication overflow', async function () {
const a = MAX_UINT256;
const b = new BN('2');
await testFailsCommutative(this.safeMath.doMul, a, b, undefined);
await testFailsCommutative(this.safeMath.$mul, a, b, undefined);
});
});
@ -227,28 +228,28 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('5678');
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal(a.div(b));
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal(a.div(b));
});
it('divides zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('0');
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('0');
});
it('returns complete number result on non-even division', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('1');
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('1');
});
it('reverts on division by zero', async function () {
const a = new BN('5678');
const b = new BN('0');
await expectRevert.unspecified(this.safeMath.doDiv(a, b));
await expectRevert.unspecified(this.safeMath.$div(a, b));
});
});
@ -258,28 +259,28 @@ contract('SafeMath', function (accounts) {
const a = new BN('284');
const b = new BN('5678');
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
});
it('when the dividend is equal to the divisor', async function () {
const a = new BN('5678');
const b = new BN('5678');
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
});
it('when the dividend is larger than the divisor', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(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');
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
});
});
@ -287,7 +288,7 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('0');
await expectRevert.unspecified(this.safeMath.doMod(a, b));
await expectRevert.unspecified(this.safeMath.$mod(a, b));
});
});
});
@ -298,14 +299,19 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('1234');
expect(await this.safeMath.subWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.sub(b));
expect(
await this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(a.sub(b));
});
it('reverts if subtraction result would be negative', async function () {
const a = new BN('1234');
const b = new BN('5678');
await expectRevert(this.safeMath.subWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
await expectRevert(
this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
'MyErrorMessage',
);
});
});
@ -314,28 +320,37 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('5678');
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.div(b));
expect(
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(a.div(b));
});
it('divides zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('0');
expect(
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal('0');
});
it('returns complete number result on non-even division', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('1');
expect(
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal('1');
});
it('reverts on division by zero', async function () {
const a = new BN('5678');
const b = new BN('0');
await expectRevert(this.safeMath.divWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
await expectRevert(
this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
'MyErrorMessage',
);
});
});
@ -345,28 +360,36 @@ contract('SafeMath', function (accounts) {
const a = new BN('284');
const b = new BN('5678');
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
expect(
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(a.mod(b));
});
it('when the dividend is equal to the divisor', async function () {
const a = new BN('5678');
const b = new BN('5678');
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
expect(
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(a.mod(b));
});
it('when the dividend is larger than the divisor', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
expect(
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(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');
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
expect(
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
).to.be.bignumber.equal(a.mod(b));
});
});
@ -374,30 +397,37 @@ contract('SafeMath', function (accounts) {
const a = new BN('5678');
const b = new BN('0');
await expectRevert(this.safeMath.modWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
await expectRevert(
this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
'MyErrorMessage',
);
});
});
});
describe('memory leakage', function () {
beforeEach(async function () {
this.safeMathMemoryCheck = await SafeMathMemoryCheck.new();
});
it('add', async function () {
expect(await this.safeMath.addMemoryCheck()).to.be.bignumber.equal('0');
expect(await this.safeMathMemoryCheck.$addMemoryCheck()).to.be.bignumber.equal('0');
});
it('sub', async function () {
expect(await this.safeMath.subMemoryCheck()).to.be.bignumber.equal('0');
expect(await this.safeMathMemoryCheck.$subMemoryCheck()).to.be.bignumber.equal('0');
});
it('mul', async function () {
expect(await this.safeMath.mulMemoryCheck()).to.be.bignumber.equal('0');
expect(await this.safeMathMemoryCheck.$mulMemoryCheck()).to.be.bignumber.equal('0');
});
it('div', async function () {
expect(await this.safeMath.divMemoryCheck()).to.be.bignumber.equal('0');
expect(await this.safeMathMemoryCheck.$divMemoryCheck()).to.be.bignumber.equal('0');
});
it('mod', async function () {
expect(await this.safeMath.modMemoryCheck()).to.be.bignumber.equal('0');
expect(await this.safeMathMemoryCheck.$modMemoryCheck()).to.be.bignumber.equal('0');
});
});
});