forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize toString (OpenZeppelin#3573)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
- Loading branch information
1 parent
457ecc6
commit 0790afa
Showing
4 changed files
with
124 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,86 @@ | ||
const { constants, expectRevert } = require('@openzeppelin/test-helpers'); | ||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers'); | ||
|
||
const { expect } = require('chai'); | ||
|
||
const StringsMock = artifacts.require('StringsMock'); | ||
|
||
contract('Strings', function (accounts) { | ||
beforeEach(async function () { | ||
before(async function () { | ||
this.strings = await StringsMock.new(); | ||
}); | ||
|
||
describe('from uint256 - decimal format', function () { | ||
it('converts 0', async function () { | ||
expect(await this.strings.fromUint256(0)).to.equal('0'); | ||
}); | ||
|
||
it('converts a positive number', async function () { | ||
expect(await this.strings.fromUint256(4132)).to.equal('4132'); | ||
}); | ||
|
||
it('converts MAX_UINT256', async function () { | ||
expect(await this.strings.fromUint256(constants.MAX_UINT256)).to.equal(constants.MAX_UINT256.toString()); | ||
}); | ||
describe('toString', function () { | ||
for (const [ key, value ] of Object.entries([ | ||
'0', | ||
'7', | ||
'10', | ||
'99', | ||
'100', | ||
'101', | ||
'123', | ||
'4132', | ||
'12345', | ||
'1234567', | ||
'1234567890', | ||
'123456789012345', | ||
'12345678901234567890', | ||
'123456789012345678901234567890', | ||
'1234567890123456789012345678901234567890', | ||
'12345678901234567890123456789012345678901234567890', | ||
'123456789012345678901234567890123456789012345678901234567890', | ||
'1234567890123456789012345678901234567890123456789012345678901234567890', | ||
].reduce((acc, value) => Object.assign(acc, { [value]: new BN(value) }), { | ||
MAX_UINT256: constants.MAX_UINT256.toString(), | ||
}))) { | ||
it(`converts ${key}`, async function () { | ||
expect(await this.strings.methods['toString(uint256)'](value)).to.equal(value.toString(10)); | ||
}); | ||
} | ||
}); | ||
|
||
describe('from uint256 - hex format', function () { | ||
describe('toHexString', function () { | ||
it('converts 0', async function () { | ||
expect(await this.strings.fromUint256Hex(0)).to.equal('0x00'); | ||
expect(await this.strings.methods['toHexString(uint256)'](0)).to.equal('0x00'); | ||
}); | ||
|
||
it('converts a positive number', async function () { | ||
expect(await this.strings.fromUint256Hex(0x4132)).to.equal('0x4132'); | ||
expect(await this.strings.methods['toHexString(uint256)'](0x4132)).to.equal('0x4132'); | ||
}); | ||
|
||
it('converts MAX_UINT256', async function () { | ||
expect(await this.strings.fromUint256Hex(constants.MAX_UINT256)) | ||
expect(await this.strings.methods['toHexString(uint256)'](constants.MAX_UINT256)) | ||
.to.equal(web3.utils.toHex(constants.MAX_UINT256)); | ||
}); | ||
}); | ||
|
||
describe('from uint256 - fixed hex format', function () { | ||
describe('toHexString fixed', function () { | ||
it('converts a positive number (long)', async function () { | ||
expect(await this.strings.fromUint256HexFixed(0x4132, 32)) | ||
expect(await this.strings.methods['toHexString(uint256,uint256)'](0x4132, 32)) | ||
.to.equal('0x0000000000000000000000000000000000000000000000000000000000004132'); | ||
}); | ||
|
||
it('converts a positive number (short)', async function () { | ||
await expectRevert( | ||
this.strings.fromUint256HexFixed(0x4132, 1), | ||
this.strings.methods['toHexString(uint256,uint256)'](0x4132, 1), | ||
'Strings: hex length insufficient', | ||
); | ||
}); | ||
|
||
it('converts MAX_UINT256', async function () { | ||
expect(await this.strings.fromUint256HexFixed(constants.MAX_UINT256, 32)) | ||
expect(await this.strings.methods['toHexString(uint256,uint256)'](constants.MAX_UINT256, 32)) | ||
.to.equal(web3.utils.toHex(constants.MAX_UINT256)); | ||
}); | ||
}); | ||
|
||
describe('from address - fixed hex format', function () { | ||
describe('toHexString address', function () { | ||
it('converts a random address', async function () { | ||
const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f'; | ||
expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); | ||
expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr); | ||
}); | ||
|
||
it('converts an address with leading zeros', async function () { | ||
const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000'; | ||
expect(await this.strings.fromAddressHexFixed(addr)).to.equal(addr); | ||
expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr); | ||
}); | ||
}); | ||
}); |