-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_test.go
More file actions
131 lines (124 loc) · 2.42 KB
/
combination_test.go
File metadata and controls
131 lines (124 loc) · 2.42 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package combinatorics
import (
"reflect"
"testing"
)
func TestCombination(t *testing.T) {
tests := []struct {
name string
list []string
n int
r int
expect [][]string
}{
{
name: "3C2",
list: []string{"a", "b", "c"},
n: 3,
r: 2,
expect: [][]string{
[]string{"a", "b"},
[]string{"a", "c"},
[]string{"b", "c"},
},
},
{
name: "3C3",
list: []string{"a", "b", "c"},
n: 3,
r: 3,
expect: [][]string{
[]string{"a", "b", "c"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
retval := Combination(tt.list, tt.n, tt.r)
if !reflect.DeepEqual(retval, tt.expect) {
t.Errorf(`got(%v) != expect(%v)`, retval, tt.expect)
}
})
}
}
func TestCombinationWithRepetition(t *testing.T) {
tests := []struct {
name string
list []string
n int
r int
expect [][]string
}{
{
name: "3C2",
list: []string{"a", "b", "c"},
n: 3,
r: 2,
expect: [][]string{
[]string{"a", "a"},
[]string{"a", "b"},
[]string{"a", "c"},
[]string{"b", "b"},
[]string{"b", "c"},
[]string{"c", "c"},
},
},
{
name: "3C3",
list: []string{"a", "b", "c"},
n: 3,
r: 3,
expect: [][]string{
[]string{"a", "a", "a"},
[]string{"a", "a", "b"},
[]string{"a", "a", "c"},
[]string{"a", "b", "b"},
[]string{"a", "b", "c"},
[]string{"a", "c", "c"},
[]string{"b", "b", "b"},
[]string{"b", "b", "c"},
[]string{"b", "c", "c"},
[]string{"c", "c", "c"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
retval := CombinationWithRepetition(tt.list, tt.n, tt.r)
if !reflect.DeepEqual(retval, tt.expect) {
t.Errorf(`got(%v) != expect(%v)`, retval, tt.expect)
}
})
}
}
func TestUnique(t *testing.T) {
tests := []struct {
name string
in []string
expect []string
}{
{
name: "a should be removed",
in: []string{"a", "a", "a"},
expect: []string{"a"},
},
{
name: "b should be removed",
in: []string{"a", "b", "c", "b"},
expect: []string{"a", "b", "c"},
},
{
name: "already unique",
in: []string{"a", "b", "c"},
expect: []string{"a", "b", "c"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
retval := Unique(tt.in)
if !reflect.DeepEqual(retval, tt.expect) {
t.Errorf(`got(%v) != expect(%v)`, retval, tt.expect)
}
})
}
}