github.com/zhongdalu/gf@v1.0.0/g/container/garray/garray_z_unit_int_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 garray_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/util/gconv"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/zhongdalu/gf/g/container/garray"
    17  	"github.com/zhongdalu/gf/g/test/gtest"
    18  )
    19  
    20  func Test_IntArray_Basic(t *testing.T) {
    21  	gtest.Case(t, func() {
    22  		expect := []int{0, 1, 2, 3}
    23  		expect2 := []int{}
    24  		array := garray.NewIntArrayFrom(expect)
    25  		array2 := garray.NewIntArrayFrom(expect2)
    26  		gtest.Assert(array.Slice(), expect)
    27  		array.Set(0, 100)
    28  		gtest.Assert(array.Get(0), 100)
    29  		gtest.Assert(array.Get(1), 1)
    30  		gtest.Assert(array.Search(100), 0)
    31  		gtest.Assert(array2.Search(100), -1)
    32  		gtest.Assert(array.Contains(100), true)
    33  		gtest.Assert(array.Remove(0), 100)
    34  		gtest.Assert(array.Contains(100), false)
    35  		array.Append(4)
    36  		gtest.Assert(array.Len(), 4)
    37  		array.InsertBefore(0, 100)
    38  		array.InsertAfter(0, 200)
    39  		gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4})
    40  		array.InsertBefore(5, 300)
    41  		array.InsertAfter(6, 400)
    42  		gtest.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400})
    43  		gtest.Assert(array.Clear().Len(), 0)
    44  	})
    45  }
    46  
    47  func TestIntArray_Sort(t *testing.T) {
    48  	gtest.Case(t, func() {
    49  		expect1 := []int{0, 1, 2, 3}
    50  		expect2 := []int{3, 2, 1, 0}
    51  		array := garray.NewIntArray()
    52  		array2 := garray.NewIntArray(true)
    53  		for i := 3; i >= 0; i-- {
    54  			array.Append(i)
    55  			array2.Append(i)
    56  		}
    57  		array.Sort()
    58  		gtest.Assert(array.Slice(), expect1)
    59  		array.Sort(true)
    60  		gtest.Assert(array.Slice(), expect2)
    61  		gtest.Assert(array2.Slice(), expect2)
    62  	})
    63  }
    64  
    65  func TestIntArray_Unique(t *testing.T) {
    66  	gtest.Case(t, func() {
    67  		expect := []int{1, 1, 2, 3}
    68  		array := garray.NewIntArrayFrom(expect)
    69  		gtest.Assert(array.Unique().Slice(), []int{1, 2, 3})
    70  	})
    71  }
    72  
    73  func TestIntArray_PushAndPop(t *testing.T) {
    74  	gtest.Case(t, func() {
    75  		expect := []int{0, 1, 2, 3}
    76  		array := garray.NewIntArrayFrom(expect)
    77  		gtest.Assert(array.Slice(), expect)
    78  		gtest.Assert(array.PopLeft(), 0)
    79  		gtest.Assert(array.PopRight(), 3)
    80  		gtest.AssertIN(array.PopRand(), []int{1, 2})
    81  		gtest.AssertIN(array.PopRand(), []int{1, 2})
    82  		gtest.Assert(array.Len(), 0)
    83  		array.PushLeft(1).PushRight(2)
    84  		gtest.Assert(array.Slice(), []int{1, 2})
    85  	})
    86  }
    87  
    88  func TestIntArray_PopLeftsAndPopRights(t *testing.T) {
    89  	gtest.Case(t, func() {
    90  		value1 := []int{0, 1, 2, 3, 4, 5, 6}
    91  		value2 := []int{0, 1, 2, 3, 4, 5, 6}
    92  		array1 := garray.NewIntArrayFrom(value1)
    93  		array2 := garray.NewIntArrayFrom(value2)
    94  		gtest.Assert(array1.PopLefts(2), []int{0, 1})
    95  		gtest.Assert(array1.Slice(), []int{2, 3, 4, 5, 6})
    96  		gtest.Assert(array1.PopRights(2), []int{5, 6})
    97  		gtest.Assert(array1.Slice(), []int{2, 3, 4})
    98  		gtest.Assert(array1.PopRights(20), []int{2, 3, 4})
    99  		gtest.Assert(array1.Slice(), []int{})
   100  		gtest.Assert(array2.PopLefts(20), []int{0, 1, 2, 3, 4, 5, 6})
   101  		gtest.Assert(array2.Slice(), []int{})
   102  	})
   103  }
   104  
   105  func TestIntArray_Range(t *testing.T) {
   106  	gtest.Case(t, func() {
   107  		value1 := []int{0, 1, 2, 3, 4, 5, 6}
   108  		array1 := garray.NewIntArrayFrom(value1)
   109  		array2 := garray.NewIntArrayFrom(value1, true)
   110  		gtest.Assert(array1.Range(0, 1), []int{0})
   111  		gtest.Assert(array1.Range(1, 2), []int{1})
   112  		gtest.Assert(array1.Range(0, 2), []int{0, 1})
   113  		gtest.Assert(array1.Range(10, 2), nil)
   114  		gtest.Assert(array1.Range(-1, 10), value1)
   115  		gtest.Assert(array2.Range(1, 2), []int{1})
   116  	})
   117  }
   118  
   119  func TestIntArray_Merge(t *testing.T) {
   120  	gtest.Case(t, func() {
   121  		func1 := func(v1, v2 interface{}) int {
   122  			if gconv.Int(v1) < gconv.Int(v2) {
   123  				return 0
   124  			}
   125  			return 1
   126  		}
   127  
   128  		n1 := []int{0, 1, 2, 3}
   129  		n2 := []int{4, 5, 6, 7}
   130  		i1 := []interface{}{"1", "2"}
   131  		s1 := []string{"a", "b", "c"}
   132  		s2 := []string{"e", "f"}
   133  		a1 := garray.NewIntArrayFrom(n1)
   134  		a2 := garray.NewIntArrayFrom(n2)
   135  		a3 := garray.NewArrayFrom(i1)
   136  		a4 := garray.NewStringArrayFrom(s1)
   137  
   138  		a5 := garray.NewSortedStringArrayFrom(s2)
   139  		a6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
   140  
   141  		a7 := garray.NewSortedStringArrayFrom(s1)
   142  		a8 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1)
   143  
   144  		gtest.Assert(a1.Merge(a2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7})
   145  		gtest.Assert(a1.Merge(a3).Len(), 10)
   146  		gtest.Assert(a1.Merge(a4).Len(), 13)
   147  		gtest.Assert(a1.Merge(a5).Len(), 15)
   148  		gtest.Assert(a1.Merge(a6).Len(), 18)
   149  		gtest.Assert(a1.Merge(a7).Len(), 21)
   150  		gtest.Assert(a1.Merge(a8).Len(), 23)
   151  	})
   152  }
   153  
   154  func TestIntArray_Fill(t *testing.T) {
   155  	gtest.Case(t, func() {
   156  		a1 := []int{0}
   157  		a2 := []int{0}
   158  		array1 := garray.NewIntArrayFrom(a1)
   159  		array2 := garray.NewIntArrayFrom(a2)
   160  		gtest.Assert(array1.Fill(1, 2, 100).Slice(), []int{0, 100, 100})
   161  		gtest.Assert(array2.Fill(0, 2, 100).Slice(), []int{100, 100})
   162  		gtest.Assert(array2.Fill(-1, 2, 100).Slice(), []int{100, 100})
   163  	})
   164  }
   165  
   166  func TestIntArray_Chunk(t *testing.T) {
   167  	gtest.Case(t, func() {
   168  		a1 := []int{1, 2, 3, 4, 5}
   169  		array1 := garray.NewIntArrayFrom(a1)
   170  		chunks := array1.Chunk(2)
   171  		gtest.Assert(len(chunks), 3)
   172  		gtest.Assert(chunks[0], []int{1, 2})
   173  		gtest.Assert(chunks[1], []int{3, 4})
   174  		gtest.Assert(chunks[2], []int{5})
   175  		gtest.Assert(array1.Chunk(0), nil)
   176  	})
   177  }
   178  
   179  func TestIntArray_Pad(t *testing.T) {
   180  	gtest.Case(t, func() {
   181  		a1 := []int{0}
   182  		array1 := garray.NewIntArrayFrom(a1)
   183  		gtest.Assert(array1.Pad(3, 1).Slice(), []int{0, 1, 1})
   184  		gtest.Assert(array1.Pad(-4, 1).Slice(), []int{1, 0, 1, 1})
   185  		gtest.Assert(array1.Pad(3, 1).Slice(), []int{1, 0, 1, 1})
   186  	})
   187  }
   188  
   189  func TestIntArray_SubSlice(t *testing.T) {
   190  	gtest.Case(t, func() {
   191  		a1 := []int{0, 1, 2, 3, 4, 5, 6}
   192  		array1 := garray.NewIntArrayFrom(a1)
   193  		array2 := garray.NewIntArrayFrom(a1, true)
   194  		gtest.Assert(array1.SubSlice(6), []int{6})
   195  		gtest.Assert(array1.SubSlice(5), []int{5, 6})
   196  		gtest.Assert(array1.SubSlice(8), nil)
   197  		gtest.Assert(array1.SubSlice(0, 2), []int{0, 1})
   198  		gtest.Assert(array1.SubSlice(2, 2), []int{2, 3})
   199  		gtest.Assert(array1.SubSlice(5, 8), []int{5, 6})
   200  		gtest.Assert(array1.SubSlice(-1, 1), []int{6})
   201  		gtest.Assert(array1.SubSlice(-1, 9), []int{6})
   202  		gtest.Assert(array1.SubSlice(-2, 3), []int{5, 6})
   203  		gtest.Assert(array1.SubSlice(-7, 3), []int{0, 1, 2})
   204  		gtest.Assert(array1.SubSlice(-8, 3), nil)
   205  		gtest.Assert(array1.SubSlice(-1, -3), []int{3, 4, 5})
   206  		gtest.Assert(array1.SubSlice(-9, 3), nil)
   207  		gtest.Assert(array1.SubSlice(1, -1), []int{0})
   208  		gtest.Assert(array1.SubSlice(1, -3), nil)
   209  		gtest.Assert(array2.SubSlice(0, 2), []int{0, 1})
   210  	})
   211  }
   212  
   213  func TestIntArray_Rand(t *testing.T) {
   214  	gtest.Case(t, func() {
   215  		a1 := []int{0, 1, 2, 3, 4, 5, 6}
   216  		array1 := garray.NewIntArrayFrom(a1)
   217  		gtest.Assert(len(array1.Rands(2)), 2)
   218  		gtest.Assert(len(array1.Rands(10)), 7)
   219  		gtest.AssertIN(array1.Rands(1)[0], a1)
   220  		gtest.AssertIN(array1.Rand(), a1)
   221  	})
   222  }
   223  
   224  func TestIntArray_PopRands(t *testing.T) {
   225  	gtest.Case(t, func() {
   226  		a1 := []int{100, 200, 300, 400, 500, 600}
   227  		array := garray.NewIntArrayFrom(a1)
   228  		ns1 := array.PopRands(2)
   229  		gtest.AssertIN(ns1, []int{100, 200, 300, 400, 500, 600})
   230  		gtest.AssertIN(len(ns1), 2)
   231  
   232  		ns2 := array.PopRands(7)
   233  		gtest.AssertIN(len(ns2), 6)
   234  		gtest.AssertIN(ns2, []int{100, 200, 300, 400, 500, 600})
   235  	})
   236  }
   237  
   238  func TestIntArray_Shuffle(t *testing.T) {
   239  	gtest.Case(t, func() {
   240  		a1 := []int{0, 1, 2, 3, 4, 5, 6}
   241  		array1 := garray.NewIntArrayFrom(a1)
   242  		gtest.Assert(array1.Shuffle().Len(), 7)
   243  	})
   244  }
   245  
   246  func TestIntArray_Reverse(t *testing.T) {
   247  	gtest.Case(t, func() {
   248  		a1 := []int{0, 1, 2, 3, 4, 5, 6}
   249  		array1 := garray.NewIntArrayFrom(a1)
   250  		gtest.Assert(array1.Reverse().Slice(), []int{6, 5, 4, 3, 2, 1, 0})
   251  	})
   252  }
   253  
   254  func TestIntArray_Join(t *testing.T) {
   255  	gtest.Case(t, func() {
   256  		a1 := []int{0, 1, 2, 3, 4, 5, 6}
   257  		array1 := garray.NewIntArrayFrom(a1)
   258  		gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
   259  	})
   260  }
   261  
   262  func TestNewSortedIntArrayFrom(t *testing.T) {
   263  	gtest.Case(t, func() {
   264  		a1 := []int{0, 3, 2, 1, 4, 5, 6}
   265  		array1 := garray.NewSortedIntArrayFrom(a1, true)
   266  		gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
   267  		gtest.Assert(array1.Slice(), a1)
   268  	})
   269  }
   270  
   271  func TestNewSortedIntArrayFromCopy(t *testing.T) {
   272  	gtest.Case(t, func() {
   273  		a1 := []int{0, 5, 2, 1, 4, 3, 6}
   274  		array1 := garray.NewSortedIntArrayFromCopy(a1, false)
   275  		gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
   276  	})
   277  }
   278  
   279  func TestSortedIntArray_SetArray(t *testing.T) {
   280  	gtest.Case(t, func() {
   281  		a1 := []int{0, 1, 2, 3}
   282  		a2 := []int{4, 5, 6}
   283  		array1 := garray.NewSortedIntArrayFrom(a1)
   284  		array2 := array1.SetArray(a2)
   285  
   286  		gtest.Assert(array2.Len(), 3)
   287  		gtest.Assert(array2.Search(3), -1)
   288  		gtest.Assert(array2.Search(5), 1)
   289  		gtest.Assert(array2.Search(6), 2)
   290  	})
   291  }
   292  
   293  func TestSortedIntArray_Sort(t *testing.T) {
   294  	gtest.Case(t, func() {
   295  		a1 := []int{0, 3, 2, 1}
   296  
   297  		array1 := garray.NewSortedIntArrayFrom(a1)
   298  		array2 := array1.Sort()
   299  
   300  		gtest.Assert(array2.Len(), 4)
   301  		gtest.Assert(array2, []int{0, 1, 2, 3})
   302  	})
   303  }
   304  
   305  func TestSortedIntArray_Get(t *testing.T) {
   306  	gtest.Case(t, func() {
   307  		a1 := []int{1, 3, 5, 0}
   308  		array1 := garray.NewSortedIntArrayFrom(a1)
   309  		gtest.Assert(array1.Get(0), 0)
   310  		gtest.Assert(array1.Get(1), 1)
   311  		gtest.Assert(array1.Get(3), 5)
   312  	})
   313  }
   314  
   315  func TestSortedIntArray_Remove(t *testing.T) {
   316  	gtest.Case(t, func() {
   317  		a1 := []int{1, 3, 5, 0}
   318  		array1 := garray.NewSortedIntArrayFrom(a1)
   319  		i1 := array1.Remove(2)
   320  		gtest.Assert(i1, 3)
   321  		gtest.Assert(array1.Search(5), 2)
   322  
   323  		// 再次删除剩下的数组中的第一个
   324  		i2 := array1.Remove(0)
   325  		gtest.Assert(i2, 0)
   326  		gtest.Assert(array1.Search(5), 1)
   327  
   328  		a2 := []int{1, 3, 4}
   329  		array2 := garray.NewSortedIntArrayFrom(a2)
   330  		i3 := array2.Remove(1)
   331  		gtest.Assert(array2.Search(1), 0)
   332  		gtest.Assert(i3, 3)
   333  		i3 = array2.Remove(1)
   334  		gtest.Assert(array2.Search(4), -1)
   335  		gtest.Assert(i3, 4)
   336  	})
   337  }
   338  
   339  func TestSortedIntArray_PopLeft(t *testing.T) {
   340  	gtest.Case(t, func() {
   341  		a1 := []int{1, 3, 5, 2}
   342  		array1 := garray.NewSortedIntArrayFrom(a1)
   343  		i1 := array1.PopLeft()
   344  		gtest.Assert(i1, 1)
   345  		gtest.Assert(array1.Len(), 3)
   346  		gtest.Assert(array1.Search(1), -1)
   347  	})
   348  }
   349  
   350  func TestSortedIntArray_PopRight(t *testing.T) {
   351  	gtest.Case(t, func() {
   352  		a1 := []int{1, 3, 5, 2}
   353  		array1 := garray.NewSortedIntArrayFrom(a1)
   354  		i1 := array1.PopRight()
   355  		gtest.Assert(i1, 5)
   356  		gtest.Assert(array1.Len(), 3)
   357  		gtest.Assert(array1.Search(5), -1)
   358  	})
   359  }
   360  
   361  func TestSortedIntArray_PopRand(t *testing.T) {
   362  	gtest.Case(t, func() {
   363  		a1 := []int{1, 3, 5, 2}
   364  		array1 := garray.NewSortedIntArrayFrom(a1)
   365  		i1 := array1.PopRand()
   366  		gtest.Assert(array1.Len(), 3)
   367  		gtest.Assert(array1.Search(i1), -1)
   368  		gtest.AssertIN(i1, []int{1, 3, 5, 2})
   369  	})
   370  }
   371  
   372  func TestSortedIntArray_PopRands(t *testing.T) {
   373  	gtest.Case(t, func() {
   374  		a1 := []int{1, 3, 5, 2}
   375  		array1 := garray.NewSortedIntArrayFrom(a1)
   376  		ns1 := array1.PopRands(2)
   377  		gtest.Assert(array1.Len(), 2)
   378  		gtest.AssertIN(ns1, []int{1, 3, 5, 2})
   379  
   380  		a2 := []int{1, 3, 5, 2}
   381  		array2 := garray.NewSortedIntArrayFrom(a2)
   382  		ns2 := array2.PopRands(5)
   383  		gtest.Assert(array2.Len(), 0)
   384  		gtest.Assert(len(ns2), 4)
   385  		gtest.AssertIN(ns2, []int{1, 3, 5, 2})
   386  	})
   387  }
   388  
   389  func TestSortedIntArray_PopLefts(t *testing.T) {
   390  	gtest.Case(t, func() {
   391  		a1 := []int{1, 3, 5, 2}
   392  		array1 := garray.NewSortedIntArrayFrom(a1)
   393  		ns1 := array1.PopLefts(2)
   394  		gtest.Assert(array1.Len(), 2)
   395  		gtest.Assert(ns1, []int{1, 2})
   396  
   397  		a2 := []int{1, 3, 5, 2}
   398  		array2 := garray.NewSortedIntArrayFrom(a2)
   399  		ns2 := array2.PopLefts(5)
   400  		gtest.Assert(array2.Len(), 0)
   401  		gtest.AssertIN(ns2, []int{1, 3, 5, 2})
   402  	})
   403  }
   404  
   405  func TestSortedIntArray_PopRights(t *testing.T) {
   406  	gtest.Case(t, func() {
   407  		a1 := []int{1, 3, 5, 2}
   408  		array1 := garray.NewSortedIntArrayFrom(a1)
   409  		ns1 := array1.PopRights(2)
   410  		gtest.Assert(array1.Len(), 2)
   411  		gtest.Assert(ns1, []int{3, 5})
   412  
   413  		a2 := []int{1, 3, 5, 2}
   414  		array2 := garray.NewSortedIntArrayFrom(a2)
   415  		ns2 := array2.PopRights(5)
   416  		gtest.Assert(array2.Len(), 0)
   417  		gtest.AssertIN(ns2, []int{1, 3, 5, 2})
   418  	})
   419  }
   420  
   421  func TestSortedIntArray_Range(t *testing.T) {
   422  	gtest.Case(t, func() {
   423  		a1 := []int{1, 3, 5, 2, 6, 7}
   424  		array1 := garray.NewSortedIntArrayFrom(a1)
   425  		array2 := garray.NewSortedIntArrayFrom(a1, true)
   426  		ns1 := array1.Range(1, 4)
   427  		gtest.Assert(len(ns1), 3)
   428  		gtest.Assert(ns1, []int{2, 3, 5})
   429  
   430  		ns2 := array1.Range(5, 4)
   431  		gtest.Assert(len(ns2), 0)
   432  
   433  		ns3 := array1.Range(-1, 4)
   434  		gtest.Assert(len(ns3), 4)
   435  
   436  		nsl := array1.Range(5, 8)
   437  		gtest.Assert(len(nsl), 1)
   438  		gtest.Assert(array2.Range(1, 2), []int{2})
   439  	})
   440  }
   441  
   442  func TestSortedIntArray_Sum(t *testing.T) {
   443  	gtest.Case(t, func() {
   444  		a1 := []int{1, 3, 5}
   445  		array1 := garray.NewSortedIntArrayFrom(a1)
   446  		n1 := array1.Sum()
   447  		gtest.Assert(n1, 9)
   448  	})
   449  }
   450  
   451  func TestSortedIntArray_Contains(t *testing.T) {
   452  	gtest.Case(t, func() {
   453  		a1 := []int{1, 3, 5}
   454  		array1 := garray.NewSortedIntArrayFrom(a1)
   455  		gtest.Assert(array1.Contains(4), false)
   456  	})
   457  }
   458  
   459  func TestSortedIntArray_Clone(t *testing.T) {
   460  	gtest.Case(t, func() {
   461  		a1 := []int{1, 3, 5}
   462  		array1 := garray.NewSortedIntArrayFrom(a1)
   463  		array2 := array1.Clone()
   464  		gtest.Assert(array2.Len(), 3)
   465  		gtest.Assert(array2, array1)
   466  	})
   467  }
   468  
   469  func TestSortedIntArray_Clear(t *testing.T) {
   470  	gtest.Case(t, func() {
   471  		a1 := []int{1, 3, 5}
   472  		array1 := garray.NewSortedIntArrayFrom(a1)
   473  		array1.Clear()
   474  		gtest.Assert(array1.Len(), 0)
   475  	})
   476  }
   477  
   478  func TestSortedIntArray_Chunk(t *testing.T) {
   479  	gtest.Case(t, func() {
   480  		a1 := []int{1, 2, 3, 4, 5}
   481  		array1 := garray.NewSortedIntArrayFrom(a1)
   482  		ns1 := array1.Chunk(2) //按每几个元素切成一个数组
   483  		ns2 := array1.Chunk(-1)
   484  		gtest.Assert(len(ns1), 3)
   485  		gtest.Assert(ns1[0], []int{1, 2})
   486  		gtest.Assert(ns1[2], []int{5})
   487  		gtest.Assert(len(ns2), 0)
   488  	})
   489  }
   490  
   491  func TestSortedIntArray_SubSlice(t *testing.T) {
   492  	gtest.Case(t, func() {
   493  		a1 := []int{1, 2, 3, 4, 5}
   494  		array1 := garray.NewSortedIntArrayFrom(a1)
   495  		array2 := garray.NewSortedIntArrayFrom(a1, true)
   496  		ns1 := array1.SubSlice(1, 2)
   497  		gtest.Assert(len(ns1), 2)
   498  		gtest.Assert(ns1, []int{2, 3})
   499  
   500  		ns2 := array1.SubSlice(7, 2)
   501  		gtest.Assert(len(ns2), 0)
   502  
   503  		ns3 := array1.SubSlice(3, 5)
   504  		gtest.Assert(len(ns3), 2)
   505  		gtest.Assert(ns3, []int{4, 5})
   506  
   507  		ns4 := array1.SubSlice(3, 1)
   508  		gtest.Assert(len(ns4), 1)
   509  		gtest.Assert(ns4, []int{4})
   510  		gtest.Assert(array1.SubSlice(-1, 1), []int{5})
   511  		gtest.Assert(array1.SubSlice(-9, 1), nil)
   512  		gtest.Assert(array1.SubSlice(1, -9), nil)
   513  		gtest.Assert(array2.SubSlice(1, 2), []int{2, 3})
   514  	})
   515  }
   516  
   517  func TestSortedIntArray_Rand(t *testing.T) {
   518  	gtest.Case(t, func() {
   519  		a1 := []int{1, 2, 3, 4, 5}
   520  		array1 := garray.NewSortedIntArrayFrom(a1)
   521  		ns1 := array1.Rand() //按每几个元素切成一个数组
   522  		gtest.AssertIN(ns1, a1)
   523  	})
   524  }
   525  
   526  func TestSortedIntArray_Rands(t *testing.T) {
   527  	gtest.Case(t, func() {
   528  		a1 := []int{1, 2, 3, 4, 5}
   529  		array1 := garray.NewSortedIntArrayFrom(a1)
   530  		ns1 := array1.Rands(2) //按每几个元素切成一个数组
   531  		gtest.AssertIN(ns1, a1)
   532  		gtest.Assert(len(ns1), 2)
   533  
   534  		ns2 := array1.Rands(6) //按每几个元素切成一个数组
   535  		gtest.AssertIN(ns2, a1)
   536  		gtest.Assert(len(ns2), 5)
   537  	})
   538  }
   539  
   540  func TestSortedIntArray_CountValues(t *testing.T) {
   541  	gtest.Case(t, func() {
   542  		a1 := []int{1, 2, 3, 4, 5, 3}
   543  		array1 := garray.NewSortedIntArrayFrom(a1)
   544  		ns1 := array1.CountValues() //按每几个元素切成一个数组
   545  		gtest.Assert(len(ns1), 5)
   546  		gtest.Assert(ns1[2], 1)
   547  		gtest.Assert(ns1[3], 2)
   548  	})
   549  }
   550  
   551  func TestSortedIntArray_SetUnique(t *testing.T) {
   552  	gtest.Case(t, func() {
   553  		a1 := []int{1, 2, 3, 4, 5, 3}
   554  		array1 := garray.NewSortedIntArrayFrom(a1)
   555  		array1.SetUnique(true)
   556  		gtest.Assert(array1.Len(), 5)
   557  		gtest.Assert(array1, []int{1, 2, 3, 4, 5})
   558  	})
   559  }
   560  
   561  func TestIntArray_SetArray(t *testing.T) {
   562  	gtest.Case(t, func() {
   563  		a1 := []int{1, 2, 3, 5}
   564  		a2 := []int{6, 7}
   565  		array1 := garray.NewIntArrayFrom(a1)
   566  		array1.SetArray(a2)
   567  		gtest.Assert(array1.Len(), 2)
   568  		gtest.Assert(array1, []int{6, 7})
   569  	})
   570  }
   571  
   572  func TestIntArray_Replace(t *testing.T) {
   573  	gtest.Case(t, func() {
   574  		a1 := []int{1, 2, 3, 5}
   575  		a2 := []int{6, 7}
   576  		a3 := []int{9, 10, 11, 12, 13}
   577  		array1 := garray.NewIntArrayFrom(a1)
   578  		array1.Replace(a2)
   579  		gtest.Assert(array1, []int{6, 7, 3, 5})
   580  
   581  		array1.Replace(a3)
   582  		gtest.Assert(array1, []int{9, 10, 11, 12})
   583  	})
   584  }
   585  
   586  func TestIntArray_Clear(t *testing.T) {
   587  	gtest.Case(t, func() {
   588  		a1 := []int{1, 2, 3, 5}
   589  		array1 := garray.NewIntArrayFrom(a1)
   590  		array1.Clear()
   591  		gtest.Assert(array1.Len(), 0)
   592  	})
   593  }
   594  
   595  func TestIntArray_Clone(t *testing.T) {
   596  	gtest.Case(t, func() {
   597  		a1 := []int{1, 2, 3, 5}
   598  		array1 := garray.NewIntArrayFrom(a1)
   599  		array2 := array1.Clone()
   600  		gtest.Assert(array1, array2)
   601  	})
   602  }
   603  
   604  func TestArray_Get(t *testing.T) {
   605  	gtest.Case(t, func() {
   606  		a1 := []int{1, 2, 3, 5}
   607  		array1 := garray.NewIntArrayFrom(a1)
   608  		gtest.Assert(array1.Get(2), 3)
   609  		gtest.Assert(array1.Len(), 4)
   610  	})
   611  }
   612  
   613  func TestIntArray_Sum(t *testing.T) {
   614  	gtest.Case(t, func() {
   615  		a1 := []int{1, 2, 3, 5}
   616  		array1 := garray.NewIntArrayFrom(a1)
   617  		gtest.Assert(array1.Sum(), 11)
   618  	})
   619  }
   620  
   621  func TestIntArray_CountValues(t *testing.T) {
   622  	gtest.Case(t, func() {
   623  		a1 := []int{1, 2, 3, 5, 3}
   624  		array1 := garray.NewIntArrayFrom(a1)
   625  		m1 := array1.CountValues()
   626  		gtest.Assert(len(m1), 4)
   627  		gtest.Assert(m1[1], 1)
   628  		gtest.Assert(m1[3], 2)
   629  	})
   630  }
   631  
   632  func TestNewIntArrayFromCopy(t *testing.T) {
   633  	gtest.Case(t, func() {
   634  		a1 := []int{1, 2, 3, 5, 3}
   635  		array1 := garray.NewIntArrayFromCopy(a1)
   636  		gtest.Assert(array1.Len(), 5)
   637  		gtest.Assert(array1, a1)
   638  	})
   639  }
   640  
   641  func TestIntArray_Remove(t *testing.T) {
   642  	gtest.Case(t, func() {
   643  		a1 := []int{1, 2, 3, 5, 4}
   644  		array1 := garray.NewIntArrayFrom(a1)
   645  		n1 := array1.Remove(1)
   646  		gtest.Assert(n1, 2)
   647  		gtest.Assert(array1.Len(), 4)
   648  
   649  		n1 = array1.Remove(0)
   650  		gtest.Assert(n1, 1)
   651  		gtest.Assert(array1.Len(), 3)
   652  
   653  		n1 = array1.Remove(2)
   654  		gtest.Assert(n1, 4)
   655  		gtest.Assert(array1.Len(), 2)
   656  	})
   657  }
   658  
   659  func TestIntArray_LockFunc(t *testing.T) {
   660  	gtest.Case(t, func() {
   661  		s1 := []int{1, 2, 3, 4}
   662  		a1 := garray.NewIntArrayFrom(s1)
   663  
   664  		ch1 := make(chan int64, 3)
   665  		ch2 := make(chan int64, 3)
   666  		//go1
   667  		go a1.LockFunc(func(n1 []int) { //读写锁
   668  			time.Sleep(2 * time.Second) //暂停2秒
   669  			n1[2] = 6
   670  			ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   671  		})
   672  
   673  		//go2
   674  		go func() {
   675  			time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行.
   676  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   677  			a1.Len()
   678  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   679  		}()
   680  
   681  		t1 := <-ch1
   682  		t2 := <-ch1
   683  		<-ch2 //等待go1完成
   684  
   685  		// 防止ci抖动,以豪秒为单位
   686  		gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
   687  		gtest.Assert(a1.Contains(6), true)
   688  	})
   689  }
   690  
   691  func TestIntArray_SortFunc(t *testing.T) {
   692  	gtest.Case(t, func() {
   693  		s1 := []int{1, 4, 3, 2}
   694  		a1 := garray.NewIntArrayFrom(s1)
   695  		func1 := func(v1, v2 int) bool {
   696  			return v1 < v2
   697  		}
   698  		a11 := a1.SortFunc(func1)
   699  		gtest.Assert(a11, []int{1, 2, 3, 4})
   700  
   701  	})
   702  }
   703  
   704  func TestIntArray_RLockFunc(t *testing.T) {
   705  	gtest.Case(t, func() {
   706  		s1 := []int{1, 2, 3, 4}
   707  		a1 := garray.NewIntArrayFrom(s1)
   708  
   709  		ch1 := make(chan int64, 3)
   710  		ch2 := make(chan int64, 1)
   711  		//go1
   712  		go a1.RLockFunc(func(n1 []int) { //读锁
   713  			time.Sleep(2 * time.Second) //暂停1秒
   714  			n1[2] = 6
   715  			ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   716  		})
   717  
   718  		//go2
   719  		go func() {
   720  			time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行.
   721  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   722  			a1.Len()
   723  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   724  		}()
   725  
   726  		t1 := <-ch1
   727  		t2 := <-ch1
   728  		<-ch2 //等待go1完成
   729  
   730  		// 防止ci抖动,以豪秒为单位
   731  		gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
   732  		gtest.Assert(a1.Contains(6), true)
   733  	})
   734  }
   735  
   736  func TestSortedIntArray_LockFunc(t *testing.T) {
   737  	gtest.Case(t, func() {
   738  		s1 := []int{1, 2, 3, 4}
   739  		a1 := garray.NewSortedIntArrayFrom(s1)
   740  		ch1 := make(chan int64, 3)
   741  		ch2 := make(chan int64, 3)
   742  		//go1
   743  		go a1.LockFunc(func(n1 []int) { //读写锁
   744  			time.Sleep(2 * time.Second) //暂停2秒
   745  			n1[2] = 6
   746  			ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   747  		})
   748  
   749  		//go2
   750  		go func() {
   751  			time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行.
   752  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   753  			a1.Len()
   754  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   755  		}()
   756  
   757  		t1 := <-ch1
   758  		t2 := <-ch1
   759  		<-ch2 //等待go1完成
   760  
   761  		// 防止ci抖动,以豪秒为单位
   762  		gtest.AssertGT(t2-t1, 20) //go1加的读写互斥锁,所go2读的时候被阻塞。
   763  		gtest.Assert(a1.Contains(6), true)
   764  	})
   765  }
   766  
   767  func TestSortedIntArray_RLockFunc(t *testing.T) {
   768  	gtest.Case(t, func() {
   769  		s1 := []int{1, 2, 3, 4}
   770  		a1 := garray.NewSortedIntArrayFrom(s1)
   771  
   772  		ch1 := make(chan int64, 3)
   773  		ch2 := make(chan int64, 1)
   774  		//go1
   775  		go a1.RLockFunc(func(n1 []int) { //读锁
   776  			time.Sleep(2 * time.Second) //暂停1秒
   777  			n1[2] = 6
   778  			ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   779  		})
   780  
   781  		//go2
   782  		go func() {
   783  			time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行.
   784  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   785  			a1.Len()
   786  			ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
   787  		}()
   788  
   789  		t1 := <-ch1
   790  		t2 := <-ch1
   791  		<-ch2 //等待go1完成
   792  
   793  		// 防止ci抖动,以豪秒为单位
   794  		gtest.AssertLT(t2-t1, 20) //go1加的读锁,所go2读的时候,并没有阻塞。
   795  		gtest.Assert(a1.Contains(6), true)
   796  	})
   797  }
   798  
   799  func TestSortedIntArray_Merge(t *testing.T) {
   800  	gtest.Case(t, func() {
   801  		func1 := func(v1, v2 interface{}) int {
   802  			if gconv.Int(v1) < gconv.Int(v2) {
   803  				return 0
   804  			}
   805  			return 1
   806  		}
   807  		i0 := []int{1, 2, 3, 4}
   808  		s2 := []string{"e", "f"}
   809  		i1 := garray.NewIntArrayFrom([]int{1, 2, 3})
   810  		i2 := garray.NewArrayFrom([]interface{}{3})
   811  		s3 := garray.NewStringArrayFrom([]string{"g", "h"})
   812  		s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1)
   813  		s5 := garray.NewSortedStringArrayFrom(s2)
   814  		s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
   815  		a1 := garray.NewSortedIntArrayFrom(i0)
   816  
   817  		gtest.Assert(a1.Merge(s2).Len(), 6)
   818  		gtest.Assert(a1.Merge(i1).Len(), 9)
   819  		gtest.Assert(a1.Merge(i2).Len(), 10)
   820  		gtest.Assert(a1.Merge(s3).Len(), 12)
   821  		gtest.Assert(a1.Merge(s4).Len(), 14)
   822  		gtest.Assert(a1.Merge(s5).Len(), 16)
   823  		gtest.Assert(a1.Merge(s6).Len(), 19)
   824  	})
   825  }