mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
add some garray unit tests
This commit is contained in:
parent
a5d01cb547
commit
56588f3f7f
@ -17,69 +17,98 @@ import (
|
||||
)
|
||||
|
||||
func Test_IntArray_Unique(t *testing.T) {
|
||||
expect := []int{1, 2, 3, 4, 5, 6}
|
||||
array := garray.NewIntArray()
|
||||
array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
|
||||
array.Unique()
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Case(t, func() {
|
||||
expect := []int{1, 2, 3, 4, 5, 6}
|
||||
array := garray.NewIntArray()
|
||||
array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
|
||||
array.Unique()
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedIntArray1(t *testing.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Case(t, func() {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Add().Slice(), expect)
|
||||
gtest.Assert(array.Add(-1).Slice(), []int{-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedIntArray2(t *testing.T) {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Case(t, func() {
|
||||
expect := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
array := garray.NewSortedIntArray()
|
||||
array2 := garray.NewSortedIntArray(true)
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(i)
|
||||
array2.Add(i)
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedStringArray1(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStringArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Case(t, func() {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStringArray()
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Add().Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedStringArray2(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStringArray()
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Case(t, func() {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedStringArray()
|
||||
array2 := garray.NewSortedStringArray(true)
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SortedArray1(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
gtest.Case(t, func() {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
})
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
})
|
||||
for i := 10; i > -1; i-- {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
}
|
||||
|
||||
func Test_SortedArray2(t *testing.T) {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
gtest.Case(t, func() {
|
||||
expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"}
|
||||
array := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
})
|
||||
array2 := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}, true)
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
array2.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
gtest.Assert(array.Add(), expect)
|
||||
gtest.Assert(array2.Slice(), expect)
|
||||
})
|
||||
for i := 0; i <= 10; i++ {
|
||||
array.Add(gconv.String(i))
|
||||
}
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
}
|
||||
|
||||
func TestNewFromCopy(t *testing.T) {
|
||||
@ -89,6 +118,5 @@ func TestNewFromCopy(t *testing.T) {
|
||||
gtest.AssertIN(array1.PopRands(2), a1)
|
||||
gtest.Assert(len(array1.PopRands(1)), 1)
|
||||
gtest.Assert(len(array1.PopRands(9)), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
@ -9,21 +9,26 @@
|
||||
package garray_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_IntArray_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
expect := []int{0, 1, 2, 3}
|
||||
expect2 := []int{}
|
||||
array := garray.NewIntArrayFrom(expect)
|
||||
array2 := garray.NewIntArrayFrom(expect2)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
array.Set(0, 100)
|
||||
gtest.Assert(array.Get(0), 100)
|
||||
gtest.Assert(array.Get(1), 1)
|
||||
gtest.Assert(array.Search(100), 0)
|
||||
gtest.Assert(array2.Search(7), -1)
|
||||
gtest.Assert(array.Contains(100), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Contains(100), false)
|
||||
@ -44,13 +49,18 @@ func TestIntArray_Sort(t *testing.T) {
|
||||
expect1 := []int{0, 1, 2, 3}
|
||||
expect2 := []int{3, 2, 1, 0}
|
||||
array := garray.NewIntArray()
|
||||
array2 := garray.NewIntArray(true)
|
||||
for i := 3; i >= 0; i-- {
|
||||
array.Append(i)
|
||||
array2.Append(i)
|
||||
}
|
||||
|
||||
array.Sort()
|
||||
gtest.Assert(array.Slice(), expect1)
|
||||
array.Sort(true)
|
||||
gtest.Assert(array.Slice(), expect2)
|
||||
array2.Sort(true)
|
||||
gtest.Assert(array2.Slice(), expect2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -98,21 +108,47 @@ func TestIntArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
value1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(value1)
|
||||
array2 := garray.NewIntArrayFrom(value1, true)
|
||||
gtest.Assert(array1.Range(0, 1), []int{0})
|
||||
gtest.Assert(array1.Range(1, 2), []int{1})
|
||||
gtest.Assert(array1.Range(0, 2), []int{0, 1})
|
||||
gtest.Assert(array1.Range(10, 2), nil)
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
gtest.Assert(array1.Range(8, 2), nil)
|
||||
|
||||
gtest.Assert(array2.Range(2, 4), []int{2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{0, 1, 2, 3}
|
||||
a2 := []int{4, 5, 6, 7}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array2 := garray.NewIntArrayFrom(a2)
|
||||
gtest.Assert(array1.Merge(array2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewIntArrayFrom(n1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
b6 := garray.NewSortedArrayFrom(in1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 7)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 9)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 12)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 14)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 18)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 21)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 23)
|
||||
gtest.Assert(a1.Merge(b6).Len(), 27)
|
||||
})
|
||||
}
|
||||
|
||||
@ -124,6 +160,7 @@ func TestIntArray_Fill(t *testing.T) {
|
||||
array2 := garray.NewIntArrayFrom(a2)
|
||||
gtest.Assert(array1.Fill(1, 2, 100).Slice(), []int{0, 100, 100})
|
||||
gtest.Assert(array2.Fill(0, 2, 100).Slice(), []int{100, 100})
|
||||
gtest.Assert(array2.Fill(-1, 2, 100).Slice(), []int{100, 100})
|
||||
})
|
||||
}
|
||||
|
||||
@ -132,7 +169,8 @@ func TestIntArray_Chunk(t *testing.T) {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
chunks2 := array1.Chunk(0)
|
||||
gtest.Assert(chunks2, nil)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5})
|
||||
@ -153,6 +191,7 @@ func TestIntArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array2 := garray.NewIntArrayFrom(a1, true)
|
||||
gtest.Assert(array1.SubSlice(6), []int{6})
|
||||
gtest.Assert(array1.SubSlice(5), []int{5, 6})
|
||||
gtest.Assert(array1.SubSlice(8), nil)
|
||||
@ -168,6 +207,7 @@ func TestIntArray_SubSlice(t *testing.T) {
|
||||
gtest.Assert(array1.SubSlice(-9, 3), nil)
|
||||
gtest.Assert(array1.SubSlice(1, -1), []int{0})
|
||||
gtest.Assert(array1.SubSlice(1, -3), nil)
|
||||
gtest.Assert(array2.SubSlice(1, 2), []int{1, 2})
|
||||
})
|
||||
}
|
||||
|
||||
@ -193,7 +233,6 @@ func TestIntArray_PopRands(t *testing.T) {
|
||||
ns2 := array.PopRands(7)
|
||||
gtest.AssertIN(len(ns2), 6)
|
||||
gtest.AssertIN(ns2, []int{100, 200, 300, 400, 500, 600})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -254,13 +293,10 @@ func TestSortedIntArray_SetArray(t *testing.T) {
|
||||
func TestSortedIntArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{0, 3, 2, 1}
|
||||
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := array1.Sort()
|
||||
|
||||
gtest.Assert(array2.Len(), 4)
|
||||
gtest.Assert(array2, []int{0, 1, 2, 3})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -296,7 +332,6 @@ func TestSortedIntArray_Remove(t *testing.T) {
|
||||
i3 = array2.Remove(1)
|
||||
gtest.Assert(array2.Search(4), -1)
|
||||
gtest.Assert(i3, 4)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -308,7 +343,6 @@ func TestSortedIntArray_PopLeft(t *testing.T) {
|
||||
gtest.Assert(i1, 1)
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Search(1), -1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -348,7 +382,6 @@ func TestSortedIntArray_PopRands(t *testing.T) {
|
||||
gtest.Assert(array2.Len(), 0)
|
||||
gtest.Assert(len(ns2), 4)
|
||||
gtest.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -365,7 +398,6 @@ func TestSortedIntArray_PopLefts(t *testing.T) {
|
||||
ns2 := array2.PopLefts(5)
|
||||
gtest.Assert(array2.Len(), 0)
|
||||
gtest.AssertIN(ns2, []int{1, 3, 5, 2})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -389,6 +421,7 @@ func TestSortedIntArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5, 2, 6, 7}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array2 := garray.NewSortedIntArrayFrom(a1, true)
|
||||
ns1 := array1.Range(1, 4)
|
||||
gtest.Assert(len(ns1), 3)
|
||||
gtest.Assert(ns1, []int{2, 3, 5})
|
||||
@ -402,6 +435,8 @@ func TestSortedIntArray_Range(t *testing.T) {
|
||||
nsl := array1.Range(5, 8)
|
||||
gtest.Assert(len(nsl), 1)
|
||||
|
||||
ns4 := array2.Range(2, 5)
|
||||
gtest.Assert(len(ns4), 3)
|
||||
})
|
||||
}
|
||||
|
||||
@ -418,7 +453,6 @@ func TestSortedIntArray_Contains(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
//gtest.Assert(array1.Contains(3),true) //todo 这一行应该返回true
|
||||
gtest.Assert(array1.Contains(4), false)
|
||||
})
|
||||
}
|
||||
@ -439,7 +473,6 @@ func TestSortedIntArray_Clear(t *testing.T) {
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -453,7 +486,6 @@ func TestSortedIntArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(ns1[0], []int{1, 2})
|
||||
gtest.Assert(ns1[2], []int{5})
|
||||
gtest.Assert(len(ns2), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -475,6 +507,13 @@ func TestSortedIntArray_SubSlice(t *testing.T) {
|
||||
ns4 := array1.SubSlice(3, 1)
|
||||
gtest.Assert(len(ns4), 1)
|
||||
gtest.Assert(ns4, []int{4})
|
||||
|
||||
array3 := garray.NewSortedIntArrayFrom(a1, true)
|
||||
gtest.Assert(array3.SubSlice(2, 2), []int{3, 4})
|
||||
gtest.Assert(array3.SubSlice(-1, 2), []int{5})
|
||||
gtest.Assert(array3.SubSlice(-9, 2), nil)
|
||||
gtest.Assert(array3.SubSlice(4, -2), []int{3, 4})
|
||||
gtest.Assert(array3.SubSlice(1, -3), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -494,7 +533,6 @@ func TestSortedIntArray_Rands(t *testing.T) {
|
||||
ns1 := array1.Rands(2) //按每几个元素切成一个数组
|
||||
gtest.AssertIN(ns1, a1)
|
||||
gtest.Assert(len(ns1), 2)
|
||||
|
||||
ns2 := array1.Rands(6) //按每几个元素切成一个数组
|
||||
gtest.AssertIN(ns2, a1)
|
||||
gtest.Assert(len(ns2), 5)
|
||||
@ -519,7 +557,6 @@ func TestSortedIntArray_SetUnique(t *testing.T) {
|
||||
array1.SetUnique(true)
|
||||
gtest.Assert(array1.Len(), 5)
|
||||
gtest.Assert(array1, []int{1, 2, 3, 4, 5})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -531,7 +568,6 @@ func TestIntArray_SetArray(t *testing.T) {
|
||||
array1.SetArray(a2)
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
gtest.Assert(array1, []int{6, 7})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -543,7 +579,6 @@ func TestIntArray_Replace(t *testing.T) {
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
array1.Replace(a2)
|
||||
gtest.Assert(array1, []int{6, 7, 3, 5})
|
||||
|
||||
array1.Replace(a3)
|
||||
gtest.Assert(array1, []int{9, 10, 11, 12})
|
||||
})
|
||||
@ -621,3 +656,262 @@ func TestIntArray_Remove(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
a1 := garray.NewSortedIntArrayFrom(n1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []int) { //互斥锁
|
||||
for i := 1; i <= 4; i++ {
|
||||
gtest.Assert(i, n1[i-1])
|
||||
}
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedIntArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
a1 := garray.NewSortedIntArrayFrom(n1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []int) { //读锁
|
||||
for i := 1; i <= 4; i++ {
|
||||
gtest.Assert(i, n1[i-1])
|
||||
}
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 2)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewSortedIntArrayFrom(n1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
b6 := garray.NewSortedArrayFrom(in1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 7)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 9)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 12)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 14)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 18)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 21)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 23)
|
||||
gtest.Assert(a1.Merge(b6).Len(), 27)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
a1 := garray.NewSortedArrayFrom(n1, func1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
a1 := garray.NewSortedArrayFrom(n1, func1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewSortedArrayFrom(n1, func1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 7)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 9)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 12)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 14)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 18)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 21)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 23)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_SortFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 3, 5, 4}
|
||||
a1 := garray.NewIntArrayFrom(n1)
|
||||
|
||||
func1 := func(v1, v2 int) bool {
|
||||
if v1 > v2 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func2 := func(v1, v2 int) bool {
|
||||
if v1 > v2 {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
a2 := a1.SortFunc(func1)
|
||||
gtest.Assert(a2, []int{1, 2, 3, 4, 5})
|
||||
a3 := a1.SortFunc(func2)
|
||||
gtest.Assert(a3, []int{5, 4, 3, 2, 1})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
a1 := garray.NewIntArrayFrom(n1)
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []int) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []int{1, 2, 4, 3}
|
||||
a1 := garray.NewIntArrayFrom(n1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []int) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
@ -12,8 +12,10 @@ import (
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Array_Basic(t *testing.T) {
|
||||
@ -37,6 +39,24 @@ func Test_Array_Basic(t *testing.T) {
|
||||
array.InsertAfter(6, 400)
|
||||
gtest.Assert(array.Slice(), []interface{}{100, 200, 1, 2, 3, 300, 4, 400})
|
||||
gtest.Assert(array.Clear().Len(), 0)
|
||||
|
||||
n1 := []interface{}{0, 1, 2, 3}
|
||||
a1 := garray.NewArrayFrom(n1)
|
||||
i1 := a1.Remove(3)
|
||||
gtest.Assert(gconv.Int(i1), 3)
|
||||
i2 := a1.Remove(1)
|
||||
gtest.Assert(gconv.Int(i2), 1)
|
||||
gtest.Assert(a1.Len(), 2)
|
||||
gtest.Assert(a1.Contains(1), false)
|
||||
|
||||
a2 := garray.NewArrayFrom(n1, true)
|
||||
gtest.Assert(a2.Slice(), n1)
|
||||
gtest.Assert(a2.Search(100), -1)
|
||||
|
||||
n2 := []interface{}{}
|
||||
a3 := garray.NewArrayFrom(n2)
|
||||
gtest.Assert(a3.Search(3), -1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -115,16 +135,42 @@ func TestArray_Range(t *testing.T) {
|
||||
gtest.Assert(array1.Range(1, 2), []interface{}{1})
|
||||
gtest.Assert(array1.Range(0, 2), []interface{}{0, 1})
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
gtest.Assert(array1.Range(9, 1), nil)
|
||||
a1 := garray.NewArrayFrom(value1, true)
|
||||
gtest.Assert(a1.Range(0, 1), []interface{}{0})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, 2, 3}
|
||||
a2 := []interface{}{4, 5, 6, 7}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
array2 := garray.NewArrayFrom(a2)
|
||||
gtest.Assert(array1.Merge(array2).Slice(), []interface{}{0, 1, 2, 3, 4, 5, 6, 7})
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewArrayFrom(n1)
|
||||
a11 := garray.NewSortedArrayFrom(n1, func1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
b6 := garray.NewSortedArrayFrom(n1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 7)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 9)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 12)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 14)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 18)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 21)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 23)
|
||||
gtest.Assert(a1.Merge(b6).Len(), 27)
|
||||
gtest.Assert(a11.Merge(b6).Len(), 8)
|
||||
})
|
||||
}
|
||||
|
||||
@ -136,6 +182,7 @@ func TestArray_Fill(t *testing.T) {
|
||||
array2 := garray.NewArrayFrom(a2)
|
||||
gtest.Assert(array1.Fill(1, 2, 100).Slice(), []interface{}{0, 100, 100})
|
||||
gtest.Assert(array2.Fill(0, 2, 100).Slice(), []interface{}{100, 100})
|
||||
gtest.Assert(array2.Fill(-1, 2, 100).Slice(), []interface{}{100, 100})
|
||||
})
|
||||
}
|
||||
|
||||
@ -148,6 +195,7 @@ func TestArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -168,6 +216,19 @@ func TestArray_SubSlice(t *testing.T) {
|
||||
gtest.Assert(array1.SubSlice(0, 2), []interface{}{0, 1})
|
||||
gtest.Assert(array1.SubSlice(2, 2), []interface{}{2, 3})
|
||||
gtest.Assert(array1.SubSlice(5, 8), []interface{}{5, 6})
|
||||
gtest.Assert(array1.SubSlice(8, 1), nil)
|
||||
|
||||
array2 := garray.NewArrayFrom(a1, false)
|
||||
gtest.Assert(array2.SubSlice(2, 2), []interface{}{2, 3})
|
||||
|
||||
a2 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array3 := garray.NewArrayFrom(a2, true)
|
||||
gtest.Assert(array3.SubSlice(2, 2), []interface{}{2, 3})
|
||||
gtest.Assert(array3.SubSlice(-1, 2), []interface{}{6})
|
||||
gtest.Assert(array3.SubSlice(-9, 2), nil)
|
||||
gtest.Assert(array3.SubSlice(4, -2), []interface{}{2, 3})
|
||||
gtest.Assert(array3.SubSlice(1, -3), nil)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -175,6 +236,8 @@ func TestArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
i1 := array1.Rand()
|
||||
gtest.Assert(array1.Contains(i1), true)
|
||||
gtest.Assert(len(array1.Rands(2)), 2)
|
||||
gtest.Assert(len(array1.Rands(10)), 7)
|
||||
gtest.AssertIN(array1.Rands(1)[0], a1)
|
||||
@ -250,7 +313,6 @@ func TestArray_Sum(t *testing.T) {
|
||||
gtest.Assert(array1.Sum(), 6)
|
||||
gtest.Assert(array2.Sum(), 0)
|
||||
gtest.Assert(array3.Sum(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -332,7 +394,6 @@ func TestSortedArray_SetArray(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1, []interface{}{"e", "g", "h", "k"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Sort(t *testing.T) {
|
||||
@ -346,7 +407,6 @@ func TestSortedArray_Sort(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "f"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Get(t *testing.T) {
|
||||
@ -359,7 +419,6 @@ func TestSortedArray_Get(t *testing.T) {
|
||||
gtest.Assert(array1.Get(2), "f")
|
||||
gtest.Assert(array1.Get(1), "c")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_Remove(t *testing.T) {
|
||||
@ -384,7 +443,6 @@ func TestSortedArray_Remove(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 1)
|
||||
gtest.Assert(array1.Contains("d"), false)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopLeft(t *testing.T) {
|
||||
@ -399,7 +457,6 @@ func TestSortedArray_PopLeft(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"b", "c", "d"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRight(t *testing.T) {
|
||||
@ -414,7 +471,6 @@ func TestSortedArray_PopRight(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1, []interface{}{"a", "b", "c"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSortedArray_PopRand(t *testing.T) {
|
||||
@ -427,7 +483,6 @@ func TestSortedArray_PopRand(t *testing.T) {
|
||||
i1 := array1.PopRand()
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -447,7 +502,6 @@ func TestSortedArray_PopRands(t *testing.T) {
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.AssertIN(i2, []interface{}{"a", "d", "c", "b"})
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -467,7 +521,6 @@ func TestSortedArray_PopLefts(t *testing.T) {
|
||||
gtest.Assert(len(i2), 4)
|
||||
gtest.AssertIN(i1, []interface{}{"a", "d", "c", "b", "e", "f"})
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -482,10 +535,8 @@ func TestSortedArray_PopRights(t *testing.T) {
|
||||
gtest.Assert(len(i1), 2)
|
||||
gtest.Assert(i1, []interface{}{"e", "f"})
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
|
||||
i2 := array1.PopRights(10)
|
||||
gtest.Assert(len(i2), 4)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -496,6 +547,7 @@ func TestSortedArray_Range(t *testing.T) {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a1, func1, true)
|
||||
i1 := array1.Range(2, 5)
|
||||
gtest.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
gtest.Assert(array1.Len(), 6)
|
||||
@ -509,6 +561,10 @@ func TestSortedArray_Range(t *testing.T) {
|
||||
gtest.Assert(len(i2), 2)
|
||||
gtest.Assert(i2, []interface{}{"e", "f"})
|
||||
|
||||
i2 = array2.Range(4, 10)
|
||||
gtest.Assert(len(i2), 2)
|
||||
gtest.Assert(i2, []interface{}{"e", "f"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -526,7 +582,6 @@ func TestSortedArray_Sum(t *testing.T) {
|
||||
gtest.Assert(array1.Sum(), 0)
|
||||
gtest.Assert(array2.Sum(), 6)
|
||||
gtest.Assert(array3.Sum(), 15)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -542,14 +597,12 @@ func TestSortedArray_Clone(t *testing.T) {
|
||||
gtest.Assert(array1, array2)
|
||||
array1.Remove(1)
|
||||
gtest.AssertNE(array1, array2)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e", "f"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -557,14 +610,12 @@ func TestSortedArray_Clear(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 6)
|
||||
array1.Clear()
|
||||
gtest.Assert(array1.Len(), 0)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Chunk(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -582,11 +633,11 @@ func TestSortedArray_Chunk(t *testing.T) {
|
||||
func TestSortedArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c", "b", "e"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
array2 := garray.NewSortedArrayFrom(a1, func1, true)
|
||||
i1 := array1.SubSlice(2, 3)
|
||||
gtest.Assert(len(i1), 3)
|
||||
gtest.Assert(i1, []interface{}{"c", "d", "e"})
|
||||
@ -598,13 +649,20 @@ func TestSortedArray_SubSlice(t *testing.T) {
|
||||
i1 = array1.SubSlice(7, 2)
|
||||
gtest.Assert(len(i1), 0)
|
||||
|
||||
i1 = array2.SubSlice(-2, 2)
|
||||
gtest.Assert(len(i1), 2)
|
||||
|
||||
i1 = array2.SubSlice(-8, 1)
|
||||
gtest.Assert(i1, nil)
|
||||
|
||||
i1 = array2.SubSlice(1, -9)
|
||||
gtest.Assert(i1, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_Rand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -618,7 +676,6 @@ func TestSortedArray_Rand(t *testing.T) {
|
||||
func TestSortedArray_Rands(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -636,14 +693,12 @@ func TestSortedArray_Rands(t *testing.T) {
|
||||
func TestSortedArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
gtest.Assert(array1.Join(","), "a,c,d")
|
||||
gtest.Assert(array1.Join("."), "a.c.d")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -659,14 +714,12 @@ func TestSortedArray_CountValues(t *testing.T) {
|
||||
gtest.Assert(len(m1), 3)
|
||||
gtest.Assert(m1["c"], 2)
|
||||
gtest.Assert(m1["a"], 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_SetUnique(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
@ -676,3 +729,55 @@ func TestSortedArray_SetUnique(t *testing.T) {
|
||||
gtest.Assert(array1, []interface{}{"a", "c", "d"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
a1 := garray.NewArrayFrom(n1)
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
n1 := []interface{}{1, 2, 4, 3}
|
||||
a1 := garray.NewArrayFrom(n1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 20)
|
||||
gtest.Assert(a1.Contains(7), true)
|
||||
})
|
||||
}
|
||||
|
@ -14,17 +14,21 @@ import (
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_StringArray_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
expect := []string{"0", "1", "2", "3"}
|
||||
expect2 := []string{}
|
||||
array := garray.NewStringArrayFrom(expect)
|
||||
array2 := garray.NewStringArrayFrom(expect2)
|
||||
gtest.Assert(array.Slice(), expect)
|
||||
array.Set(0, "100")
|
||||
gtest.Assert(array.Get(0), 100)
|
||||
gtest.Assert(array.Get(1), 1)
|
||||
gtest.Assert(array.Search("100"), 0)
|
||||
gtest.Assert(array2.Search("100"), -1)
|
||||
gtest.Assert(array.Contains("100"), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Contains("100"), false)
|
||||
@ -45,13 +49,17 @@ func TestStringArray_Sort(t *testing.T) {
|
||||
expect1 := []string{"0", "1", "2", "3"}
|
||||
expect2 := []string{"3", "2", "1", "0"}
|
||||
array := garray.NewStringArray()
|
||||
array2 := garray.NewStringArray(true)
|
||||
for i := 3; i >= 0; i-- {
|
||||
array.Append(gconv.String(i))
|
||||
array2.Append(gconv.String(i))
|
||||
}
|
||||
array.Sort()
|
||||
gtest.Assert(array.Slice(), expect1)
|
||||
array.Sort(true)
|
||||
gtest.Assert(array.Slice(), expect2)
|
||||
array2.Sort(true)
|
||||
gtest.Assert(array2.Slice(), expect2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -99,20 +107,13 @@ func TestString_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
value1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStringArrayFrom(value1)
|
||||
array2 := garray.NewStringArrayFrom(value1, true)
|
||||
gtest.Assert(array1.Range(0, 1), []interface{}{"0"})
|
||||
gtest.Assert(array1.Range(1, 2), []interface{}{"1"})
|
||||
gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"})
|
||||
gtest.Assert(array1.Range(-1, 10), value1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", "2", "3"}
|
||||
a2 := []string{"4", "5", "6", "7"}
|
||||
array1 := garray.NewStringArrayFrom(a1)
|
||||
array2 := garray.NewStringArrayFrom(a2)
|
||||
gtest.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"})
|
||||
gtest.Assert(array1.Range(8, 1), nil)
|
||||
gtest.Assert(len(array2.Range(2, 4)), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -160,6 +161,14 @@ func TestStringArray_SubSlice(t *testing.T) {
|
||||
gtest.Assert(array1.SubSlice(0, 2), []string{"0", "1"})
|
||||
gtest.Assert(array1.SubSlice(2, 2), []string{"2", "3"})
|
||||
gtest.Assert(array1.SubSlice(5, 8), []string{"5", "6"})
|
||||
gtest.Assert(array1.SubSlice(8, 1), nil)
|
||||
|
||||
array3 := garray.NewStringArrayFrom(a1, true)
|
||||
gtest.Assert(array3.SubSlice(2, 2), []string{"2", "3"})
|
||||
gtest.Assert(array3.SubSlice(-1, 2), []string{"6"})
|
||||
gtest.Assert(array3.SubSlice(-9, 2), nil)
|
||||
gtest.Assert(array3.SubSlice(4, -2), []string{"2", "3"})
|
||||
gtest.Assert(array3.SubSlice(1, -3), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -181,9 +190,9 @@ func TestStringArray_PopRands(t *testing.T) {
|
||||
a1 := []string{"a", "b", "c", "d", "e", "f", "g"}
|
||||
a2 := []string{"1", "2", "3", "4", "5", "6", "7"}
|
||||
array1 := garray.NewStringArrayFrom(a1)
|
||||
//todo gtest.AssertIN(array1.PopRands(1),a1)
|
||||
gtest.AssertIN(array1.PopRands(1), strings.Join(a1, ","))
|
||||
gtest.AssertNI(array1.PopRands(1), strings.Join(a2, ","))
|
||||
gtest.AssertNI(len(array1.PopRands(10)), 7)
|
||||
|
||||
})
|
||||
}
|
||||
@ -275,26 +284,6 @@ func TestStringArray_Sum(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//func TestStringArray_SortFunc(t *testing.T) {
|
||||
// gtest.Case(t, func() {
|
||||
// a1 := []string{"0","1","2","3","4","5","6"}
|
||||
// //a2 := []string{"0","a","3","4","5","6"}
|
||||
// array1 := garray.NewStringArrayFrom(a1)
|
||||
//
|
||||
// lesss:=func(v1,v2 string)bool{
|
||||
// if v1>v2{
|
||||
// return true
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
// gtest.Assert(array1.Len(),7)
|
||||
// gtest.Assert(lesss("1","2"),false)
|
||||
// gtest.Assert(array1.SortFunc(lesss("1","2")) ,false)
|
||||
//
|
||||
//
|
||||
// })
|
||||
//}
|
||||
|
||||
func TestStringArray_PopRand(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
@ -319,7 +308,6 @@ func TestStringArray_CountValues(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "4", "6"}
|
||||
array1 := garray.NewStringArrayFrom(a1)
|
||||
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(len(m1), 6)
|
||||
gtest.Assert(m1["2"], 1)
|
||||
@ -365,9 +353,8 @@ func TestSortedStringArray_Sort(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
array1.Sort() //todo 这个SortedStringArray.sort这个方法没有必要,
|
||||
array1.Sort()
|
||||
gtest.Assert(array1.Len(), 4)
|
||||
gtest.Assert(array1.Contains("c"), true)
|
||||
gtest.Assert(array1, []string{"a", "b", "c", "d"})
|
||||
@ -380,7 +367,6 @@ func TestSortedStringArray_Get(t *testing.T) {
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
gtest.Assert(array1.Get(2), "c")
|
||||
gtest.Assert(array1.Get(0), "a")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -486,6 +472,7 @@ func TestSortedStringArray_Range(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
array2 := garray.NewSortedStringArrayFrom(a1, true)
|
||||
s1 := array1.Range(2, 4)
|
||||
gtest.Assert(len(s1), 2)
|
||||
gtest.Assert(s1, []string{"c", "d"})
|
||||
@ -494,9 +481,10 @@ func TestSortedStringArray_Range(t *testing.T) {
|
||||
gtest.Assert(len(s1), 2)
|
||||
gtest.Assert(s1, []string{"a", "b"})
|
||||
|
||||
s1 = array1.Range(4, 8)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.Assert(s1, []string{"e", "f", "g"})
|
||||
gtest.Assert(array1.Range(4, 8), []string{"e", "f", "g"})
|
||||
gtest.Assert(array1.Range(10, 2), nil)
|
||||
gtest.Assert(array2.Range(4, 8), []string{"e", "f", "g"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -536,6 +524,7 @@ func TestSortedStringArray_SubSlice(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
array2 := garray.NewSortedStringArrayFrom(a1, true)
|
||||
s1 := array1.SubSlice(1, 3)
|
||||
gtest.Assert(len(s1), 3)
|
||||
gtest.Assert(s1, []string{"b", "c", "d"})
|
||||
@ -546,6 +535,10 @@ func TestSortedStringArray_SubSlice(t *testing.T) {
|
||||
|
||||
s3 := array1.SubSlice(10, 2)
|
||||
gtest.Assert(len(s3), 0)
|
||||
gtest.Assert(array1.SubSlice(-2, 2), []string{"f", "g"})
|
||||
gtest.Assert(array1.SubSlice(-10, 2), nil)
|
||||
gtest.Assert(array1.SubSlice(2, -3), nil)
|
||||
gtest.Assert(array2.SubSlice(2, 3), []string{"c", "d", "e"})
|
||||
|
||||
})
|
||||
}
|
||||
@ -555,7 +548,6 @@ func TestSortedStringArray_Len(t *testing.T) {
|
||||
a1 := []string{"e", "a", "d", "c", "b", "f", "g"}
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
gtest.Assert(array1.Len(), 7)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -564,7 +556,6 @@ func TestSortedStringArray_Rand(t *testing.T) {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStringArrayFrom(a1)
|
||||
gtest.AssertIN(array1.Rand(), []string{"e", "a", "d"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -599,7 +590,6 @@ func TestSortedStringArray_CountValues(t *testing.T) {
|
||||
m1 := array1.CountValues()
|
||||
gtest.Assert(m1["a"], 2)
|
||||
gtest.Assert(m1["d"], 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -611,6 +601,7 @@ func TestSortedStringArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(len(array2), 3)
|
||||
gtest.Assert(len(array2[0]), 2)
|
||||
gtest.Assert(array2[1], []string{"c", "d"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -634,6 +625,195 @@ func TestStringArray_Remove(t *testing.T) {
|
||||
s1 = array1.Remove(3)
|
||||
gtest.Assert(s1, "c")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStringArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedStringArrayFrom(s1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []string) { //互斥锁
|
||||
n1[3] = "e"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains("e"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStringArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewSortedStringArrayFrom(s1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []string) { //读锁
|
||||
n1[3] = "e"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 2)
|
||||
gtest.Assert(a1.Contains("e"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStringArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
//n1 := []int{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewSortedStringArrayFrom(s1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
b6 := garray.NewSortedArrayFrom(in1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 8)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 11)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 13)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 17)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 20)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 22)
|
||||
gtest.Assert(a1.Merge(b6).Len(), 26)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringArray_SortFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := []string{"a", "b", "d", "c"}
|
||||
a1 := garray.NewStringArrayFrom(s1)
|
||||
func1 := func(v1, v2 string) bool {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2)) < 0
|
||||
}
|
||||
func2 := func(v1, v2 string) bool {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2)) > 0
|
||||
}
|
||||
|
||||
a2 := a1.SortFunc(func1)
|
||||
gtest.Assert(a2, []string{"a", "b", "c", "d"})
|
||||
|
||||
a3 := a1.SortFunc(func2)
|
||||
gtest.Assert(a3, []string{"d", "c", "b", "a"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestStringArray_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewStringArrayFrom(s1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []string) { //互斥锁
|
||||
n1[3] = "f"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 相差大于0.6秒,说明在读取a1.len时,发生了等待。 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertGT(t2-t1, 600)
|
||||
gtest.Assert(a1.Contains("f"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringArray_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := []string{"a", "b", "c", "d"}
|
||||
a1 := garray.NewStringArrayFrom(s1)
|
||||
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []string) { //读锁
|
||||
n1[2] = "g"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
a1.Len()
|
||||
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
|
||||
}()
|
||||
|
||||
t1 := <-ch1
|
||||
t2 := <-ch1
|
||||
// 由于另一个goroutine加的读锁,其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
|
||||
// 防止ci抖动,以豪秒为单位
|
||||
gtest.AssertLT(t2-t1, 2)
|
||||
gtest.Assert(a1.Contains("g"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringArray_Merge(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
//n1 := []int{1, 2, 4, 3}
|
||||
n2 := []int{7, 8, 9}
|
||||
n3 := []int{3, 6}
|
||||
|
||||
s1 := []string{"a", "b", "c"}
|
||||
in1 := []interface{}{1, "a", 2, "b"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
|
||||
a1 := garray.NewStringArrayFrom(s1)
|
||||
b1 := garray.NewStringArrayFrom(s1)
|
||||
b2 := garray.NewIntArrayFrom(n3)
|
||||
b3 := garray.NewArrayFrom(in1)
|
||||
b4 := garray.NewSortedStringArrayFrom(s1)
|
||||
b5 := garray.NewSortedIntArrayFrom(n3)
|
||||
b6 := garray.NewSortedArrayFrom(in1, func1)
|
||||
|
||||
gtest.Assert(a1.Merge(n2).Len(), 6)
|
||||
gtest.Assert(a1.Merge(n3).Len(), 8)
|
||||
gtest.Assert(a1.Merge(b1).Len(), 11)
|
||||
gtest.Assert(a1.Merge(b2).Len(), 13)
|
||||
gtest.Assert(a1.Merge(b3).Len(), 17)
|
||||
gtest.Assert(a1.Merge(b4).Len(), 20)
|
||||
gtest.Assert(a1.Merge(b5).Len(), 22)
|
||||
gtest.Assert(a1.Merge(b6).Len(), 26)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user