2008-11-24 16:17:47 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sort
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:40:16 -07:00
|
|
|
"fmt"
|
|
|
|
"rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2008-11-24 16:17:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
|
|
|
|
var floats = [...]float{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8}
|
|
|
|
var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
|
2008-11-24 16:17:47 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortIntArray(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := ints
|
|
|
|
a := IntArray(&data)
|
|
|
|
Sort(a)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IsSorted(a) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", ints)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortFloatArray(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := floats
|
|
|
|
a := FloatArray(&data)
|
|
|
|
Sort(a)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IsSorted(a) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", floats)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortStringArray(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := strings
|
|
|
|
a := StringArray(&data)
|
|
|
|
Sort(a)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IsSorted(a) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", strings)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortInts(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := ints
|
|
|
|
SortInts(&data)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IntsAreSorted(&data) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", ints)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortFloats(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := floats
|
|
|
|
SortFloats(&data)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !FloatsAreSorted(&data) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", floats)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortStrings(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := strings
|
|
|
|
SortStrings(&data)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !StringsAreSorted(&data) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("sorted %v", strings)
|
|
|
|
t.Errorf(" got %v", data)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestSortLarge_Random(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := make([]int, 1000000)
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < len(data); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
data[i] = rand.Intn(100)
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-08-12 14:19:17 -06:00
|
|
|
if IntsAreSorted(data) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("terrible rand.rand")
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
SortInts(data)
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IntsAreSorted(data) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("sort didn't sort - 1M ints")
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 01:21:50 -07:00
|
|
|
func BenchmarkSortString1K(b *testing.B) {
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := make([]string, 1<<10)
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
data[i] = strconv.Itoa(i ^ 0x2cc)
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StartTimer()
|
|
|
|
SortStrings(data)
|
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortInt1K(b *testing.B) {
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := make([]int, 1<<10)
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
data[i] = i ^ 0x2cc
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StartTimer()
|
|
|
|
SortInts(data)
|
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortInt64K(b *testing.B) {
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < b.N; i++ {
|
2009-12-15 16:40:16 -07:00
|
|
|
data := make([]int, 1<<16)
|
2009-11-24 01:21:50 -07:00
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
data[i] = i ^ 0xcccc
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
b.StartTimer()
|
|
|
|
SortInts(data)
|
|
|
|
b.StopTimer()
|
2009-11-24 01:21:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 16:17:47 -07:00
|
|
|
const (
|
2009-12-15 16:40:16 -07:00
|
|
|
_Sawtooth = iota
|
|
|
|
_Rand
|
|
|
|
_Stagger
|
|
|
|
_Plateau
|
|
|
|
_Shuffle
|
|
|
|
_NDist
|
2008-11-24 16:17:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2009-12-15 16:40:16 -07:00
|
|
|
_Copy = iota
|
|
|
|
_Reverse
|
|
|
|
_ReverseFirstHalf
|
|
|
|
_ReverseSecondHalf
|
|
|
|
_Sorted
|
|
|
|
_Dither
|
|
|
|
_NMode
|
2009-01-16 11:34:21 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type testingData struct {
|
2009-12-15 16:40:16 -07:00
|
|
|
desc string
|
|
|
|
t *testing.T
|
|
|
|
data []int
|
|
|
|
maxswap int // number of swaps allowed
|
|
|
|
nswap int
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
func (d *testingData) Len() int { return len(d.data) }
|
|
|
|
func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j] }
|
2009-01-16 11:34:21 -07:00
|
|
|
func (d *testingData) Swap(i, j int) {
|
2008-11-24 16:17:47 -07:00
|
|
|
if d.nswap >= d.maxswap {
|
2009-12-15 16:40:16 -07:00
|
|
|
d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data))
|
|
|
|
d.t.FailNow()
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
d.nswap++
|
|
|
|
d.data[i], d.data[j] = d.data[j], d.data[i]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:21 -07:00
|
|
|
func lg(n int) int {
|
2009-12-15 16:40:16 -07:00
|
|
|
i := 0
|
2008-11-24 16:17:47 -07:00
|
|
|
for 1<<uint(i) < n {
|
2009-11-09 13:07:39 -07:00
|
|
|
i++
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return i
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestBentleyMcIlroy(t *testing.T) {
|
2009-12-15 16:40:16 -07:00
|
|
|
sizes := []int{100, 1023, 1024, 1025}
|
|
|
|
dists := []string{"sawtooth", "rand", "stagger", "plateau", "shuffle"}
|
|
|
|
modes := []string{"copy", "reverse", "reverse1", "reverse2", "sort", "dither"}
|
|
|
|
var tmp1, tmp2 [1025]int
|
2008-11-24 16:17:47 -07:00
|
|
|
for ni := 0; ni < len(sizes); ni++ {
|
2009-12-15 16:40:16 -07:00
|
|
|
n := sizes[ni]
|
2008-11-24 16:17:47 -07:00
|
|
|
for m := 1; m < 2*n; m *= 2 {
|
2009-01-16 11:34:21 -07:00
|
|
|
for dist := 0; dist < _NDist; dist++ {
|
2009-12-15 16:40:16 -07:00
|
|
|
j := 0
|
|
|
|
k := 1
|
|
|
|
data := tmp1[0:n]
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
switch dist {
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Sawtooth:
|
2009-11-09 22:23:52 -07:00
|
|
|
data[i] = i % m
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Rand:
|
2009-11-09 13:07:39 -07:00
|
|
|
data[i] = rand.Intn(m)
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Stagger:
|
2009-11-09 22:23:52 -07:00
|
|
|
data[i] = (i*m + i) % n
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Plateau:
|
2009-11-09 13:07:39 -07:00
|
|
|
data[i] = min(i, m)
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Shuffle:
|
2009-01-16 13:47:24 -07:00
|
|
|
if rand.Intn(m) != 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
j += 2
|
|
|
|
data[i] = j
|
2008-11-24 16:17:47 -07:00
|
|
|
} else {
|
2009-12-15 16:40:16 -07:00
|
|
|
k += 2
|
|
|
|
data[i] = k
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
mdata := tmp2[0:n]
|
2009-01-16 11:34:21 -07:00
|
|
|
for mode := 0; mode < _NMode; mode++ {
|
2008-11-24 16:17:47 -07:00
|
|
|
switch mode {
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Copy:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[i]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Reverse:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[n-i-1]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-01-16 11:34:21 -07:00
|
|
|
case _ReverseFirstHalf:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n/2; i++ {
|
2009-11-09 22:23:52 -07:00
|
|
|
mdata[i] = data[n/2-i-1]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
for i := n / 2; i < n; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[i]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-01-16 11:34:21 -07:00
|
|
|
case _ReverseSecondHalf:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n/2; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[i]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
for i := n / 2; i < n; i++ {
|
|
|
|
mdata[i] = data[n-(i-n/2)-1]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Sorted:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[i]
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
2009-08-12 14:19:17 -06:00
|
|
|
// SortInts is known to be correct
|
2009-01-16 11:34:21 -07:00
|
|
|
// because mode Sort runs after mode _Copy.
|
2009-12-15 16:40:16 -07:00
|
|
|
SortInts(mdata)
|
2009-01-16 11:34:21 -07:00
|
|
|
case _Dither:
|
2008-11-24 16:17:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
mdata[i] = data[i] + i%5
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
desc := fmt.Sprintf("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode])
|
|
|
|
d := &testingData{desc, t, mdata[0:n], n * lg(n) * 12 / 10, 0}
|
|
|
|
Sort(d)
|
2008-11-24 16:17:47 -07:00
|
|
|
|
|
|
|
// If we were testing C qsort, we'd have to make a copy
|
|
|
|
// of the array and sort it ourselves and then compare
|
|
|
|
// x against it, to ensure that qsort was only permuting
|
|
|
|
// the data, not (for example) overwriting it with zeros.
|
|
|
|
//
|
|
|
|
// In go, we don't have to be so paranoid: since the only
|
2009-08-12 14:19:17 -06:00
|
|
|
// mutating method Sort can call is TestingData.swap,
|
2008-11-24 16:17:47 -07:00
|
|
|
// it suffices here just to check that the final array is sorted.
|
2009-08-12 14:19:17 -06:00
|
|
|
if !IntsAreSorted(mdata) {
|
2009-12-15 16:40:16 -07:00
|
|
|
t.Errorf("%s: ints not sorted", desc)
|
|
|
|
t.Errorf("\t%v", mdata)
|
|
|
|
t.FailNow()
|
2008-11-24 16:17:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|