github.com/mailgun/holster/v4@v4.20.0/slice/remove_test.go (about)

     1  package slice_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/mailgun/holster/v4/slice"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func ExampleRemove() {
    12  	s := []string{"1", "2", "3"}
    13  	i := []int{1, 2, 3}
    14  
    15  	fmt.Println(slice.Remove(s, 0, 2))
    16  	fmt.Println(slice.Remove(i, 1, 2))
    17  	// Output:
    18  	// [3]
    19  	// [1 3]
    20  }
    21  
    22  func TestRemove(t *testing.T) {
    23  	for _, test := range []struct {
    24  		name     string
    25  		i        int
    26  		j        int
    27  		inSlice  []int
    28  		outSlice []int
    29  	}{{
    30  		name:     "empty slice",
    31  		inSlice:  []int{},
    32  		outSlice: []int{},
    33  	}, {
    34  		name:     "nil check",
    35  		i:        1,
    36  		j:        3,
    37  		inSlice:  nil,
    38  		outSlice: nil,
    39  	}, {
    40  		name:     "start index out of range should not panic or modify the slice",
    41  		i:        -2,
    42  		j:        2,
    43  		inSlice:  []int{2},
    44  		outSlice: []int{2},
    45  	}, {
    46  		name:     "end index out of range should not panic or modify the slice",
    47  		i:        0,
    48  		j:        5,
    49  		inSlice:  []int{2},
    50  		outSlice: []int{2},
    51  	}, {
    52  		name:     "invalid start and end index should not panic or modify the slice",
    53  		i:        4,
    54  		j:        7,
    55  		inSlice:  []int{2},
    56  		outSlice: []int{2},
    57  	}, {
    58  		name:     "single value remove",
    59  		i:        0,
    60  		j:        1,
    61  		inSlice:  []int{1, 2, 3},
    62  		outSlice: []int{2, 3},
    63  	}, {
    64  		name:     "middle value removed",
    65  		i:        1,
    66  		j:        2,
    67  		inSlice:  []int{1, 2, 3},
    68  		outSlice: []int{1, 3},
    69  	}, {
    70  		name:     "last value removed",
    71  		i:        2,
    72  		j:        3,
    73  		inSlice:  []int{1, 2, 3},
    74  		outSlice: []int{1, 2},
    75  	}, {
    76  		name:     "multi-value remove",
    77  		i:        1,
    78  		j:        3,
    79  		inSlice:  []int{1, 2, 3},
    80  		outSlice: []int{1},
    81  	}, {
    82  		name:     "end of slice gut check for invalid ending index",
    83  		i:        2,
    84  		j:        5, // This index being out of bounds shouldn't matter
    85  		inSlice:  []int{1, 2, 3},
    86  		outSlice: []int{1, 2},
    87  	}} {
    88  		t.Run(test.name, func(t *testing.T) {
    89  			got := slice.Remove(test.inSlice, test.i, test.j)
    90  			for i := range test.outSlice {
    91  				assert.Equal(t, test.outSlice[i], got[i])
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  var out []string
    98  
    99  func BenchmarkRemove(b *testing.B) {
   100  	o := []string{}
   101  	in := []string{"localhost", "mg.example.com", "testlabs.io", "localhost", "m.example.com.br", "localhost", "localhost"}
   102  
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		o = slice.Remove(in, 2, 4)
   106  	}
   107  	out = o
   108  }