mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { Buffer } from "./buffer.ts";
|
|
|
|
Deno.test("Buffer handles write and read", async function () {
|
|
const buf = new Buffer();
|
|
const writer = buf.writable.getWriter();
|
|
const reader = buf.readable.getReader({ mode: "byob" });
|
|
const data = new Uint8Array([4, 21, 45, 19]);
|
|
await writer.write(data);
|
|
const read = await reader.read(new Uint8Array(4));
|
|
assertEquals(read.value, data);
|
|
});
|
|
|
|
Deno.test("Buffer handles read empty", async function () {
|
|
const buf = new Buffer();
|
|
const reader = buf.readable.getReader({ mode: "byob" });
|
|
const read = await reader.read(new Uint8Array(5));
|
|
assert(read.done);
|
|
assertEquals(read.value!.byteLength, 0);
|
|
});
|
|
|
|
Deno.test("Buffer handles write and get bytes", async function () {
|
|
const buf = new Buffer();
|
|
const writer = buf.writable.getWriter();
|
|
const data = new Uint8Array([4, 21, 45, 19]);
|
|
await writer.write(data);
|
|
assertEquals(buf.bytes(), data);
|
|
});
|
|
|
|
Deno.test("Buffer handles truncate", async function () {
|
|
const buf = new Buffer();
|
|
const writer = buf.writable.getWriter();
|
|
await writer.write(new Uint8Array([4, 21, 45, 19]));
|
|
buf.truncate(3);
|
|
assertEquals(buf.bytes(), new Uint8Array([4, 21, 45]));
|
|
});
|