test(collections): add tests for filterInPlace() util (#6089)

This commit is contained in:
Liam Tait 2024-10-09 02:55:22 +13:00 committed by GitHub
parent a6a9b86fec
commit 0312678936
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,31 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { filterInPlace } from "./_utils.ts";
import { assert, assertEquals } from "@std/assert";
Deno.test("filterInPlace() filters out elements not matching predicate", () => {
const array = [1, 2, 3, 4, 5];
const result = filterInPlace(array, (el) => el > 2 && el < 5);
assertEquals(result, [3, 4]);
assert(array === result);
});
Deno.test("filterInPlace() filters out all elements when none match predicate", () => {
const array = [1, 2, 3, 4, 5];
const result = filterInPlace(array, (el) => el > 5);
assertEquals(result, []);
assert(array === result);
});
Deno.test("filterInPlace() filters out no elements when all match predicate", () => {
const array = [1, 2, 3, 4, 5];
const result = filterInPlace(array, (el) => el > 0);
assertEquals(result, [1, 2, 3, 4, 5]);
assert(array === result);
});
Deno.test("filterInPlace() filters out no elements when array is empty", () => {
const array: number[] = [];
const result = filterInPlace(array, (el) => el > 0);
assertEquals(result, []);
assert(array === result);
});