github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/greater_test.go (about)

     1  package slices_test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/hamba/slices"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGreaterOf(t *testing.T) {
    12  	tests := []struct {
    13  		name  string
    14  		slice interface{}
    15  		want  interface{}
    16  	}{
    17  		{
    18  			name:  "string",
    19  			slice: []string{"foo", "bar", "baz", "bat"},
    20  			want:  []string{"foo", "baz", "bat", "bar"},
    21  		},
    22  		{
    23  			name:  "int",
    24  			slice: []int{4, 2, 1, 3},
    25  			want:  []int{4, 3, 2, 1},
    26  		},
    27  		{
    28  			name:  "int8",
    29  			slice: []int8{4, 2, 1, 3},
    30  			want:  []int8{4, 3, 2, 1},
    31  		},
    32  		{
    33  			name:  "int16",
    34  			slice: []int16{4, 2, 1, 3},
    35  			want:  []int16{4, 3, 2, 1},
    36  		},
    37  		{
    38  			name:  "int32",
    39  			slice: []int32{4, 2, 1, 3},
    40  			want:  []int32{4, 3, 2, 1},
    41  		},
    42  		{
    43  			name:  "int64",
    44  			slice: []int64{4, 2, 1, 3},
    45  			want:  []int64{4, 3, 2, 1},
    46  		},
    47  		{
    48  			name:  "uint",
    49  			slice: []uint{4, 2, 1, 3},
    50  			want:  []uint{4, 3, 2, 1},
    51  		},
    52  		{
    53  			name:  "uint8",
    54  			slice: []uint8{4, 2, 1, 3},
    55  			want:  []uint8{4, 3, 2, 1},
    56  		},
    57  		{
    58  			name:  "uint16",
    59  			slice: []uint16{4, 2, 1, 3},
    60  			want:  []uint16{4, 3, 2, 1},
    61  		},
    62  		{
    63  			name:  "uint32",
    64  			slice: []uint32{4, 2, 1, 3},
    65  			want:  []uint32{4, 3, 2, 1},
    66  		},
    67  		{
    68  			name:  "uint64",
    69  			slice: []uint64{4, 2, 1, 3},
    70  			want:  []uint64{4, 3, 2, 1},
    71  		},
    72  		{
    73  			name:  "float32",
    74  			slice: []float32{4, 2, 1, 3},
    75  			want:  []float32{4, 3, 2, 1},
    76  		},
    77  		{
    78  			name:  "float64",
    79  			slice: []float64{4, 2, 1, 3},
    80  			want:  []float64{4, 3, 2, 1},
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			sort.Slice(tt.slice, slices.GreaterOf(tt.slice))
    87  
    88  			assert.Equal(t, tt.want, tt.slice)
    89  		})
    90  	}
    91  }
    92  
    93  func TestGreaterOf_ChecksSliceType(t *testing.T) {
    94  	assert.Panics(t, func() {
    95  		slices.GreaterOf("test")
    96  	})
    97  }
    98  
    99  func BenchmarkGreaterOf(b *testing.B) {
   100  	slice := []string{"foo", "bar", "baz", "bat"}
   101  
   102  	b.ReportAllocs()
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		sort.Slice(slice, slices.GreaterOf(slice))
   106  	}
   107  }
   108  
   109  func BenchmarkGreaterOfNative(b *testing.B) {
   110  	slice := []string{"foo", "bar", "baz", "bat"}
   111  	greaterOf := func(i, j int) bool {
   112  		return slice[i] > slice[j]
   113  	}
   114  
   115  	b.ReportAllocs()
   116  	b.ResetTimer()
   117  	for i := 0; i < b.N; i++ {
   118  		sort.Slice(slice, greaterOf)
   119  	}
   120  }