github.com/fafucoder/cilium@v1.6.11/pkg/set/set_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package set
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cilium/cilium/pkg/checker"
    23  
    24  	. "gopkg.in/check.v1"
    25  )
    26  
    27  // Hook up gocheck into the "go test" runner.
    28  func Test(t *testing.T) {
    29  	TestingT(t)
    30  }
    31  
    32  type SetTestSuite struct{}
    33  
    34  var _ = Suite(&SetTestSuite{})
    35  
    36  func (s *SetTestSuite) TestSliceSubsetOf(c *C) {
    37  	testCases := []struct {
    38  		sub          []string
    39  		main         []string
    40  		isSubset     bool
    41  		expectedDiff []string
    42  	}{
    43  		{
    44  			sub:          []string{"foo", "bar"},
    45  			main:         []string{"foo", "bar", "baz"},
    46  			isSubset:     true,
    47  			expectedDiff: nil,
    48  		},
    49  		{
    50  			sub:          []string{"foo", "bar"},
    51  			main:         []string{"foo", "bar"},
    52  			isSubset:     true,
    53  			expectedDiff: nil,
    54  		},
    55  		{
    56  			sub:          []string{"foo", "bar"},
    57  			main:         []string{"foo", "baz"},
    58  			isSubset:     false,
    59  			expectedDiff: []string{"bar"},
    60  		},
    61  		{
    62  			sub:          []string{"baz"},
    63  			main:         []string{"foo", "bar"},
    64  			isSubset:     false,
    65  			expectedDiff: []string{"baz"},
    66  		},
    67  		{
    68  			sub:          []string{"foo", "bar", "fizz"},
    69  			main:         []string{"fizz", "buzz"},
    70  			isSubset:     false,
    71  			expectedDiff: []string{"foo", "bar"},
    72  		},
    73  		{
    74  			sub:          []string{"foo", "foo", "bar"},
    75  			main:         []string{"foo", "bar"},
    76  			isSubset:     false,
    77  			expectedDiff: nil,
    78  		},
    79  		{
    80  			sub:          []string{"foo", "foo", "foo", "bar", "bar"},
    81  			main:         []string{"foo", "foo", "bar"},
    82  			isSubset:     false,
    83  			expectedDiff: nil,
    84  		},
    85  	}
    86  	for _, tc := range testCases {
    87  		isSubset, diff := SliceSubsetOf(tc.sub, tc.main)
    88  		c.Assert(isSubset, Equals, tc.isSubset)
    89  		c.Assert(diff, checker.DeepEquals, tc.expectedDiff)
    90  	}
    91  }