aboutsummaryrefslogtreecommitdiff
path: root/js/decode_test.js
blob: cd1cbcf0d57d7659b694257245e575b0186425cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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";
const testSuite = goog.require('goog.testing.testSuite');
goog.require('goog.testing.asserts');

/**
 * @param {!Int8Array} bytes
 * @return {string}
 */
function bytesToString(bytes) {
  return String.fromCharCode.apply(null, new Uint16Array(bytes));
}

/**
 * @param {string} str
 * @return {!Int8Array}
 */
function stringToBytes(str) {
  var out = new Int8Array(str.length);
  for (var i = 0; i < str.length; ++i) out[i] = str.charCodeAt(i);
  return out;
}

testSuite({
  testMetadata() {
    assertEquals("", bytesToString(BrotliDecode(Int8Array.from([1, 11, 0, 42, 3]))));
  },

  testCompoundDictionary() {
    var txt = "kot lomom kolol slona\n";
    var dictionary = stringToBytes(txt);
    var compressed = [0xa1, 0xa8, 0x00, 0xc0, 0x2f, 0x01, 0x10, 0xc4, 0x44, 0x09, 0x00];
    assertEquals(txt.length, compressed.length * 2);
    var options = {"customDictionary": dictionary};
    assertEquals(txt, bytesToString(BrotliDecode(Int8Array.from(compressed), options)));
  }
});