Add tests for TS brotli decoder

PiperOrigin-RevId: 527326003
This commit is contained in:
Evgenii Kliuchnikov 2023-04-26 18:32:46 +00:00 committed by Evgenii Kliuchnikov
parent efe140adae
commit 11abde4c96
6 changed files with 2472 additions and 51 deletions

View File

@ -15,8 +15,8 @@ const CRC_64_POLY = new Uint32Array([0xD7870F42, 0xC96C5795]);
* @return {string} footprint
*/
function calculateCrc64(data) {
let crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
let c = new Uint32Array(2);
const crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
const c = new Uint32Array(2);
for (let i = 0; i < data.length; ++i) {
c[1] = 0;
c[0] = (crc[0] ^ data[i]) & 0xFF;
@ -58,7 +58,7 @@ function checkEntry(entry, data) {
describe("BundleTest", () => {
const testData = makeTestData();
for (let entry in testData) {
for (const entry in testData) {
if (!testData.hasOwnProperty(entry)) {
continue;
}

63
js/bundle_test.ts Normal file
View File

@ -0,0 +1,63 @@
/* Copyright 2023 Google Inc. All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
import {brotliDecode} from "./decode";
import {makeTestData} from "./test_data";
const CRC_64_POLY = new Uint32Array([0xD7870F42, 0xC96C5795]);
/**
* Calculates binary data footprint.
*/
function calculateCrc64(data: Int8Array): string {
const crc = new Uint32Array([0xFFFFFFFF, 0xFFFFFFFF]);
const c = new Uint32Array(2);
for (let i = 0; i < data.length; ++i) {
c[1] = 0;
c[0] = (crc[0] ^ data[i]) & 0xFF;
for (let k = 0; k < 8; ++k) {
const isOdd = c[0] & 1;
c[0] = (c[0] >>> 1) | ((c[1] & 1) << 31);
c[1] = c[1] >>> 1;
if (isOdd) {
c[0] = c[0] ^ CRC_64_POLY[0];
c[1] = c[1] ^ CRC_64_POLY[1];
}
}
crc[0] = ((crc[0] >>> 8) | ((crc[1] & 0xFF) << 24)) ^ c[0];
crc[1] = (crc[1] >>> 8) ^ c[1];
}
crc[0] = ~crc[0];
crc[1] = ~crc[1];
let lo = crc[0].toString(16);
lo = "0".repeat(8 - lo.length) + lo;
let hi = crc[1].toString(16);
hi = "0".repeat(8 - hi.length) + hi;
return hi + lo;
}
/**
* Decompresses data and checks that output footprint is correct.
*/
function checkEntry(entry: string, data: Int8Array) {
const expectedCrc = entry.substring(0, 16);
const decompressed = brotliDecode(data);
const crc = calculateCrc64(decompressed);
expect(expectedCrc).toEqual(crc);
}
describe("BundleTest", () => {
const testData = makeTestData();
for (const entry in testData) {
if (!testData.hasOwnProperty(entry)) {
continue;
}
const name = entry.substring(17);
const data = testData[entry];
it('test_' + name, checkEntry.bind(null, entry, data));
}
});

View File

@ -7,15 +7,18 @@ import {BrotliDecode} from "./decode.js";
/**
* NB: Use intermediate chunks to avoid "Maximum call stack size exceeded".
*
* @param {!Int8Array} bytes
* @return {string}
*/
function bytesToString(bytes) {
const kChunkSize = 4096;
if (bytes.length <= kChunkSize) {
return String.fromCharCode.apply(null, new Uint8Array(bytes));
}
let chunks = [];
const chunks = [];
for (let start = 0; start < bytes.length; start += kChunkSize) {
let end = Math.min(start + 4096, bytes.length)
const end = Math.min(start + 4096, bytes.length);
chunks.push(bytesToString(bytes.slice(start, end)));
}
return chunks.join('');
@ -32,6 +35,11 @@ function repeat(char, count) {
return char.repeat(count);
}
/**
* @param {!Array<number>} compressed
* @param {boolean} expectSuccess
* @param {string} expectedOutput
*/
function checkSynth(compressed, expectSuccess, expectedOutput) {
let success = true;
let actual;
@ -50,7 +58,7 @@ const allTests = {
/* GENERATED CODE START */
testAllTransforms10() {
let compressed = [
const compressed = [
0x1b, 0xfc, 0x05, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x13, 0x7c,
0x84, 0x26, 0xf8, 0x04, 0x10, 0x4c, 0xf0, 0x89, 0x38, 0x30, 0xc1, 0x27,
@ -257,7 +265,7 @@ testAllTransforms10() {
},
testAllTransforms4() {
let compressed = [
const compressed = [
0x1b, 0x40, 0x03, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x51, 0x3e,
0x42, 0x51, 0x3e, 0x81, 0x02, 0x51, 0x3e, 0x11, 0x04, 0xa2, 0x7c, 0xe2,
@ -453,7 +461,7 @@ testAllTransforms4() {
},
testBaseDictWord() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x02
];
@ -472,7 +480,7 @@ testBaseDictWord() {
},
testBaseDictWordFinishBlockOnRingbufferWrap() {
let compressed = [
const compressed = [
0x1b, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x9b, 0x58, 0x32,
0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
@ -492,7 +500,7 @@ testBaseDictWordFinishBlockOnRingbufferWrap() {
},
testBaseDictWordTooLong() {
let compressed = [
const compressed = [
0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x02
];
@ -512,7 +520,7 @@ testBaseDictWordTooLong() {
},
testBlockCountMessage() {
let compressed = [
const compressed = [
0x1b, 0x0b, 0x00, 0x11, 0x01, 0x8c, 0xc1, 0xc5, 0x0d, 0x08, 0x00, 0x22,
0x65, 0xe1, 0xfc, 0xfd, 0x22, 0x2c, 0xc4, 0x00, 0x00, 0x38, 0xd8, 0x32,
0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0x04, 0x10, 0x42, 0x00, 0x00,
@ -563,7 +571,7 @@ testBlockCountMessage() {
},
testBlockSwitchMessage() {
let compressed = [
const compressed = [
0x1b, 0x0b, 0x00, 0xd1, 0xe1, 0x01, 0xc6, 0xe0, 0xe2, 0x06, 0x04, 0x00,
0x91, 0xb2, 0x70, 0xfe, 0x7e, 0x11, 0x16, 0x62, 0x00, 0x00, 0x1c, 0x6c,
0x99, 0xc4, 0x00, 0x09, 0x00, 0x80, 0x3b, 0x6d, 0x02, 0x08, 0x82, 0x00,
@ -616,7 +624,7 @@ testBlockSwitchMessage() {
},
testClClTreeDeficiency() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0x43, 0x01, 0xe0, 0x05, 0x88,
0x55, 0x90, 0x01, 0x00, 0x38, 0xd8, 0x32, 0x89, 0x01, 0x12, 0x00, 0x00,
0x77, 0xda, 0x28, 0x40, 0x23
@ -647,7 +655,7 @@ testClClTreeDeficiency() {
},
testClClTreeExcess() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc3, 0x7b, 0x80, 0x58, 0x41,
0x06, 0x00, 0xe0, 0x60, 0xcb, 0x24, 0x06, 0x48, 0x00, 0x00, 0xdc, 0x69,
0xa3, 0x00, 0x8d, 0x00
@ -678,7 +686,7 @@ testClClTreeExcess() {
},
testComplexHuffmanCodeTwoSymbols() {
let compressed = [
const compressed = [
0x1b, 0x01, 0x00, 0x00, 0x80, 0x03, 0xe0, 0xa2, 0x1a, 0x00, 0x00, 0x0e,
0xb6, 0x4c, 0x62, 0x80, 0x04, 0x00, 0xc0, 0x9d, 0x36, 0x12, 0x04
];
@ -711,7 +719,7 @@ testComplexHuffmanCodeTwoSymbols() {
},
testCompressedUncompressedShortCompressed() {
let compressed = [
const compressed = [
0x8b, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x9b, 0x66, 0x6f,
0x1b, 0x0a, 0x50, 0x00, 0x10, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x31,
@ -748,7 +756,7 @@ testCompressedUncompressedShortCompressed() {
},
testCompressedUncompressedShortCompressedSmallWindow() {
let compressed = [
const compressed = [
0x21, 0xf4, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xa7, 0x6d, 0x00, 0x00,
0x38, 0xd8, 0x32, 0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0x34, 0x7b,
0xdb, 0x50, 0x80, 0x02, 0x80, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x31,
@ -785,7 +793,7 @@ testCompressedUncompressedShortCompressedSmallWindow() {
},
testCopyLengthTooLong() {
let compressed = [
const compressed = [
0x1b, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x11, 0x86,
0x02
@ -805,7 +813,7 @@ testCopyLengthTooLong() {
},
testCopyTooLong() {
let compressed = [
const compressed = [
0xa1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xa7, 0x6d, 0x00, 0x00,
0x38, 0xd8, 0x32, 0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0x34, 0xab,
0xdb, 0x50, 0x00
@ -825,7 +833,7 @@ testCopyTooLong() {
},
testCustomHuffmanCode() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc3, 0x3d, 0x80, 0x58, 0x82,
0x08, 0x00, 0xc0, 0xc1, 0x96, 0x49, 0x0c, 0x90, 0x00, 0x00, 0xb8, 0xd3,
0x46, 0x01, 0x1a, 0x01
@ -870,7 +878,7 @@ testCustomHuffmanCode() {
},
testDistanceLut() {
let compressed = [
const compressed = [
0x8b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x99, 0x86,
0x46, 0xc6, 0x22, 0x14, 0x00, 0x00, 0x03, 0x00, 0x00, 0x1c, 0xa7, 0x6d,
@ -906,7 +914,7 @@ testDistanceLut() {
},
testEmpty() {
let compressed = [
const compressed = [
0x3b
];
checkSynth(
@ -921,7 +929,7 @@ testEmpty() {
},
testHelloWorld() {
let compressed = [
const compressed = [
0x1b, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x9b, 0x00, 0x59,
0x98, 0xda, 0xd8, 0xd8, 0x13, 0xb8, 0xdb, 0x3b, 0xd9, 0x98, 0x00
@ -938,7 +946,7 @@ testHelloWorld() {
},
testInsertTooLong() {
let compressed = [
const compressed = [
0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x09, 0x86,
0x46
@ -958,7 +966,7 @@ testInsertTooLong() {
},
testIntactDistanceRingBuffer0() {
let compressed = [
const compressed = [
0x1b, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0xa1, 0x80,
0x20, 0x00
@ -979,7 +987,7 @@ testIntactDistanceRingBuffer0() {
},
testIntactDistanceRingBuffer1() {
let compressed = [
const compressed = [
0x1b, 0x09, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x21, 0xa0,
0x20, 0x00
@ -1000,7 +1008,7 @@ testIntactDistanceRingBuffer1() {
},
testIntactDistanceRingBuffer2() {
let compressed = [
const compressed = [
0x1b, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x80,
0x20, 0x50, 0x10, 0x24, 0x08, 0x06
@ -1025,7 +1033,7 @@ testIntactDistanceRingBuffer2() {
},
testIntactDistanceRingBufferNoDistanceValue0() {
let compressed = [
const compressed = [
0x1b, 0x17, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x40, 0x82,
0x40, 0x41, 0x90, 0x20, 0x58, 0x18, 0x00
@ -1055,7 +1063,7 @@ testIntactDistanceRingBufferNoDistanceValue0() {
},
testIntactDistanceRingBufferNoDistanceValue1() {
let compressed = [
const compressed = [
0x1b, 0x19, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0xc0, 0x82,
0x41, 0x41, 0x90, 0x20, 0x58, 0x18, 0x00
@ -1085,7 +1093,7 @@ testIntactDistanceRingBufferNoDistanceValue1() {
},
testInvalidNoLastMetablock() {
let compressed = [
const compressed = [
0x0b, 0x06, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x9b, 0x00, 0x13,
0x59, 0x98, 0xda, 0xd8, 0xd8, 0x13, 0xb8, 0xdb, 0x3b, 0xd9, 0x98, 0xe8,
@ -1103,7 +1111,7 @@ testInvalidNoLastMetablock() {
},
testInvalidNoMetaBlocks() {
let compressed = [
const compressed = [
0x0b
];
checkSynth(
@ -1117,7 +1125,7 @@ testInvalidNoMetaBlocks() {
},
testInvalidTooFarDist() {
let compressed = [
const compressed = [
0xa1, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xa7, 0x6d, 0x00, 0x00,
0x38, 0xd8, 0x32, 0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0xe8, 0xe0,
0x62, 0x6f, 0x4f, 0x60, 0x66, 0xe8, 0x44, 0x38, 0x0f, 0x09, 0x0d
@ -1139,7 +1147,7 @@ testInvalidTooFarDist() {
},
testInvalidTooLargeContextMap() {
let compressed = [
const compressed = [
0x1b, 0x00, 0x00, 0xd1, 0xe1, 0x01, 0xc6, 0xe0, 0xe2, 0x06, 0x00, 0x00,
0x91, 0xb2, 0x70, 0xfe, 0xfb, 0x45, 0x58, 0x88, 0x01, 0x00, 0x70, 0xb0,
0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x11, 0x01
@ -1185,7 +1193,7 @@ testInvalidTooLargeContextMap() {
},
testInvalidTransformType() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x2d,
0x01, 0x19
@ -1204,7 +1212,7 @@ testInvalidTransformType() {
},
testInvalidWindowBits9() {
let compressed = [
const compressed = [
0x91, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xa7, 0x6d, 0x00, 0x00,
0x38, 0xd8, 0x32, 0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0xc8, 0x20,
0x32, 0xd4, 0x01
@ -1221,7 +1229,7 @@ testInvalidWindowBits9() {
},
testManyTinyMetablocks() {
let compressed = [
const compressed = [
0x0b, 0x00, 0x80, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x4e,
0xdb, 0x00, 0x00, 0x70, 0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee,
0xb4, 0x11, 0x61, 0x04, 0x00, 0x80, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -1923,7 +1931,7 @@ testManyTinyMetablocks() {
},
testNegativeDistance() {
let compressed = [
const compressed = [
0x1b, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x02,
0x01, 0x42, 0x01, 0x42, 0x01, 0x42, 0x01, 0x42, 0x01, 0x42, 0x01, 0x1c
@ -1954,7 +1962,7 @@ testNegativeDistance() {
},
testNegativeRemainingLenBetweenMetablocks() {
let compressed = [
const compressed = [
0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x09, 0x86,
0x46, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x4e, 0xdb, 0x00, 0x00,
@ -1976,7 +1984,7 @@ testNegativeRemainingLenBetweenMetablocks() {
},
testOneCommand() {
let compressed = [
const compressed = [
0x1b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x11, 0x86,
0x02
@ -1995,7 +2003,7 @@ testOneCommand() {
},
testOneInsert() {
let compressed = [
const compressed = [
0x1b, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x09, 0x86,
0x46
@ -2014,7 +2022,7 @@ testOneInsert() {
},
testSimplePrefix() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x51, 0xa0, 0x1d
];
@ -2036,7 +2044,7 @@ testSimplePrefix() {
},
testSimplePrefixDuplicateSymbols() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc2, 0xc4, 0x02, 0x00, 0x70,
0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x51, 0xa0, 0x1d
];
@ -2058,7 +2066,7 @@ testSimplePrefixDuplicateSymbols() {
},
testSimplePrefixOutOfRangeSymbols() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x4d, 0xff, 0xef,
0x7f, 0xff, 0xfc, 0x07, 0x00, 0xb8, 0xd3, 0x06
];
@ -2080,7 +2088,7 @@ testSimplePrefixOutOfRangeSymbols() {
testSimplePrefixPlusExtraData() {
// SKIP: JS decoder does not tolerate extra input after the brotli stream.
if ({}.toString() == {}) return; // same as 'if (true) return'
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x51, 0xa0, 0x1d,
0x55, 0xaa
@ -2105,7 +2113,7 @@ testSimplePrefixPlusExtraData() {
},
testStressReadDistanceExtraBits() {
let compressed = [
const compressed = [
0x4f, 0xfe, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00,
0x00, 0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x9b, 0xf6,
0x69, 0xef, 0xff, 0x0c, 0x8d, 0x8c, 0x05, 0x10, 0x00, 0x00, 0x00, 0x00,
@ -2119,7 +2127,7 @@ testStressReadDistanceExtraBits() {
0x46, 0x03
];
/* This line is added manually. */
let stub = repeat("c", 8388602); let hex = "0123456789ABCDEF";
const stub = repeat("c", 8388602); const hex = "0123456789ABCDEF";
checkSynth(
/*
* main_header: 24
@ -2178,7 +2186,7 @@ testStressReadDistanceExtraBits() {
},
testTooManySymbolsRepeated() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc3, 0x3d, 0x80, 0x58, 0x82,
0x0c, 0x00, 0xc0, 0xc1, 0x96, 0x49, 0x0c, 0x90, 0x00, 0x00, 0xb8, 0xd3,
0x46, 0x01, 0x1a, 0x01
@ -2209,7 +2217,7 @@ testTooManySymbolsRepeated() {
},
testTransformedDictWord() {
let compressed = [
const compressed = [
0x1b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x09,
0x01, 0x01
@ -2229,7 +2237,7 @@ testTransformedDictWord() {
},
testTransformedDictWordTooLong() {
let compressed = [
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe3, 0xb4, 0x0d, 0x00, 0x00,
0x07, 0x5b, 0x26, 0x31, 0x40, 0x02, 0x00, 0xe0, 0x4e, 0x1b, 0x41, 0x09,
0x01, 0x01
@ -2251,12 +2259,12 @@ testTransformedDictWordTooLong() {
},
testZeroCostLiterals() {
let compressed = [
const compressed = [
0x9b, 0xff, 0xff, 0xff, 0x00, 0x20, 0x54, 0x00, 0x00, 0x38, 0xd8, 0x32,
0x89, 0x01, 0x12, 0x00, 0x00, 0x77, 0xda, 0xcc, 0xe1, 0x7b, 0xfa, 0x0f
];
/* This line is added manually. */
let expected = repeat("*", 16777216);
const expected = repeat("*", 16777216);
checkSynth(
/*
* main_header

2285
js/decode_synth_test.ts Normal file

File diff suppressed because it is too large Load Diff

38
js/decode_test.ts Normal file
View File

@ -0,0 +1,38 @@
/* Copyright 2023 Google Inc. All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
import 'jasmine';
import {brotliDecode} from './decode';
function bytesToString(bytes: Int8Array): string {
const chars: number[] = new Uint16Array(bytes) as unknown as number[];
return String.fromCharCode.apply(null, chars);
}
function stringToBytes(str: string): Int8Array {
const out = new Int8Array(str.length);
for (let i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
return out;
}
describe('DecodeTest', () => {
it('testMetadata', () => {
expect('').toEqual(
bytesToString(brotliDecode(Int8Array.from([1, 11, 0, 42, 3]))));
});
it('testCompoundDictionary', () => {
const txt = 'kot lomom kolol slona\n';
const dictionary = stringToBytes(txt);
const compressed =
[0xa1, 0xa8, 0x00, 0xc0, 0x2f, 0x01, 0x10, 0xc4, 0x44, 0x09, 0x00];
expect(txt.length).toEqual(compressed.length * 2);
const options = {'customDictionary': dictionary};
expect(txt).toEqual(
bytesToString(brotliDecode(Int8Array.from(compressed), options)));
});
});

27
js/test_data.ts Normal file

File diff suppressed because one or more lines are too long