github.com/wangyougui/gf/v2@v2.6.5/container/gset/gset_z_unit_any_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/wangyougui/gf.
     6  
     7  // go test *.go
     8  
     9  package gset_test
    10  
    11  import (
    12  	"strings"
    13  	"sync"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/wangyougui/gf/v2/container/garray"
    18  	"github.com/wangyougui/gf/v2/container/gset"
    19  	"github.com/wangyougui/gf/v2/frame/g"
    20  	"github.com/wangyougui/gf/v2/internal/json"
    21  	"github.com/wangyougui/gf/v2/test/gtest"
    22  	"github.com/wangyougui/gf/v2/util/gconv"
    23  )
    24  
    25  func TestSet_Var(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		var s gset.Set
    28  		s.Add(1, 1, 2)
    29  		s.Add([]interface{}{3, 4}...)
    30  		t.Assert(s.Size(), 4)
    31  		t.AssertIN(1, s.Slice())
    32  		t.AssertIN(2, s.Slice())
    33  		t.AssertIN(3, s.Slice())
    34  		t.AssertIN(4, s.Slice())
    35  		t.AssertNI(0, s.Slice())
    36  		t.Assert(s.Contains(4), true)
    37  		t.Assert(s.Contains(5), false)
    38  		s.Remove(1)
    39  		t.Assert(s.Size(), 3)
    40  		s.Clear()
    41  		t.Assert(s.Size(), 0)
    42  	})
    43  }
    44  
    45  func TestSet_New(t *testing.T) {
    46  	gtest.C(t, func(t *gtest.T) {
    47  		s := gset.New()
    48  		s.Add(1, 1, 2)
    49  		s.Add([]interface{}{3, 4}...)
    50  		t.Assert(s.Size(), 4)
    51  		t.AssertIN(1, s.Slice())
    52  		t.AssertIN(2, s.Slice())
    53  		t.AssertIN(3, s.Slice())
    54  		t.AssertIN(4, s.Slice())
    55  		t.AssertNI(0, s.Slice())
    56  		t.Assert(s.Contains(4), true)
    57  		t.Assert(s.Contains(5), false)
    58  		s.Remove(1)
    59  		t.Assert(s.Size(), 3)
    60  		s.Clear()
    61  		t.Assert(s.Size(), 0)
    62  	})
    63  }
    64  
    65  func TestSet_Basic(t *testing.T) {
    66  	gtest.C(t, func(t *gtest.T) {
    67  		s := gset.NewSet()
    68  		s.Add(1, 1, 2)
    69  		s.Add([]interface{}{3, 4}...)
    70  		t.Assert(s.Size(), 4)
    71  		t.AssertIN(1, s.Slice())
    72  		t.AssertIN(2, s.Slice())
    73  		t.AssertIN(3, s.Slice())
    74  		t.AssertIN(4, s.Slice())
    75  		t.AssertNI(0, s.Slice())
    76  		t.Assert(s.Contains(4), true)
    77  		t.Assert(s.Contains(5), false)
    78  		s.Remove(1)
    79  		t.Assert(s.Size(), 3)
    80  		s.Clear()
    81  		t.Assert(s.Size(), 0)
    82  	})
    83  }
    84  
    85  func TestSet_Iterator(t *testing.T) {
    86  	gtest.C(t, func(t *gtest.T) {
    87  		s := gset.NewSet()
    88  		s.Add(1, 2, 3)
    89  		t.Assert(s.Size(), 3)
    90  
    91  		a1 := garray.New(true)
    92  		a2 := garray.New(true)
    93  		s.Iterator(func(v interface{}) bool {
    94  			a1.Append(1)
    95  			return false
    96  		})
    97  		s.Iterator(func(v interface{}) bool {
    98  			a2.Append(1)
    99  			return true
   100  		})
   101  		t.Assert(a1.Len(), 1)
   102  		t.Assert(a2.Len(), 3)
   103  	})
   104  }
   105  
   106  func TestSet_LockFunc(t *testing.T) {
   107  	gtest.C(t, func(t *gtest.T) {
   108  		s := gset.NewSet()
   109  		s.Add(1, 2, 3)
   110  		t.Assert(s.Size(), 3)
   111  		s.LockFunc(func(m map[interface{}]struct{}) {
   112  			delete(m, 1)
   113  		})
   114  		t.Assert(s.Size(), 2)
   115  		s.RLockFunc(func(m map[interface{}]struct{}) {
   116  			t.Assert(m, map[interface{}]struct{}{
   117  				3: {},
   118  				2: {},
   119  			})
   120  		})
   121  	})
   122  }
   123  
   124  func TestSet_Equal(t *testing.T) {
   125  	gtest.C(t, func(t *gtest.T) {
   126  		s1 := gset.NewSet()
   127  		s2 := gset.NewSet()
   128  		s3 := gset.NewSet()
   129  		s4 := gset.NewSet()
   130  		s1.Add(1, 2, 3)
   131  		s2.Add(1, 2, 3)
   132  		s3.Add(1, 2, 3, 4)
   133  		s4.Add(4, 5, 6)
   134  		t.Assert(s1.Equal(s2), true)
   135  		t.Assert(s1.Equal(s3), false)
   136  		t.Assert(s1.Equal(s4), false)
   137  		s5 := s1
   138  		t.Assert(s1.Equal(s5), true)
   139  	})
   140  }
   141  
   142  func TestSet_IsSubsetOf(t *testing.T) {
   143  	gtest.C(t, func(t *gtest.T) {
   144  		s1 := gset.NewSet()
   145  		s2 := gset.NewSet()
   146  		s3 := gset.NewSet()
   147  		s1.Add(1, 2)
   148  		s2.Add(1, 2, 3)
   149  		s3.Add(1, 2, 3, 4)
   150  		t.Assert(s1.IsSubsetOf(s2), true)
   151  		t.Assert(s2.IsSubsetOf(s3), true)
   152  		t.Assert(s1.IsSubsetOf(s3), true)
   153  		t.Assert(s2.IsSubsetOf(s1), false)
   154  		t.Assert(s3.IsSubsetOf(s2), false)
   155  
   156  		s4 := s1
   157  		t.Assert(s1.IsSubsetOf(s4), true)
   158  	})
   159  }
   160  
   161  func TestSet_Union(t *testing.T) {
   162  	gtest.C(t, func(t *gtest.T) {
   163  		s1 := gset.NewSet()
   164  		s2 := gset.NewSet()
   165  		s1.Add(1, 2)
   166  		s2.Add(3, 4)
   167  		s3 := s1.Union(s2)
   168  		t.Assert(s3.Contains(1), true)
   169  		t.Assert(s3.Contains(2), true)
   170  		t.Assert(s3.Contains(3), true)
   171  		t.Assert(s3.Contains(4), true)
   172  	})
   173  }
   174  
   175  func TestSet_Diff(t *testing.T) {
   176  	gtest.C(t, func(t *gtest.T) {
   177  		s1 := gset.NewSet()
   178  		s2 := gset.NewSet()
   179  		s1.Add(1, 2, 3)
   180  		s2.Add(3, 4, 5)
   181  		s3 := s1.Diff(s2)
   182  		t.Assert(s3.Contains(1), true)
   183  		t.Assert(s3.Contains(2), true)
   184  		t.Assert(s3.Contains(3), false)
   185  		t.Assert(s3.Contains(4), false)
   186  
   187  		s4 := s1
   188  		s5 := s1.Diff(s2, s4)
   189  		t.Assert(s5.Contains(1), true)
   190  		t.Assert(s5.Contains(2), true)
   191  		t.Assert(s5.Contains(3), false)
   192  		t.Assert(s5.Contains(4), false)
   193  	})
   194  }
   195  
   196  func TestSet_Intersect(t *testing.T) {
   197  	gtest.C(t, func(t *gtest.T) {
   198  		s1 := gset.NewSet()
   199  		s2 := gset.NewSet()
   200  		s1.Add(1, 2, 3)
   201  		s2.Add(3, 4, 5)
   202  		s3 := s1.Intersect(s2)
   203  		t.Assert(s3.Contains(1), false)
   204  		t.Assert(s3.Contains(2), false)
   205  		t.Assert(s3.Contains(3), true)
   206  		t.Assert(s3.Contains(4), false)
   207  	})
   208  }
   209  
   210  func TestSet_Complement(t *testing.T) {
   211  	gtest.C(t, func(t *gtest.T) {
   212  		s1 := gset.NewSet()
   213  		s2 := gset.NewSet()
   214  		s1.Add(1, 2, 3)
   215  		s2.Add(3, 4, 5)
   216  		s3 := s1.Complement(s2)
   217  		t.Assert(s3.Contains(1), false)
   218  		t.Assert(s3.Contains(2), false)
   219  		t.Assert(s3.Contains(4), true)
   220  		t.Assert(s3.Contains(5), true)
   221  	})
   222  }
   223  
   224  func TestNewFrom(t *testing.T) {
   225  	gtest.C(t, func(t *gtest.T) {
   226  		s1 := gset.NewFrom("a")
   227  		s2 := gset.NewFrom("b", false)
   228  		s3 := gset.NewFrom(3, true)
   229  		s4 := gset.NewFrom([]string{"s1", "s2"}, true)
   230  		t.Assert(s1.Contains("a"), true)
   231  		t.Assert(s2.Contains("b"), true)
   232  		t.Assert(s3.Contains(3), true)
   233  		t.Assert(s4.Contains("s1"), true)
   234  		t.Assert(s4.Contains("s3"), false)
   235  
   236  	})
   237  }
   238  
   239  func TestNew(t *testing.T) {
   240  	gtest.C(t, func(t *gtest.T) {
   241  		s1 := gset.New()
   242  		s1.Add("a", 2)
   243  		s2 := gset.New(true)
   244  		s2.Add("b", 3)
   245  		t.Assert(s1.Contains("a"), true)
   246  
   247  	})
   248  }
   249  
   250  func TestSet_Join(t *testing.T) {
   251  	gtest.C(t, func(t *gtest.T) {
   252  		s1 := gset.New(true)
   253  		s1.Add("a", "a1", "b", "c")
   254  		str1 := s1.Join(",")
   255  		t.Assert(strings.Contains(str1, "a1"), true)
   256  	})
   257  	gtest.C(t, func(t *gtest.T) {
   258  		s1 := gset.New(true)
   259  		s1.Add("a", `"b"`, `\c`)
   260  		str1 := s1.Join(",")
   261  		t.Assert(strings.Contains(str1, `"b"`), true)
   262  		t.Assert(strings.Contains(str1, `\c`), true)
   263  		t.Assert(strings.Contains(str1, `a`), true)
   264  	})
   265  	gtest.C(t, func(t *gtest.T) {
   266  		s1 := gset.Set{}
   267  		t.Assert(s1.Join(","), "")
   268  	})
   269  }
   270  
   271  func TestSet_String(t *testing.T) {
   272  	gtest.C(t, func(t *gtest.T) {
   273  		s1 := gset.New(true)
   274  		s1.Add("a", "a2", "b", "c")
   275  		str1 := s1.String()
   276  		t.Assert(strings.Contains(str1, "["), true)
   277  		t.Assert(strings.Contains(str1, "]"), true)
   278  		t.Assert(strings.Contains(str1, "a2"), true)
   279  
   280  		s1 = nil
   281  		t.Assert(s1.String(), "")
   282  
   283  		s2 := gset.New()
   284  		s2.Add(1)
   285  		t.Assert(s2.String(), "[1]")
   286  	})
   287  }
   288  
   289  func TestSet_Merge(t *testing.T) {
   290  	gtest.C(t, func(t *gtest.T) {
   291  		s1 := gset.New(true)
   292  		s2 := gset.New(true)
   293  		s1.Add("a", "a2", "b", "c")
   294  		s2.Add("b", "b1", "e", "f")
   295  		ss := s1.Merge(s2)
   296  		t.Assert(ss.Contains("a2"), true)
   297  		t.Assert(ss.Contains("b1"), true)
   298  
   299  	})
   300  }
   301  
   302  func TestSet_Sum(t *testing.T) {
   303  	gtest.C(t, func(t *gtest.T) {
   304  		s1 := gset.New(true)
   305  		s1.Add(1, 2, 3, 4)
   306  		t.Assert(s1.Sum(), int(10))
   307  
   308  	})
   309  }
   310  
   311  func TestSet_Pop(t *testing.T) {
   312  	gtest.C(t, func(t *gtest.T) {
   313  		s := gset.New(true)
   314  		t.Assert(s.Pop(), nil)
   315  		s.Add(1, 2, 3, 4)
   316  		t.Assert(s.Size(), 4)
   317  		t.AssertIN(s.Pop(), []int{1, 2, 3, 4})
   318  		t.Assert(s.Size(), 3)
   319  	})
   320  }
   321  
   322  func TestSet_Pops(t *testing.T) {
   323  	gtest.C(t, func(t *gtest.T) {
   324  		s := gset.New(true)
   325  		s.Add(1, 2, 3, 4)
   326  		t.Assert(s.Size(), 4)
   327  		t.Assert(s.Pops(0), nil)
   328  		t.AssertIN(s.Pops(1), []int{1, 2, 3, 4})
   329  		t.Assert(s.Size(), 3)
   330  		a := s.Pops(6)
   331  		t.Assert(len(a), 3)
   332  		t.AssertIN(a, []int{1, 2, 3, 4})
   333  		t.Assert(s.Size(), 0)
   334  	})
   335  
   336  	gtest.C(t, func(t *gtest.T) {
   337  		s := gset.New(true)
   338  		a := []interface{}{1, 2, 3, 4}
   339  		s.Add(a...)
   340  		t.Assert(s.Size(), 4)
   341  		t.Assert(s.Pops(-2), nil)
   342  		t.AssertIN(s.Pops(-1), a)
   343  	})
   344  }
   345  
   346  func TestSet_Json(t *testing.T) {
   347  	gtest.C(t, func(t *gtest.T) {
   348  		s1 := []interface{}{"a", "b", "d", "c"}
   349  		a1 := gset.NewFrom(s1)
   350  		b1, err1 := json.Marshal(a1)
   351  		b2, err2 := json.Marshal(s1)
   352  		t.Assert(len(b1), len(b2))
   353  		t.Assert(err1, err2)
   354  
   355  		a2 := gset.New()
   356  		err2 = json.UnmarshalUseNumber(b2, &a2)
   357  		t.Assert(err2, nil)
   358  		t.Assert(a2.Contains("a"), true)
   359  		t.Assert(a2.Contains("b"), true)
   360  		t.Assert(a2.Contains("c"), true)
   361  		t.Assert(a2.Contains("d"), true)
   362  		t.Assert(a2.Contains("e"), false)
   363  
   364  		var a3 gset.Set
   365  		err := json.UnmarshalUseNumber(b2, &a3)
   366  		t.AssertNil(err)
   367  		t.Assert(a3.Contains("a"), true)
   368  		t.Assert(a3.Contains("b"), true)
   369  		t.Assert(a3.Contains("c"), true)
   370  		t.Assert(a3.Contains("d"), true)
   371  		t.Assert(a3.Contains("e"), false)
   372  	})
   373  }
   374  
   375  func TestSet_AddIfNotExist(t *testing.T) {
   376  	gtest.C(t, func(t *gtest.T) {
   377  		s := gset.New(true)
   378  		s.Add(1)
   379  		t.Assert(s.Contains(1), true)
   380  		t.Assert(s.AddIfNotExist(1), false)
   381  		t.Assert(s.AddIfNotExist(2), true)
   382  		t.Assert(s.Contains(2), true)
   383  		t.Assert(s.AddIfNotExist(2), false)
   384  		t.Assert(s.AddIfNotExist(nil), false)
   385  		t.Assert(s.Contains(2), true)
   386  	})
   387  }
   388  
   389  func TestSet_AddIfNotExistFunc(t *testing.T) {
   390  	gtest.C(t, func(t *gtest.T) {
   391  		s := gset.New(true)
   392  		s.Add(1)
   393  		t.Assert(s.Contains(1), true)
   394  		t.Assert(s.Contains(2), false)
   395  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false)
   396  		t.Assert(s.Contains(2), false)
   397  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true)
   398  		t.Assert(s.Contains(2), true)
   399  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false)
   400  		t.Assert(s.Contains(2), true)
   401  		t.Assert(s.AddIfNotExistFunc(nil, func() bool { return false }), false)
   402  	})
   403  	gtest.C(t, func(t *gtest.T) {
   404  		s := gset.New(true)
   405  		wg := sync.WaitGroup{}
   406  		wg.Add(1)
   407  		go func() {
   408  			defer wg.Done()
   409  			r := s.AddIfNotExistFunc(1, func() bool {
   410  				time.Sleep(100 * time.Millisecond)
   411  				return true
   412  			})
   413  			t.Assert(r, false)
   414  		}()
   415  		s.Add(1)
   416  		wg.Wait()
   417  	})
   418  	gtest.C(t, func(t *gtest.T) {
   419  		s := gset.Set{}
   420  		t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true)
   421  	})
   422  }
   423  
   424  func TestSet_Walk(t *testing.T) {
   425  	gtest.C(t, func(t *gtest.T) {
   426  		var set gset.Set
   427  		set.Add(g.Slice{1, 2}...)
   428  		set.Walk(func(item interface{}) interface{} {
   429  			return gconv.Int(item) + 10
   430  		})
   431  		t.Assert(set.Size(), 2)
   432  		t.Assert(set.Contains(11), true)
   433  		t.Assert(set.Contains(12), true)
   434  	})
   435  }
   436  
   437  func TestSet_AddIfNotExistFuncLock(t *testing.T) {
   438  	gtest.C(t, func(t *gtest.T) {
   439  		s := gset.New(true)
   440  		wg := sync.WaitGroup{}
   441  		wg.Add(2)
   442  		go func() {
   443  			defer wg.Done()
   444  			r := s.AddIfNotExistFuncLock(1, func() bool {
   445  				time.Sleep(500 * time.Millisecond)
   446  				return true
   447  			})
   448  			t.Assert(r, true)
   449  		}()
   450  		time.Sleep(100 * time.Millisecond)
   451  		go func() {
   452  			defer wg.Done()
   453  			r := s.AddIfNotExistFuncLock(1, func() bool {
   454  				return true
   455  			})
   456  			t.Assert(r, false)
   457  		}()
   458  		wg.Wait()
   459  	})
   460  	gtest.C(t, func(t *gtest.T) {
   461  		s := gset.New(true)
   462  		t.Assert(s.AddIfNotExistFuncLock(nil, func() bool { return true }), false)
   463  		s1 := gset.Set{}
   464  		t.Assert(s1.AddIfNotExistFuncLock(1, func() bool { return true }), true)
   465  	})
   466  }
   467  
   468  func TestSet_UnmarshalValue(t *testing.T) {
   469  	type V struct {
   470  		Name string
   471  		Set  *gset.Set
   472  	}
   473  	// JSON
   474  	gtest.C(t, func(t *gtest.T) {
   475  		var v *V
   476  		err := gconv.Struct(g.Map{
   477  			"name": "john",
   478  			"set":  []byte(`["k1","k2","k3"]`),
   479  		}, &v)
   480  		t.AssertNil(err)
   481  		t.Assert(v.Name, "john")
   482  		t.Assert(v.Set.Size(), 3)
   483  		t.Assert(v.Set.Contains("k1"), true)
   484  		t.Assert(v.Set.Contains("k2"), true)
   485  		t.Assert(v.Set.Contains("k3"), true)
   486  		t.Assert(v.Set.Contains("k4"), false)
   487  	})
   488  	// Map
   489  	gtest.C(t, func(t *gtest.T) {
   490  		var v *V
   491  		err := gconv.Struct(g.Map{
   492  			"name": "john",
   493  			"set":  g.Slice{"k1", "k2", "k3"},
   494  		}, &v)
   495  		t.AssertNil(err)
   496  		t.Assert(v.Name, "john")
   497  		t.Assert(v.Set.Size(), 3)
   498  		t.Assert(v.Set.Contains("k1"), true)
   499  		t.Assert(v.Set.Contains("k2"), true)
   500  		t.Assert(v.Set.Contains("k3"), true)
   501  		t.Assert(v.Set.Contains("k4"), false)
   502  	})
   503  }
   504  
   505  func TestSet_DeepCopy(t *testing.T) {
   506  	gtest.C(t, func(t *gtest.T) {
   507  		set := gset.New()
   508  		set.Add(1, 2, 3)
   509  
   510  		copySet := set.DeepCopy().(*gset.Set)
   511  		copySet.Add(4)
   512  		t.AssertNE(set.Size(), copySet.Size())
   513  		t.AssertNE(set.String(), copySet.String())
   514  
   515  		set = nil
   516  		t.AssertNil(set.DeepCopy())
   517  	})
   518  }