github.com/richardwilkes/toolbox@v1.121.0/collection/slice/slice_test.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package slice_test
    11  
    12  import (
    13  	"testing"
    14  
    15  	"github.com/richardwilkes/toolbox/check"
    16  	"github.com/richardwilkes/toolbox/collection/slice"
    17  )
    18  
    19  func TestZeroedDelete(t *testing.T) {
    20  	type data struct {
    21  		a int
    22  	}
    23  	d1 := &data{a: 1}
    24  	d2 := &data{a: 2}
    25  	d3 := &data{a: 3}
    26  	for _, test := range []struct {
    27  		s    []*data
    28  		want []*data
    29  		i, j int
    30  	}{
    31  		{
    32  			[]*data{d1, d2, d3},
    33  			[]*data{d1, d2, d3},
    34  			0,
    35  			0,
    36  		},
    37  		{
    38  			[]*data{d1, d2, d3},
    39  			[]*data{d2, d3},
    40  			0,
    41  			1,
    42  		},
    43  		{
    44  			[]*data{d1, d2, d3},
    45  			[]*data{d1, d2, d3},
    46  			3,
    47  			3,
    48  		},
    49  		{
    50  			[]*data{d1, d2, d3},
    51  			[]*data{d3},
    52  			0,
    53  			2,
    54  		},
    55  		{
    56  			[]*data{d1, d2, d3},
    57  			[]*data{},
    58  			0,
    59  			3,
    60  		},
    61  	} {
    62  		theCopy := append([]*data{}, test.s...)
    63  		result := slice.ZeroedDelete(theCopy, test.i, test.j)
    64  		check.Equal(t, result, test.want, "ZeroedDelete(%v, %d, %d) = %v, want %v", test.s, test.i, test.j, result, test.want)
    65  		for i := len(result); i < len(theCopy); i++ {
    66  			check.Nil(t, theCopy[i], "residual element %d should have been nil, was %v", i, theCopy[i])
    67  		}
    68  	}
    69  }