forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_utils_test.go
62 lines (56 loc) · 1.45 KB
/
api_utils_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
package tensor
import (
"testing"
)
type testInt []int
func (m testInt) Less(i, j int) bool { return m[i] < m[j] }
func (m testInt) Len() int { return len(m) }
func (m testInt) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func TestSortIndexInts(t *testing.T) {
in := []int{9, 8, 7, 6, 5, 4, 10, -1, -2, -4, 11, 13, 15, 100, 99}
inCopy := make([]int, len(in))
copy(inCopy, in)
out := SortIndex(in)
for i := 1; i < len(out); i++ {
if inCopy[out[i]] < inCopy[out[i-1]] {
t.Fatalf("Unexpected output")
}
}
for i := range in {
if in[i] != inCopy[i] {
t.Fatalf("The input slice should not be changed")
}
}
}
func TestSortIndexFloats(t *testing.T) {
in := []float64{.9, .8, .7, .6, .5, .4, .10, -.1, -.2, -.4, .11, .13, .15, .100, .99}
inCopy := make([]float64, len(in))
copy(inCopy, in)
out := SortIndex(in)
for i := 1; i < len(out); i++ {
if inCopy[out[i]] < inCopy[out[i-1]] {
t.Fatalf("Unexpected output")
}
}
for i := range in {
if in[i] != inCopy[i] {
t.Fatalf("The input slice should not be changed")
}
}
}
func TestSortIndexSortInterface(t *testing.T) {
in := testInt{9, 8, 7, 6, 5, 4, 10, -1, -2, -4, 11, 13, 15, 100, 99}
inCopy := make(testInt, len(in))
copy(inCopy, in)
out := SortIndex(in)
for i := 1; i < len(out); i++ {
if inCopy[out[i]] < inCopy[out[i-1]] {
t.Fatalf("Unexpected output")
}
}
for i := range in {
if in[i] != inCopy[i] {
t.Fatalf("The input slice should not be changed")
}
}
}