-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation_test.go
59 lines (50 loc) · 1.31 KB
/
permutation_test.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package permutation
import (
"testing"
"sort"
"reflect"
)
func TestNextPermutation(t *testing.T) {
arr := []int{0, 1, 2, 5, 3, 3, 0}
perm := [][]int {
{0, 1, 3, 0, 2, 3, 5},
{0, 1, 3, 0, 2, 5, 3},
{0, 1, 3, 0, 3, 2, 5},
{0, 1, 3, 0, 3, 5, 2},
{0, 1, 3, 0, 5, 2, 3},
{0, 1, 3, 0, 5, 3, 2},
{0, 1, 3, 2, 0, 3, 5},
{0, 1, 3, 2, 0, 5, 3},
{0, 1, 3, 2, 3, 0, 5},
{0, 1, 3, 2, 3, 5, 0},
}
for i, p := range perm {
r1 := NextPermutation(sort.IntSlice(arr))
r2 := reflect.DeepEqual(arr, p)
if !r1 || !r2 {
t.Error("Failed at: ", i, r1, r2, arr, p)
}
}
}
func TestPrevPermutation(t *testing.T) {
arr := []int{0, 1, 3, 2, 3, 5, 0}
perm := [][]int {
{0, 1, 3, 2, 3, 0, 5},
{0, 1, 3, 2, 0, 5, 3},
{0, 1, 3, 2, 0, 3, 5},
{0, 1, 3, 0, 5, 3, 2},
{0, 1, 3, 0, 5, 2, 3},
{0, 1, 3, 0, 3, 5, 2},
{0, 1, 3, 0, 3, 2, 5},
{0, 1, 3, 0, 2, 5, 3},
{0, 1, 3, 0, 2, 3, 5},
{0, 1, 2, 5, 3, 3, 0},
}
for i, p := range perm {
r1 := PrevPermutation(sort.IntSlice(arr))
r2 := reflect.DeepEqual(arr, p)
if !r1 || !r2 {
t.Error("Failed at: ", i, r1, r2, arr, p)
}
}
}