Fix one-by-error for run-length encoding (#141555)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Joseph Crail 2022-09-23 04:09:41 -07:00 committed by GitHub
parent 62e535263c
commit d4c17b8f61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View file

@ -98,7 +98,7 @@ describe('Stack trace operations', () => {
}
});
test('runLengthDecodeReverse with optional parameter', () => {
test('runLengthDecode with optional parameter', () => {
const tests: Array<{
bytes: Buffer;
expected: number[];
@ -118,7 +118,7 @@ describe('Stack trace operations', () => {
}
});
test('runLengthDecodeReverse without optional parameter', () => {
test('runLengthDecode without optional parameter', () => {
const tests: Array<{
bytes: Buffer;
expected: number[];
@ -137,4 +137,44 @@ describe('Stack trace operations', () => {
expect(runLengthDecode(t.bytes)).toEqual(t.expected);
}
});
test('runLengthDecode works for very long runs', () => {
const tests: Array<{
bytes: Buffer;
expected: number[];
}> = [
{
bytes: Buffer.from([0x5, 0x2, 0xff, 0x0]),
expected: [2, 2, 2, 2, 2].concat(Array(255).fill(0)),
},
{
bytes: Buffer.from([0xff, 0x2, 0x1, 0x2]),
expected: Array(256).fill(2),
},
];
for (const t of tests) {
expect(runLengthDecode(t.bytes)).toEqual(t.expected);
}
});
test('runLengthEncode works for very long runs', () => {
const tests: Array<{
numbers: number[];
expected: Buffer;
}> = [
{
numbers: [2, 2, 2, 2, 2].concat(Array(255).fill(0)),
expected: Buffer.from([0x5, 0x2, 0xff, 0x0]),
},
{
numbers: Array(256).fill(2),
expected: Buffer.from([0xff, 0x2, 0x1, 0x2]),
},
];
for (const t of tests) {
expect(runLengthEncode(t.numbers)).toEqual(t.expected);
}
});
});

View file

@ -68,7 +68,7 @@ export function runLengthEncode(input: number[]): Buffer {
return Buffer.from(output);
}
let count = 0;
let count = 1;
let current = input[0];
for (let i = 1; i < input.length; i++) {
@ -79,13 +79,13 @@ export function runLengthEncode(input: number[]): Buffer {
continue;
}
output.push(count + 1, current);
output.push(count, current);
count = 0;
count = 1;
current = next;
}
output.push(count + 1, current);
output.push(count, current);
return Buffer.from(output);
}