OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [sort/] [sort_test.go] - Blame information for rev 760

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package sort_test
6
 
7
import (
8
        "fmt"
9
        "math"
10
        "math/rand"
11
        . "sort"
12
        "strconv"
13
        "testing"
14
)
15
 
16
var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
17
var float64s = [...]float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.NaN(), math.NaN(), math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8}
18
var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
19
 
20
func TestSortIntSlice(t *testing.T) {
21
        data := ints
22
        a := IntSlice(data[0:])
23
        Sort(a)
24
        if !IsSorted(a) {
25
                t.Errorf("sorted %v", ints)
26
                t.Errorf("   got %v", data)
27
        }
28
}
29
 
30
func TestSortFloat64Slice(t *testing.T) {
31
        data := float64s
32
        a := Float64Slice(data[0:])
33
        Sort(a)
34
        if !IsSorted(a) {
35
                t.Errorf("sorted %v", float64s)
36
                t.Errorf("   got %v", data)
37
        }
38
}
39
 
40
func TestSortStringSlice(t *testing.T) {
41
        data := strings
42
        a := StringSlice(data[0:])
43
        Sort(a)
44
        if !IsSorted(a) {
45
                t.Errorf("sorted %v", strings)
46
                t.Errorf("   got %v", data)
47
        }
48
}
49
 
50
func TestInts(t *testing.T) {
51
        data := ints
52
        Ints(data[0:])
53
        if !IntsAreSorted(data[0:]) {
54
                t.Errorf("sorted %v", ints)
55
                t.Errorf("   got %v", data)
56
        }
57
}
58
 
59
func TestFloat64s(t *testing.T) {
60
        data := float64s
61
        Float64s(data[0:])
62
        if !Float64sAreSorted(data[0:]) {
63
                t.Errorf("sorted %v", float64s)
64
                t.Errorf("   got %v", data)
65
        }
66
}
67
 
68
func TestStrings(t *testing.T) {
69
        data := strings
70
        Strings(data[0:])
71
        if !StringsAreSorted(data[0:]) {
72
                t.Errorf("sorted %v", strings)
73
                t.Errorf("   got %v", data)
74
        }
75
}
76
 
77
func TestSortLarge_Random(t *testing.T) {
78
        n := 1000000
79
        if testing.Short() {
80
                n /= 100
81
        }
82
        data := make([]int, n)
83
        for i := 0; i < len(data); i++ {
84
                data[i] = rand.Intn(100)
85
        }
86
        if IntsAreSorted(data) {
87
                t.Fatalf("terrible rand.rand")
88
        }
89
        Ints(data)
90
        if !IntsAreSorted(data) {
91
                t.Errorf("sort didn't sort - 1M ints")
92
        }
93
}
94
 
95
func BenchmarkSortString1K(b *testing.B) {
96
        b.StopTimer()
97
        for i := 0; i < b.N; i++ {
98
                data := make([]string, 1<<10)
99
                for i := 0; i < len(data); i++ {
100
                        data[i] = strconv.Itoa(i ^ 0x2cc)
101
                }
102
                b.StartTimer()
103
                Strings(data)
104
                b.StopTimer()
105
        }
106
}
107
 
108
func BenchmarkSortInt1K(b *testing.B) {
109
        b.StopTimer()
110
        for i := 0; i < b.N; i++ {
111
                data := make([]int, 1<<10)
112
                for i := 0; i < len(data); i++ {
113
                        data[i] = i ^ 0x2cc
114
                }
115
                b.StartTimer()
116
                Ints(data)
117
                b.StopTimer()
118
        }
119
}
120
 
121
func BenchmarkSortInt64K(b *testing.B) {
122
        b.StopTimer()
123
        for i := 0; i < b.N; i++ {
124
                data := make([]int, 1<<16)
125
                for i := 0; i < len(data); i++ {
126
                        data[i] = i ^ 0xcccc
127
                }
128
                b.StartTimer()
129
                Ints(data)
130
                b.StopTimer()
131
        }
132
}
133
 
134
const (
135
        _Sawtooth = iota
136
        _Rand
137
        _Stagger
138
        _Plateau
139
        _Shuffle
140
        _NDist
141
)
142
 
143
const (
144
        _Copy = iota
145
        _Reverse
146
        _ReverseFirstHalf
147
        _ReverseSecondHalf
148
        _Sorted
149
        _Dither
150
        _NMode
151
)
152
 
153
type testingData struct {
154
        desc    string
155
        t       *testing.T
156
        data    []int
157
        maxswap int // number of swaps allowed
158
        nswap   int
159
}
160
 
161
func (d *testingData) Len() int           { return len(d.data) }
162
func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j] }
163
func (d *testingData) Swap(i, j int) {
164
        if d.nswap >= d.maxswap {
165
                d.t.Errorf("%s: used %d swaps sorting slice of %d", d.desc, d.nswap, len(d.data))
166
                d.t.FailNow()
167
        }
168
        d.nswap++
169
        d.data[i], d.data[j] = d.data[j], d.data[i]
170
}
171
 
172
func min(a, b int) int {
173
        if a < b {
174
                return a
175
        }
176
        return b
177
}
178
 
179
func lg(n int) int {
180
        i := 0
181
        for 1<
182
                i++
183
        }
184
        return i
185
}
186
 
187
func testBentleyMcIlroy(t *testing.T, sort func(Interface)) {
188
        sizes := []int{100, 1023, 1024, 1025}
189
        if testing.Short() {
190
                sizes = []int{100, 127, 128, 129}
191
        }
192
        dists := []string{"sawtooth", "rand", "stagger", "plateau", "shuffle"}
193
        modes := []string{"copy", "reverse", "reverse1", "reverse2", "sort", "dither"}
194
        var tmp1, tmp2 [1025]int
195
        for ni := 0; ni < len(sizes); ni++ {
196
                n := sizes[ni]
197
                for m := 1; m < 2*n; m *= 2 {
198
                        for dist := 0; dist < _NDist; dist++ {
199
                                j := 0
200
                                k := 1
201
                                data := tmp1[0:n]
202
                                for i := 0; i < n; i++ {
203
                                        switch dist {
204
                                        case _Sawtooth:
205
                                                data[i] = i % m
206
                                        case _Rand:
207
                                                data[i] = rand.Intn(m)
208
                                        case _Stagger:
209
                                                data[i] = (i*m + i) % n
210
                                        case _Plateau:
211
                                                data[i] = min(i, m)
212
                                        case _Shuffle:
213
                                                if rand.Intn(m) != 0 {
214
                                                        j += 2
215
                                                        data[i] = j
216
                                                } else {
217
                                                        k += 2
218
                                                        data[i] = k
219
                                                }
220
                                        }
221
                                }
222
 
223
                                mdata := tmp2[0:n]
224
                                for mode := 0; mode < _NMode; mode++ {
225
                                        switch mode {
226
                                        case _Copy:
227
                                                for i := 0; i < n; i++ {
228
                                                        mdata[i] = data[i]
229
                                                }
230
                                        case _Reverse:
231
                                                for i := 0; i < n; i++ {
232
                                                        mdata[i] = data[n-i-1]
233
                                                }
234
                                        case _ReverseFirstHalf:
235
                                                for i := 0; i < n/2; i++ {
236
                                                        mdata[i] = data[n/2-i-1]
237
                                                }
238
                                                for i := n / 2; i < n; i++ {
239
                                                        mdata[i] = data[i]
240
                                                }
241
                                        case _ReverseSecondHalf:
242
                                                for i := 0; i < n/2; i++ {
243
                                                        mdata[i] = data[i]
244
                                                }
245
                                                for i := n / 2; i < n; i++ {
246
                                                        mdata[i] = data[n-(i-n/2)-1]
247
                                                }
248
                                        case _Sorted:
249
                                                for i := 0; i < n; i++ {
250
                                                        mdata[i] = data[i]
251
                                                }
252
                                                // Ints is known to be correct
253
                                                // because mode Sort runs after mode _Copy.
254
                                                Ints(mdata)
255
                                        case _Dither:
256
                                                for i := 0; i < n; i++ {
257
                                                        mdata[i] = data[i] + i%5
258
                                                }
259
                                        }
260
 
261
                                        desc := fmt.Sprintf("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode])
262
                                        d := &testingData{desc, t, mdata[0:n], n * lg(n) * 12 / 10, 0}
263
                                        sort(d)
264
 
265
                                        // If we were testing C qsort, we'd have to make a copy
266
                                        // of the slice and sort it ourselves and then compare
267
                                        // x against it, to ensure that qsort was only permuting
268
                                        // the data, not (for example) overwriting it with zeros.
269
                                        //
270
                                        // In go, we don't have to be so paranoid: since the only
271
                                        // mutating method Sort can call is TestingData.swap,
272
                                        // it suffices here just to check that the final slice is sorted.
273
                                        if !IntsAreSorted(mdata) {
274
                                                t.Errorf("%s: ints not sorted", desc)
275
                                                t.Errorf("\t%v", mdata)
276
                                                t.FailNow()
277
                                        }
278
                                }
279
                        }
280
                }
281
        }
282
}
283
 
284
func TestSortBM(t *testing.T) {
285
        testBentleyMcIlroy(t, Sort)
286
}
287
 
288
func TestHeapsortBM(t *testing.T) {
289
        testBentleyMcIlroy(t, Heapsort)
290
}
291
 
292
// This is based on the "antiquicksort" implementation by M. Douglas McIlroy.
293
// See http://www.cs.dartmouth.edu/~doug/mdmspe.pdf for more info.
294
type adversaryTestingData struct {
295
        data      []int
296
        keys      map[int]int
297
        candidate int
298
}
299
 
300
func (d *adversaryTestingData) Len() int { return len(d.data) }
301
 
302
func (d *adversaryTestingData) Less(i, j int) bool {
303
        if _, present := d.keys[i]; !present {
304
                if _, present := d.keys[j]; !present {
305
                        if i == d.candidate {
306
                                d.keys[i] = len(d.keys)
307
                        } else {
308
                                d.keys[j] = len(d.keys)
309
                        }
310
                }
311
        }
312
 
313
        if _, present := d.keys[i]; !present {
314
                d.candidate = i
315
                return false
316
        }
317
        if _, present := d.keys[j]; !present {
318
                d.candidate = j
319
                return true
320
        }
321
 
322
        return d.keys[i] >= d.keys[j]
323
}
324
 
325
func (d *adversaryTestingData) Swap(i, j int) {
326
        d.data[i], d.data[j] = d.data[j], d.data[i]
327
}
328
 
329
func TestAdversary(t *testing.T) {
330
        const size = 100
331
        data := make([]int, size)
332
        for i := 0; i < size; i++ {
333
                data[i] = i
334
        }
335
 
336
        d := &adversaryTestingData{data, make(map[int]int), 0}
337
        Sort(d) // This should degenerate to heapsort.
338
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.