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