2021-01-17 21:46:25 +08:00
|
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-29 20:47:59 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
// go test *.go
|
|
|
|
|
|
|
|
|
|
package garray_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
|
"github.com/gogf/gf/v2/container/garray"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2021-10-11 21:41:56 +08:00
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-09-29 20:47:59 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_Array_Basic(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
expect := []interface{}{0, 1, 2, 3}
|
|
|
|
|
array := garray.NewArrayFrom(expect)
|
|
|
|
|
array2 := garray.NewArrayFrom(expect)
|
|
|
|
|
array3 := garray.NewArrayFrom([]interface{}{})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), expect)
|
|
|
|
|
t.Assert(array.Interfaces(), expect)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.Set(0, 100)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
|
|
|
|
|
v, ok := array.Get(0)
|
|
|
|
|
t.Assert(v, 100)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array.Get(1)
|
|
|
|
|
t.Assert(v, 1)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Search(100), 0)
|
|
|
|
|
t.Assert(array3.Search(100), -1)
|
|
|
|
|
t.Assert(array.Contains(100), true)
|
|
|
|
|
|
2020-03-30 20:31:47 +08:00
|
|
|
|
v, ok = array.Remove(0)
|
|
|
|
|
t.Assert(v, 100)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array.Remove(-1)
|
|
|
|
|
t.Assert(v, nil)
|
|
|
|
|
t.Assert(ok, false)
|
|
|
|
|
|
|
|
|
|
v, ok = array.Remove(100000)
|
|
|
|
|
t.Assert(v, nil)
|
|
|
|
|
t.Assert(ok, false)
|
|
|
|
|
|
|
|
|
|
v, ok = array2.Remove(3)
|
|
|
|
|
t.Assert(v, 3)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array2.Remove(1)
|
|
|
|
|
t.Assert(v, 1)
|
|
|
|
|
t.Assert(ok, true)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
|
|
|
|
|
t.Assert(array.Contains(100), false)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.Append(4)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Len(), 4)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.InsertBefore(0, 100)
|
|
|
|
|
array.InsertAfter(0, 200)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 4})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.InsertBefore(5, 300)
|
|
|
|
|
array.InsertAfter(6, 400)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 300, 4, 400})
|
|
|
|
|
t.Assert(array.Clear().Len(), 0)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Sort(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
expect1 := []interface{}{0, 1, 2, 3}
|
|
|
|
|
expect2 := []interface{}{3, 2, 1, 0}
|
|
|
|
|
array := garray.NewArray()
|
|
|
|
|
for i := 3; i >= 0; i-- {
|
|
|
|
|
array.Append(i)
|
|
|
|
|
}
|
|
|
|
|
array.SortFunc(func(v1, v2 interface{}) bool {
|
|
|
|
|
return v1.(int) < v2.(int)
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), expect1)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.SortFunc(func(v1, v2 interface{}) bool {
|
|
|
|
|
return v1.(int) > v2.(int)
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), expect2)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Unique(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-07-10 22:26:02 +08:00
|
|
|
|
expect := []interface{}{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array := garray.NewArrayFrom(expect)
|
2020-07-10 22:26:02 +08:00
|
|
|
|
t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3, 4, 5})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_PushAndPop(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
expect := []interface{}{0, 1, 2, 3}
|
|
|
|
|
array := garray.NewArrayFrom(expect)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), expect)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
|
|
|
|
|
v, ok := array.PopLeft()
|
|
|
|
|
t.Assert(v, 0)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array.PopRight()
|
|
|
|
|
t.Assert(v, 3)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array.PopRand()
|
|
|
|
|
t.AssertIN(v, []interface{}{1, 2})
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
|
|
|
|
v, ok = array.PopRand()
|
|
|
|
|
t.AssertIN(v, []interface{}{1, 2})
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Len(), 0)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array.PushLeft(1).PushRight(2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.Slice(), []interface{}{1, 2})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_PopRands(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{100, 200, 300, 400, 500, 600}
|
|
|
|
|
array := garray.NewFromCopy(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.AssertIN(array.PopRands(2), []interface{}{100, 200, 300, 400, 500, 600})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-08 21:08:06 +08:00
|
|
|
|
func TestArray_PopLeft(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.NewFrom(g.Slice{1, 2, 3})
|
|
|
|
|
v, ok := array.PopLeft()
|
|
|
|
|
t.Assert(v, 1)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 2)
|
|
|
|
|
v, ok = array.PopLeft()
|
|
|
|
|
t.Assert(v, 2)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 1)
|
|
|
|
|
v, ok = array.PopLeft()
|
|
|
|
|
t.Assert(v, 3)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 0)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_PopRight(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.NewFrom(g.Slice{1, 2, 3})
|
|
|
|
|
|
|
|
|
|
v, ok := array.PopRight()
|
|
|
|
|
t.Assert(v, 3)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 2)
|
|
|
|
|
|
|
|
|
|
v, ok = array.PopRight()
|
|
|
|
|
t.Assert(v, 2)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 1)
|
|
|
|
|
|
|
|
|
|
v, ok = array.PopRight()
|
|
|
|
|
t.Assert(v, 1)
|
|
|
|
|
t.Assert(ok, true)
|
|
|
|
|
t.Assert(array.Len(), 0)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_PopLefts(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.NewFrom(g.Slice{1, 2, 3})
|
|
|
|
|
t.Assert(array.PopLefts(2), g.Slice{1, 2})
|
|
|
|
|
t.Assert(array.Len(), 1)
|
|
|
|
|
t.Assert(array.PopLefts(2), g.Slice{3})
|
|
|
|
|
t.Assert(array.Len(), 0)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_PopRights(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.NewFrom(g.Slice{1, 2, 3})
|
|
|
|
|
t.Assert(array.PopRights(2), g.Slice{2, 3})
|
|
|
|
|
t.Assert(array.Len(), 1)
|
|
|
|
|
t.Assert(array.PopLefts(2), g.Slice{1})
|
|
|
|
|
t.Assert(array.Len(), 0)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 20:47:59 +08:00
|
|
|
|
func TestArray_PopLeftsAndPopRights(t *testing.T) {
|
2020-03-25 23:36:56 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.New()
|
2020-03-30 20:31:47 +08:00
|
|
|
|
v, ok := array.PopLeft()
|
|
|
|
|
t.Assert(v, nil)
|
|
|
|
|
t.Assert(ok, false)
|
2020-03-25 23:36:56 +08:00
|
|
|
|
t.Assert(array.PopLefts(10), nil)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
|
|
|
|
|
v, ok = array.PopRight()
|
|
|
|
|
t.Assert(v, nil)
|
|
|
|
|
t.Assert(ok, false)
|
2020-03-25 23:36:56 +08:00
|
|
|
|
t.Assert(array.PopRights(10), nil)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
|
|
|
|
|
v, ok = array.PopRand()
|
|
|
|
|
t.Assert(v, nil)
|
|
|
|
|
t.Assert(ok, false)
|
2020-03-25 23:36:56 +08:00
|
|
|
|
t.Assert(array.PopRands(10), nil)
|
|
|
|
|
})
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
value1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
value2 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(value1)
|
|
|
|
|
array2 := garray.NewArrayFrom(value2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.PopLefts(2), []interface{}{0, 1})
|
|
|
|
|
t.Assert(array1.Slice(), []interface{}{2, 3, 4, 5, 6})
|
|
|
|
|
t.Assert(array1.PopRights(2), []interface{}{5, 6})
|
|
|
|
|
t.Assert(array1.Slice(), []interface{}{2, 3, 4})
|
|
|
|
|
t.Assert(array1.PopRights(20), []interface{}{2, 3, 4})
|
|
|
|
|
t.Assert(array1.Slice(), []interface{}{})
|
|
|
|
|
t.Assert(array2.PopLefts(20), []interface{}{0, 1, 2, 3, 4, 5, 6})
|
|
|
|
|
t.Assert(array2.Slice(), []interface{}{})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Range(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
value1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(value1)
|
|
|
|
|
array2 := garray.NewArrayFrom(value1, true)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Range(0, 1), []interface{}{0})
|
|
|
|
|
t.Assert(array1.Range(1, 2), []interface{}{1})
|
|
|
|
|
t.Assert(array1.Range(0, 2), []interface{}{0, 1})
|
|
|
|
|
t.Assert(array1.Range(-1, 10), value1)
|
|
|
|
|
t.Assert(array1.Range(10, 2), nil)
|
|
|
|
|
t.Assert(array2.Range(1, 3), []interface{}{1, 2})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Merge(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
func1 := func(v1, v2 interface{}) int {
|
|
|
|
|
if gconv.Int(v1) < gconv.Int(v2) {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i1 := []interface{}{0, 1, 2, 3}
|
|
|
|
|
i2 := []interface{}{4, 5, 6, 7}
|
|
|
|
|
array1 := garray.NewArrayFrom(i1)
|
|
|
|
|
array2 := garray.NewArrayFrom(i2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
|
// s1 := []string{"a", "b", "c", "d"}
|
2019-09-29 20:47:59 +08:00
|
|
|
|
s2 := []string{"e", "f"}
|
|
|
|
|
i3 := garray.NewIntArrayFrom([]int{1, 2, 3})
|
|
|
|
|
i4 := garray.NewArrayFrom([]interface{}{3})
|
|
|
|
|
s3 := garray.NewStrArrayFrom([]string{"g", "h"})
|
|
|
|
|
s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1)
|
|
|
|
|
s5 := garray.NewSortedStrArrayFrom(s2)
|
|
|
|
|
s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3})
|
|
|
|
|
a1 := garray.NewArrayFrom(i1)
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(a1.Merge(s2).Len(), 6)
|
|
|
|
|
t.Assert(a1.Merge(i3).Len(), 9)
|
|
|
|
|
t.Assert(a1.Merge(i4).Len(), 10)
|
|
|
|
|
t.Assert(a1.Merge(s3).Len(), 12)
|
|
|
|
|
t.Assert(a1.Merge(s4).Len(), 14)
|
|
|
|
|
t.Assert(a1.Merge(s5).Len(), 16)
|
|
|
|
|
t.Assert(a1.Merge(s6).Len(), 19)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Fill(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0}
|
|
|
|
|
a2 := []interface{}{0}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := garray.NewArrayFrom(a2, true)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
|
|
|
|
|
t.Assert(array1.Fill(1, 2, 100), nil)
|
|
|
|
|
t.Assert(array1.Slice(), []interface{}{0, 100, 100})
|
|
|
|
|
|
|
|
|
|
t.Assert(array2.Fill(0, 2, 100), nil)
|
|
|
|
|
t.Assert(array2.Slice(), []interface{}{100, 100})
|
|
|
|
|
|
|
|
|
|
t.AssertNE(array2.Fill(-1, 2, 100), nil)
|
|
|
|
|
t.Assert(array2.Slice(), []interface{}{100, 100})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Chunk(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{1, 2, 3, 4, 5}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
chunks := array1.Chunk(2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(chunks), 3)
|
|
|
|
|
t.Assert(chunks[0], []interface{}{1, 2})
|
|
|
|
|
t.Assert(chunks[1], []interface{}{3, 4})
|
|
|
|
|
t.Assert(chunks[2], []interface{}{5})
|
|
|
|
|
t.Assert(array1.Chunk(0), nil)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-25 23:19:37 +08:00
|
|
|
|
a1 := []interface{}{1, 2, 3, 4, 5}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
chunks := array1.Chunk(3)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(chunks), 2)
|
|
|
|
|
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
|
|
|
|
t.Assert(chunks[1], []interface{}{4, 5})
|
|
|
|
|
t.Assert(array1.Chunk(0), nil)
|
2020-02-25 23:19:37 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-25 23:19:37 +08:00
|
|
|
|
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
chunks := array1.Chunk(2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(chunks), 3)
|
|
|
|
|
t.Assert(chunks[0], []interface{}{1, 2})
|
|
|
|
|
t.Assert(chunks[1], []interface{}{3, 4})
|
|
|
|
|
t.Assert(chunks[2], []interface{}{5, 6})
|
|
|
|
|
t.Assert(array1.Chunk(0), nil)
|
2020-02-25 23:19:37 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-25 23:19:37 +08:00
|
|
|
|
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
chunks := array1.Chunk(3)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(chunks), 2)
|
|
|
|
|
t.Assert(chunks[0], []interface{}{1, 2, 3})
|
|
|
|
|
t.Assert(chunks[1], []interface{}{4, 5, 6})
|
|
|
|
|
t.Assert(array1.Chunk(0), nil)
|
2020-02-25 23:19:37 +08:00
|
|
|
|
})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Pad(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Pad(3, 1).Slice(), []interface{}{0, 1, 1})
|
|
|
|
|
t.Assert(array1.Pad(-4, 1).Slice(), []interface{}{1, 0, 1, 1})
|
|
|
|
|
t.Assert(array1.Pad(3, 1).Slice(), []interface{}{1, 0, 1, 1})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_SubSlice(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := garray.NewArrayFrom(a1, true)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.SubSlice(0, 2), []interface{}{0, 1})
|
|
|
|
|
t.Assert(array1.SubSlice(2, 2), []interface{}{2, 3})
|
|
|
|
|
t.Assert(array1.SubSlice(5, 8), []interface{}{5, 6})
|
|
|
|
|
t.Assert(array1.SubSlice(9, 1), nil)
|
|
|
|
|
t.Assert(array1.SubSlice(-2, 2), []interface{}{5, 6})
|
|
|
|
|
t.Assert(array1.SubSlice(-9, 2), nil)
|
|
|
|
|
t.Assert(array1.SubSlice(1, -2), nil)
|
|
|
|
|
t.Assert(array2.SubSlice(0, 2), []interface{}{0, 1})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Rand(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(array1.Rands(2)), 2)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
t.Assert(len(array1.Rands(10)), 10)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.AssertIN(array1.Rands(1)[0], a1)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
s1 := []interface{}{"a", "b", "c", "d"}
|
|
|
|
|
a1 := garray.NewArrayFrom(s1)
|
2020-03-30 20:31:47 +08:00
|
|
|
|
i1, ok := a1.Rand()
|
|
|
|
|
t.Assert(ok, true)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(a1.Contains(i1), true)
|
|
|
|
|
t.Assert(a1.Len(), 4)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Shuffle(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Shuffle().Len(), 7)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Reverse(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Reverse().Slice(), []interface{}{6, 5, 4, 3, 2, 1, 0})
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Join(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
2019-10-01 16:03:18 +08:00
|
|
|
|
})
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-01 16:03:18 +08:00
|
|
|
|
a1 := []interface{}{0, 1, `"a"`, `\a`}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Join("."), `0.1."a".\a`)
|
2019-10-01 16:03:18 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_String(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-01 16:03:18 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.String(), `[0,1,2,3,4,5,6]`)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Replace(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
a2 := []interface{}{"a", "b", "c"}
|
|
|
|
|
a3 := []interface{}{"m", "n", "p", "z", "x", "y", "d", "u"}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := array1.Replace(a2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array2.Len(), 7)
|
|
|
|
|
t.Assert(array2.Contains("b"), true)
|
|
|
|
|
t.Assert(array2.Contains(4), true)
|
|
|
|
|
t.Assert(array2.Contains("v"), false)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
array3 := array1.Replace(a3)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array3.Len(), 7)
|
|
|
|
|
t.Assert(array3.Contains(4), false)
|
|
|
|
|
t.Assert(array3.Contains("p"), true)
|
|
|
|
|
t.Assert(array3.Contains("u"), false)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_SetArray(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
|
|
|
|
a2 := []interface{}{"a", "b", "c"}
|
|
|
|
|
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array1 = array1.SetArray(a2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Len(), 3)
|
|
|
|
|
t.Assert(array1.Contains("b"), true)
|
|
|
|
|
t.Assert(array1.Contains("5"), false)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Sum(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3}
|
|
|
|
|
a2 := []interface{}{"a", "b", "c"}
|
|
|
|
|
a3 := []interface{}{"a", "1", "2"}
|
|
|
|
|
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := garray.NewArrayFrom(a2)
|
|
|
|
|
array3 := garray.NewArrayFrom(a3)
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Sum(), 6)
|
|
|
|
|
t.Assert(array2.Sum(), 0)
|
|
|
|
|
t.Assert(array3.Sum(), 3)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Clone(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{0, 1, 2, 3}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := array1.Clone()
|
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array1.Len(), 4)
|
|
|
|
|
t.Assert(array2.Sum(), 6)
|
|
|
|
|
t.AssertEQ(array1, array2)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_CountValues(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
a1 := []interface{}{"a", "b", "c", "d", "e", "d"}
|
|
|
|
|
array1 := garray.NewArrayFrom(a1)
|
|
|
|
|
array2 := array1.CountValues()
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(len(array2), 5)
|
|
|
|
|
t.Assert(array2["b"], 1)
|
|
|
|
|
t.Assert(array2["d"], 2)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_LockFunc(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
s1 := []interface{}{"a", "b", "c", "d"}
|
|
|
|
|
a1 := garray.NewArrayFrom(s1, true)
|
|
|
|
|
|
|
|
|
|
ch1 := make(chan int64, 3)
|
|
|
|
|
ch2 := make(chan int64, 3)
|
2021-11-13 23:23:55 +08:00
|
|
|
|
// go1
|
|
|
|
|
go a1.LockFunc(func(n1 []interface{}) { // 读写锁
|
|
|
|
|
time.Sleep(2 * time.Second) // 暂停2秒
|
2019-09-29 20:47:59 +08:00
|
|
|
|
n1[2] = "g"
|
|
|
|
|
ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
})
|
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
|
// go2
|
2019-09-29 20:47:59 +08:00
|
|
|
|
go func() {
|
2021-11-13 23:23:55 +08:00
|
|
|
|
time.Sleep(100 * time.Millisecond) // 故意暂停0.01秒,等go1执行锁后,再开始执行.
|
2019-09-29 20:47:59 +08:00
|
|
|
|
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
a1.Len()
|
|
|
|
|
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
t1 := <-ch1
|
|
|
|
|
t2 := <-ch1
|
2021-11-13 23:23:55 +08:00
|
|
|
|
<-ch2 // 等待go1完成
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
|
|
|
|
// 防止ci抖动,以豪秒为单位
|
2021-11-13 23:23:55 +08:00
|
|
|
|
t.AssertGT(t2-t1, 20) // go1加的读写互斥锁,所go2读的时候被阻塞。
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(a1.Contains("g"), true)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_RLockFunc(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
s1 := []interface{}{"a", "b", "c", "d"}
|
|
|
|
|
a1 := garray.NewArrayFrom(s1, true)
|
|
|
|
|
|
|
|
|
|
ch1 := make(chan int64, 3)
|
|
|
|
|
ch2 := make(chan int64, 1)
|
2021-11-13 23:23:55 +08:00
|
|
|
|
// go1
|
|
|
|
|
go a1.RLockFunc(func(n1 []interface{}) { // 读锁
|
|
|
|
|
time.Sleep(2 * time.Second) // 暂停1秒
|
2019-09-29 20:47:59 +08:00
|
|
|
|
n1[2] = "g"
|
|
|
|
|
ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
})
|
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
|
// go2
|
2019-09-29 20:47:59 +08:00
|
|
|
|
go func() {
|
2021-11-13 23:23:55 +08:00
|
|
|
|
time.Sleep(100 * time.Millisecond) // 故意暂停0.01秒,等go1执行锁后,再开始执行.
|
2019-09-29 20:47:59 +08:00
|
|
|
|
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
a1.Len()
|
|
|
|
|
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
t1 := <-ch1
|
|
|
|
|
t2 := <-ch1
|
2021-11-13 23:23:55 +08:00
|
|
|
|
<-ch2 // 等待go1完成
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
|
|
|
|
// 防止ci抖动,以豪秒为单位
|
2021-11-13 23:23:55 +08:00
|
|
|
|
t.AssertLT(t2-t1, 20) // go1加的读锁,所go2读的时候,并没有阻塞。
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(a1.Contains("g"), true)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_Json(t *testing.T) {
|
2020-05-08 21:08:06 +08:00
|
|
|
|
// pointer
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 20:47:59 +08:00
|
|
|
|
s1 := []interface{}{"a", "b", "d", "c"}
|
|
|
|
|
a1 := garray.NewArrayFrom(s1)
|
|
|
|
|
b1, err1 := json.Marshal(a1)
|
|
|
|
|
b2, err2 := json.Marshal(s1)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(b1, b2)
|
|
|
|
|
t.Assert(err1, err2)
|
2019-09-29 20:47:59 +08:00
|
|
|
|
|
|
|
|
|
a2 := garray.New()
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err2 = json.UnmarshalUseNumber(b2, &a2)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
|
t.Assert(a2.Slice(), s1)
|
2019-09-29 22:16:36 +08:00
|
|
|
|
|
|
|
|
|
var a3 garray.Array
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err := json.UnmarshalUseNumber(b2, &a3)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(a3.Slice(), s1)
|
2019-09-29 22:16:36 +08:00
|
|
|
|
})
|
2020-05-08 21:08:06 +08:00
|
|
|
|
// value.
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
s1 := []interface{}{"a", "b", "d", "c"}
|
|
|
|
|
a1 := *garray.NewArrayFrom(s1)
|
|
|
|
|
b1, err1 := json.Marshal(a1)
|
|
|
|
|
b2, err2 := json.Marshal(s1)
|
|
|
|
|
t.Assert(b1, b2)
|
|
|
|
|
t.Assert(err1, err2)
|
|
|
|
|
|
|
|
|
|
a2 := garray.New()
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err2 = json.UnmarshalUseNumber(b2, &a2)
|
2020-05-08 21:08:06 +08:00
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
|
t.Assert(a2.Slice(), s1)
|
2019-09-29 22:16:36 +08:00
|
|
|
|
|
2020-05-08 21:08:06 +08:00
|
|
|
|
var a3 garray.Array
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err := json.UnmarshalUseNumber(b2, &a3)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-05-08 21:08:06 +08:00
|
|
|
|
t.Assert(a3.Slice(), s1)
|
|
|
|
|
})
|
|
|
|
|
// pointer
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 22:16:36 +08:00
|
|
|
|
type User struct {
|
|
|
|
|
Name string
|
|
|
|
|
Scores *garray.Array
|
|
|
|
|
}
|
|
|
|
|
data := g.Map{
|
|
|
|
|
"Name": "john",
|
|
|
|
|
"Scores": []int{99, 100, 98},
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-05-08 21:08:06 +08:00
|
|
|
|
|
|
|
|
|
user := new(User)
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err = json.UnmarshalUseNumber(b, user)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-05-08 21:08:06 +08:00
|
|
|
|
t.Assert(user.Name, data["Name"])
|
|
|
|
|
t.Assert(user.Scores, data["Scores"])
|
|
|
|
|
})
|
|
|
|
|
// value
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
type User struct {
|
|
|
|
|
Name string
|
|
|
|
|
Scores garray.Array
|
|
|
|
|
}
|
|
|
|
|
data := g.Map{
|
|
|
|
|
"Name": "john",
|
|
|
|
|
"Scores": []int{99, 100, 98},
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2019-09-29 22:16:36 +08:00
|
|
|
|
|
|
|
|
|
user := new(User)
|
2021-05-15 22:38:07 +08:00
|
|
|
|
err = json.UnmarshalUseNumber(b, user)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(user.Name, data["Name"])
|
|
|
|
|
t.Assert(user.Scores, data["Scores"])
|
2019-09-29 20:47:59 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2019-12-25 20:56:39 +08:00
|
|
|
|
|
|
|
|
|
func TestArray_Iterator(t *testing.T) {
|
|
|
|
|
slice := g.Slice{"a", "b", "d", "c"}
|
|
|
|
|
array := garray.NewArrayFrom(slice)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
array.Iterator(func(k int, v interface{}) bool {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(v, slice[k])
|
2019-12-25 20:56:39 +08:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
array.IteratorAsc(func(k int, v interface{}) bool {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(v, slice[k])
|
2019-12-25 20:56:39 +08:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
array.IteratorDesc(func(k int, v interface{}) bool {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(v, slice[k])
|
2019-12-25 20:56:39 +08:00
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
index := 0
|
|
|
|
|
array.Iterator(func(k int, v interface{}) bool {
|
|
|
|
|
index++
|
|
|
|
|
return false
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(index, 1)
|
2019-12-25 20:56:39 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
index := 0
|
|
|
|
|
array.IteratorAsc(func(k int, v interface{}) bool {
|
|
|
|
|
index++
|
|
|
|
|
return false
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(index, 1)
|
2019-12-25 20:56:39 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-25 20:56:39 +08:00
|
|
|
|
index := 0
|
|
|
|
|
array.IteratorDesc(func(k int, v interface{}) bool {
|
|
|
|
|
index++
|
|
|
|
|
return false
|
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(index, 1)
|
2019-12-25 20:56:39 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2020-01-17 19:48:50 +08:00
|
|
|
|
|
|
|
|
|
func TestArray_RemoveValue(t *testing.T) {
|
|
|
|
|
slice := g.Slice{"a", "b", "d", "c"}
|
|
|
|
|
array := garray.NewArrayFrom(slice)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
t.Assert(array.RemoveValue("e"), false)
|
|
|
|
|
t.Assert(array.RemoveValue("b"), true)
|
|
|
|
|
t.Assert(array.RemoveValue("a"), true)
|
|
|
|
|
t.Assert(array.RemoveValue("c"), true)
|
|
|
|
|
t.Assert(array.RemoveValue("f"), false)
|
2020-01-17 19:48:50 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2020-01-20 19:56:42 +08:00
|
|
|
|
|
|
|
|
|
func TestArray_UnmarshalValue(t *testing.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
|
type V struct {
|
2020-01-20 19:56:42 +08:00
|
|
|
|
Name string
|
|
|
|
|
Array *garray.Array
|
|
|
|
|
}
|
|
|
|
|
// JSON
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
|
var v *V
|
2020-01-20 19:56:42 +08:00
|
|
|
|
err := gconv.Struct(g.Map{
|
|
|
|
|
"name": "john",
|
|
|
|
|
"array": []byte(`[1,2,3]`),
|
2020-03-19 23:53:03 +08:00
|
|
|
|
}, &v)
|
2021-09-15 21:46:58 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-03-19 23:53:03 +08:00
|
|
|
|
t.Assert(v.Name, "john")
|
|
|
|
|
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
2020-01-20 19:56:42 +08:00
|
|
|
|
})
|
|
|
|
|
// Map
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
|
var v *V
|
2020-01-20 19:56:42 +08:00
|
|
|
|
err := gconv.Struct(g.Map{
|
|
|
|
|
"name": "john",
|
|
|
|
|
"array": g.Slice{1, 2, 3},
|
2020-03-19 23:53:03 +08:00
|
|
|
|
}, &v)
|
2022-03-10 11:36:40 +08:00
|
|
|
|
t.AssertNil(err)
|
2020-03-19 23:53:03 +08:00
|
|
|
|
t.Assert(v.Name, "john")
|
|
|
|
|
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
2020-01-20 19:56:42 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2020-02-22 14:26:36 +08:00
|
|
|
|
|
|
|
|
|
func TestArray_FilterNil(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-22 14:26:36 +08:00
|
|
|
|
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
|
|
|
|
|
array := garray.NewArrayFromCopy(values)
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.FilterNil().Slice(), values)
|
2020-02-22 14:26:36 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-22 14:26:36 +08:00
|
|
|
|
array := garray.NewArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
2020-02-22 14:26:36 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArray_FilterEmpty(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-22 14:26:36 +08:00
|
|
|
|
array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
2020-02-22 14:26:36 +08:00
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-22 14:26:36 +08:00
|
|
|
|
array := garray.NewArrayFrom(g.Slice{1, 2, 3, 4})
|
2020-03-19 22:56:12 +08:00
|
|
|
|
t.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
2020-02-22 14:26:36 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2020-04-18 13:30:49 +08:00
|
|
|
|
|
|
|
|
|
func TestArray_Walk(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
array := garray.NewArrayFrom(g.Slice{"1", "2"})
|
|
|
|
|
t.Assert(array.Walk(func(value interface{}) interface{} {
|
|
|
|
|
return "key-" + gconv.String(value)
|
|
|
|
|
}), g.Slice{"key-1", "key-2"})
|
|
|
|
|
})
|
|
|
|
|
}
|