From 3935e661fe457ae3bbc3a5b0aec2b4b695b555e5 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Tue, 3 Sep 2019 17:05:32 -0700 Subject: [PATCH] Add more writeLength tests --- contracts/utils/contracts/test/TestLibBytes.sol | 14 ++++++++++++++ contracts/utils/test/lib_bytes.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/contracts/utils/contracts/test/TestLibBytes.sol b/contracts/utils/contracts/test/TestLibBytes.sol index ab316b08d8..b2f39e5fcc 100644 --- a/contracts/utils/contracts/test/TestLibBytes.sol +++ b/contracts/utils/contracts/test/TestLibBytes.sol @@ -325,4 +325,18 @@ contract TestLibBytes { b.writeLength(length); return b; } + + function assertBytesUnchangedAfterLengthReset( + bytes memory b, + uint256 tempLength + ) + public + pure + { + uint256 length = b.length; + bytes memory bCopy = b.slice(0, length); + b.writeLength(tempLength); + b.writeLength(length); + assert(b.equals(bCopy)); + } } diff --git a/contracts/utils/test/lib_bytes.ts b/contracts/utils/test/lib_bytes.ts index ace87b530e..5efe1dc113 100644 --- a/contracts/utils/test/lib_bytes.ts +++ b/contracts/utils/test/lib_bytes.ts @@ -1131,6 +1131,20 @@ describe('LibBytes', () => { ); expect(result).to.eq(`${byteArrayLongerThan32Bytes}${byteArrayLongerThan32Bytes.slice(2)}`); }); + it('should result in the same byte array if length is reduced and reset', async () => { + const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2; + const tempByteLen = new BigNumber(byteLen).dividedToIntegerBy(2); + return expect( + libBytes.assertBytesUnchangedAfterLengthReset.callAsync(byteArrayLongerThan32Bytes, tempByteLen), + ).to.be.fulfilled(''); + }); + it('should result in the same byte array if length is increased and reset', async () => { + const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2; + const tempByteLen = new BigNumber(byteLen).multipliedBy(2); + return expect( + libBytes.assertBytesUnchangedAfterLengthReset.callAsync(byteArrayLongerThan32Bytes, tempByteLen), + ).to.be.fulfilled(''); + }); }); }); // tslint:disable:max-file-line-count