93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package sort
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// Test the mergesort and deduplication with a predefined set of slices.
|
|
func TestSortAndDedup(t *testing.T) {
|
|
to_sort := [][]int{
|
|
{},
|
|
{7},
|
|
{1, 4, 5, 6, 3, 2},
|
|
{1, 2, 3, 4, 5, 6, 7},
|
|
{1, 1, 1, 3, 3, 2, 1},
|
|
{84, 32, 32, 7, 1, 2, 1},
|
|
{1, 3, 5, 5, 7, 8, 10, 17, 19, 24, 27, 34, 76, 1, 1, 2, 3, 5, 8, 13, 21},
|
|
{1, 3, 5, 5, 7, 8, 10, 17, 19, 24, 27, 34, 76, 1, 1, 2, 3, 5, 8, 13, 21, 1, 3, 5, 5, 7, 8, 10, 17, 19, 24, 27, 34, 76, 1, 1, 2, 3, 5, 8, 13, 21, 1, 3, 5, 5, 7, 8, 10, 17, 19, 24, 27, 34, 76, 1, 1, 2},
|
|
}
|
|
|
|
result := [][]int{
|
|
{},
|
|
{7},
|
|
{1, 2, 3, 4, 5, 6},
|
|
{1, 2, 3, 4, 5, 6, 7},
|
|
{1, 2, 3},
|
|
{1, 2, 7, 32, 84},
|
|
{1, 2, 3, 5, 7, 8, 10, 13, 17, 19, 21, 24, 27, 34, 76},
|
|
{1, 2, 3, 5, 7, 8, 10, 13, 17, 19, 21, 24, 27, 34, 76},
|
|
}
|
|
|
|
for i := range to_sort {
|
|
sorted, _ := SortedAndDedup(make(chan bool, 1), to_sort[i])
|
|
if slice_equal(sorted, result[i]) != true {
|
|
t.Errorf("Failure in sorting + dedup, expected %s got %s", result[i], sorted)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Test the mergesort with a predefined set of slices.
|
|
func TestSort(t *testing.T) {
|
|
// ok
|
|
to_sort := [][]int{
|
|
{},
|
|
{7},
|
|
{1, 4, 5, 6, 3, 2},
|
|
{1, 2, 3, 4, 5, 6, 7},
|
|
{1, 1, 1, 3, 3, 2, 1},
|
|
{84, 32, 32, 7, 1, 2, 1},
|
|
{1, 3, 5, 5, 7, 8, 10, 17, 19, 24, 27, 34, 76, 1, 1, 2, 3, 5, 8, 13, 21},
|
|
}
|
|
|
|
result := [][]int{
|
|
{},
|
|
{7},
|
|
{1, 2, 3, 4, 5, 6},
|
|
{1, 2, 3, 4, 5, 6, 7},
|
|
{1, 1, 1, 1, 2, 3, 3},
|
|
{1, 1, 2, 7, 32, 32, 84},
|
|
{1, 1, 1, 2, 3, 3, 5, 5, 5, 7, 8, 8, 10, 13, 17, 19, 21, 24, 27, 34, 76},
|
|
}
|
|
|
|
for i := range to_sort {
|
|
sorted, _ := Mergesort(make(chan bool, 1), to_sort[i])
|
|
if slice_equal(sorted, result[i]) != true {
|
|
t.Errorf("Failure in sorting, expected %s got %s", result[i], sorted)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper function to compare int slices for equality.
|
|
func slice_equal(s1 []int, s2 []int) bool {
|
|
if s1 == nil && s2 == nil {
|
|
return true
|
|
}
|
|
|
|
if s1 == nil || s2 == nil {
|
|
return false
|
|
}
|
|
|
|
if len(s1) != len(s2) {
|
|
return false
|
|
}
|
|
|
|
for i := range s1 {
|
|
if s1[i] != s2[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|