github.com/zhongdalu/gf@v1.0.0/g/container/gset/gset_z_unit_string_test.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // go test *.go
     8  
     9  package gset_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/container/garray"
    13  	"github.com/zhongdalu/gf/g/container/gset"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestStringSet_Basic(t *testing.T) {
    20  	gtest.Case(t, func() {
    21  		s := gset.NewStringSet()
    22  		s.Add("1").Add("1").Add("2")
    23  		s.Add([]string{"3", "4"}...)
    24  		gtest.Assert(s.Size(), 4)
    25  		gtest.AssertIN("1", s.Slice())
    26  		gtest.AssertIN("2", s.Slice())
    27  		gtest.AssertIN("3", s.Slice())
    28  		gtest.AssertIN("4", s.Slice())
    29  		gtest.AssertNI("0", s.Slice())
    30  		gtest.Assert(s.Contains("4"), true)
    31  		gtest.Assert(s.Contains("5"), false)
    32  		s.Remove("1")
    33  		gtest.Assert(s.Size(), 3)
    34  		s.Clear()
    35  		gtest.Assert(s.Size(), 0)
    36  	})
    37  }
    38  
    39  func TestStringSet_Iterator(t *testing.T) {
    40  	gtest.Case(t, func() {
    41  		s := gset.NewStringSet()
    42  		s.Add("1").Add("2").Add("3")
    43  		gtest.Assert(s.Size(), 3)
    44  
    45  		a1 := garray.New()
    46  		a2 := garray.New()
    47  		s.Iterator(func(v string) bool {
    48  			a1.Append("1")
    49  			return false
    50  		})
    51  		s.Iterator(func(v string) bool {
    52  			a2.Append("1")
    53  			return true
    54  		})
    55  		gtest.Assert(a1.Len(), 1)
    56  		gtest.Assert(a2.Len(), 3)
    57  	})
    58  }
    59  
    60  func TestStringSet_LockFunc(t *testing.T) {
    61  	gtest.Case(t, func() {
    62  		s := gset.NewStringSet()
    63  		s.Add("1").Add("2").Add("3")
    64  		gtest.Assert(s.Size(), 3)
    65  		s.LockFunc(func(m map[string]struct{}) {
    66  			delete(m, "1")
    67  		})
    68  		gtest.Assert(s.Size(), 2)
    69  		s.RLockFunc(func(m map[string]struct{}) {
    70  			gtest.Assert(m, map[string]struct{}{
    71  				"3": struct{}{},
    72  				"2": struct{}{},
    73  			})
    74  		})
    75  	})
    76  }
    77  
    78  func TestStringSet_Equal(t *testing.T) {
    79  	gtest.Case(t, func() {
    80  		s1 := gset.NewStringSet()
    81  		s2 := gset.NewStringSet()
    82  		s3 := gset.NewStringSet()
    83  		s1.Add("1").Add("2").Add("3")
    84  		s2.Add("1").Add("2").Add("3")
    85  		s3.Add("1").Add("2").Add("3").Add("4")
    86  		gtest.Assert(s1.Equal(s2), true)
    87  		gtest.Assert(s1.Equal(s3), false)
    88  	})
    89  }
    90  
    91  func TestStringSet_IsSubsetOf(t *testing.T) {
    92  	gtest.Case(t, func() {
    93  		s1 := gset.NewStringSet()
    94  		s2 := gset.NewStringSet()
    95  		s3 := gset.NewStringSet()
    96  		s1.Add("1").Add("2")
    97  		s2.Add("1").Add("2").Add("3")
    98  		s3.Add("1").Add("2").Add("3").Add("4")
    99  		gtest.Assert(s1.IsSubsetOf(s2), true)
   100  		gtest.Assert(s2.IsSubsetOf(s3), true)
   101  		gtest.Assert(s1.IsSubsetOf(s3), true)
   102  		gtest.Assert(s2.IsSubsetOf(s1), false)
   103  		gtest.Assert(s3.IsSubsetOf(s2), false)
   104  	})
   105  }
   106  
   107  func TestStringSet_Union(t *testing.T) {
   108  	gtest.Case(t, func() {
   109  		s1 := gset.NewStringSet()
   110  		s2 := gset.NewStringSet()
   111  		s1.Add("1").Add("2")
   112  		s2.Add("3").Add("4")
   113  		s3 := s1.Union(s2)
   114  		gtest.Assert(s3.Contains("1"), true)
   115  		gtest.Assert(s3.Contains("2"), true)
   116  		gtest.Assert(s3.Contains("3"), true)
   117  		gtest.Assert(s3.Contains("4"), true)
   118  	})
   119  }
   120  
   121  func TestStringSet_Diff(t *testing.T) {
   122  	gtest.Case(t, func() {
   123  		s1 := gset.NewStringSet()
   124  		s2 := gset.NewStringSet()
   125  		s1.Add("1").Add("2").Add("3")
   126  		s2.Add("3").Add("4").Add("5")
   127  		s3 := s1.Diff(s2)
   128  		gtest.Assert(s3.Contains("1"), true)
   129  		gtest.Assert(s3.Contains("2"), true)
   130  		gtest.Assert(s3.Contains("3"), false)
   131  		gtest.Assert(s3.Contains("4"), false)
   132  	})
   133  }
   134  
   135  func TestStringSet_Intersect(t *testing.T) {
   136  	gtest.Case(t, func() {
   137  		s1 := gset.NewStringSet()
   138  		s2 := gset.NewStringSet()
   139  		s1.Add("1").Add("2").Add("3")
   140  		s2.Add("3").Add("4").Add("5")
   141  		s3 := s1.Intersect(s2)
   142  		gtest.Assert(s3.Contains("1"), false)
   143  		gtest.Assert(s3.Contains("2"), false)
   144  		gtest.Assert(s3.Contains("3"), true)
   145  		gtest.Assert(s3.Contains("4"), false)
   146  	})
   147  }
   148  
   149  func TestStringSet_Complement(t *testing.T) {
   150  	gtest.Case(t, func() {
   151  		s1 := gset.NewStringSet()
   152  		s2 := gset.NewStringSet()
   153  		s1.Add("1").Add("2").Add("3")
   154  		s2.Add("3").Add("4").Add("5")
   155  		s3 := s1.Complement(s2)
   156  		gtest.Assert(s3.Contains("1"), false)
   157  		gtest.Assert(s3.Contains("2"), false)
   158  		gtest.Assert(s3.Contains("4"), true)
   159  		gtest.Assert(s3.Contains("5"), true)
   160  	})
   161  }
   162  
   163  func TestNewIntSetFrom(t *testing.T) {
   164  	gtest.Case(t, func() {
   165  		s1 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
   166  		s2 := gset.NewIntSetFrom([]int{5, 6, 7, 8})
   167  		gtest.Assert(s1.Contains(3), true)
   168  		gtest.Assert(s1.Contains(5), false)
   169  		gtest.Assert(s2.Contains(3), false)
   170  		gtest.Assert(s2.Contains(5), true)
   171  	})
   172  }
   173  
   174  func TestStringSet_Merge(t *testing.T) {
   175  	gtest.Case(t, func() {
   176  		s1 := gset.NewStringSet()
   177  		s2 := gset.NewStringSet()
   178  		s1.Add("1").Add("2").Add("3")
   179  		s2.Add("3").Add("4").Add("5")
   180  		s3 := s1.Merge(s2)
   181  		gtest.Assert(s3.Contains("1"), true)
   182  		gtest.Assert(s3.Contains("6"), false)
   183  		gtest.Assert(s3.Contains("4"), true)
   184  		gtest.Assert(s3.Contains("5"), true)
   185  	})
   186  }
   187  
   188  func TestNewStringSetFrom(t *testing.T) {
   189  	gtest.Case(t, func() {
   190  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   191  		gtest.Assert(s1.Contains("b"), true)
   192  		gtest.Assert(s1.Contains("d"), false)
   193  	})
   194  }
   195  
   196  func TestStringSet_Join(t *testing.T) {
   197  	s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   198  	str1 := s1.Join(",")
   199  	gtest.Assert(strings.Contains(str1, "b"), true)
   200  	gtest.Assert(strings.Contains(str1, "d"), false)
   201  }
   202  
   203  func TestStringSet_String(t *testing.T) {
   204  	gtest.Case(t, func() {
   205  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   206  		str1 := s1.String()
   207  		gtest.Assert(strings.Contains(str1, "b"), true)
   208  		gtest.Assert(strings.Contains(str1, "d"), false)
   209  	})
   210  
   211  }
   212  
   213  func TestStringSet_Sum(t *testing.T) {
   214  	gtest.Case(t, func() {
   215  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   216  		s2 := gset.NewIntSetFrom([]int{2, 3, 4}, true)
   217  		gtest.Assert(s1.Sum(), 0)
   218  		gtest.Assert(s2.Sum(), 9)
   219  	})
   220  }
   221  
   222  func TestStringSet_Size(t *testing.T) {
   223  	gtest.Case(t, func() {
   224  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   225  		gtest.Assert(s1.Size(), 3)
   226  
   227  	})
   228  }
   229  
   230  func TestStringSet_Remove(t *testing.T) {
   231  	gtest.Case(t, func() {
   232  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   233  		s1 = s1.Remove("b")
   234  		gtest.Assert(s1.Contains("b"), false)
   235  		gtest.Assert(s1.Contains("c"), true)
   236  	})
   237  }
   238  
   239  func TestStringSet_Pop(t *testing.T) {
   240  	gtest.Case(t, func() {
   241  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   242  		str1 := s1.Pop(1)
   243  		gtest.Assert(strings.Contains("a,b,c", str1), true)
   244  	})
   245  }
   246  
   247  func TestStringSet_Pops(t *testing.T) {
   248  	gtest.Case(t, func() {
   249  		s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true)
   250  		strs1 := s1.Pops(2)
   251  		gtest.AssertIN(strs1, []string{"a", "b", "c"})
   252  		gtest.Assert(len(strs1), 2)
   253  		str2 := s1.Pops(7)
   254  		gtest.AssertIN(str2, []string{"a", "b", "c"})
   255  	})
   256  }