2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2021-07-15 21:37:53 +00:00
|
|
|
import { permutations } from "./permutations.ts";
|
|
|
|
|
|
|
|
function permutationsTest<T>(
|
|
|
|
input: [Array<T>],
|
|
|
|
expected: Array<Array<T>>,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = permutations(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "permutations() handles no mutation",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
const array = [1, 2, 3];
|
|
|
|
permutations(array);
|
|
|
|
|
|
|
|
assertEquals(array, [1, 2, 3]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "permutations() handles empty input",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
permutationsTest(
|
|
|
|
[[]],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "permutations() handles one element",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
permutationsTest(
|
|
|
|
[
|
|
|
|
[true],
|
|
|
|
],
|
|
|
|
[[true]],
|
|
|
|
);
|
|
|
|
permutationsTest(
|
|
|
|
[
|
|
|
|
[undefined],
|
|
|
|
],
|
|
|
|
[[undefined]],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "permutations() ignores equality",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
permutationsTest(
|
|
|
|
[[1, 1]],
|
|
|
|
[[1, 1], [1, 1]],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "permutations() handles examples",
|
2021-07-15 21:37:53 +00:00
|
|
|
fn() {
|
|
|
|
permutationsTest(
|
|
|
|
[["a", "b", "c"]],
|
|
|
|
[
|
|
|
|
["a", "b", "c"],
|
|
|
|
["b", "a", "c"],
|
|
|
|
["c", "a", "b"],
|
|
|
|
["a", "c", "b"],
|
|
|
|
["b", "c", "a"],
|
|
|
|
["c", "b", "a"],
|
|
|
|
],
|
|
|
|
);
|
|
|
|
permutationsTest(
|
|
|
|
[[true, false, true]],
|
|
|
|
[
|
|
|
|
[true, false, true],
|
|
|
|
[false, true, true],
|
|
|
|
[true, true, false],
|
|
|
|
[true, true, false],
|
|
|
|
[false, true, true],
|
|
|
|
[true, false, true],
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|