github.com/gogf/gf/v2@v2.7.4/container/gset/gset_z_unit_int_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/gogf/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/gogf/gf/v2/container/garray"
    18  	"github.com/gogf/gf/v2/container/gset"
    19  	"github.com/gogf/gf/v2/frame/g"
    20  	"github.com/gogf/gf/v2/internal/json"
    21  	"github.com/gogf/gf/v2/test/gtest"
    22  	"github.com/gogf/gf/v2/util/gconv"
    23  )
    24  
    25  func TestIntSet_Var(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		var s gset.IntSet
    28  		s.Add(1, 1, 2)
    29  		s.Add([]int{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 TestIntSet_Basic(t *testing.T) {
    46  	gtest.C(t, func(t *gtest.T) {
    47  		s := gset.NewIntSet()
    48  		s.Add(1, 1, 2)
    49  		s.Add([]int{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 TestIntSet_Iterator_Deadlock(t *testing.T) {
    66  	gtest.C(t, func(t *gtest.T) {
    67  		set := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5}, true)
    68  		set.Iterator(func(k int) bool {
    69  			if k%2 == 0 {
    70  				set.Remove(k)
    71  			}
    72  			return true
    73  		})
    74  		t.Assert(set.Contains(1), true)
    75  		t.Assert(set.Contains(2), false)
    76  		t.Assert(set.Contains(3), true)
    77  		t.Assert(set.Contains(4), false)
    78  		t.Assert(set.Contains(5), true)
    79  	})
    80  }
    81  
    82  func TestIntSet_Iterator(t *testing.T) {
    83  	gtest.C(t, func(t *gtest.T) {
    84  		s := gset.NewIntSet()
    85  		s.Add(1, 2, 3)
    86  		t.Assert(s.Size(), 3)
    87  
    88  		a1 := garray.New(true)
    89  		a2 := garray.New(true)
    90  		s.Iterator(func(v int) bool {
    91  			a1.Append(1)
    92  			return false
    93  		})
    94  		s.Iterator(func(v int) bool {
    95  			a2.Append(1)
    96  			return true
    97  		})
    98  		t.Assert(a1.Len(), 1)
    99  		t.Assert(a2.Len(), 3)
   100  	})
   101  }
   102  
   103  func TestIntSet_LockFunc(t *testing.T) {
   104  	gtest.C(t, func(t *gtest.T) {
   105  		s := gset.NewIntSet()
   106  		s.Add(1, 2, 3)
   107  		t.Assert(s.Size(), 3)
   108  		s.LockFunc(func(m map[int]struct{}) {
   109  			delete(m, 1)
   110  		})
   111  		t.Assert(s.Size(), 2)
   112  		s.RLockFunc(func(m map[int]struct{}) {
   113  			t.Assert(m, map[int]struct{}{
   114  				3: {},
   115  				2: {},
   116  			})
   117  		})
   118  	})
   119  }
   120  
   121  func TestIntSet_Equal(t *testing.T) {
   122  	gtest.C(t, func(t *gtest.T) {
   123  		s1 := gset.NewIntSet()
   124  		s2 := gset.NewIntSet()
   125  		s3 := gset.NewIntSet()
   126  		s4 := gset.NewIntSet()
   127  		s1.Add(1, 2, 3)
   128  		s2.Add(1, 2, 3)
   129  		s3.Add(1, 2, 3, 4)
   130  		s4.Add(4, 5, 6)
   131  		t.Assert(s1.Equal(s2), true)
   132  		t.Assert(s1.Equal(s3), false)
   133  		t.Assert(s1.Equal(s4), false)
   134  		s5 := s1
   135  		t.Assert(s1.Equal(s5), true)
   136  	})
   137  }
   138  
   139  func TestIntSet_IsSubsetOf(t *testing.T) {
   140  	gtest.C(t, func(t *gtest.T) {
   141  		s1 := gset.NewIntSet()
   142  		s2 := gset.NewIntSet()
   143  		s3 := gset.NewIntSet()
   144  		s1.Add(1, 2)
   145  		s2.Add(1, 2, 3)
   146  		s3.Add(1, 2, 3, 4)
   147  		t.Assert(s1.IsSubsetOf(s2), true)
   148  		t.Assert(s2.IsSubsetOf(s3), true)
   149  		t.Assert(s1.IsSubsetOf(s3), true)
   150  		t.Assert(s2.IsSubsetOf(s1), false)
   151  		t.Assert(s3.IsSubsetOf(s2), false)
   152  
   153  		s4 := s1
   154  		t.Assert(s1.IsSubsetOf(s4), true)
   155  	})
   156  }
   157  
   158  func TestIntSet_Union(t *testing.T) {
   159  	gtest.C(t, func(t *gtest.T) {
   160  		s1 := gset.NewIntSet()
   161  		s2 := gset.NewIntSet()
   162  		s1.Add(1, 2)
   163  		s2.Add(3, 4)
   164  		s3 := s1.Union(s2)
   165  		t.Assert(s3.Contains(1), true)
   166  		t.Assert(s3.Contains(2), true)
   167  		t.Assert(s3.Contains(3), true)
   168  		t.Assert(s3.Contains(4), true)
   169  	})
   170  }
   171  
   172  func TestIntSet_Diff(t *testing.T) {
   173  	gtest.C(t, func(t *gtest.T) {
   174  		s1 := gset.NewIntSet()
   175  		s2 := gset.NewIntSet()
   176  		s1.Add(1, 2, 3)
   177  		s2.Add(3, 4, 5)
   178  		s3 := s1.Diff(s2)
   179  		t.Assert(s3.Contains(1), true)
   180  		t.Assert(s3.Contains(2), true)
   181  		t.Assert(s3.Contains(3), false)
   182  		t.Assert(s3.Contains(4), false)
   183  
   184  		s4 := s1
   185  		s5 := s1.Diff(s2, s4)
   186  		t.Assert(s5.Contains(1), true)
   187  		t.Assert(s5.Contains(2), true)
   188  		t.Assert(s5.Contains(3), false)
   189  		t.Assert(s5.Contains(4), false)
   190  	})
   191  }
   192  
   193  func TestIntSet_Intersect(t *testing.T) {
   194  	gtest.C(t, func(t *gtest.T) {
   195  		s1 := gset.NewIntSet()
   196  		s2 := gset.NewIntSet()
   197  		s1.Add(1, 2, 3)
   198  		s2.Add(3, 4, 5)
   199  		s3 := s1.Intersect(s2)
   200  		t.Assert(s3.Contains(1), false)
   201  		t.Assert(s3.Contains(2), false)
   202  		t.Assert(s3.Contains(3), true)
   203  		t.Assert(s3.Contains(4), false)
   204  	})
   205  }
   206  
   207  func TestIntSet_Complement(t *testing.T) {
   208  	gtest.C(t, func(t *gtest.T) {
   209  		s1 := gset.NewIntSet()
   210  		s2 := gset.NewIntSet()
   211  		s1.Add(1, 2, 3)
   212  		s2.Add(3, 4, 5)
   213  		s3 := s1.Complement(s2)
   214  		t.Assert(s3.Contains(1), false)
   215  		t.Assert(s3.Contains(2), false)
   216  		t.Assert(s3.Contains(4), true)
   217  		t.Assert(s3.Contains(5), true)
   218  	})
   219  }
   220  
   221  func TestIntSet_Size(t *testing.T) {
   222  	gtest.C(t, func(t *gtest.T) {
   223  		s1 := gset.NewIntSet(true)
   224  		s1.Add(1, 2, 3)
   225  		t.Assert(s1.Size(), 3)
   226  
   227  	})
   228  
   229  }
   230  
   231  func TestIntSet_Merge(t *testing.T) {
   232  	gtest.C(t, func(t *gtest.T) {
   233  		s1 := gset.NewIntSet()
   234  		s2 := gset.NewIntSet()
   235  		s1.Add(1, 2, 3)
   236  		s2.Add(3, 4, 5)
   237  		s3 := s1.Merge(s2)
   238  		t.Assert(s3.Contains(1), true)
   239  		t.Assert(s3.Contains(5), true)
   240  		t.Assert(s3.Contains(6), false)
   241  	})
   242  }
   243  
   244  func TestIntSet_Join(t *testing.T) {
   245  	gtest.C(t, func(t *gtest.T) {
   246  		s1 := gset.NewIntSet()
   247  		t.Assert(s1.Join(","), "")
   248  		s1.Add(1, 2, 3)
   249  		s3 := s1.Join(",")
   250  		t.Assert(strings.Contains(s3, "1"), true)
   251  		t.Assert(strings.Contains(s3, "2"), true)
   252  		t.Assert(strings.Contains(s3, "3"), true)
   253  	})
   254  }
   255  
   256  func TestIntSet_String(t *testing.T) {
   257  	gtest.C(t, func(t *gtest.T) {
   258  		s1 := gset.NewIntSet()
   259  		s1.Add(1, 2, 3)
   260  		s3 := s1.String()
   261  		t.Assert(strings.Contains(s3, "["), true)
   262  		t.Assert(strings.Contains(s3, "]"), true)
   263  		t.Assert(strings.Contains(s3, "1"), true)
   264  		t.Assert(strings.Contains(s3, "2"), true)
   265  		t.Assert(strings.Contains(s3, "3"), true)
   266  		s1 = nil
   267  		t.Assert(s1.String(), "")
   268  	})
   269  }
   270  
   271  func TestIntSet_Sum(t *testing.T) {
   272  	gtest.C(t, func(t *gtest.T) {
   273  		s1 := gset.NewIntSet()
   274  		s1.Add(1, 2, 3)
   275  		s2 := gset.NewIntSet()
   276  		s2.Add(5, 6, 7)
   277  		t.Assert(s2.Sum(), 18)
   278  
   279  	})
   280  
   281  }
   282  
   283  func TestIntSet_Pop(t *testing.T) {
   284  	gtest.C(t, func(t *gtest.T) {
   285  		s := gset.NewIntSet()
   286  		t.Assert(s.Pop(), 0)
   287  		s.Add(4, 2, 3)
   288  		t.Assert(s.Size(), 3)
   289  		t.AssertIN(s.Pop(), []int{4, 2, 3})
   290  		t.AssertIN(s.Pop(), []int{4, 2, 3})
   291  		t.Assert(s.Size(), 1)
   292  	})
   293  }
   294  
   295  func TestIntSet_Pops(t *testing.T) {
   296  	gtest.C(t, func(t *gtest.T) {
   297  		s := gset.NewIntSet()
   298  		s.Add(1, 4, 2, 3)
   299  		t.Assert(s.Size(), 4)
   300  		t.Assert(s.Pops(0), nil)
   301  		t.AssertIN(s.Pops(1), []int{1, 4, 2, 3})
   302  		t.Assert(s.Size(), 3)
   303  		a := s.Pops(2)
   304  		t.Assert(len(a), 2)
   305  		t.AssertIN(a, []int{1, 4, 2, 3})
   306  		t.Assert(s.Size(), 1)
   307  	})
   308  
   309  	gtest.C(t, func(t *gtest.T) {
   310  		s := gset.NewIntSet(true)
   311  		a := []int{1, 2, 3, 4}
   312  		s.Add(a...)
   313  		t.Assert(s.Size(), 4)
   314  		t.Assert(s.Pops(-2), nil)
   315  		t.AssertIN(s.Pops(-1), a)
   316  	})
   317  }
   318  
   319  func TestIntSet_AddIfNotExist(t *testing.T) {
   320  	gtest.C(t, func(t *gtest.T) {
   321  		s := gset.NewIntSet(true)
   322  		s.Add(1)
   323  		t.Assert(s.Contains(1), true)
   324  		t.Assert(s.AddIfNotExist(1), false)
   325  		t.Assert(s.AddIfNotExist(2), true)
   326  		t.Assert(s.Contains(2), true)
   327  		t.Assert(s.AddIfNotExist(2), false)
   328  		t.Assert(s.Contains(2), true)
   329  	})
   330  	gtest.C(t, func(t *gtest.T) {
   331  		s := gset.IntSet{}
   332  		t.Assert(s.AddIfNotExist(1), true)
   333  	})
   334  }
   335  
   336  func TestIntSet_AddIfNotExistFunc(t *testing.T) {
   337  	gtest.C(t, func(t *gtest.T) {
   338  		s := gset.NewIntSet(true)
   339  		s.Add(1)
   340  		t.Assert(s.Contains(1), true)
   341  		t.Assert(s.Contains(2), false)
   342  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return false }), false)
   343  		t.Assert(s.Contains(2), false)
   344  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), true)
   345  		t.Assert(s.Contains(2), true)
   346  		t.Assert(s.AddIfNotExistFunc(2, func() bool { return true }), false)
   347  		t.Assert(s.Contains(2), true)
   348  	})
   349  	gtest.C(t, func(t *gtest.T) {
   350  		s := gset.NewIntSet(true)
   351  		wg := sync.WaitGroup{}
   352  		wg.Add(1)
   353  		go func() {
   354  			defer wg.Done()
   355  			r := s.AddIfNotExistFunc(1, func() bool {
   356  				time.Sleep(100 * time.Millisecond)
   357  				return true
   358  			})
   359  			t.Assert(r, false)
   360  		}()
   361  		s.Add(1)
   362  		wg.Wait()
   363  	})
   364  	gtest.C(t, func(t *gtest.T) {
   365  		s := gset.IntSet{}
   366  		t.Assert(s.AddIfNotExistFunc(1, func() bool { return true }), true)
   367  	})
   368  }
   369  
   370  func TestIntSet_AddIfNotExistFuncLock(t *testing.T) {
   371  	gtest.C(t, func(t *gtest.T) {
   372  		s := gset.NewIntSet(true)
   373  		wg := sync.WaitGroup{}
   374  		wg.Add(2)
   375  		go func() {
   376  			defer wg.Done()
   377  			r := s.AddIfNotExistFuncLock(1, func() bool {
   378  				time.Sleep(500 * time.Millisecond)
   379  				return true
   380  			})
   381  			t.Assert(r, true)
   382  		}()
   383  		time.Sleep(100 * time.Millisecond)
   384  		go func() {
   385  			defer wg.Done()
   386  			r := s.AddIfNotExistFuncLock(1, func() bool {
   387  				return true
   388  			})
   389  			t.Assert(r, false)
   390  		}()
   391  		wg.Wait()
   392  	})
   393  	gtest.C(t, func(t *gtest.T) {
   394  		s := gset.IntSet{}
   395  		t.Assert(s.AddIfNotExistFuncLock(1, func() bool { return true }), true)
   396  	})
   397  }
   398  
   399  func TestIntSet_Json(t *testing.T) {
   400  	gtest.C(t, func(t *gtest.T) {
   401  		s1 := []int{1, 3, 2, 4}
   402  		a1 := gset.NewIntSetFrom(s1)
   403  		b1, err1 := json.Marshal(a1)
   404  		b2, err2 := json.Marshal(s1)
   405  		t.Assert(len(b1), len(b2))
   406  		t.Assert(err1, err2)
   407  
   408  		a2 := gset.NewIntSet()
   409  		err2 = json.UnmarshalUseNumber(b2, &a2)
   410  		t.Assert(err2, nil)
   411  		t.Assert(a2.Contains(1), true)
   412  		t.Assert(a2.Contains(2), true)
   413  		t.Assert(a2.Contains(3), true)
   414  		t.Assert(a2.Contains(4), true)
   415  		t.Assert(a2.Contains(5), false)
   416  
   417  		var a3 gset.IntSet
   418  		err := json.UnmarshalUseNumber(b2, &a3)
   419  		t.AssertNil(err)
   420  		t.Assert(a2.Contains(1), true)
   421  		t.Assert(a2.Contains(2), true)
   422  		t.Assert(a2.Contains(3), true)
   423  		t.Assert(a2.Contains(4), true)
   424  		t.Assert(a2.Contains(5), false)
   425  	})
   426  }
   427  
   428  func TestIntSet_Walk(t *testing.T) {
   429  	gtest.C(t, func(t *gtest.T) {
   430  		var set gset.IntSet
   431  		set.Add(g.SliceInt{1, 2}...)
   432  		set.Walk(func(item int) int {
   433  			return item + 10
   434  		})
   435  		t.Assert(set.Size(), 2)
   436  		t.Assert(set.Contains(11), true)
   437  		t.Assert(set.Contains(12), true)
   438  	})
   439  }
   440  
   441  func TestIntSet_UnmarshalValue(t *testing.T) {
   442  	type V struct {
   443  		Name string
   444  		Set  *gset.IntSet
   445  	}
   446  	// JSON
   447  	gtest.C(t, func(t *gtest.T) {
   448  		var v *V
   449  		err := gconv.Struct(g.Map{
   450  			"name": "john",
   451  			"set":  []byte(`[1,2,3]`),
   452  		}, &v)
   453  		t.AssertNil(err)
   454  		t.Assert(v.Name, "john")
   455  		t.Assert(v.Set.Size(), 3)
   456  		t.Assert(v.Set.Contains(1), true)
   457  		t.Assert(v.Set.Contains(2), true)
   458  		t.Assert(v.Set.Contains(3), true)
   459  		t.Assert(v.Set.Contains(4), false)
   460  	})
   461  	// Map
   462  	gtest.C(t, func(t *gtest.T) {
   463  		var v *V
   464  		err := gconv.Struct(g.Map{
   465  			"name": "john",
   466  			"set":  g.Slice{1, 2, 3},
   467  		}, &v)
   468  		t.AssertNil(err)
   469  		t.Assert(v.Name, "john")
   470  		t.Assert(v.Set.Size(), 3)
   471  		t.Assert(v.Set.Contains(1), true)
   472  		t.Assert(v.Set.Contains(2), true)
   473  		t.Assert(v.Set.Contains(3), true)
   474  		t.Assert(v.Set.Contains(4), false)
   475  	})
   476  }
   477  
   478  func TestIntSet_DeepCopy(t *testing.T) {
   479  	gtest.C(t, func(t *gtest.T) {
   480  		set := gset.NewIntSet()
   481  		set.Add(1, 2, 3)
   482  
   483  		copySet := set.DeepCopy().(*gset.IntSet)
   484  		copySet.Add(4)
   485  		t.AssertNE(set.Size(), copySet.Size())
   486  		t.AssertNE(set.String(), copySet.String())
   487  
   488  		set = nil
   489  		t.AssertNil(set.DeepCopy())
   490  	})
   491  }