2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
import { ByteSliceStream } from "./byte_slice_stream.ts";
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("ByteSliceStream", async function () {
|
2022-11-29 13:55:38 +00:00
|
|
|
function createStream(start = 0, end = Infinity) {
|
2023-11-10 03:57:52 +00:00
|
|
|
return ReadableStream.from([
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]).pipeThrough(new ByteSliceStream(start, end));
|
2022-11-29 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
let chunks = await Array.fromAsync(createStream(0, 3));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(0, 1));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(0, 2));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(0, 3));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(1, 3));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(2, 3));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
chunks = await Array.fromAsync(createStream(0, 10));
|
2022-11-29 13:55:38 +00:00
|
|
|
assertEquals(chunks, [
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assertThrows(() => createStream(-1, Infinity));
|
|
|
|
});
|