11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=resources/readable-stream-from-array.js
31cb0ef41Sopenharmony_ci// META: script=resources/readable-stream-to-array.js
41cb0ef41Sopenharmony_ci// META: script=/common/sab.js
51cb0ef41Sopenharmony_ci'use strict';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
81cb0ef41Sopenharmony_ci  const inputChunkData = [73, 32, 240, 159, 146, 153, 32, 115, 116, 114, 101, 97, 109, 115];
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  const emptyChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 0));
111cb0ef41Sopenharmony_ci  const inputChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, inputChunkData.length));
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  inputChunk.set(inputChunkData);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const expectedOutputString = 'I \u{1F499} streams';
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  promise_test(async () => {
181cb0ef41Sopenharmony_ci    const input = readableStreamFromArray([inputChunk]);
191cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
201cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
211cb0ef41Sopenharmony_ci    assert_array_equals(array, [expectedOutputString],
221cb0ef41Sopenharmony_ci                        'the output should be in one chunk');
231cb0ef41Sopenharmony_ci  }, 'decoding one UTF-8 chunk should give one output string - ' + arrayBufferOrSharedArrayBuffer);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  promise_test(async () => {
261cb0ef41Sopenharmony_ci    const input = readableStreamFromArray([emptyChunk]);
271cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
281cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
291cb0ef41Sopenharmony_ci    assert_array_equals(array, [], 'no chunks should be output');
301cb0ef41Sopenharmony_ci  }, 'decoding an empty chunk should give no output chunks - ' + arrayBufferOrSharedArrayBuffer);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  promise_test(async () => {
331cb0ef41Sopenharmony_ci    const input = readableStreamFromArray([emptyChunk, inputChunk]);
341cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
351cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
361cb0ef41Sopenharmony_ci    assert_array_equals(array, [expectedOutputString],
371cb0ef41Sopenharmony_ci                        'the output should be in one chunk');
381cb0ef41Sopenharmony_ci  }, 'an initial empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  promise_test(async () => {
411cb0ef41Sopenharmony_ci    const input = readableStreamFromArray([inputChunk, emptyChunk]);
421cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
431cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
441cb0ef41Sopenharmony_ci    assert_array_equals(array, [expectedOutputString],
451cb0ef41Sopenharmony_ci                        'the output should be in one chunk');
461cb0ef41Sopenharmony_ci  }, 'a trailing empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  promise_test(async () => {
491cb0ef41Sopenharmony_ci    const chunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 3));
501cb0ef41Sopenharmony_ci    chunk.set([0xF0, 0x9F, 0x92]);
511cb0ef41Sopenharmony_ci    const input = readableStreamFromArray([chunk]);
521cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
531cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
541cb0ef41Sopenharmony_ci    assert_array_equals(array, ['\uFFFD']);
551cb0ef41Sopenharmony_ci  }, 'UTF-8 EOF handling - ' + arrayBufferOrSharedArrayBuffer);
561cb0ef41Sopenharmony_ci});
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cipromise_test(async () => {
591cb0ef41Sopenharmony_ci  const buffer = new ArrayBuffer(3);
601cb0ef41Sopenharmony_ci  const view = new Uint8Array(buffer, 1, 1);
611cb0ef41Sopenharmony_ci  view[0] = 65;
621cb0ef41Sopenharmony_ci  new MessageChannel().port1.postMessage(buffer, [buffer]);
631cb0ef41Sopenharmony_ci  const input = readableStreamFromArray([view]);
641cb0ef41Sopenharmony_ci  const output = input.pipeThrough(new TextDecoderStream());
651cb0ef41Sopenharmony_ci  const array = await readableStreamToArray(output);
661cb0ef41Sopenharmony_ci  assert_array_equals(array, [], 'no chunks should be output');
671cb0ef41Sopenharmony_ci}, 'decoding a transferred Uint8Array chunk should give no output');
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cipromise_test(async () => {
701cb0ef41Sopenharmony_ci  const buffer = new ArrayBuffer(1);
711cb0ef41Sopenharmony_ci  new MessageChannel().port1.postMessage(buffer, [buffer]);
721cb0ef41Sopenharmony_ci  const input = readableStreamFromArray([buffer]);
731cb0ef41Sopenharmony_ci  const output = input.pipeThrough(new TextDecoderStream());
741cb0ef41Sopenharmony_ci  const array = await readableStreamToArray(output);
751cb0ef41Sopenharmony_ci  assert_array_equals(array, [], 'no chunks should be output');
761cb0ef41Sopenharmony_ci}, 'decoding a transferred ArrayBuffer chunk should give no output');
77