2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assert, assertEquals } from "@std/assert";
|
2022-11-29 06:01:21 +00:00
|
|
|
import { concat } from "./concat.ts";
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("concat()", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const u1 = encoder.encode("Hello ");
|
|
|
|
const u2 = encoder.encode("World");
|
2023-11-13 05:34:32 +00:00
|
|
|
const joined = concat([u1, u2]);
|
2022-11-29 06:01:21 +00:00
|
|
|
assertEquals(new TextDecoder().decode(joined), "Hello World");
|
|
|
|
assert(u1 !== joined);
|
|
|
|
assert(u2 !== joined);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("concat() handles empty arrays", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const u1 = new Uint8Array();
|
|
|
|
const u2 = new Uint8Array();
|
2023-11-13 05:34:32 +00:00
|
|
|
const joined = concat([u1, u2]);
|
2022-11-29 06:01:21 +00:00
|
|
|
assertEquals(joined.byteLength, 0);
|
|
|
|
assert(u1 !== joined);
|
|
|
|
assert(u2 !== joined);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("concat() handles multiple Uint8Array", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const u1 = encoder.encode("Hello ");
|
|
|
|
const u2 = encoder.encode("W");
|
|
|
|
const u3 = encoder.encode("o");
|
|
|
|
const u4 = encoder.encode("r");
|
|
|
|
const u5 = encoder.encode("l");
|
|
|
|
const u6 = encoder.encode("d");
|
2023-11-13 05:34:32 +00:00
|
|
|
const joined = concat([u1, u2, u3, u4, u5, u6]);
|
2022-11-29 06:01:21 +00:00
|
|
|
assertEquals(new TextDecoder().decode(joined), "Hello World");
|
|
|
|
assert(u1 !== joined);
|
|
|
|
assert(u2 !== joined);
|
|
|
|
});
|
2023-11-08 08:30:22 +00:00
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("concat() handles an array of Uint8Array", () => {
|
2023-11-08 08:30:22 +00:00
|
|
|
const a = [
|
|
|
|
new Uint8Array([0, 1, 2, 3]),
|
|
|
|
new Uint8Array([4, 5, 6]),
|
|
|
|
new Uint8Array([7, 8, 9]),
|
|
|
|
];
|
|
|
|
const joined = concat(a);
|
|
|
|
const expected = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
|
|
assertEquals(joined, expected);
|
|
|
|
});
|