github.com/searKing/golang/go@v1.2.117/sort/sort_test.go (about)

     1  // Copyright 2023 The searKing Author. 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  	"sort"
     9  	"testing"
    10  
    11  	sort_ "github.com/searKing/golang/go/sort"
    12  )
    13  
    14  var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
    15  
    16  // Convenience types for common cases
    17  
    18  // IntSlice attaches the methods of Interface to []int, sorting in increasing order.
    19  type IntSlice []int
    20  
    21  func (x IntSlice) Len() int           { return len(x) }
    22  func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
    23  func (x IntSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
    24  
    25  // Sort is a convenience method: x.Sort() calls Sort(x).
    26  func (x IntSlice) Sort() { sort.Sort(x) }
    27  
    28  func TestPartialSortIntSlice(t *testing.T) {
    29  	data := ints
    30  	data1 := ints
    31  	k := 3
    32  	a := IntSlice(data[:])
    33  	sort.Sort(a[:k])
    34  	if sort_.IsPartialSorted(a, k) {
    35  		t.Errorf("partial sort did sort")
    36  	}
    37  	r := IntSlice(data1[:])
    38  	sort.Sort(r)
    39  	if !sort_.IsPartialSorted(r, k) {
    40  		t.Errorf("partial sort didn't sort")
    41  	}
    42  }