github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/set/strset_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  package set
    16  
    17  import (
    18  	"reflect"
    19  	"sort"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestStrSet(t *testing.T) {
    26  	strSet := NewStrSet([]string{"a", "b", "c", "c", "d", "d", "d"})
    27  
    28  	if !isAsExpected(strSet, []string{"a", "b", "c", "d"}) {
    29  		t.Error("Set doesn't match expectation after creation", strSet.AsSlice())
    30  	}
    31  
    32  	strSet.Add("a")
    33  	strSet.Add("e")
    34  
    35  	if !isAsExpected(strSet, []string{"a", "b", "c", "d", "e"}) {
    36  		t.Error("Set doesn't match expectation after adds", strSet.AsSlice())
    37  	}
    38  
    39  	joinedStr := strSet.JoinStrings(",")
    40  
    41  	if joinedStr != "a,b,c,d,e" {
    42  		t.Error("JoinStrings failed to yield correct result:", joinedStr)
    43  	}
    44  
    45  	strSet.Remove("b", "d")
    46  
    47  	if !isAsExpected(strSet, []string{"a", "c", "e"}) {
    48  		t.Error("Set doesn't match expectation after removes", strSet.AsSlice())
    49  	}
    50  
    51  	strSet.Remove("non-existent string")
    52  
    53  	if !isAsExpected(strSet, []string{"a", "c", "e"}) {
    54  		t.Error("Set doesn't match expectation after noop remove", strSet.AsSlice())
    55  	}
    56  }
    57  
    58  // tests Size(), ContainsAll, Contains(), and AsSlice()
    59  func isAsExpected(strSet *StrSet, expected []string) bool {
    60  	if strSet.Size() != len(expected) {
    61  		return false
    62  	}
    63  
    64  	if !strSet.ContainsAll(expected) {
    65  		return false
    66  	}
    67  
    68  	if strSet.Contains("This should fail as it shouldn't be in the set") {
    69  		return false
    70  	}
    71  
    72  	actual := strSet.AsSlice()
    73  
    74  	sort.Strings(expected)
    75  	sort.Strings(actual)
    76  
    77  	return reflect.DeepEqual(actual, expected)
    78  }
    79  
    80  func TestUnique(t *testing.T) {
    81  	uStrs := Unique([]string{"a", "b", "b", "c", "c", "c"})
    82  
    83  	sort.Strings(uStrs)
    84  
    85  	if !reflect.DeepEqual(uStrs, []string{"a", "b", "c"}) {
    86  		t.Error(`Unique failed. expected: ["a", "b", "c"] actual:`, uStrs)
    87  	}
    88  }
    89  
    90  func TestIterateDifferent(t *testing.T) {
    91  	strSet1 := NewStrSet([]string{"a", "b", "c", "d"})
    92  	strSet2 := NewStrSet([]string{"e", "f", "g"})
    93  
    94  	strSet1.Iterate(func(s string) (cont bool) {
    95  		if strSet2.Contains(s) {
    96  			t.Error(s, " should not be in strSet2")
    97  		}
    98  
    99  		return true
   100  	})
   101  
   102  	if strSet1.ContainsAll(strSet2.AsSlice()) {
   103  		t.Error("strSet1 does not contain all or any of strSet2")
   104  	}
   105  }
   106  
   107  func TestEquality(t *testing.T) {
   108  	strSet1 := NewStrSet([]string{"a", "b", "c"})
   109  	strSet2 := NewStrSet([]string{"a", "b", "c", "b", "c", "c"})
   110  	assert.True(t, strSet1.Equals(strSet2))
   111  
   112  	strSet3 := NewStrSet([]string{"c", "b", "a"})
   113  	assert.True(t, strSet3.Equals(strSet1))
   114  	assert.True(t, strSet3.Equals(strSet2))
   115  
   116  	strSet3.Add("z")
   117  	assert.False(t, strSet3.Equals(strSet1))
   118  	assert.False(t, strSet3.Equals(strSet2))
   119  
   120  	strSet1.Remove("a")
   121  	assert.False(t, strSet1.Equals(strSet2))
   122  }