github.com/wrgl/wrgl@v0.14.0/pkg/slice/slice_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright © 2022 Wrangle Ltd
     3  
     4  package slice
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDuplicatedString(t *testing.T) {
    14  	for _, r := range []struct {
    15  		Ssl []string
    16  		R   string
    17  	}{
    18  		{[]string{"1", "2"}, ""},
    19  		{[]string{}, ""},
    20  		{[]string{"abc"}, ""},
    21  		{[]string{"1", "1"}, "1"},
    22  		{[]string{"abc", "def", "abc"}, "abc"},
    23  	} {
    24  		assert.Equal(t, r.R, DuplicatedString(r.Ssl))
    25  	}
    26  }
    27  
    28  func TestStringNotInSubset(t *testing.T) {
    29  	for _, r := range []struct {
    30  		S1 []string
    31  		S2 []string
    32  		R  string
    33  	}{
    34  		{[]string{}, []string{"1", "2"}, ""},
    35  		{[]string{}, []string{}, ""},
    36  		{[]string{"1"}, []string{"1", "2"}, ""},
    37  		{[]string{"1"}, []string{"1"}, ""},
    38  		{[]string{"1", "3"}, []string{"1", "3"}, ""},
    39  		{[]string{"3"}, []string{"1", "2"}, "3"},
    40  		{[]string{"1"}, []string{}, "1"},
    41  		{[]string{"2"}, []string{"1"}, "2"},
    42  		{[]string{"1", "3"}, []string{"1", "2"}, "3"},
    43  	} {
    44  		assert.Equal(t, r.R, StringNotInSubset(r.S1, r.S2))
    45  	}
    46  }
    47  
    48  func TestIndicesToValues(t *testing.T) {
    49  	for _, r := range []struct {
    50  		S []string
    51  		I []uint32
    52  		V []string
    53  	}{
    54  		{[]string{}, []uint32{}, []string{}},
    55  		{[]string{"a", "b"}, []uint32{0}, []string{"a"}},
    56  		{[]string{"c", "d", "e"}, []uint32{2, 1}, []string{"e", "d"}},
    57  	} {
    58  		assert.Equal(t, r.V, IndicesToValues(r.S, r.I))
    59  	}
    60  }
    61  
    62  func TestKeyIndices(t *testing.T) {
    63  	for _, r := range []struct {
    64  		S []string
    65  		K []string
    66  		I []uint32
    67  		E error
    68  	}{
    69  		{[]string{"a", "b"}, []string{"b"}, []uint32{1}, nil},
    70  		{[]string{"a", "b"}, []string{}, nil, nil},
    71  		{[]string{"a", "b"}, []string{"c"}, []uint32(nil), fmt.Errorf(`key "c" not found in string slice`)},
    72  		{[]string{}, []string{}, nil, nil},
    73  	} {
    74  		i, e := KeyIndices(r.S, r.K)
    75  		assert.Equal(t, r.I, i)
    76  		assert.Equal(t, r.E, e)
    77  	}
    78  }
    79  
    80  func TestStringSliceEqual(t *testing.T) {
    81  	for _, r := range []struct {
    82  		Sl1 []string
    83  		Sl2 []string
    84  		R   bool
    85  	}{
    86  		{[]string{"1"}, []string{"1"}, true},
    87  		{[]string{"1", "2"}, []string{"1", "2"}, true},
    88  		{[]string{}, []string{}, true},
    89  		{[]string{"1"}, []string{"2"}, false},
    90  		{[]string{"2", "1"}, []string{"2"}, false},
    91  		{[]string{"1"}, []string{"1", "2"}, false},
    92  		{[]string{}, []string{"2"}, false},
    93  		{[]string{"1"}, []string{}, false},
    94  	} {
    95  		b := StringSliceEqual(r.Sl1, r.Sl2)
    96  		assert.Equal(t, r.R, b)
    97  	}
    98  }
    99  
   100  func TestCompareStringSlices(t *testing.T) {
   101  	for i, c := range []struct {
   102  		sl, oldSl                 []string
   103  		unchanged, added, removed []string
   104  	}{
   105  		{
   106  			nil, nil,
   107  			nil, nil, nil,
   108  		},
   109  		{
   110  			[]string{"a"}, []string{"a"},
   111  			[]string{"a"}, nil, nil,
   112  		},
   113  		{
   114  			[]string{"a", "b"}, []string{"a"},
   115  			[]string{"a"}, []string{"b"}, nil,
   116  		},
   117  		{
   118  			[]string{"a"}, []string{"a", "c"},
   119  			[]string{"a"}, nil, []string{"c"},
   120  		},
   121  		{
   122  			[]string{"d"}, []string{"e"},
   123  			nil, []string{"d"}, []string{"e"},
   124  		},
   125  	} {
   126  		unchanged, added, removed := CompareStringSlices(c.sl, c.oldSl)
   127  		assert.Equal(t, c.unchanged, unchanged, "case %d", i)
   128  		assert.Equal(t, c.added, added, "case %d", i)
   129  		assert.Equal(t, c.removed, removed, "case %d", i)
   130  	}
   131  }
   132  
   133  func TestInsertToSortedStringSlice(t *testing.T) {
   134  	for i, c := range []struct {
   135  		Strings  []string
   136  		String   string
   137  		Expected []string
   138  	}{
   139  		{nil, "a", []string{"a"}},
   140  		{[]string{"b"}, "a", []string{"a", "b"}},
   141  		{[]string{"a", "c"}, "b", []string{"a", "b", "c"}},
   142  		{[]string{"a", "b"}, "d", []string{"a", "b", "d"}},
   143  	} {
   144  		assert.Equal(t, c.Expected, InsertToSortedStringSlice(c.Strings, c.String), "case %d", i)
   145  	}
   146  }
   147  
   148  func TestInsertToSortedBytesSlice(t *testing.T) {
   149  	for i, c := range []struct {
   150  		Bytes    [][]byte
   151  		Byte     []byte
   152  		Expected [][]byte
   153  	}{
   154  		{nil, []byte("a"), [][]byte{[]byte("a")}},
   155  		{[][]byte{[]byte("b")}, []byte("a"), [][]byte{[]byte("a"), []byte("b")}},
   156  		{[][]byte{[]byte("a"), []byte("c")}, []byte("b"), [][]byte{[]byte("a"), []byte("b"), []byte("c")}},
   157  		{[][]byte{[]byte("a"), []byte("b")}, []byte("d"), [][]byte{[]byte("a"), []byte("b"), []byte("d")}},
   158  	} {
   159  		assert.Equal(t, c.Expected, InsertToSortedBytesSlice(c.Bytes, c.Byte), "case %d", i)
   160  	}
   161  }