test(bytes): document the cases being tested for equals/startsWith/endsWith (#6163)

This commit is contained in:
lionel-rowe 2024-10-31 22:22:19 +08:00 committed by GitHub
parent 6637a2067a
commit 689fb69c14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 105 additions and 25 deletions

View File

@ -1,13 +1,42 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert } from "@std/assert"; import { assert } from "@std/assert";
import { endsWith } from "./ends_with.ts"; import { endsWith } from "./ends_with.ts";
Deno.test("endsWith()", () => { Deno.test("endsWith()", async (t) => {
const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); await t.step("`true` where `source` and `suffix` are identical", () => {
const v2 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); assert(endsWith(
const v3 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2, 3])); new Uint8Array([0, 1, 2, 3]),
assert(v); new Uint8Array([0, 1, 2, 3]),
assert(!v2); ));
assert(!v3); });
await t.step("`true` where `source` ends with `suffix`", () => {
assert(endsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([1, 2]),
));
});
await t.step("`false` with a common but only partial suffix", () => {
assert(
!endsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 2]),
),
);
});
await t.step("`false` where `suffix` is longer", () => {
assert(
!endsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 2, 3, 4]),
),
);
});
await t.step("`false` where `suffix` ends with `source`", () => {
assert(
!endsWith(
new Uint8Array([1, 2]),
new Uint8Array([0, 1, 2]),
),
);
});
}); });

View File

@ -2,13 +2,37 @@
import { equals } from "./equals.ts"; import { equals } from "./equals.ts";
import { assert, assertEquals, assertNotEquals } from "@std/assert"; import { assert, assertEquals, assertNotEquals } from "@std/assert";
Deno.test("equals()", () => { Deno.test("equals()", async (t) => {
const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); await t.step("`true` where `a` and `b` are identical", () => {
const v2 = equals(new Uint8Array([0, 1, 2, 2]), new Uint8Array([0, 1, 2, 3])); assert(equals(
const v3 = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2])); new Uint8Array([0, 1, 2, 3]),
assert(v); new Uint8Array([0, 1, 2, 3]),
assert(!v2); ));
assert(!v3); });
await t.step("`false` where last byte differs", () => {
assert(
!equals(
new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2, 255]),
),
);
});
await t.step("`false` where `b` is a truncated version of `a`", () => {
assert(
!equals(
new Uint8Array([0, 1, 2, 0]),
new Uint8Array([0, 1, 2]),
),
);
});
await t.step("`false` where `a` is a truncated version of `b`", () => {
assert(
!equals(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 1, 2, 0]),
),
);
});
}); });
const THRESHOLD_32_BIT = 160; const THRESHOLD_32_BIT = 160;

View File

@ -2,14 +2,41 @@
import { assert } from "@std/assert"; import { assert } from "@std/assert";
import { startsWith } from "./starts_with.ts"; import { startsWith } from "./starts_with.ts";
Deno.test("startsWith()", () => { Deno.test("startsWith()", async (t) => {
const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); await t.step("`true` where `source` and `prefix` are identical", () => {
const v2 = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 2])); assert(startsWith(
const v3 = startsWith( new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 2, 3, 4]), ));
); });
assert(v); await t.step("`true` where `source` starts with `prefix`", () => {
assert(!v2); assert(startsWith(
assert(!v3); new Uint8Array([0, 1, 2]),
new Uint8Array([0, 1]),
));
});
await t.step("`false` with a common but only partial prefix", () => {
assert(
!startsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 2]),
),
);
});
await t.step("`false` where `prefix` is longer", () => {
assert(
!startsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 2, 3, 4]),
),
);
});
await t.step("`false` where `prefix` starts with `source`", () => {
assert(
!startsWith(
new Uint8Array([0, 1]),
new Uint8Array([0, 1, 2]),
),
);
});
}); });