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 { assertEquals } from "@std/assert";
|
2022-11-29 06:01:21 +00:00
|
|
|
import { lastIndexOfNeedle } from "./last_index_of_needle.ts";
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle1() handles repeating occurence", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
|
|
|
new Uint8Array([0, 1, 2]),
|
|
|
|
);
|
|
|
|
assertEquals(i, 3);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() handles single occurence", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 1]),
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
);
|
|
|
|
assertEquals(i, 0);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() handles missing occurence", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const i = lastIndexOfNeedle(new Uint8Array(), new Uint8Array([0, 1]));
|
|
|
|
assertEquals(i, -1);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() returns index of occurence after start", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2]),
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
assertEquals(i, 0);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() returns -1 with too small start", () => {
|
2022-11-29 06:01:21 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2]),
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
-1,
|
|
|
|
);
|
|
|
|
assertEquals(i, -1);
|
|
|
|
});
|
2022-12-03 12:27:32 +00:00
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() returns index if start is greater than source index", () => {
|
2022-12-03 12:27:32 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2]),
|
|
|
|
new Uint8Array([0, 1]),
|
|
|
|
7,
|
|
|
|
);
|
|
|
|
assertEquals(i, 3);
|
|
|
|
});
|
|
|
|
|
2023-12-19 01:16:10 +00:00
|
|
|
Deno.test("lastIndexOfNeedle() returns -1 if needle doesn't exist within source", () => {
|
2022-12-03 12:27:32 +00:00
|
|
|
const i = lastIndexOfNeedle(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2]),
|
|
|
|
new Uint8Array([2, 3]),
|
|
|
|
);
|
|
|
|
assertEquals(i, -1);
|
|
|
|
});
|