Fix SafeMath test file (#865)
This commit is contained in:
committed by
GitHub
parent
a6bef44790
commit
bd5f7f5774
@ -5,17 +5,20 @@ import "../math/SafeMath.sol";
|
|||||||
|
|
||||||
|
|
||||||
contract SafeMathMock {
|
contract SafeMathMock {
|
||||||
uint256 public result;
|
|
||||||
|
|
||||||
function multiply(uint256 a, uint256 b) public {
|
function mul(uint256 a, uint256 b) public pure returns (uint256) {
|
||||||
result = SafeMath.mul(a, b);
|
return SafeMath.mul(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function subtract(uint256 a, uint256 b) public {
|
function div(uint256 a, uint256 b) public pure returns (uint256) {
|
||||||
result = SafeMath.sub(a, b);
|
return SafeMath.div(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function add(uint256 a, uint256 b) public {
|
function sub(uint256 a, uint256 b) public pure returns (uint256) {
|
||||||
result = SafeMath.add(a, b);
|
return SafeMath.sub(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
function add(uint256 a, uint256 b) public pure returns (uint256) {
|
||||||
|
return SafeMath.add(a, b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
module.exports = function (error) {
|
export default async promise => {
|
||||||
assert.isAbove(error.message.search('invalid opcode'), -1, 'Invalid opcode error must be returned');
|
try {
|
||||||
|
await promise;
|
||||||
|
assert.fail('Expected invalid opcode not received');
|
||||||
|
} catch (error) {
|
||||||
|
const invalidOpcodeReceived = error.message.search('invalid opcode') >= 0;
|
||||||
|
assert(invalidOpcodeReceived, `Expected "invalid opcode", got ${error} instead`);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,60 +1,91 @@
|
|||||||
import assertRevert from '../helpers/assertRevert';
|
import assertJump from '../helpers/assertJump';
|
||||||
const assertJump = require('../helpers/assertJump');
|
const BigNumber = web3.BigNumber;
|
||||||
var SafeMathMock = artifacts.require('SafeMathMock');
|
const SafeMathMock = artifacts.require('SafeMathMock');
|
||||||
|
|
||||||
contract('SafeMath', function (accounts) {
|
require('chai')
|
||||||
let safeMath;
|
.use(require('chai-bignumber')(BigNumber))
|
||||||
|
.should();
|
||||||
|
|
||||||
|
contract('SafeMath', () => {
|
||||||
|
const MAX_UINT = new BigNumber('115792089237316195423570985008687907853269984665640564039457584007913129639935');
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
safeMath = await SafeMathMock.new();
|
this.safeMath = await SafeMathMock.new();
|
||||||
});
|
|
||||||
|
|
||||||
it('multiplies correctly', async function () {
|
|
||||||
let a = 5678;
|
|
||||||
let b = 1234;
|
|
||||||
await safeMath.multiply(a, b);
|
|
||||||
let result = await safeMath.result();
|
|
||||||
assert.equal(result, a * b);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('add', function () {
|
||||||
it('adds correctly', async function () {
|
it('adds correctly', async function () {
|
||||||
let a = 5678;
|
const a = new BigNumber(5678);
|
||||||
let b = 1234;
|
const b = new BigNumber(1234);
|
||||||
await safeMath.add(a, b);
|
|
||||||
let result = await safeMath.result();
|
|
||||||
|
|
||||||
assert.equal(result, a + b);
|
const result = await this.safeMath.add(a, b);
|
||||||
|
result.should.be.bignumber.equal(a.plus(b));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('throws an error on addition overflow', async function () {
|
||||||
|
const a = MAX_UINT;
|
||||||
|
const b = new BigNumber(1);
|
||||||
|
|
||||||
|
await assertJump(this.safeMath.add(a, b));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('sub', function () {
|
||||||
it('subtracts correctly', async function () {
|
it('subtracts correctly', async function () {
|
||||||
let a = 5678;
|
const a = new BigNumber(5678);
|
||||||
let b = 1234;
|
const b = new BigNumber(1234);
|
||||||
await safeMath.subtract(a, b);
|
|
||||||
let result = await safeMath.result();
|
|
||||||
|
|
||||||
assert.equal(result, a - b);
|
const result = await this.safeMath.sub(a, b);
|
||||||
|
result.should.be.bignumber.equal(a.minus(b));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if subtraction result would be negative', async function () {
|
it('throws an error if subtraction result would be negative', async function () {
|
||||||
let a = 1234;
|
const a = new BigNumber(1234);
|
||||||
let b = 5678;
|
const b = new BigNumber(5678);
|
||||||
try {
|
|
||||||
await safeMath.subtract(a, b);
|
await assertJump(this.safeMath.sub(a, b));
|
||||||
assert.fail('should have thrown before');
|
});
|
||||||
} catch (error) {
|
|
||||||
assertJump(error);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error on addition overflow', async function () {
|
describe('mul', function () {
|
||||||
let a = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
|
it('multiplies correctly', async function () {
|
||||||
let b = 1;
|
const a = new BigNumber(1234);
|
||||||
await assertRevert(safeMath.add(a, b));
|
const b = new BigNumber(5678);
|
||||||
|
|
||||||
|
const result = await this.safeMath.mul(a, b);
|
||||||
|
result.should.be.bignumber.equal(a.times(b));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error on multiplication overflow', async function () {
|
it('handles a zero product correctly', async function () {
|
||||||
let a = 115792089237316195423570985008687907853269984665640564039457584007913129639933;
|
const a = new BigNumber(0);
|
||||||
let b = 2;
|
const b = new BigNumber(5678);
|
||||||
await assertRevert(safeMath.multiply(a, b));
|
|
||||||
|
const result = await this.safeMath.mul(a, b);
|
||||||
|
result.should.be.bignumber.equal(a.times(b));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an error on multiplication overflow', async function () {
|
||||||
|
const a = MAX_UINT;
|
||||||
|
const b = new BigNumber(2);
|
||||||
|
|
||||||
|
await assertJump(this.safeMath.mul(a, b));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('div', function () {
|
||||||
|
it('divides correctly', async function () {
|
||||||
|
const a = new BigNumber(5678);
|
||||||
|
const b = new BigNumber(5678);
|
||||||
|
|
||||||
|
const result = await this.safeMath.div(a, b);
|
||||||
|
result.should.be.bignumber.equal(a.div(b));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an error on zero division', async function () {
|
||||||
|
const a = new BigNumber(5678);
|
||||||
|
const b = new BigNumber(0);
|
||||||
|
|
||||||
|
await assertJump(this.safeMath.div(a, b));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user