1 // Copyright 2023 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.
18 var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
19 var float64s = [...]float64{74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3, math.Inf(-1), 9845.768, -959.7485, 905, 7.8, 7.8, 74.3, 59.0, math.Inf(1), 238.2, -784.0, 2.3}
20 var strs = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
22 func TestSortIntSlice(t *testing.T) {
23 data := Clone(ints[:])
26 t.Errorf("sorted %v", ints)
27 t.Errorf(" got %v", data)
31 func TestSortFuncIntSlice(t *testing.T) {
32 data := Clone(ints[:])
33 SortFunc(data, func(a, b int) int { return a - b })
35 t.Errorf("sorted %v", ints)
36 t.Errorf(" got %v", data)
40 func TestSortFloat64Slice(t *testing.T) {
41 data := Clone(float64s[:])
44 t.Errorf("sorted %v", float64s)
45 t.Errorf(" got %v", data)
49 func TestSortStringSlice(t *testing.T) {
50 data := Clone(strs[:])
53 t.Errorf("sorted %v", strs)
54 t.Errorf(" got %v", data)
58 func TestSortLarge_Random(t *testing.T) {
63 data := make([]int, n)
64 for i := 0; i < len(data); i++ {
65 data[i] = rand.Intn(100)
68 t.Fatalf("terrible rand.rand")
72 t.Errorf("sort didn't sort - 1M ints")
80 type intPairs []intPair
82 // Pairs compare on a only.
83 func intPairCmp(x, y intPair) int {
87 // Record initial order in B.
88 func (d intPairs) initB() {
94 // InOrder checks if a-equal elements were not reordered.
95 func (d intPairs) inOrder() bool {
97 for i := 0; i < len(d); i++ {
111 func TestStability(t *testing.T) {
116 data := make(intPairs, n)
118 // random distribution
119 for i := 0; i < len(data); i++ {
120 data[i].a = rand.Intn(m)
122 if IsSortedFunc(data, intPairCmp) {
123 t.Fatalf("terrible rand.rand")
126 SortStableFunc(data, intPairCmp)
127 if !IsSortedFunc(data, intPairCmp) {
128 t.Errorf("Stable didn't sort %d ints", n)
131 t.Errorf("Stable wasn't stable on %d ints", n)
136 SortStableFunc(data, intPairCmp)
137 if !IsSortedFunc(data, intPairCmp) {
138 t.Errorf("Stable shuffled sorted %d ints (order)", n)
141 t.Errorf("Stable shuffled sorted %d ints (stability)", n)
145 for i := 0; i < len(data); i++ {
146 data[i].a = len(data) - i
149 SortStableFunc(data, intPairCmp)
150 if !IsSortedFunc(data, intPairCmp) {
151 t.Errorf("Stable didn't sort %d ints", n)
154 t.Errorf("Stable wasn't stable on %d ints", n)
163 func cmpS(s1, s2 S) int {
164 return cmp.Compare(s1.a, s2.a)
167 func TestMinMax(t *testing.T) {
168 intCmp := func(a, b int) int { return a - b }
178 {[]int{1, 2, 3}, 1, 3},
179 {[]int{3, 2, 1}, 1, 3},
180 {[]int{2, 1, 3}, 1, 3},
181 {[]int{2, 2, 3}, 2, 3},
182 {[]int{3, 2, 3}, 2, 3},
183 {[]int{0, 2, -9}, -9, 2},
185 for _, tt := range tests {
186 t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
187 gotMin := Min(tt.data)
188 if gotMin != tt.wantMin {
189 t.Errorf("Min got %v, want %v", gotMin, tt.wantMin)
192 gotMinFunc := MinFunc(tt.data, intCmp)
193 if gotMinFunc != tt.wantMin {
194 t.Errorf("MinFunc got %v, want %v", gotMinFunc, tt.wantMin)
197 gotMax := Max(tt.data)
198 if gotMax != tt.wantMax {
199 t.Errorf("Max got %v, want %v", gotMax, tt.wantMax)
202 gotMaxFunc := MaxFunc(tt.data, intCmp)
203 if gotMaxFunc != tt.wantMax {
204 t.Errorf("MaxFunc got %v, want %v", gotMaxFunc, tt.wantMax)
216 gotMin := MinFunc(svals, cmpS)
218 if gotMin != wantMin {
219 t.Errorf("MinFunc(%v) = %v, want %v", svals, gotMin, wantMin)
222 gotMax := MaxFunc(svals, cmpS)
224 if gotMax != wantMax {
225 t.Errorf("MaxFunc(%v) = %v, want %v", svals, gotMax, wantMax)
229 func TestMinMaxNaNs(t *testing.T) {
230 fs := []float64{1.0, 999.9, 3.14, -400.4, -5.14}
231 if Min(fs) != -400.4 {
232 t.Errorf("got min %v, want -400.4", Min(fs))
234 if Max(fs) != 999.9 {
235 t.Errorf("got max %v, want 999.9", Max(fs))
238 // No matter which element of fs is replaced with a NaN, both Min and Max
239 // should propagate the NaN to their output.
240 for i := 0; i < len(fs); i++ {
242 testfs[i] = math.NaN()
245 if !math.IsNaN(fmin) {
246 t.Errorf("got min %v, want NaN", fmin)
250 if !math.IsNaN(fmax) {
251 t.Errorf("got max %v, want NaN", fmax)
256 func TestMinMaxPanics(t *testing.T) {
257 intCmp := func(a, b int) int { return a - b }
258 emptySlice := []int{}
260 if !panics(func() { Min(emptySlice) }) {
261 t.Errorf("Min([]): got no panic, want panic")
264 if !panics(func() { Max(emptySlice) }) {
265 t.Errorf("Max([]): got no panic, want panic")
268 if !panics(func() { MinFunc(emptySlice, intCmp) }) {
269 t.Errorf("MinFunc([]): got no panic, want panic")
272 if !panics(func() { MaxFunc(emptySlice, intCmp) }) {
273 t.Errorf("MaxFunc([]): got no panic, want panic")
277 func TestBinarySearch(t *testing.T) {
278 str1 := []string{"foo"}
279 str2 := []string{"ab", "ca"}
280 str3 := []string{"mo", "qo", "vo"}
281 str4 := []string{"ab", "ad", "ca", "xy"}
283 // slice with repeating elements
284 strRepeats := []string{"ba", "ca", "da", "da", "da", "ka", "ma", "ma", "ta"}
286 // slice with all element equal
287 strSame := []string{"xx", "xx", "xx"}
295 {[]string{}, "foo", 0, false},
296 {[]string{}, "", 0, false},
298 {str1, "foo", 0, true},
299 {str1, "bar", 0, false},
300 {str1, "zx", 1, false},
302 {str2, "aa", 0, false},
303 {str2, "ab", 0, true},
304 {str2, "ad", 1, false},
305 {str2, "ca", 1, true},
306 {str2, "ra", 2, false},
308 {str3, "bb", 0, false},
309 {str3, "mo", 0, true},
310 {str3, "nb", 1, false},
311 {str3, "qo", 1, true},
312 {str3, "tr", 2, false},
313 {str3, "vo", 2, true},
314 {str3, "xr", 3, false},
316 {str4, "aa", 0, false},
317 {str4, "ab", 0, true},
318 {str4, "ac", 1, false},
319 {str4, "ad", 1, true},
320 {str4, "ax", 2, false},
321 {str4, "ca", 2, true},
322 {str4, "cc", 3, false},
323 {str4, "dd", 3, false},
324 {str4, "xy", 3, true},
325 {str4, "zz", 4, false},
327 {strRepeats, "da", 2, true},
328 {strRepeats, "db", 5, false},
329 {strRepeats, "ma", 6, true},
330 {strRepeats, "mb", 8, false},
332 {strSame, "xx", 0, true},
333 {strSame, "ab", 0, false},
334 {strSame, "zz", 3, false},
336 for _, tt := range tests {
337 t.Run(tt.target, func(t *testing.T) {
339 pos, found := BinarySearch(tt.data, tt.target)
340 if pos != tt.wantPos || found != tt.wantFound {
341 t.Errorf("BinarySearch got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
346 pos, found := BinarySearchFunc(tt.data, tt.target, strings.Compare)
347 if pos != tt.wantPos || found != tt.wantFound {
348 t.Errorf("BinarySearchFunc got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
355 func TestBinarySearchInts(t *testing.T) {
356 data := []int{20, 30, 40, 50, 60, 70, 80, 90}
367 for _, tt := range tests {
368 t.Run(strconv.Itoa(tt.target), func(t *testing.T) {
370 pos, found := BinarySearch(data, tt.target)
371 if pos != tt.wantPos || found != tt.wantFound {
372 t.Errorf("BinarySearch got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
377 cmp := func(a, b int) int {
380 pos, found := BinarySearchFunc(data, tt.target, cmp)
381 if pos != tt.wantPos || found != tt.wantFound {
382 t.Errorf("BinarySearchFunc got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
389 func TestBinarySearchFloats(t *testing.T) {
390 data := []float64{math.NaN(), -0.25, 0.0, 1.4}
396 {math.NaN(), 0, true},
397 {math.Inf(-1), 1, false},
403 for _, tt := range tests {
404 t.Run(fmt.Sprintf("%v", tt.target), func(t *testing.T) {
406 pos, found := BinarySearch(data, tt.target)
407 if pos != tt.wantPos || found != tt.wantFound {
408 t.Errorf("BinarySearch got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
415 func TestBinarySearchFunc(t *testing.T) {
416 data := []int{1, 10, 11, 2} // sorted lexicographically
417 cmp := func(a int, b string) int {
418 return strings.Compare(strconv.Itoa(a), b)
420 pos, found := BinarySearchFunc(data, "2", cmp)
421 if pos != 3 || !found {
422 t.Errorf("BinarySearchFunc(%v, %q, cmp) = %v, %v, want %v, %v", data, "2", pos, found, 3, true)