decode_test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Copyright 2017 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 {BrotliDecode} from "./decode.js";
  6. /**
  7. * @param {!Int8Array} bytes
  8. * @return {string}
  9. */
  10. function bytesToString(bytes) {
  11. return String.fromCharCode.apply(null, new Uint16Array(bytes));
  12. }
  13. /**
  14. * @param {string} str
  15. * @return {!Int8Array}
  16. */
  17. function stringToBytes(str) {
  18. let out = new Int8Array(str.length);
  19. for (let i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
  20. return out;
  21. }
  22. describe('DecodeTest', () => {
  23. it('testMetadata', () => {
  24. expect('').toEqual(
  25. bytesToString(BrotliDecode(Int8Array.from([1, 11, 0, 42, 3]))));
  26. });
  27. it('testCompoundDictionary', () => {
  28. const txt = 'kot lomom kolol slona\n';
  29. const dictionary = stringToBytes(txt);
  30. const compressed =
  31. [0xa1, 0xa8, 0x00, 0xc0, 0x2f, 0x01, 0x10, 0xc4, 0x44, 0x09, 0x00];
  32. expect(txt.length).toEqual(compressed.length * 2);
  33. const options = {'customDictionary': dictionary};
  34. expect(txt).toEqual(
  35. bytesToString(BrotliDecode(Int8Array.from(compressed), options)));
  36. });
  37. });