-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix #140 + Fix SortIndex() + Add SortIndexStable() * `any` is not supported in Go1.15 --------- Co-authored-by: Chewxy <chewxy@gmail.com>
- Loading branch information
Showing
2 changed files
with
96 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") | ||
} | ||
} | ||
} |