继续完善garray的测试

This commit is contained in:
jroam
2019-06-21 17:46:42 +08:00
parent f30c9020fa
commit adf2ddb510
4 changed files with 53 additions and 9 deletions

View File

@ -31,6 +31,8 @@ func Test_SortedIntArray1(t *testing.T) {
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) {

View File

@ -20,12 +20,15 @@ import (
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)
@ -46,13 +49,20 @@ 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)
})
}
@ -100,10 +110,14 @@ 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(-1, 10), value1)
gtest.Assert(array1.Range(8, 2), nil)
gtest.Assert(array2.Range(2, 4), []int{2, 3})
})
}
@ -147,6 +161,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})
})
}
@ -155,7 +170,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})
@ -179,6 +195,7 @@ func TestIntArray_SubSlice(t *testing.T) {
gtest.Assert(array1.SubSlice(0, 2), []int{0, 1})
gtest.Assert(array1.SubSlice(2, 2), []int{2, 3})
gtest.Assert(array1.SubSlice(5, 8), []int{5, 6})
gtest.Assert(array1.SubSlice(8, 2), nil)
})
}
@ -400,6 +417,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})
@ -413,6 +431,10 @@ func TestSortedIntArray_Range(t *testing.T) {
nsl := array1.Range(5, 8)
gtest.Assert(len(nsl), 1)
ns4 := array2.Range(2, 2)
t.Log(array2)
gtest.Assert(len(ns4), 2)
})
}

View File

@ -12,7 +12,7 @@ import (
"github.com/gogf/gf/g/container/garray"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gconv"
"github.com/gogf/gf/g/internal/rwmutex"
"strings"
"testing"
"time"
@ -223,22 +223,24 @@ func TestArray_SubSlice(t *testing.T) {
gtest.Assert(array1.SubSlice(5, 8), []interface{}{5, 6})
gtest.Assert(array1.SubSlice(8, 1), nil)
//array2 := garray.NewArrayFrom(a1,false)
array2 := garray.NewArrayFrom(a1,false)
gtest.Assert(array2.SubSlice(2, 2), []interface{}{2, 3})
//gtest.Assert(array2.SubSlice(2, 2), []interface{}{3, 4})
a2 := []interface{}{0, 1, 2, 3, 4, 5, 6}
array3 := garray.NewArrayFrom(a2,false) //@todo 如果这里为 true 时,为报错
gtest.Assert(array3.SubSlice(2, 2), []interface{}{2, 3})
})
}
func TestRwMutex(t *testing.T){
mu:=rwmutex.New(true)
gtest.Assert(mu.IsSafe(),false) // @todo 不理解为什么
}
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)
@ -791,3 +793,4 @@ func TestArray_RLockFunc(t *testing.T) {
}

View File

@ -20,12 +20,15 @@ import (
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)
@ -46,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)
})
}
@ -100,10 +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)
gtest.Assert(array1.Range(8, 1), nil)
gtest.Assert(len(array2.Range(2, 4)), 2)
})
}
@ -152,6 +162,7 @@ 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)
})
}
@ -173,9 +184,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)
})
}
@ -780,12 +791,17 @@ func TestStringArray_Merge(t *testing.T) {
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)
@ -794,4 +810,5 @@ func TestStringArray_Merge(t *testing.T) {
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)
}