| 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. |
| 4 | |
| 5 | package sort_testscip-go gomod go4.org . `go4.org/sort_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 9 | "mathscip-go gomod github.com/golang/go/src go1.13 math/" |
| 10 | "math/randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/" |
| 11 | "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/" |
| 12 | "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.13 testing/" |
| 14 | |
| 15 | "go4.org/reflectutilscip-go gomod go4.org . `go4.org/reflectutil`/" |
| 16 | . "go4.org/sortscip-go gomod go4.org . `go4.org/sort`/" |
| 17 | ) |
| 18 | |
| 19 | var intsscip-go gomod go4.org . `go4.org/sort_test`/ints. = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} |
| 20 | var float64sscip-go gomod go4.org . `go4.org/sort_test`/float64s. = [...]float64{74.3, 59.0, mathscip-go gomod github.com/golang/go/src go1.13 math/.Infscip-go gomod github.com/golang/go/src go1.13 math/Inf().(1), 238.2, -784.0, 2.3, mathscip-go gomod github.com/golang/go/src go1.13 math/.NaNscip-go gomod github.com/golang/go/src go1.13 math/NaN().(), mathscip-go gomod github.com/golang/go/src go1.13 math/.NaNscip-go gomod github.com/golang/go/src go1.13 math/NaN().(), mathscip-go gomod github.com/golang/go/src go1.13 math/.Infscip-go gomod github.com/golang/go/src go1.13 math/Inf().(-1), 9845.768, -959.7485, 905, 7.8, 7.8} |
| 21 | var stringsscip-go gomod go4.org . `go4.org/sort_test`/strings. = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"} |
| 22 | |
| 23 | func TestSlicescip-go gomod go4.org . `go4.org/sort_test`/TestSlice().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 24 | s := []int{5, 4, 3, 2, 1} |
| 25 | want := []int{1, 2, 3, 4, 5} |
| 26 | Slicescip-go gomod go4.org . `go4.org/sort`/Slice().(s, func(i, j int) bool { return s[i] < s[j] }) |
| 27 | if !reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(s, want) { |
| 28 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted = %v; want %v", s, want) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestSortIntSlicescip-go gomod go4.org . `go4.org/sort_test`/TestSortIntSlice().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 33 | data := intsscip-go gomod go4.org . `go4.org/sort_test`/ints. |
| 34 | a := IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data[0:]) |
| 35 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(a) |
| 36 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(a) { |
| 37 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", intsscip-go gomod go4.org . `go4.org/sort_test`/ints.) |
| 38 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestSortFloat64Slicescip-go gomod go4.org . `go4.org/sort_test`/TestSortFloat64Slice().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 43 | data := float64sscip-go gomod go4.org . `go4.org/sort_test`/float64s. |
| 44 | a := Float64Slicescip-go gomod go4.org . `go4.org/sort`/Float64Slice#(data[0:]) |
| 45 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(a) |
| 46 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(a) { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", float64sscip-go gomod go4.org . `go4.org/sort_test`/float64s.) |
| 48 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestSortStringSlicescip-go gomod go4.org . `go4.org/sort_test`/TestSortStringSlice().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 53 | data := stringsscip-go gomod go4.org . `go4.org/sort_test`/strings. |
| 54 | a := StringSlicescip-go gomod go4.org . `go4.org/sort`/StringSlice#(data[0:]) |
| 55 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(a) |
| 56 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(a) { |
| 57 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", stringsscip-go gomod go4.org . `go4.org/sort_test`/strings.) |
| 58 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestIntsscip-go gomod go4.org . `go4.org/sort_test`/TestInts().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 63 | data := intsscip-go gomod go4.org . `go4.org/sort_test`/ints. |
| 64 | Intsscip-go gomod go4.org . `go4.org/sort`/Ints().(data[0:]) |
| 65 | if !IntsAreSortedscip-go gomod go4.org . `go4.org/sort`/IntsAreSorted().(data[0:]) { |
| 66 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", intsscip-go gomod go4.org . `go4.org/sort_test`/ints.) |
| 67 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestFloat64sscip-go gomod go4.org . `go4.org/sort_test`/TestFloat64s().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 72 | data := float64sscip-go gomod go4.org . `go4.org/sort_test`/float64s. |
| 73 | Float64sscip-go gomod go4.org . `go4.org/sort`/Float64s().(data[0:]) |
| 74 | if !Float64sAreSortedscip-go gomod go4.org . `go4.org/sort`/Float64sAreSorted().(data[0:]) { |
| 75 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", float64sscip-go gomod go4.org . `go4.org/sort_test`/float64s.) |
| 76 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestStringsscip-go gomod go4.org . `go4.org/sort_test`/TestStrings().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 81 | data := stringsscip-go gomod go4.org . `go4.org/sort_test`/strings. |
| 82 | Stringsscip-go gomod go4.org . `go4.org/sort`/Strings().(data[0:]) |
| 83 | if !StringsAreSortedscip-go gomod go4.org . `go4.org/sort`/StringsAreSorted().(data[0:]) { |
| 84 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", stringsscip-go gomod go4.org . `go4.org/sort_test`/strings.) |
| 85 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestStringsWithSwapperscip-go gomod go4.org . `go4.org/sort_test`/TestStringsWithSwapper().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 90 | data := stringsscip-go gomod go4.org . `go4.org/sort_test`/strings. |
| 91 | Withscip-go gomod go4.org . `go4.org/sort`/With().(len(data), reflectutilscip-go gomod go4.org . `go4.org/reflectutil`/.Swapperscip-go gomod go4.org . `go4.org/reflectutil`/Swapper().(data[:]), func(i, j int) bool { |
| 92 | return data[i] < data[j] |
| 93 | }) |
| 94 | if !StringsAreSortedscip-go gomod go4.org . `go4.org/sort`/StringsAreSorted().(data[:]) { |
| 95 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sorted %v", stringsscip-go gomod go4.org . `go4.org/sort_test`/strings.) |
| 96 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(" got %v", data) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestSortLarge_Randomscip-go gomod go4.org . `go4.org/sort_test`/TestSortLarge_Random().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 101 | n := 1000000 |
| 102 | if testingscip-go gomod github.com/golang/go/src go1.13 testing/.Shortscip-go gomod github.com/golang/go/src go1.13 testing/Short().() { |
| 103 | n /= 100 |
| 104 | } |
| 105 | data := make([]int, n) |
| 106 | for i := 0; i < len(data); i++ { |
| 107 | data[i] = randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Intn().(100) |
| 108 | } |
| 109 | if IntsAreSortedscip-go gomod go4.org . `go4.org/sort`/IntsAreSorted().(data) { |
| 110 | t.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("terrible rand.rand") |
| 111 | } |
| 112 | Intsscip-go gomod go4.org . `go4.org/sort`/Ints().(data) |
| 113 | if !IntsAreSortedscip-go gomod go4.org . `go4.org/sort`/IntsAreSorted().(data) { |
| 114 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("sort didn't sort - 1M ints") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestReverseSortIntSlicescip-go gomod go4.org . `go4.org/sort_test`/TestReverseSortIntSlice().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 119 | data := intsscip-go gomod go4.org . `go4.org/sort_test`/ints. |
| 120 | data1 := intsscip-go gomod go4.org . `go4.org/sort_test`/ints. |
| 121 | a := IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data[0:]) |
| 122 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(a) |
| 123 | r := IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data1[0:]) |
| 124 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(Reversescip-go gomod go4.org . `go4.org/sort`/Reverse().(r)) |
| 125 | for i := 0; i < len(data); i++ { |
| 126 | if a[i] != r[len(data)-1-i] { |
| 127 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("reverse sort didn't sort") |
| 128 | } |
| 129 | if i > len(data)/2 { |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | type nonDeterministicTestingDatascip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData# struct { |
| 136 | rscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#r. *randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Rand# |
| 137 | } |
| 138 | |
| 139 | func (t *nonDeterministicTestingDatascip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#) Lenscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Len().() int { |
| 140 | return 500 |
| 141 | } |
| 142 | func (t *nonDeterministicTestingDatascip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#) Lessscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Less().(i, j int) bool { |
| 143 | if i < 0 || j < 0 || i >= t.Lenscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Len().() || j >= t.Lenscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Len().() { |
| 144 | panic("nondeterministic comparison out of bounds") |
| 145 | } |
| 146 | return t.rscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#r..Float32scip-go gomod github.com/golang/go/src go1.13 `math/rand`/Rand#Float32().() < 0.5 |
| 147 | } |
| 148 | func (t *nonDeterministicTestingDatascip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#) Swapscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Swap().(i, j int) { |
| 149 | if i < 0 || j < 0 || i >= t.Lenscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Len().() || j >= t.Lenscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#Len().() { |
| 150 | panic("nondeterministic comparison out of bounds") |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestNonDeterministicComparisonscip-go gomod go4.org . `go4.org/sort_test`/TestNonDeterministicComparison().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 155 | // Ensure that sort.Sort does not panic when Less returns inconsistent results. |
| 156 | // See https://golang.org/issue/14377. |
| 157 | defer func() { |
| 158 | if r := recover(); r != nil { |
| 159 | t.Errorscip-go gomod github.com/golang/go/src go1.13 testing/common#Error().(r) |
| 160 | } |
| 161 | }() |
| 162 | |
| 163 | td := &nonDeterministicTestingDatascip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#{ |
| 164 | rscip-go gomod go4.org . `go4.org/sort_test`/nonDeterministicTestingData#r.: randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.13 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.13 `math/rand`/NewSource().(0)), |
| 165 | } |
| 166 | |
| 167 | for i := 0; i < 10; i++ { |
| 168 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(td) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func BenchmarkSortString1Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSortString1K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 173 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 174 | unsorted := make([]string, 1<<10) |
| 175 | for i := range unsorted { |
| 176 | unsorted[i] = strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Itoascip-go gomod github.com/golang/go/src go1.13 strconv/Itoa().(i ^ 0x2cc) |
| 177 | } |
| 178 | data := make([]string, len(unsorted)) |
| 179 | |
| 180 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 181 | copy(data, unsorted) |
| 182 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 183 | Stringsscip-go gomod go4.org . `go4.org/sort`/Strings().(data) |
| 184 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func BenchmarkSortString1K_Withscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSortString1K_With().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 189 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 190 | unsorted := make([]string, 1<<10) |
| 191 | for i := range unsorted { |
| 192 | unsorted[i] = strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Itoascip-go gomod github.com/golang/go/src go1.13 strconv/Itoa().(i ^ 0x2cc) |
| 193 | } |
| 194 | data := make([]string, len(unsorted)) |
| 195 | |
| 196 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 197 | copy(data, unsorted) |
| 198 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 199 | Withscip-go gomod go4.org . `go4.org/sort`/With().(len(data), |
| 200 | func(i, j int) { data[i], data[j] = data[j], data[i] }, |
| 201 | func(i, j int) bool { |
| 202 | return data[i] < data[j] |
| 203 | }) |
| 204 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func BenchmarkSortString1K_WithSwapperscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSortString1K_WithSwapper().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 209 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 210 | unsorted := make([]string, 1<<10) |
| 211 | for i := range unsorted { |
| 212 | unsorted[i] = strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Itoascip-go gomod github.com/golang/go/src go1.13 strconv/Itoa().(i ^ 0x2cc) |
| 213 | } |
| 214 | data := make([]string, len(unsorted)) |
| 215 | |
| 216 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 217 | copy(data, unsorted) |
| 218 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 219 | Withscip-go gomod go4.org . `go4.org/sort`/With().(len(data), reflectutilscip-go gomod go4.org . `go4.org/reflectutil`/.Swapperscip-go gomod go4.org . `go4.org/reflectutil`/Swapper().(data), func(i, j int) bool { |
| 220 | return data[i] < data[j] |
| 221 | }) |
| 222 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func BenchmarkStableString1Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStableString1K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 227 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 228 | unsorted := make([]string, 1<<10) |
| 229 | for i := 0; i < len(datascip-go gomod go4.org . `go4.org/sort_test`/data.); i++ { |
| 230 | unsorted[i] = strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Itoascip-go gomod github.com/golang/go/src go1.13 strconv/Itoa().(i ^ 0x2cc) |
| 231 | } |
| 232 | data := make([]string, len(unsorted)) |
| 233 | |
| 234 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 235 | copy(data, unsorted) |
| 236 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 237 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(StringSlicescip-go gomod go4.org . `go4.org/sort`/StringSlice#(data)) |
| 238 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func BenchmarkSortInt1Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSortInt1K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 243 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 244 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 245 | data := make([]int, 1<<10) |
| 246 | for i := 0; i < len(data); i++ { |
| 247 | data[i] = i ^ 0x2cc |
| 248 | } |
| 249 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 250 | Intsscip-go gomod go4.org . `go4.org/sort`/Ints().(data) |
| 251 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func BenchmarkStableInt1Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStableInt1K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 256 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 257 | unsorted := make([]int, 1<<10) |
| 258 | for i := range unsorted { |
| 259 | unsorted[i] = i ^ 0x2cc |
| 260 | } |
| 261 | data := make([]int, len(unsorted)) |
| 262 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 263 | copy(data, unsorted) |
| 264 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 265 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data)) |
| 266 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func BenchmarkStableInt1K_Withscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStableInt1K_With().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 271 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 272 | unsorted := make([]int, 1<<10) |
| 273 | for i := range unsorted { |
| 274 | unsorted[i] = i ^ 0x2cc |
| 275 | } |
| 276 | data := make([]int, len(unsorted)) |
| 277 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 278 | copy(data, unsorted) |
| 279 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 280 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(MakeInterfacescip-go gomod go4.org . `go4.org/sort`/MakeInterface().( |
| 281 | len(data), |
| 282 | func(i, j int) { data[i], data[j] = data[j], data[i] }, |
| 283 | func(i, j int) bool { return data[i] < data[j] }, |
| 284 | )) |
| 285 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | func BenchmarkStableInt1K_WithSwapperscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStableInt1K_WithSwapper().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 290 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 291 | unsorted := make([]int, 1<<10) |
| 292 | for i := range unsorted { |
| 293 | unsorted[i] = i ^ 0x2cc |
| 294 | } |
| 295 | data := make([]int, len(unsorted)) |
| 296 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 297 | copy(data, unsorted) |
| 298 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 299 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(MakeInterfacescip-go gomod go4.org . `go4.org/sort`/MakeInterface().(len(data), reflectutilscip-go gomod go4.org . `go4.org/reflectutil`/.Swapperscip-go gomod go4.org . `go4.org/reflectutil`/Swapper().(data), func(i, j int) bool { |
| 300 | return data[i] < data[j] |
| 301 | })) |
| 302 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func BenchmarkSortInt64Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSortInt64K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 307 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 308 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 309 | data := make([]int, 1<<16) |
| 310 | for i := 0; i < len(data); i++ { |
| 311 | data[i] = i ^ 0xcccc |
| 312 | } |
| 313 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 314 | Intsscip-go gomod go4.org . `go4.org/sort`/Ints().(data) |
| 315 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | func BenchmarkStableInt64Kscip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStableInt64K().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 320 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 321 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 322 | data := make([]int, 1<<16) |
| 323 | for i := 0; i < len(data); i++ { |
| 324 | data[i] = i ^ 0xcccc |
| 325 | } |
| 326 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 327 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data)) |
| 328 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | const ( |
| 333 | _Sawtoothscip-go gomod go4.org . `go4.org/sort_test`/_Sawtooth. = iota |
| 334 | _Randscip-go gomod go4.org . `go4.org/sort_test`/_Rand. |
| 335 | _Staggerscip-go gomod go4.org . `go4.org/sort_test`/_Stagger. |
| 336 | _Plateauscip-go gomod go4.org . `go4.org/sort_test`/_Plateau. |
| 337 | _Shufflescip-go gomod go4.org . `go4.org/sort_test`/_Shuffle. |
| 338 | _NDistscip-go gomod go4.org . `go4.org/sort_test`/_NDist. |
| 339 | ) |
| 340 | |
| 341 | const ( |
| 342 | _Copyscip-go gomod go4.org . `go4.org/sort_test`/_Copy. = iota |
| 343 | _Reversescip-go gomod go4.org . `go4.org/sort_test`/_Reverse. |
| 344 | _ReverseFirstHalfscip-go gomod go4.org . `go4.org/sort_test`/_ReverseFirstHalf. |
| 345 | _ReverseSecondHalfscip-go gomod go4.org . `go4.org/sort_test`/_ReverseSecondHalf. |
| 346 | _Sortedscip-go gomod go4.org . `go4.org/sort_test`/_Sorted. |
| 347 | _Ditherscip-go gomod go4.org . `go4.org/sort_test`/_Dither. |
| 348 | _NModescip-go gomod go4.org . `go4.org/sort_test`/_NMode. |
| 349 | ) |
| 350 | |
| 351 | type testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData# struct { |
| 352 | descscip-go gomod go4.org . `go4.org/sort_test`/testingData#desc. string |
| 353 | tscip-go gomod go4.org . `go4.org/sort_test`/testingData#t. *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T# |
| 354 | datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data. []int |
| 355 | maxswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#maxswap. int // number of swaps allowed |
| 356 | ncmpscip-go gomod go4.org . `go4.org/sort_test`/testingData#ncmp., nswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#nswap. int |
| 357 | } |
| 358 | |
| 359 | func (d *testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData#) Lenscip-go gomod go4.org . `go4.org/sort_test`/testingData#Len().() int { return len(d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.) } |
| 360 | func (d *testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData#) Lessscip-go gomod go4.org . `go4.org/sort_test`/testingData#Less().(i, j int) bool { |
| 361 | d.ncmpscip-go gomod go4.org . `go4.org/sort_test`/testingData#ncmp.++ |
| 362 | return d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[i] < d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[j] |
| 363 | } |
| 364 | func (d *testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData#) Swapscip-go gomod go4.org . `go4.org/sort_test`/testingData#Swap().(i, j int) { |
| 365 | if d.nswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#nswap. >= d.maxswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#maxswap. { |
| 366 | d.tscip-go gomod go4.org . `go4.org/sort_test`/testingData#t..Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("%s: used %d swaps sorting slice of %d", d.descscip-go gomod go4.org . `go4.org/sort_test`/testingData#desc., d.nswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#nswap., len(d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.)) |
| 367 | d.tscip-go gomod go4.org . `go4.org/sort_test`/testingData#t..FailNowscip-go gomod github.com/golang/go/src go1.13 testing/common#FailNow().() |
| 368 | } |
| 369 | d.nswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#nswap.++ |
| 370 | d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[i], d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[j] = d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[j], d.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[i] |
| 371 | } |
| 372 | |
| 373 | func minscip-go gomod go4.org . `go4.org/sort_test`/min().(a, b int) int { |
| 374 | if a < b { |
| 375 | return a |
| 376 | } |
| 377 | return b |
| 378 | } |
| 379 | |
| 380 | func lgscip-go gomod go4.org . `go4.org/sort_test`/lg().(n int) int { |
| 381 | i := 0 |
| 382 | for 1<<uint(i) < n { |
| 383 | i++ |
| 384 | } |
| 385 | return i |
| 386 | } |
| 387 | |
| 388 | func testBentleyMcIlroyscip-go gomod go4.org . `go4.org/sort_test`/testBentleyMcIlroy().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#, sort func(Interfacescip-go gomod go4.org . `go4.org/sort`/Interface#), maxswap func(int) int) { |
| 389 | sizes := []int{100, 1023, 1024, 1025} |
| 390 | if testingscip-go gomod github.com/golang/go/src go1.13 testing/.Shortscip-go gomod github.com/golang/go/src go1.13 testing/Short().() { |
| 391 | sizes = []int{100, 127, 128, 129} |
| 392 | } |
| 393 | dists := []string{"sawtooth", "rand", "stagger", "plateau", "shuffle"} |
| 394 | modes := []string{"copy", "reverse", "reverse1", "reverse2", "sort", "dither"} |
| 395 | var tmp1, tmp2 [1025]int |
| 396 | for _, n := range sizes { |
| 397 | for m := 1; m < 2*n; m *= 2 { |
| 398 | for dist := 0; dist < _NDistscip-go gomod go4.org . `go4.org/sort_test`/_NDist.; dist++ { |
| 399 | j := 0 |
| 400 | k := 1 |
| 401 | data := tmp1[0:n] |
| 402 | for i := 0; i < n; i++ { |
| 403 | switch dist { |
| 404 | case _Sawtoothscip-go gomod go4.org . `go4.org/sort_test`/_Sawtooth.: |
| 405 | data[i] = i % m |
| 406 | case _Randscip-go gomod go4.org . `go4.org/sort_test`/_Rand.: |
| 407 | data[i] = randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Intn().(m) |
| 408 | case _Staggerscip-go gomod go4.org . `go4.org/sort_test`/_Stagger.: |
| 409 | data[i] = (i*m + i) % n |
| 410 | case _Plateauscip-go gomod go4.org . `go4.org/sort_test`/_Plateau.: |
| 411 | data[i] = minscip-go gomod go4.org . `go4.org/sort_test`/min().(i, m) |
| 412 | case _Shufflescip-go gomod go4.org . `go4.org/sort_test`/_Shuffle.: |
| 413 | if randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Intn().(m) != 0 { |
| 414 | j += 2 |
| 415 | data[i] = j |
| 416 | } else { |
| 417 | k += 2 |
| 418 | data[i] = k |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | mdata := tmp2[0:n] |
| 424 | for mode := 0; mode < _NModescip-go gomod go4.org . `go4.org/sort_test`/_NMode.; mode++ { |
| 425 | switch mode { |
| 426 | case _Copyscip-go gomod go4.org . `go4.org/sort_test`/_Copy.: |
| 427 | for i := 0; i < n; i++ { |
| 428 | mdata[i] = data[i] |
| 429 | } |
| 430 | case _Reversescip-go gomod go4.org . `go4.org/sort_test`/_Reverse.: |
| 431 | for i := 0; i < n; i++ { |
| 432 | mdata[i] = data[n-i-1] |
| 433 | } |
| 434 | case _ReverseFirstHalfscip-go gomod go4.org . `go4.org/sort_test`/_ReverseFirstHalf.: |
| 435 | for i := 0; i < n/2; i++ { |
| 436 | mdata[i] = data[n/2-i-1] |
| 437 | } |
| 438 | for i := n / 2; i < n; i++ { |
| 439 | mdata[i] = data[i] |
| 440 | } |
| 441 | case _ReverseSecondHalfscip-go gomod go4.org . `go4.org/sort_test`/_ReverseSecondHalf.: |
| 442 | for i := 0; i < n/2; i++ { |
| 443 | mdata[i] = data[i] |
| 444 | } |
| 445 | for i := n / 2; i < n; i++ { |
| 446 | mdata[i] = data[n-(i-n/2)-1] |
| 447 | } |
| 448 | case _Sortedscip-go gomod go4.org . `go4.org/sort_test`/_Sorted.: |
| 449 | for i := 0; i < n; i++ { |
| 450 | mdata[i] = data[i] |
| 451 | } |
| 452 | // Ints is known to be correct |
| 453 | // because mode Sort runs after mode _Copy. |
| 454 | Intsscip-go gomod go4.org . `go4.org/sort`/Ints().(mdata) |
| 455 | case _Ditherscip-go gomod go4.org . `go4.org/sort_test`/_Dither.: |
| 456 | for i := 0; i < n; i++ { |
| 457 | mdata[i] = data[i] + i%5 |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | desc := fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("n=%d m=%d dist=%s mode=%s", n, m, dists[dist], modes[mode]) |
| 462 | d := &testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData#{descscip-go gomod go4.org . `go4.org/sort_test`/testingData#desc.: desc, tscip-go gomod go4.org . `go4.org/sort_test`/testingData#t.: t, datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.: mdata[0:n], maxswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#maxswap.: maxswap(n)} |
| 463 | sort(d) |
| 464 | // Uncomment if you are trying to improve the number of compares/swaps. |
| 465 | //t.Logf("%s: ncmp=%d, nswp=%d", desc, d.ncmp, d.nswap) |
| 466 | |
| 467 | // If we were testing C qsort, we'd have to make a copy |
| 468 | // of the slice and sort it ourselves and then compare |
| 469 | // x against it, to ensure that qsort was only permuting |
| 470 | // the data, not (for example) overwriting it with zeros. |
| 471 | // |
| 472 | // In go, we don't have to be so paranoid: since the only |
| 473 | // mutating method Sort can call is TestingData.swap, |
| 474 | // it suffices here just to check that the final slice is sorted. |
| 475 | if !IntsAreSortedscip-go gomod go4.org . `go4.org/sort`/IntsAreSorted().(mdata) { |
| 476 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("%s: ints not sorted", desc) |
| 477 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("\t%v", mdata) |
| 478 | t.FailNowscip-go gomod github.com/golang/go/src go1.13 testing/common#FailNow().() |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | func TestSortBMscip-go gomod go4.org . `go4.org/sort_test`/TestSortBM().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 487 | testBentleyMcIlroyscip-go gomod go4.org . `go4.org/sort_test`/testBentleyMcIlroy().(t, Sortscip-go gomod go4.org . `go4.org/sort`/Sort()., func(n int) int { return n * lgscip-go gomod go4.org . `go4.org/sort_test`/lg().(n) * 12 / 10 }) |
| 488 | } |
| 489 | |
| 490 | func TestHeapsortBMscip-go gomod go4.org . `go4.org/sort_test`/TestHeapsortBM().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 491 | testBentleyMcIlroyscip-go gomod go4.org . `go4.org/sort_test`/testBentleyMcIlroy().(t, Heapsortscip-go gomod go4.org . `go4.org/sort`/Heapsort()., func(n int) int { return n * lgscip-go gomod go4.org . `go4.org/sort_test`/lg().(n) * 12 / 10 }) |
| 492 | } |
| 493 | |
| 494 | func TestStableBMscip-go gomod go4.org . `go4.org/sort_test`/TestStableBM().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 495 | testBentleyMcIlroyscip-go gomod go4.org . `go4.org/sort_test`/testBentleyMcIlroy().(t, Stablescip-go gomod go4.org . `go4.org/sort`/Stable()., func(n int) int { return n * lgscip-go gomod go4.org . `go4.org/sort_test`/lg().(n) * lgscip-go gomod go4.org . `go4.org/sort_test`/lg().(n) / 3 }) |
| 496 | } |
| 497 | |
| 498 | // This is based on the "antiquicksort" implementation by M. Douglas McIlroy. |
| 499 | // See http://www.cs.dartmouth.edu/~doug/mdmspe.pdf for more info. |
| 500 | type adversaryTestingDatascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData# struct { |
| 501 | datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data. []int |
| 502 | keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys. map[int]int |
| 503 | candidatescip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#candidate. int |
| 504 | } |
| 505 | |
| 506 | func (d *adversaryTestingDatascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#) Lenscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#Len().() int { return len(d.datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data.) } |
| 507 | |
| 508 | func (d *adversaryTestingDatascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#) Lessscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#Less().(i, j int) bool { |
| 509 | if _, present := d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[i]; !present { |
| 510 | if _, present := d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[j]; !present { |
| 511 | if i == d.candidatescip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#candidate. { |
| 512 | d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[i] = len(d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.) |
| 513 | } else { |
| 514 | d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[j] = len(d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.) |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if _, present := d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[i]; !present { |
| 520 | d.candidatescip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#candidate. = i |
| 521 | return false |
| 522 | } |
| 523 | if _, present := d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[j]; !present { |
| 524 | d.candidatescip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#candidate. = j |
| 525 | return true |
| 526 | } |
| 527 | |
| 528 | return d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[i] >= d.keysscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#keys.[j] |
| 529 | } |
| 530 | |
| 531 | func (d *adversaryTestingDatascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#) Swapscip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#Swap().(i, j int) { |
| 532 | d.datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data.[i], d.datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data.[j] = d.datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data.[j], d.datascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#data.[i] |
| 533 | } |
| 534 | |
| 535 | func TestAdversaryscip-go gomod go4.org . `go4.org/sort_test`/TestAdversary().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 536 | const size = 100 |
| 537 | data := make([]int, size) |
| 538 | for i := 0; i < size; i++ { |
| 539 | data[i] = i |
| 540 | } |
| 541 | |
| 542 | d := &adversaryTestingDatascip-go gomod go4.org . `go4.org/sort_test`/adversaryTestingData#{data, make(map[int]int), 0} |
| 543 | Sortscip-go gomod go4.org . `go4.org/sort`/Sort().(d) // This should degenerate to heapsort. |
| 544 | } |
| 545 | |
| 546 | func TestStableIntsscip-go gomod go4.org . `go4.org/sort_test`/TestStableInts().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 547 | data := intsscip-go gomod go4.org . `go4.org/sort_test`/ints. |
| 548 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(IntSlicescip-go gomod go4.org . `go4.org/sort`/IntSlice#(data[0:])) |
| 549 | if !IntsAreSortedscip-go gomod go4.org . `go4.org/sort`/IntsAreSorted().(data[0:]) { |
| 550 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("nsorted %v\n got %v", intsscip-go gomod go4.org . `go4.org/sort_test`/ints., data) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | type intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs# []struct { |
| 555 | a, b int |
| 556 | } |
| 557 | |
| 558 | // IntPairs compare on a only. |
| 559 | func (d intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#) Lenscip-go gomod go4.org . `go4.org/sort_test`/intPairs#Len().() int { return len(d) } |
| 560 | func (d intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#) Lessscip-go gomod go4.org . `go4.org/sort_test`/intPairs#Less().(i, j int) bool { return d[i].a < d[j].a } |
| 561 | func (d intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#) Swapscip-go gomod go4.org . `go4.org/sort_test`/intPairs#Swap().(i, j int) { d[i], d[j] = d[j], d[i] } |
| 562 | |
| 563 | // Record initial order in B. |
| 564 | func (d intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#) initBscip-go gomod go4.org . `go4.org/sort_test`/intPairs#initB().() { |
| 565 | for i := range d { |
| 566 | d[i].b = i |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // InOrder checks if a-equal elements were not reordered. |
| 571 | func (d intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#) inOrderscip-go gomod go4.org . `go4.org/sort_test`/intPairs#inOrder().() bool { |
| 572 | lastA, lastB := -1, 0 |
| 573 | for i := 0; i < len(d); i++ { |
| 574 | if lastA != d[i].a { |
| 575 | lastA = d[i].a |
| 576 | lastB = d[i].b |
| 577 | continue |
| 578 | } |
| 579 | if d[i].b <= lastB { |
| 580 | return false |
| 581 | } |
| 582 | lastB = d[i].b |
| 583 | } |
| 584 | return true |
| 585 | } |
| 586 | |
| 587 | func TestStabilityscip-go gomod go4.org . `go4.org/sort_test`/TestStability().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 588 | n, m := 100000, 1000 |
| 589 | if testingscip-go gomod github.com/golang/go/src go1.13 testing/.Shortscip-go gomod github.com/golang/go/src go1.13 testing/Short().() { |
| 590 | n, m = 1000, 100 |
| 591 | } |
| 592 | data := make(intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#, n) |
| 593 | |
| 594 | // random distribution |
| 595 | for i := 0; i < len(data); i++ { |
| 596 | data[i].a = randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Intn().(m) |
| 597 | } |
| 598 | if IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(data) { |
| 599 | t.Fatalfscip-go gomod github.com/golang/go/src go1.13 testing/common#Fatalf().("terrible rand.rand") |
| 600 | } |
| 601 | data.initBscip-go gomod go4.org . `go4.org/sort_test`/intPairs#initB().() |
| 602 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(data) |
| 603 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(data) { |
| 604 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable didn't sort %d ints", n) |
| 605 | } |
| 606 | if !data.inOrderscip-go gomod go4.org . `go4.org/sort_test`/intPairs#inOrder().() { |
| 607 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable wasn't stable on %d ints", n) |
| 608 | } |
| 609 | |
| 610 | // already sorted |
| 611 | data.initBscip-go gomod go4.org . `go4.org/sort_test`/intPairs#initB().() |
| 612 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(data) |
| 613 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(data) { |
| 614 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable shuffled sorted %d ints (order)", n) |
| 615 | } |
| 616 | if !data.inOrderscip-go gomod go4.org . `go4.org/sort_test`/intPairs#inOrder().() { |
| 617 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable shuffled sorted %d ints (stability)", n) |
| 618 | } |
| 619 | |
| 620 | // sorted reversed |
| 621 | for i := 0; i < len(data); i++ { |
| 622 | data[i].a = len(data) - i |
| 623 | } |
| 624 | data.initBscip-go gomod go4.org . `go4.org/sort_test`/intPairs#initB().() |
| 625 | Stablescip-go gomod go4.org . `go4.org/sort`/Stable().(data) |
| 626 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(data) { |
| 627 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable didn't sort %d ints", n) |
| 628 | } |
| 629 | if !data.inOrderscip-go gomod go4.org . `go4.org/sort_test`/intPairs#inOrder().() { |
| 630 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Stable wasn't stable on %d ints", n) |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | var countOpsSizesscip-go gomod go4.org . `go4.org/sort_test`/countOpsSizes. = []int{1e2, 3e2, 1e3, 3e3, 1e4, 3e4, 1e5, 3e5, 1e6} |
| 635 | |
| 636 | func countOpsscip-go gomod go4.org . `go4.org/sort_test`/countOps().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#, algo func(Interfacescip-go gomod go4.org . `go4.org/sort`/Interface#), name string) { |
| 637 | sizes := countOpsSizesscip-go gomod go4.org . `go4.org/sort_test`/countOpsSizes. |
| 638 | if testingscip-go gomod github.com/golang/go/src go1.13 testing/.Shortscip-go gomod github.com/golang/go/src go1.13 testing/Short().() { |
| 639 | sizes = sizes[:5] |
| 640 | } |
| 641 | if !testingscip-go gomod github.com/golang/go/src go1.13 testing/.Verbosescip-go gomod github.com/golang/go/src go1.13 testing/Verbose().() { |
| 642 | t.Skipscip-go gomod github.com/golang/go/src go1.13 testing/common#Skip().("Counting skipped as non-verbose mode.") |
| 643 | } |
| 644 | for _, n := range sizes { |
| 645 | td := testingDatascip-go gomod go4.org . `go4.org/sort_test`/testingData#{ |
| 646 | descscip-go gomod go4.org . `go4.org/sort_test`/testingData#desc.: name, |
| 647 | tscip-go gomod go4.org . `go4.org/sort_test`/testingData#t.: t, |
| 648 | datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.: make([]int, n), |
| 649 | maxswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#maxswap.: 1<<31 - 1, |
| 650 | } |
| 651 | for i := 0; i < n; i++ { |
| 652 | td.datascip-go gomod go4.org . `go4.org/sort_test`/testingData#data.[i] = randscip-go gomod github.com/golang/go/src go1.13 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.13 `math/rand`/Intn().(n / 5) |
| 653 | } |
| 654 | algo(&td) |
| 655 | t.Logfscip-go gomod github.com/golang/go/src go1.13 testing/common#Logf().("%s %8d elements: %11d Swap, %10d Less", name, n, td.nswapscip-go gomod go4.org . `go4.org/sort_test`/testingData#nswap., td.ncmpscip-go gomod go4.org . `go4.org/sort_test`/testingData#ncmp.) |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | func TestCountStableOpsscip-go gomod go4.org . `go4.org/sort_test`/TestCountStableOps().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { countOpsscip-go gomod go4.org . `go4.org/sort_test`/countOps().(t, Stablescip-go gomod go4.org . `go4.org/sort`/Stable()., "Stable") } |
| 660 | func TestCountSortOpsscip-go gomod go4.org . `go4.org/sort_test`/TestCountSortOps().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { countOpsscip-go gomod go4.org . `go4.org/sort_test`/countOps().(t, Sortscip-go gomod go4.org . `go4.org/sort`/Sort()., "Sort ") } |
| 661 | |
| 662 | func benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#, size int, algo func(Interfacescip-go gomod go4.org . `go4.org/sort`/Interface#), name string) { |
| 663 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 664 | data := make(intPairsscip-go gomod go4.org . `go4.org/sort_test`/intPairs#, size) |
| 665 | x := ^uint32(0) |
| 666 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 667 | for n := size - 3; n <= size+3; n++ { |
| 668 | for i := 0; i < len(data); i++ { |
| 669 | x += x |
| 670 | x ^= 1 |
| 671 | if int32(x) < 0 { |
| 672 | x ^= 0x88888eef |
| 673 | } |
| 674 | data[i].a = int(x % uint32(n/5)) |
| 675 | } |
| 676 | data.initBscip-go gomod go4.org . `go4.org/sort_test`/intPairs#initB().() |
| 677 | b.StartTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StartTimer().() |
| 678 | algo(data) |
| 679 | b.StopTimerscip-go gomod github.com/golang/go/src go1.13 testing/B#StopTimer().() |
| 680 | if !IsSortedscip-go gomod go4.org . `go4.org/sort`/IsSorted().(data) { |
| 681 | b.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("%s did not sort %d ints", name, n) |
| 682 | } |
| 683 | if name == "Stable" && !data.inOrderscip-go gomod go4.org . `go4.org/sort_test`/intPairs#inOrder().() { |
| 684 | b.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("%s unstable on %d ints", name, n) |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | func BenchmarkSort1e2scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSort1e2().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e2, Sortscip-go gomod go4.org . `go4.org/sort`/Sort()., "Sort") } |
| 691 | func BenchmarkStable1e2scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStable1e2().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e2, Stablescip-go gomod go4.org . `go4.org/sort`/Stable()., "Stable") } |
| 692 | func BenchmarkSort1e4scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSort1e4().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e4, Sortscip-go gomod go4.org . `go4.org/sort`/Sort()., "Sort") } |
| 693 | func BenchmarkStable1e4scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStable1e4().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e4, Stablescip-go gomod go4.org . `go4.org/sort`/Stable()., "Stable") } |
| 694 | func BenchmarkSort1e6scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkSort1e6().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e6, Sortscip-go gomod go4.org . `go4.org/sort`/Sort()., "Sort") } |
| 695 | func BenchmarkStable1e6scip-go gomod go4.org . `go4.org/sort_test`/BenchmarkStable1e6().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { benchscip-go gomod go4.org . `go4.org/sort_test`/bench().(b, 1e6, Stablescip-go gomod go4.org . `go4.org/sort`/Stable()., "Stable") } |