From 9aa30e19605e686d324448645c6594d2d31464c6 Mon Sep 17 00:00:00 2001 From: jeano <33420400+JeanoLee@users.noreply.github.com> Date: Tue, 14 Aug 2018 05:40:48 +0900 Subject: [PATCH] Add Modulo operation for getting the quotient (#915) * Add Modulo operation for getting the quotient * Improved modulo tests and implementation. * Removed assertion. --- contracts/math/SafeMath.sol | 9 +++++++ contracts/mocks/SafeMathMock.sol | 4 ++++ test/math/SafeMath.test.js | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/contracts/math/SafeMath.sol b/contracts/math/SafeMath.sol index e978fc42899..da591c3b207 100644 --- a/contracts/math/SafeMath.sol +++ b/contracts/math/SafeMath.sol @@ -54,4 +54,13 @@ library SafeMath { return c; } + + /** + * @dev Divides two numbers and returns the remainder (unsigned integer modulo), + * reverts when dividing by zero. + */ + function mod(uint256 a, uint256 b) internal pure returns (uint256) { + require(b != 0); + return a % b; + } } diff --git a/contracts/mocks/SafeMathMock.sol b/contracts/mocks/SafeMathMock.sol index df749b5ac22..d69d6e5cc3d 100644 --- a/contracts/mocks/SafeMathMock.sol +++ b/contracts/mocks/SafeMathMock.sol @@ -21,4 +21,8 @@ contract SafeMathMock { function add(uint256 _a, uint256 _b) public pure returns (uint256) { return SafeMath.add(_a, _b); } + + function mod(uint256 a, uint256 b) public pure returns (uint256) { + return SafeMath.mod(a, b); + } } diff --git a/test/math/SafeMath.test.js b/test/math/SafeMath.test.js index 2c0018c184e..3a96e2609ee 100644 --- a/test/math/SafeMath.test.js +++ b/test/math/SafeMath.test.js @@ -1,4 +1,5 @@ const { assertRevert } = require('../helpers/assertRevert'); + const BigNumber = web3.BigNumber; const SafeMathMock = artifacts.require('SafeMathMock'); @@ -88,4 +89,43 @@ contract('SafeMath', () => { await assertRevert(this.safeMath.div(a, b)); }); }); + + describe('mod', function () { + describe('modulos correctly', async function () { + it('when the dividend is smaller than the divisor', async function () { + const a = new BigNumber(284); + const b = new BigNumber(5678); + + (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b)); + }); + + it('when the dividend is equal to the divisor', async function () { + const a = new BigNumber(5678); + const b = new BigNumber(5678); + + (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b)); + }); + + it('when the dividend is larger than the divisor', async function () { + const a = new BigNumber(7000); + const b = new BigNumber(5678); + + (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b)); + }); + + it('when the dividend is a multiple of the divisor', async function () { + const a = new BigNumber(17034); // 17034 == 5678 * 3 + const b = new BigNumber(5678); + + (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b)); + }); + }); + + it('reverts with a 0 divisor', async function () { + const a = new BigNumber(5678); + const b = new BigNumber(0); + + await assertRevert(this.safeMath.mod(a, b)); + }); + }); });