std/io/copy_n_test.ts
2024-04-29 11:57:30 +09:00

37 lines
1.1 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { copyN } from "./copy_n.ts";
import { Buffer } from "./buffer.ts";
import { StringReader } from "./string_reader.ts";
Deno.test("testCopyN1", async function () {
const w = new Buffer();
const r = new StringReader("abcdefghij");
const n = await copyN(r, w, 3);
assertEquals(n, 3);
assertEquals(new TextDecoder().decode(w.bytes()), "abc");
});
Deno.test("testCopyN2", async function () {
const w = new Buffer();
const r = new StringReader("abcdefghij");
const n = await copyN(r, w, 11);
assertEquals(n, 10);
assertEquals(new TextDecoder().decode(w.bytes()), "abcdefghij");
});
Deno.test("copyNWriteAllData", async function () {
const tmpDir = await Deno.makeTempDir();
const filepath = `${tmpDir}/data`;
using file = await Deno.open(filepath, { create: true, write: true });
const size = 16 * 1024 + 1;
const data = "a".repeat(32 * 1024);
const r = new StringReader(data);
const n = await copyN(r, file, size); // Over max file possible buffer
await Deno.remove(filepath);
assertEquals(n, size);
});