Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Modulo operation for getting the quotient #915

Merged
merged 7 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contracts/math/SafeMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions contracts/mocks/SafeMathMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
40 changes: 40 additions & 0 deletions test/math/SafeMath.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { assertRevert } = require('../helpers/assertRevert');

const BigNumber = web3.BigNumber;
const SafeMathMock = artifacts.require('SafeMathMock');

Expand Down Expand Up @@ -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));
});
});
});