Files
openzeppelin-contracts/test/utils/SafeCast.test.js
pepelu 4476a2d531 Unsigned conversion #2111 (#2123)
* Add signed to unsigned conversion to SafeCast

* Update SafeCast exception message

* Add test for SafeCast int to uint conversion

- Update SafeCastMock
- Add tests for SafeCast int256 to uint256

* Update SafeCast int to uint definition

Apply suggestions from code review.

Co-Authored-By: Nicolás Venturo <nicolas.venturo@gmail.com>

* Update test for SafeCast int to uint conversion

* Update SafeCast test after code review

- Change "downcasts" to "casts"
- Move test closer to its function

* Fix error in SafeCast toUint256 description

* Fix breaking error in SafeCast

* Add uint256 to int256 conversion to SafeCast

- Add function
- Add mock
- Add test

* Update SafeCast unsigned to signed conversion

- Update error in conversion to be more clear
- Update constants in test to be powers of 2 instead of shifts

* Add changelog entry

* Update SafeCast tests

- Add minus in INT256_MIN for clarity

Co-Authored-By: Nicolás Venturo <nicolas.venturo@gmail.com>

Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
2020-03-27 10:56:30 -03:00

117 lines
3.7 KiB
JavaScript

const { contract } = require('@openzeppelin/test-environment');
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const SafeCastMock = contract.fromArtifact('SafeCastMock');
describe('SafeCast', async () => {
beforeEach(async function () {
this.safeCast = await SafeCastMock.new();
});
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`
);
});
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`
);
});
});
}
[8, 16, 32, 64, 128].forEach(bits => testToUint(bits));
describe('toUint256', () => {
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
const minInt256 = new BN('2').pow(new BN(255)).neg();
const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
it('casts 0', async function () {
expect(await this.safeCast.toUint256(0)).to.be.bignumber.equal('0');
});
it('casts 1', async function () {
expect(await this.safeCast.toUint256(1)).to.be.bignumber.equal('1');
});
it(`casts INT256_MAX (${maxInt256})`, async function () {
expect(await this.safeCast.toUint256(maxInt256)).to.be.bignumber.equal(maxInt256);
});
it('reverts when casting -1', async function () {
await expectRevert(
this.safeCast.toUint256(-1),
'SafeCast: value must be positive'
);
});
it(`reverts when casting INT256_MIN (${minInt256})`, async function () {
await expectRevert(
this.safeCast.toUint256(minInt256),
'SafeCast: value must be positive'
);
});
it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
await expectRevert(
this.safeCast.toUint256(maxUint256),
'SafeCast: value must be positive'
);
});
});
describe('toInt256', () => {
const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
it('casts 0', async function () {
expect(await this.safeCast.toInt256(0)).to.be.bignumber.equal('0');
});
it('casts 1', async function () {
expect(await this.safeCast.toInt256(1)).to.be.bignumber.equal('1');
});
it(`casts INT256_MAX (${maxInt256})`, async function () {
expect(await this.safeCast.toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
});
it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
await expectRevert(
this.safeCast.toInt256(maxInt256.addn(1)),
'SafeCast: value doesn\'t fit in an int256'
);
});
it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
await expectRevert(
this.safeCast.toInt256(maxUint256),
'SafeCast: value doesn\'t fit in an int256'
);
});
});
});