2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-05 01:20:14 +00:00
|
|
|
|
2024-06-17 10:09:36 +00:00
|
|
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
import { Buffer } from "./buffer.ts";
|
2022-03-05 01:20:14 +00:00
|
|
|
|
2024-06-17 10:09:36 +00:00
|
|
|
Deno.test("Buffer handles write and read", async () => {
|
2022-03-05 01:20:14 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-06-17 10:09:36 +00:00
|
|
|
Deno.test("Buffer can constrruct with initial data", async () => {
|
|
|
|
const data = new Uint8Array([4, 21, 45, 19]);
|
|
|
|
const buf = new Buffer(data);
|
|
|
|
const reader = buf.readable.getReader({ mode: "byob" });
|
|
|
|
const read = await reader.read(new Uint8Array(4));
|
|
|
|
assertEquals(read.value, data);
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("Buffer handles read empty", async function () {
|
2022-03-05 01:20:14 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-06-17 10:09:36 +00:00
|
|
|
Deno.test("Buffer handles write and get bytes", async () => {
|
2022-03-05 01:20:14 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-06-17 10:09:36 +00:00
|
|
|
Deno.test("Buffer.truncate(n) truncates the data except the first n bytes", async () => {
|
2022-03-05 01:20:14 +00:00
|
|
|
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]));
|
|
|
|
});
|
2024-06-17 10:09:36 +00:00
|
|
|
|
|
|
|
Deno.test("Buffer.truncate(0) truncates the entire buffer", async () => {
|
|
|
|
const data = new Uint8Array([4, 21, 45, 19]);
|
|
|
|
const buf = new Buffer(data.buffer);
|
|
|
|
buf.truncate(0);
|
|
|
|
assertEquals(await buf.bytes(), new Uint8Array(0));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.truncate(n) throws if n is negative", () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
buf.truncate(-1);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-23 03:31:01 +00:00
|
|
|
'Buffer truncation value "-1" is not between 0 and 0',
|
2024-06-17 10:09:36 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.truncate(n) throws if n is greater than the length of the buffer", () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
buf.truncate(1);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-23 03:31:01 +00:00
|
|
|
'Buffer truncation value "1" is not between 0 and 0',
|
2024-06-17 10:09:36 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.bytes({ copy: false }) returns subarray, not an slice", () => {
|
|
|
|
const data = new Uint8Array([4, 21, 45, 19]);
|
|
|
|
const buf = new Buffer(data.buffer);
|
|
|
|
const subarray = buf.bytes({ copy: false });
|
|
|
|
subarray.set([0, 0], 0);
|
|
|
|
// The original array is affected
|
|
|
|
assertEquals(data, new Uint8Array([0, 0, 45, 19]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.grow(n) allocates necessary array buffer (increases the capacity of the buffer) if necessary", () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
assertEquals(buf.capacity, 0);
|
|
|
|
buf.grow(10);
|
|
|
|
assertEquals(buf.capacity, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.grow(n) doesn't allocate a new array buffer if it has enough capacity", async () => {
|
|
|
|
const buf = new Buffer(new Uint8Array(100).buffer);
|
|
|
|
buf.reset();
|
|
|
|
assertEquals(buf.capacity, 100);
|
|
|
|
await buf.writable.getWriter().write(new Uint8Array([4, 21, 45, 19]));
|
|
|
|
await buf.readable.getReader({ mode: "byob" }).read(new Uint8Array(4));
|
|
|
|
buf.grow(10);
|
|
|
|
assertEquals(buf.capacity, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.grow(n) slides down the unread data if the required n + buf.length <= buf.capacity / 2", async () => {
|
|
|
|
const array = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
|
|
const buf = new Buffer(array.buffer);
|
|
|
|
await buf.readable.getReader({ mode: "byob" }).read(new Uint8Array(8)); // read 8 bytes
|
|
|
|
buf.grow(2); // grow the buffer by 2 bytes
|
|
|
|
assertEquals(buf.bytes(), new Uint8Array([8, 9]));
|
|
|
|
assertEquals(array, new Uint8Array([8, 9, 2, 3, 4, 5, 6, 7, 8, 9]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.grow(n) throws an error if n is a negative value", () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
buf.grow(-1);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-23 03:31:01 +00:00
|
|
|
"Cannot grow buffer as growth must be positive: received -1",
|
2024-06-17 10:09:36 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Buffer.grow(n) throws if the total of byte size exceeds 2 ** 32 - 2", () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
buf.grow(2 ** 32);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-23 03:31:01 +00:00
|
|
|
"The buffer cannot grow beyond the maximum size of",
|
2024-06-17 10:09:36 +00:00
|
|
|
);
|
|
|
|
});
|