// 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 ( "fmt"; "rand"; "sort"; "testing"; ) func BentleyMcIlroyTests(); 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", "%*&^*&^&", "***"} export func TestSortIntArray(t *testing.T) { data := ints; a := sort.IntArray{data}; sort.Sort(&a); if !sort.IsSorted(&a) { t.Errorf("sorted %v", ints); t.Errorf(" got %v", data); } } export func TestSortFloatArray(t *testing.T) { data := floats; a := sort.FloatArray{data}; sort.Sort(&a); if !sort.IsSorted(&a) { t.Errorf("sorted %v", floats); t.Errorf(" got %v", data); } } export func TestSortStringArray(t *testing.T) { data := strings; a := sort.StringArray{data}; sort.Sort(&a); if !sort.IsSorted(&a) { t.Errorf("sorted %v", strings); t.Errorf(" got %v", data); } } export func TestSortInts(t *testing.T) { data := ints; sort.SortInts(data); if !sort.IntsAreSorted(data) { t.Errorf("sorted %v", ints); t.Errorf(" got %v", data); } } export func TestSortFloats(t *testing.T) { data := floats; sort.SortFloats(data); if !sort.FloatsAreSorted(data) { t.Errorf("sorted %v", floats); t.Errorf(" got %v", data); } } export func TestSortStrings(t *testing.T) { data := strings; sort.SortStrings(data); if !sort.StringsAreSorted(data) { t.Errorf("sorted %v", strings); t.Errorf(" got %v", data); } } export func TestSortLargeRandom(t *testing.T) { data := new([]int, 1000000); for i := 0; i < len(data); i++ { data[i] = rand.rand() % 100; } if sort.IntsAreSorted(data) { t.Fatalf("terrible rand.rand"); } sort.SortInts(data); if !sort.IntsAreSorted(data) { t.Errorf("sort didn't sort - 1M ints"); } } const ( Sawtooth = iota; Rand; Stagger; Plateau; Shuffle; NDist; ) const ( Copy = iota; Reverse; ReverseFirstHalf; ReverseSecondHalf; Sorted; Dither; NMode; ); type TestingData struct { desc string; t *testing.T; data []int; maxswap int; // number of swaps allowed nswap int; } func (d *TestingData) Len() int { return len(d.data); } func (d *TestingData) Less(i, j int) bool { return d.data[i] < d.data[j]; } func (d *TestingData) Swap(i, j int) { if d.nswap >= d.maxswap { d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data)); d.t.FailNow(); } d.nswap++; d.data[i], d.data[j] = d.data[j], d.data[i]; } func Lg(n int) int { i := 0; for 1<