-
-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathgeneratepermutations.test.ts
39 lines (34 loc) · 1.21 KB
/
generatepermutations.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { permutations } from '../generatepermutations'
const factorial = (n: number): number => {
if (n === 0 || n === 1) {
return 1
}
return n * factorial(n - 1)
}
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]', () => {
const perms = permutations([1, 2, 3])
expect(perms).toHaveLength(factorial(3))
expect(perms).toContainEqual([1, 2, 3])
expect(perms).toContainEqual([1, 3, 2])
expect(perms).toContainEqual([2, 1, 3])
expect(perms).toContainEqual([2, 3, 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))
})
})