tests: improve for GeneratePermutations (#1263)

This commit is contained in:
k ho k ho?
2022-11-13 09:41:54 -08:00
committed by GitHub
parent 9d4adbb0b3
commit 71d3d4470f

View File

@ -1,14 +1,33 @@
import { factorial } from '../../Recursive/Factorial'
import { permutations } from '../GeneratePermutations' import { permutations } from '../GeneratePermutations'
describe('Permutations', () => { describe('Permutations', () => {
it('Permutations of [a]', () => {
const perms = permutations(['a'])
expect(perms).toHaveLength(factorial(1))
expect(perms).toContainEqual(['a'])
})
it('Permutations of [true, false]', () => {
const perms = permutations([true, false])
expect(perms).toHaveLength(factorial(2))
expect(perms).toContainEqual([true, false])
expect(perms).toContainEqual([false, true])
})
it('Permutations of [1, 2, 3]', () => { it('Permutations of [1, 2, 3]', () => {
expect(permutations([1, 2, 3])).toEqual([ const perms = permutations([1, 2, 3])
[1, 2, 3], expect(perms).toHaveLength(factorial(3))
[1, 3, 2], expect(perms).toContainEqual([1, 2, 3])
[2, 1, 3], expect(perms).toContainEqual([1, 3, 2])
[2, 3, 1], expect(perms).toContainEqual([2, 1, 3])
[3, 1, 2], expect(perms).toContainEqual([2, 3, 1])
[3, 2, 1] expect(perms).toContainEqual([3, 1, 2])
]) expect(perms).toContainEqual([3, 2, 1])
})
it('Permutation counts across larger input arrays', () => {
expect(permutations([1, 2, 3, 4, 5, 6, 7, 8])).toHaveLength(factorial(8))
expect(permutations([1, 2, 3, 4, 5, 6, 7, 8, 9])).toHaveLength(factorial(9))
}) })
}) })