-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpermutor_test.go
70 lines (63 loc) · 1.51 KB
/
permutor_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
59
60
61
62
63
64
65
66
67
68
69
70
package permutation
import "testing"
func TestNewPermErrors(t *testing.T) {
var tests = []struct {
Name string
K interface{}
Expect error
}{
{
Name: "String",
K: "not a slice",
Expect: NotASliceError,
},
{
Name: "Int",
K: 1735,
Expect: NotASliceError,
},
{
Name: "Struct",
K: struct{ Data int }{Data: 21},
Expect: NotASliceError,
},
{
Name: "Empty",
K: []int{},
Expect: EmptyCollectionError,
},
}
for _, test := range tests {
p, err := NewPerm(test.K, nil)
if p != nil {
t.Errorf("%s: got non-nil permutator pointer with k=%v\n", test.Name, test.K)
}
if err != test.Expect {
t.Errorf("%s: got wrong error type '%s' from k=%v\n", test.Name, err, test.K)
}
}
}
func TestValueIsCopy(t *testing.T) {
var orig = []int{1, 2, 3, 4}
p, err := NewPerm(orig, nil)
if err != nil {
t.Errorf("Error when creating permutator: '%s'\n", err)
}
// the elements of orig and p.value should be the same
for i, elem := range orig {
if p.value.Index(i).Int() != int64(elem) {
t.Errorf("Element mismatch: orig[%d] = %d; p.value[%d] = %d\n",
i, orig[i], i, p.value.Index(i).Int())
}
}
for i := 0; i < len(orig); i++ {
orig[i] = orig[i] * 2
}
// not the elements of orig should all be twice the corresponding elements of p.value
for i, elem := range orig {
if p.value.Index(i).Int() != int64(elem/2) {
t.Errorf("Element mismatch: orig[%d] = %d; p.value[%d] = %d\n",
i, orig[i], i, p.value.Index(i).Int())
}
}
}