| 1 |
// Copyright 2015 Randall Farmer. 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 sorts_test |
| 6 |
|
| 7 |
import . "github.com/twotwotwo/sorts" |
| 8 |
import . "github.com/twotwotwo/sorts/sortutil" |
| 9 |
import "strconv" |
| 10 |
import "sort" |
| 11 |
|
| 12 |
// Helpers to sort in different ways: with the quicksort cutoff lowered or |
| 13 |
// raised (to exercise radix code more or less), and sorting ints as uints, |
| 14 |
// bytes or strings (to exercise those types' sort code). |
| 15 |
|
| 16 |
// varyQSortCutoff runs a function with qSortCutoff set to 1, the default |
| 17 |
// value, and 1e9. |
| 18 |
func varyQSortCutoff(f func()) { |
| 19 |
f() |
| 20 |
defer SetQSortCutoff(SetQSortCutoff(1)) |
| 21 |
f() |
| 22 |
SetQSortCutoff(1e9) |
| 23 |
f() |
| 24 |
} |
| 25 |
|
| 26 |
// forceRadix runs a function with qSortCutoff forced to 1 |
| 27 |
func forceRadix(f func()) { |
| 28 |
defer SetQSortCutoff(SetQSortCutoff(1)) |
| 29 |
f() |
| 30 |
} |
| 31 |
|
| 32 |
// convertInts turns a []int of only positive numbers into a [][]byte, a |
| 33 |
// []string, and a []uint ordered the same way, for exercising all of the |
| 34 |
// underlying type-specific sort code. |
| 35 |
func convertInts(a []int) ([][]byte, []string, []uint) { |
| 36 |
const l = 20 // length of converted number |
| 37 |
outSpace := make([]byte, l*len(a)) |
| 38 |
for i := range outSpace { |
| 39 |
outSpace[i] = '0' |
| 40 |
} |
| 41 |
|
| 42 |
outBytes := make([][]byte, len(a)) |
| 43 |
for i := range a { |
| 44 |
outBytes[i] = outSpace[l*i : l*i+l] |
| 45 |
} |
| 46 |
|
| 47 |
t := make([]byte, 20) |
| 48 |
for i, v := range a { |
| 49 |
s := strconv.AppendUint(t[:0], uint64(v), 10) |
| 50 |
copy(outBytes[i][l-len(s):], s) |
| 51 |
} |
| 52 |
|
| 53 |
strSpace := string(outSpace) |
| 54 |
outStrings := make([]string, len(a)) |
| 55 |
for i := range a { |
| 56 |
outStrings[i] = strSpace[l*i : l*i+l] |
| 57 |
} |
| 58 |
|
| 59 |
outUints := make([]uint, len(a)) |
| 60 |
for i := range outUints { |
| 61 |
outUints[i] = uint(a[i]) |
| 62 |
} |
| 63 |
|
| 64 |
return outBytes, outStrings, outUints |
| 65 |
} |
| 66 |
|
| 67 |
// multiSort sorts incoming integers using integer, []byte, and string sorts. |
| 68 |
func multiSort(a []int) { |
| 69 |
asBytes, asStrings, asUints := convertInts(a) |
| 70 |
IntSlice(a).Sort() |
| 71 |
BytesSlice(asBytes).Sort() |
| 72 |
StringSlice(asStrings).Sort() |
| 73 |
UintSlice(asUints).Sort() |
| 74 |
} |
| 75 |
|
| 76 |
// manySort sorts integers with all QSort cutoffs and all data types, saving |
| 77 |
// the sorted ints back to a and relying on the sorted checks for the other |
| 78 |
// sorts. (It also throws away just tons of RAM but not sure we care.) |
| 79 |
func manySort(a []int) { |
| 80 |
aBytes, aStrings, aUints := convertInts(a) |
| 81 |
|
| 82 |
myInts, myUints, myBytes, myStrings := IntSlice{}, UintSlice{}, BytesSlice{}, StringSlice{} |
| 83 |
varyQSortCutoff(func() { |
| 84 |
myInts = append(myInts[:0], a...) |
| 85 |
|
| 86 |
// make the parallel qsort extra parallel, as it were |
| 87 |
oldMinOffload := SetMinOffload(2) // 1 never hits Quicksort insertionSort |
| 88 |
defer SetMinOffload(oldMinOffload) // in case of panic |
| 89 |
Quicksort(myInts) |
| 90 |
SetMinOffload(oldMinOffload) // for other sorts |
| 91 |
|
| 92 |
myInts = append(myInts[:0], a...) |
| 93 |
myInts.Sort() |
| 94 |
myBytes = append(myBytes[:0], aBytes...) |
| 95 |
myBytes.Sort() |
| 96 |
myStrings = append(myStrings[:0], aStrings...) |
| 97 |
myStrings.Sort() |
| 98 |
myUints = append(myUints[:0], aUints...) |
| 99 |
myUints.Sort() |
| 100 |
}) |
| 101 |
// caller wants to see the sorted ints |
| 102 |
copy(a, myInts) |
| 103 |
} |
| 104 |
|
| 105 |
// manySortWrapper lets us use testBM with manySort to exercise all the sorts. |
| 106 |
func manySortWrapper(d sort.Interface) { |
| 107 |
ints := d.(*testingData).data |
| 108 |
manySort(ints) |
| 109 |
} |