add some garray tests

This commit is contained in:
jroam 2019-06-25 18:17:10 +08:00
parent 735c5fc7ed
commit 055c6a668e
4 changed files with 57 additions and 4 deletions

View File

@ -38,10 +38,13 @@ func Test_SortedIntArray1(t *testing.T) {
func Test_SortedIntArray2(t *testing.T) {
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) {
@ -51,15 +54,19 @@ func Test_SortedStringArray1(t *testing.T) {
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()
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) {
@ -78,10 +85,16 @@ func Test_SortedArray2(t *testing.T) {
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)
}
func TestNewFromCopy(t *testing.T) {

View File

@ -737,12 +737,17 @@ func TestSortedIntArray_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.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)
@ -751,6 +756,8 @@ func TestSortedIntArray_Merge(t *testing.T) {
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) {

View File

@ -159,6 +159,7 @@ func TestArray_Merge(t *testing.T) {
}
a1 := garray.NewArrayFrom(n1)
a11 := garray.NewSortedArrayFrom(n1,func1)
b1 := garray.NewStringArrayFrom(s1)
b2:=garray.NewIntArrayFrom(n3)
b3:=garray.NewArrayFrom(in1)
@ -174,6 +175,8 @@ func TestArray_Merge(t *testing.T) {
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)
})
}
@ -566,6 +569,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)
@ -579,6 +583,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"})
})
}
@ -657,6 +665,7 @@ func TestSortedArray_SubSlice(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.SubSlice(2, 3)
gtest.Assert(len(i1), 3)
gtest.Assert(i1, []interface{}{"c", "d", "e"})
@ -668,6 +677,15 @@ 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)
})
}

View File

@ -377,7 +377,7 @@ func TestSortedStringArray_Sort(t *testing.T) {
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"})
@ -496,6 +496,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"})
@ -504,9 +505,11 @@ 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"})
})
}
@ -546,6 +549,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"})
@ -556,6 +560,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"})
})
}
@ -621,6 +629,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)
})
}
@ -705,12 +714,17 @@ func TestSortedStringArray_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.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)
@ -719,6 +733,7 @@ func TestSortedStringArray_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)
}
func TestStringArray_SortFunc(t *testing.T) {