Skip to content

Commit

Permalink
test(bytes): document the cases being tested for equals/startsWith/en…
Browse files Browse the repository at this point in the history
…dsWith (denoland#6163)
  • Loading branch information
lionel-rowe authored Oct 31, 2024
1 parent 6637a20 commit 689fb69
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 25 deletions.
45 changes: 37 additions & 8 deletions bytes/ends_with_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assert } from "@std/assert";
import { endsWith } from "./ends_with.ts";

Deno.test("endsWith()", () => {
const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
const v2 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
const v3 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2, 3]));
assert(v);
assert(!v2);
assert(!v3);
Deno.test("endsWith()", async (t) => {
await t.step("`true` where `source` and `suffix` are identical", () => {
assert(endsWith(
new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2, 3]),
));
});
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]),
),
);
});
});
38 changes: 31 additions & 7 deletions bytes/equals_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@
import { equals } from "./equals.ts";
import { assert, assertEquals, assertNotEquals } from "@std/assert";

Deno.test("equals()", () => {
const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
const v2 = equals(new Uint8Array([0, 1, 2, 2]), new Uint8Array([0, 1, 2, 3]));
const v3 = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2]));
assert(v);
assert(!v2);
assert(!v3);
Deno.test("equals()", async (t) => {
await t.step("`true` where `a` and `b` are identical", () => {
assert(equals(
new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2, 3]),
));
});
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;
Expand Down
47 changes: 37 additions & 10 deletions bytes/starts_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,41 @@
import { assert } from "@std/assert";
import { startsWith } from "./starts_with.ts";

Deno.test("startsWith()", () => {
const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
const v2 = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 2]));
const v3 = startsWith(
new Uint8Array([0, 1, 2]),
new Uint8Array([0, 2, 3, 4]),
);
assert(v);
assert(!v2);
assert(!v3);
Deno.test("startsWith()", async (t) => {
await t.step("`true` where `source` and `prefix` are identical", () => {
assert(startsWith(
new Uint8Array([0, 1, 2, 3]),
new Uint8Array([0, 1, 2, 3]),
));
});
await t.step("`true` where `source` starts with `prefix`", () => {
assert(startsWith(
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]),
),
);
});
});

0 comments on commit 689fb69

Please sign in to comment.