Fix bug in slice functions that disallow slicing the last byte of a byte array

This commit is contained in:
Amir Bandeali
2019-02-07 14:00:04 -08:00
parent 1f895d0f27
commit 64401f1031
4 changed files with 121 additions and 2 deletions

View File

@@ -870,5 +870,85 @@ describe('LibBytes', () => {
describe('copies forward within one word and one byte overlap', () =>
test([[0, 0, 1, 'one byte'], [0, 10, 11, 'eleven bytes'], [0, 15, 16, 'sixteen bytes']]));
});
describe('slice', () => {
it('should revert if from > to', async () => {
const from = new BigNumber(1);
const to = new BigNumber(0);
expectContractCallFailedAsync(
libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to),
RevertReason.FromLessThanToRequired,
);
});
it('should return a byte array of length 0 if from == to', async () => {
const from = new BigNumber(0);
const to = from;
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(constants.NULL_BYTES);
});
it('should revert if to > input.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0);
const to = new BigNumber(byteLen).plus(1);
expectContractCallFailedAsync(
libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to),
RevertReason.ToLessThanLengthRequired,
);
});
it('should slice a section of the input', async () => {
const from = new BigNumber(1);
const to = new BigNumber(2);
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`;
expect(result).to.eq(expectedResult);
});
it('should copy the entire input if from = 0 and to = input.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0);
const to = new BigNumber(byteLen);
const result = await libBytes.publicSlice.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(byteArrayLongerThan32Bytes);
});
});
describe('sliceDestructive', () => {
it('should revert if from > to', async () => {
const from = new BigNumber(1);
const to = new BigNumber(0);
expectContractCallFailedAsync(
libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to),
RevertReason.FromLessThanToRequired,
);
});
it('should return a byte array of length 0 if from == to', async () => {
const from = new BigNumber(0);
const to = from;
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(constants.NULL_BYTES);
});
it('should revert if to > input.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0);
const to = new BigNumber(byteLen).plus(1);
expectContractCallFailedAsync(
libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to),
RevertReason.ToLessThanLengthRequired,
);
});
it('should slice a section of the input', async () => {
const from = new BigNumber(1);
const to = new BigNumber(2);
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
const expectedResult = `0x${byteArrayLongerThan32Bytes.slice(4, 6)}`;
expect(result).to.eq(expectedResult);
});
it('should copy the entire input if from = 0 and to = input.length', async () => {
const byteLen = (byteArrayLongerThan32Bytes.length - 2) / 2;
const from = new BigNumber(0);
const to = new BigNumber(byteLen);
const result = await libBytes.publicSliceDestructive.callAsync(byteArrayLongerThan32Bytes, from, to);
expect(result).to.eq(byteArrayLongerThan32Bytes);
});
});
});
// tslint:disable:max-file-line-count