decode_test.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* Copyright 2023 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. import 'jasmine';
  6. import {brotliDecode} from './decode';
  7. function bytesToString(bytes: Int8Array): string {
  8. const chars: number[] = new Uint16Array(bytes) as unknown as number[];
  9. return String.fromCharCode.apply(null, chars);
  10. }
  11. function stringToBytes(str: string): Int8Array {
  12. const out = new Int8Array(str.length);
  13. for (let i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
  14. return out;
  15. }
  16. describe('DecodeTest', () => {
  17. it('testMetadata', () => {
  18. expect('').toEqual(
  19. bytesToString(brotliDecode(Int8Array.from([1, 11, 0, 42, 3]))));
  20. });
  21. it('testCompoundDictionary', () => {
  22. const txt = 'kot lomom kolol slona\n';
  23. const dictionary = stringToBytes(txt);
  24. const compressed =
  25. [0xa1, 0xa8, 0x00, 0xc0, 0x2f, 0x01, 0x10, 0xc4, 0x44, 0x09, 0x00];
  26. expect(txt.length).toEqual(compressed.length * 2);
  27. const options = {'customDictionary': dictionary};
  28. expect(txt).toEqual(
  29. bytesToString(brotliDecode(Int8Array.from(compressed), options)));
  30. });
  31. });