From a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 9 May 2018 16:27:50 -0700 Subject: Add deepCopyBytes method to LibBytes --- packages/contracts/test/libraries/lib_bytes.ts | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index 968bac300..1fd30a3e0 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -60,6 +60,40 @@ describe('LibBytes', () => { await blockchainLifecycle.revertAsync(); }); + describe.only('deepCopyBytes', () => { + const byteArrayLongerThan32BytesLen = (byteArrayLongerThan32Bytes.length - 2) / 2; + it('should throw if length of copy is 0', async () => { + const index = new BigNumber(0); + const len = new BigNumber(0); + return expect( + libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if start index + length to copy is greater than length of byte array', async () => { + const index = new BigNumber(0); + const len = new BigNumber(byteArrayLongerThan32BytesLen + 1); + return expect( + libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should copy the entire byte array if index = 0 and len = b.length', async () => { + const index = new BigNumber(0); + const len = new BigNumber(byteArrayLongerThan32BytesLen); + const copy = await libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len); + expect(copy).to.equal(byteArrayLongerThan32Bytes); + }); + + it('should copy part of the byte array if area to copy is less than b.length', async () => { + const index = new BigNumber(10); + const len = new BigNumber(4); + const copy = await libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len); + const expectedCopy = `0x${byteArrayLongerThan32Bytes.slice(22, 30)}`; + expect(copy).to.equal(expectedCopy); + }); + }); + describe('areBytesEqual', () => { it('should return true if byte arrays are equal (both arrays < 32 bytes)', async () => { const areBytesEqual = await libBytes.publicAreBytesEqual.callAsync( -- cgit v1.2.3