2021-06-23 07:40:57 +00:00
|
|
|
/* Copyright 2017 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.js";
|
2017-08-28 09:31:29 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-09 15:07:34 +00:00
|
|
|
* @param {!Int8Array} bytes
|
2017-08-28 09:31:29 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function bytesToString(bytes) {
|
|
|
|
return String.fromCharCode.apply(null, new Uint16Array(bytes));
|
|
|
|
}
|
|
|
|
|
2021-01-18 09:56:39 +00:00
|
|
|
/**
|
|
|
|
* @param {string} str
|
|
|
|
* @return {!Int8Array}
|
|
|
|
*/
|
|
|
|
function stringToBytes(str) {
|
2021-07-29 20:29:43 +00:00
|
|
|
let out = new Int8Array(str.length);
|
|
|
|
for (let i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
|
2021-01-18 09:56:39 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-03-16 20:38:28 +00:00
|
|
|
describe('DecodeTest', () => {
|
|
|
|
it('testMetadata', () => {
|
|
|
|
expect('').toEqual(
|
|
|
|
bytesToString(BrotliDecode(Int8Array.from([1, 11, 0, 42, 3]))));
|
|
|
|
});
|
2021-06-23 07:40:57 +00:00
|
|
|
|
2023-03-16 20:38:28 +00:00
|
|
|
it('testCompoundDictionary', () => {
|
2021-07-29 20:29:43 +00:00
|
|
|
const txt = 'kot lomom kolol slona\n';
|
|
|
|
const dictionary = stringToBytes(txt);
|
|
|
|
const compressed =
|
|
|
|
[0xa1, 0xa8, 0x00, 0xc0, 0x2f, 0x01, 0x10, 0xc4, 0x44, 0x09, 0x00];
|
2023-03-16 20:38:28 +00:00
|
|
|
expect(txt.length).toEqual(compressed.length * 2);
|
2021-07-29 20:29:43 +00:00
|
|
|
const options = {'customDictionary': dictionary};
|
2023-03-16 20:38:28 +00:00
|
|
|
expect(txt).toEqual(
|
|
|
|
bytesToString(BrotliDecode(Int8Array.from(compressed), options)));
|
|
|
|
});
|
2021-06-23 07:40:57 +00:00
|
|
|
});
|