summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/compression/compression-including-empty-chunk.tentative.any.js
blob: a7fd1ceb24f086e7b7579c9826a352774dc97f96 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// META: global=window,worker,shadowrealm
// META: script=third_party/pako/pako_inflate.min.js
// META: timeout=long

'use strict';

// This test asserts that compressing '' doesn't affect the compressed data.
// Example: compressing ['Hello', '', 'Hello'] results in 'HelloHello'

async function compressChunkList(chunkList, format) {
  const cs = new CompressionStream(format);
  const writer = cs.writable.getWriter();
  for (const chunk of chunkList) {
    const chunkByte = new TextEncoder().encode(chunk);
    writer.write(chunkByte);
  }
  const closePromise = writer.close();
  const out = [];
  const reader = cs.readable.getReader();
  let totalSize = 0;
  while (true) {
    const { value, done } = await reader.read();
    if (done)
      break;
    out.push(value);
    totalSize += value.byteLength;
  }
  await closePromise;
  const concatenated = new Uint8Array(totalSize);
  let offset = 0;
  for (const array of out) {
    concatenated.set(array, offset);
    offset += array.byteLength;
  }
  return concatenated;
}

const chunkLists = [
  ['', 'Hello', 'Hello'],
  ['Hello', '', 'Hello'],
  ['Hello', 'Hello', '']
];
const expectedValue = new TextEncoder().encode('HelloHello');

for (const chunkList of chunkLists) {
  promise_test(async t => {
    const compressedData = await compressChunkList(chunkList, 'deflate');
    // decompress with pako, and check that we got the same result as our original string
    assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
  }, `the result of compressing [${chunkList}] with deflate should be 'HelloHello'`);

  promise_test(async t => {
    const compressedData = await compressChunkList(chunkList, 'gzip');
    // decompress with pako, and check that we got the same result as our original string
    assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
  }, `the result of compressing [${chunkList}] with gzip should be 'HelloHello'`);

  promise_test(async t => {
    const compressedData = await compressChunkList(chunkList, 'deflate-raw');
    // decompress with pako, and check that we got the same result as our original string
    assert_array_equals(expectedValue, pako.inflateRaw(compressedData), 'value should match');
  }, `the result of compressing [${chunkList}] with deflate-raw should be 'HelloHello'`);
}