summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/compression/compression-output-length.tentative.any.js
blob: 07efe92a826e787fb00dba2929f94b930c8f8316 (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
64
// META: global=window,worker

'use strict';

// This test asserts that compressed data length is shorter than the original
// data length. If the input is extremely small, the compressed data may be
// larger than the original data.

const LARGE_FILE = '/media/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr.webm';

async function compressArrayBuffer(input, format) {
  const cs = new CompressionStream(format);
  const writer = cs.writable.getWriter();
  writer.write(input);
  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;
}

promise_test(async () => {
  const response = await fetch(LARGE_FILE);
  const buffer = await response.arrayBuffer();
  const bufferView = new Uint8Array(buffer);
  const originalLength = bufferView.length;
  const compressedData = await compressArrayBuffer(bufferView, 'deflate');
  const compressedLength = compressedData.length;
  assert_less_than(compressedLength, originalLength, 'output should be smaller');
}, 'the length of deflated data should be shorter than that of the original data');

promise_test(async () => {
  const response = await fetch(LARGE_FILE);
  const buffer = await response.arrayBuffer();
  const bufferView = new Uint8Array(buffer);
  const originalLength = bufferView.length;
  const compressedData = await compressArrayBuffer(bufferView, 'gzip');
  const compressedLength = compressedData.length;
  assert_less_than(compressedLength, originalLength, 'output should be smaller');
}, 'the length of gzipped data should be shorter than that of the original data');

promise_test(async () => {
  const response = await fetch(LARGE_FILE);
  const buffer = await response.arrayBuffer();
  const bufferView = new Uint8Array(buffer);
  const originalLength = bufferView.length;
  const compressedData = await compressArrayBuffer(bufferView, 'deflate-raw');
  const compressedLength = compressedData.length;
  assert_less_than(compressedLength, originalLength, 'output should be smaller');
}, 'the length of deflated (with -raw) data should be shorter than that of the original data');