github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/lesser_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 TestLesserOf(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{"bar", "bat", "baz", "foo"},
    21  		},
    22  		{
    23  			name:  "int",
    24  			slice: []int{4, 2, 1, 3},
    25  			want:  []int{1, 2, 3, 4},
    26  		},
    27  		{
    28  			name:  "int8",
    29  			slice: []int8{4, 2, 1, 3},
    30  			want:  []int8{1, 2, 3, 4},
    31  		},
    32  		{
    33  			name:  "int16",
    34  			slice: []int16{4, 2, 1, 3},
    35  			want:  []int16{1, 2, 3, 4},
    36  		},
    37  		{
    38  			name:  "int32",
    39  			slice: []int32{4, 2, 1, 3},
    40  			want:  []int32{1, 2, 3, 4},
    41  		},
    42  		{
    43  			name:  "int64",
    44  			slice: []int64{4, 2, 1, 3},
    45  			want:  []int64{1, 2, 3, 4},
    46  		},
    47  		{
    48  			name:  "uint",
    49  			slice: []uint{4, 2, 1, 3},
    50  			want:  []uint{1, 2, 3, 4},
    51  		},
    52  		{
    53  			name:  "uint8",
    54  			slice: []uint8{4, 2, 1, 3},
    55  			want:  []uint8{1, 2, 3, 4},
    56  		},
    57  		{
    58  			name:  "uint16",
    59  			slice: []uint16{4, 2, 1, 3},
    60  			want:  []uint16{1, 2, 3, 4},
    61  		},
    62  		{
    63  			name:  "uint32",
    64  			slice: []uint32{4, 2, 1, 3},
    65  			want:  []uint32{1, 2, 3, 4},
    66  		},
    67  		{
    68  			name:  "uint64",
    69  			slice: []uint64{4, 2, 1, 3},
    70  			want:  []uint64{1, 2, 3, 4},
    71  		},
    72  		{
    73  			name:  "float32",
    74  			slice: []float32{4, 2, 1, 3},
    75  			want:  []float32{1, 2, 3, 4},
    76  		},
    77  		{
    78  			name:  "float64",
    79  			slice: []float64{4, 2, 1, 3},
    80  			want:  []float64{1, 2, 3, 4},
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			sort.Slice(tt.slice, slices.LesserOf(tt.slice))
    87  
    88  			assert.Equal(t, tt.want, tt.slice)
    89  		})
    90  	}
    91  }
    92  
    93  func TestLesserOf_ChecksSliceType(t *testing.T) {
    94  	assert.Panics(t, func() {
    95  		slices.LesserOf("test")
    96  	})
    97  }
    98  
    99  func BenchmarkLesserOf(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.LesserOf(slice))
   106  	}
   107  }
   108  
   109  func BenchmarkLesserOfNative(b *testing.B) {
   110  	slice := []string{"foo", "bar", "baz", "bat"}
   111  	lesserOf := 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, lesserOf)
   119  	}
   120  }