1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
16 var float64s = [...]float64{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8}
17 var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
19 func TestSortIntArray(t *testing.T) {
21 a := IntArray(data[0:])
24 t.Errorf("sorted %v", ints)
25 t.Errorf(" got %v", data)
29 func TestSortFloat64Array(t *testing.T) {
31 a := Float64Array(data[0:])
34 t.Errorf("sorted %v", float64s)
35 t.Errorf(" got %v", data)
39 func TestSortStringArray(t *testing.T) {
41 a := StringArray(data[0:])
44 t.Errorf("sorted %v", strings)
45 t.Errorf(" got %v", data)
49 func TestSortInts(t *testing.T) {
52 if !IntsAreSorted(data[0:]) {
53 t.Errorf("sorted %v", ints)
54 t.Errorf(" got %v", data)
58 func TestSortFloat64s(t *testing.T) {
60 SortFloat64s(data[0:])
61 if !Float64sAreSorted(data[0:]) {
62 t.Errorf("sorted %v", float64s)
63 t.Errorf(" got %v", data)
67 func TestSortStrings(t *testing.T) {
70 if !StringsAreSorted(data[0:]) {
71 t.Errorf("sorted %v", strings)
72 t.Errorf(" got %v", data)
76 func TestSortLarge_Random(t *testing.T) {
77 data := make([]int, 1000000)
78 for i := 0; i < len(data); i++ {
79 data[i] = rand.Intn(100)
81 if IntsAreSorted(data) {
82 t.Fatalf("terrible rand.rand")
85 if !IntsAreSorted(data) {
86 t.Errorf("sort didn't sort - 1M ints")
90 func BenchmarkSortString1K(b *testing.B) {
92 for i := 0; i < b.N; i++ {
93 data := make([]string, 1<<10)
94 for i := 0; i < len(data); i++ {
95 data[i] = strconv.Itoa(i ^ 0x2cc)
103 func BenchmarkSortInt1K(b *testing.B) {
105 for i := 0; i < b.N; i++ {
106 data := make([]int, 1<<10)
107 for i := 0; i < len(data); i++ {
116 func BenchmarkSortInt64K(b *testing.B) {
118 for i := 0; i < b.N; i++ {
119 data := make([]int, 1<<16)
120 for i := 0; i < len(data); i++ {
148 type testingData struct {
152 maxswap int // number of swaps allowed
156 func (d *testingData) Len() int { return len(d.data) }
157 func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j] }
158 func (d *testingData) Swap(i, j int) {
159 if d.nswap >= d.maxswap {
160 d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data))
164 d.data[i], d.data[j] = d.data[j], d.data[i]
175 func TestBentleyMcIlroy(t *testing.T) {
176 sizes := []int{100, 1023, 1024, 1025}
177 dists := []string{"sawtooth", "rand", "stagger", "plateau", "shuffle"}
178 modes := []string{"copy", "reverse", "reverse1", "reverse2", "sort", "dither"}
179 var tmp1, tmp2 [1025]int
180 for ni := 0; ni < len(sizes); ni++ {
182 for m := 1; m < 2*n; m *= 2 {
183 for dist := 0; dist < _NDist; dist++ {
187 for i := 0; i < n; i++ {
192 data[i] = rand.Intn(m)
194 data[i] = (i*m + i) % n
198 if rand.Intn(m) != 0 {
209 for mode := 0; mode < _NMode; mode++ {
212 for i := 0; i < n; i++ {
216 for i := 0; i < n; i++ {
217 mdata[i] = data[n-i-1]
219 case _ReverseFirstHalf:
220 for i := 0; i < n/2; i++ {
221 mdata[i] = data[n/2-i-1]
223 for i := n / 2; i < n; i++ {
226 case _ReverseSecondHalf:
227 for i := 0; i < n/2; i++ {
230 for i := n / 2; i < n; i++ {
231 mdata[i] = data[n-(i-n/2)-1]
234 for i := 0; i < n; i++ {
237 // SortInts is known to be correct
238 // because mode Sort runs after mode _Copy.
241 for i := 0; i < n; i++ {
242 mdata[i] = data[i] + i%5
246 desc := fmt.Sprintf("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode])
247 d := &testingData{desc, t, mdata[0:n], n * lg(n) * 12 / 10, 0}
250 // If we were testing C qsort, we'd have to make a copy
251 // of the array and sort it ourselves and then compare
252 // x against it, to ensure that qsort was only permuting
253 // the data, not (for example) overwriting it with zeros.
255 // In go, we don't have to be so paranoid: since the only
256 // mutating method Sort can call is TestingData.swap,
257 // it suffices here just to check that the final array is sorted.
258 if !IntsAreSorted(mdata) {
259 t.Errorf("%s: ints not sorted", desc)
260 t.Errorf("\t%v", mdata)