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