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