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

Safe Casting Library from uint256 to uintXX #1926

Merged
merged 19 commits into from
Oct 22, 2019
Merged
Changes from 1 commit
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
107 changes: 33 additions & 74 deletions test/utils/SafeCast.test.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,39 @@
const { BN, expectRevert } = require('openzeppelin-test-helpers');

bh2smith marked this conversation as resolved.
Show resolved Hide resolved
const { expect } = require('chai');

const SafeCastMock = artifacts.require('SafeCastMock');
const { expectRevert } = require('openzeppelin-test-helpers');

contract('SafeCast', async (accounts) => {
describe('toUint128()', () => {
it('reverts on overflow (i.e. input >= 2^128)', async () => {
const contract = await SafeCastMock.new();
await expectRevert(
contract.toUint128('340282366920938463463374607431768211456'),
'SafeCast: value doesn\'t fit in 128 bits'
);
await expectRevert(
contract.toUint128('999340282366920938463463374607431768211455'),
'SafeCast: value doesn\'t fit in 128 bits'
);
});
it('passes where appropriate (i.e. 0 <= input < 2^128)', async () => {
const contract = await SafeCastMock.new();
assert.equal(await contract.toUint128(0), 0);
assert.equal(await contract.toUint128(1), 1);
assert.equal(
(await contract.toUint128('340282366920938463463374607431768211455')).toString(),
'340282366920938463463374607431768211455'
);
});
contract('SafeCast', async () => {
beforeEach(async function () {
this.safeCast = await SafeCastMock.new();
});
describe('toUint64()', () => {
it('reverts on overflow (i.e. input >= 2^64)', async () => {
const contract = await SafeCastMock.new();
await expectRevert(contract.toUint64('18446744073709551616'), 'SafeCast: value doesn\'t fit in 64 bits');
await expectRevert(contract.toUint64('18446744073709551617'), 'SafeCast: value doesn\'t fit in 64 bits');
});
it('passes where appropriate (i.e. 0 <= input < 2^64)', async () => {
const contract = await SafeCastMock.new();
assert.equal(await contract.toUint64(0), 0);
assert.equal(await contract.toUint64(1), 1);
assert.equal((await contract.toUint64('18446744073709551615')).toString(), '18446744073709551615');
});
});
describe('toUint32()', () => {
it('reverts on overflow (i.e. input >= 2^32)', async () => {
const contract = await SafeCastMock.new();
await expectRevert(contract.toUint32('4294967296'), 'SafeCast: value doesn\'t fit in 32 bits');
await expectRevert(contract.toUint32('4294967297'), 'SafeCast: value doesn\'t fit in 32 bits');
});
it('passes where appropriate (i.e. 0 <= input < 2^32)', async () => {
const contract = await SafeCastMock.new();
assert.equal(await contract.toUint32(0), 0);
assert.equal(await contract.toUint32(1), 1);
assert.equal(await contract.toUint32(4294967295), 4294967295);
});
});
describe('toUint16()', () => {
it('reverts on overflow (i.e. input >= 2^16)', async () => {
const contract = await SafeCastMock.new();
await expectRevert(contract.toUint16('65536'), 'SafeCast: value doesn\'t fit in 16 bits');
await expectRevert(contract.toUint16('65537'), 'SafeCast: value doesn\'t fit in 16 bits');
});

it('passes where appropriate (i.e. 0 <= input < 2^16)', async () => {
const contract = await SafeCastMock.new();
assert.equal(await contract.toUint16(0), 0);
assert.equal(await contract.toUint16(1), 1);
assert.equal(await contract.toUint16(65535), 65535);
});
});
describe('toUint8()', () => {
it('reverts on overflow (i.e. input >= 2^8)', async () => {
const contract = await SafeCastMock.new();
await expectRevert(contract.toUint8(256), 'SafeCast: value doesn\'t fit in 8 bits');
await expectRevert(contract.toUint8(257), 'SafeCast: value doesn\'t fit in 8 bits');
});
it('passes where appropriate (i.e. 0 <= input < 2^8)', async () => {
const contract = await SafeCastMock.new();
assert.equal(await contract.toUint8(0), 0);
assert.equal(await contract.toUint8(1), 1);
assert.equal(await contract.toUint8(255), 255);
function testToUint (bits) {
describe(`toUint${bits}`, () => {
const maxValue = new BN('2').pow(new BN(bits)).subn(1);

it('downcasts 0', async function () {
expect(await this.safeCast[`toUint${bits}`](0)).to.be.bignumber.equal('0');
});

it('downcasts 1', async function () {
expect(await this.safeCast[`toUint${bits}`](1)).to.be.bignumber.equal('1');
});

it(`downcasts 2^${bits} - 1 (${maxValue})`, async function () {
expect(await this.safeCast[`toUint${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
});

it(`reverts when downcasting 2^${bits} (${maxValue.addn(1)})`, async function () {
await expectRevert(this.safeCast[`toUint${bits}`](maxValue.addn(1)), `SafeCast: value doesn't fit in ${bits} bits`);
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
});

it(`reverts when downcasting 2^${bits} + 1 (${maxValue.addn(2)})`, async function () {
await expectRevert(this.safeCast[`toUint${bits}`](maxValue.addn(2)), `SafeCast: value doesn't fit in ${bits} bits`);
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
}

[8, 16, 32, 64, 128].forEach(bits => testToUint(bits));
nventuro marked this conversation as resolved.
Show resolved Hide resolved
});