Add more slice tests

This commit is contained in:
Amir Bandeali 2019-02-25 10:12:59 -08:00
parent c1cee8e951
commit 8279ef0e05
2 changed files with 28 additions and 10 deletions

View File

@ -279,10 +279,10 @@ contract TestLibBytes {
) )
public public
pure pure
returns (bytes memory result) returns (bytes memory result, bytes memory original)
{ {
result = LibBytes.slice(b, from, to); result = LibBytes.slice(b, from, to);
return result; return (result, b);
} }
/// @dev Returns a slice from a byte array without preserving the input. /// @dev Returns a slice from a byte array without preserving the input.
@ -298,9 +298,9 @@ contract TestLibBytes {
) )
public public
pure pure
returns (bytes memory result) returns (bytes memory result, bytes memory original)
{ {
result = LibBytes.sliceDestructive(b, from, to); result = LibBytes.sliceDestructive(b, from, to);
return result; return (result, b);
} }
} }

View File

@ -883,7 +883,16 @@ describe('LibBytes', () => {
it('should return a byte array of length 0 if from == to', async () => { it('should return a byte array of length 0 if from == to', async () => {
const from = new BigNumber(0); const from = new BigNumber(0);
const to = from; const to = from;
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to); const [result, original] = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(original).to.eq(byteArrayLongerThan32Bytes);
expect(result).to.eq(constants.NULL_BYTES);
});
it('should return a byte array of length 0 if from == to == b.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(byteLen);
const to = from;
const [result, original] = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(original).to.eq(byteArrayLongerThan32Bytes);
expect(result).to.eq(constants.NULL_BYTES); expect(result).to.eq(constants.NULL_BYTES);
}); });
it('should revert if to > input.length', async () => { it('should revert if to > input.length', async () => {
@ -898,15 +907,17 @@ describe('LibBytes', () => {
it('should slice a section of the input', async () => { it('should slice a section of the input', async () => {
const from = new BigNumber(1); const from = new BigNumber(1);
const to = new BigNumber(2); const to = new BigNumber(2);
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to); const [result, original] = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`; const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`;
expect(original).to.eq(byteArrayLongerThan32Bytes);
expect(result).to.eq(expectedResult); expect(result).to.eq(expectedResult);
}); });
it('should copy the entire input if from = 0 and to = input.length', async () => { it('should copy the entire input if from = 0 and to = input.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2; const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0); const from = new BigNumber(0);
const to = new BigNumber(byteLen); const to = new BigNumber(byteLen);
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to); const [result, original] = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(original).to.eq(byteArrayLongerThan32Bytes);
expect(result).to.eq(byteArrayLongerThan32Bytes); expect(result).to.eq(byteArrayLongerThan32Bytes);
}); });
}); });
@ -923,7 +934,14 @@ describe('LibBytes', () => {
it('should return a byte array of length 0 if from == to', async () => { it('should return a byte array of length 0 if from == to', async () => {
const from = new BigNumber(0); const from = new BigNumber(0);
const to = from; const to = from;
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to); const [result] = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(constants.NULL_BYTES);
});
it('should return a byte array of length 0 if from == to == b.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(byteLen);
const to = from;
const [result] = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(constants.NULL_BYTES); expect(result).to.eq(constants.NULL_BYTES);
}); });
it('should revert if to > input.length', async () => { it('should revert if to > input.length', async () => {
@ -938,7 +956,7 @@ describe('LibBytes', () => {
it('should slice a section of the input', async () => { it('should slice a section of the input', async () => {
const from = new BigNumber(1); const from = new BigNumber(1);
const to = new BigNumber(2); const to = new BigNumber(2);
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to); const [result] = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`; const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`;
expect(result).to.eq(expectedResult); expect(result).to.eq(expectedResult);
}); });
@ -946,7 +964,7 @@ describe('LibBytes', () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2; const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0); const from = new BigNumber(0);
const to = new BigNumber(byteLen); const to = new BigNumber(byteLen);
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to); const [result] = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(byteArrayLongerThan32Bytes); expect(result).to.eq(byteArrayLongerThan32Bytes);
}); });
}); });