Skip to content

Commit b0650f3

Browse files
Improve the code coverage of the garray,glist module (gogf#1908)
1 parent b62b2f3 commit b0650f3

9 files changed

+337
-1
lines changed

container/garray/garray_z_example_normal_any_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func ExampleNew() {
4646
a.Set(0, 100)
4747
fmt.Println(a.Slice())
4848

49+
fmt.Println(a.At(0))
50+
4951
// Search item and return its index.
5052
fmt.Println(a.Search(5))
5153

@@ -66,6 +68,7 @@ func ExampleNew() {
6668
// false
6769
// [0 1 2 3 4 5 6 7 8 9 10 11]
6870
// [100 1 2 3 4 5 6 7 8 9 10 11]
71+
// 100
6972
// 5
7073
// [1 2 3 4 5 6 7 8 9 10 11]
7174
// [1 2 3 4 5 6 7 8 9 10 11]

container/garray/garray_z_example_normal_int_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func ExampleNewIntArraySize() {
5454
// [10 20 15] 3 5
5555
}
5656

57+
func ExampleNewIntArrayRange() {
58+
s := garray.NewIntArrayRange(1, 5, 1)
59+
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
60+
61+
// Output:
62+
// [1 2 3 4 5] 5 5
63+
}
64+
5765
func ExampleNewIntArrayFrom() {
5866
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
5967
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
@@ -83,9 +91,12 @@ func ExampleIntArray_Get() {
8391
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
8492
sGet, sBool := s.Get(3)
8593
fmt.Println(sGet, sBool)
94+
sGet, sBool = s.Get(99)
95+
fmt.Println(sGet, sBool)
8696

8797
// Output:
8898
// 30 true
99+
// 0 false
89100
}
90101

91102
func ExampleIntArray_Set() {

container/garray/garray_z_unit_normal_any_test.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@ func Test_Array_Basic(t *testing.T) {
2525
array := garray.NewArrayFrom(expect)
2626
array2 := garray.NewArrayFrom(expect)
2727
array3 := garray.NewArrayFrom([]interface{}{})
28+
array4 := garray.NewArrayRange(1, 5, 1)
29+
2830
t.Assert(array.Slice(), expect)
2931
t.Assert(array.Interfaces(), expect)
30-
array.Set(0, 100)
32+
err := array.Set(0, 100)
33+
t.AssertNil(err)
34+
35+
err = array.Set(100, 100)
36+
t.AssertNE(err, nil)
37+
38+
t.Assert(array.IsEmpty(), false)
39+
40+
copyArray := array.DeepCopy()
41+
ca := copyArray.(*garray.Array)
42+
ca.Set(0, 1)
43+
cval, _ := ca.Get(0)
44+
val, _ := array.Get(0)
45+
t.AssertNE(cval, val)
3146

3247
v, ok := array.Get(0)
3348
t.Assert(v, 100)
@@ -37,6 +52,10 @@ func Test_Array_Basic(t *testing.T) {
3752
t.Assert(v, 1)
3853
t.Assert(ok, true)
3954

55+
v, ok = array.Get(4)
56+
t.Assert(v, nil)
57+
t.Assert(ok, false)
58+
4059
t.Assert(array.Search(100), 0)
4160
t.Assert(array3.Search(100), -1)
4261
t.Assert(array.Contains(100), true)
@@ -71,6 +90,12 @@ func Test_Array_Basic(t *testing.T) {
7190
array.InsertAfter(6, 400)
7291
t.Assert(array.Slice(), []interface{}{100, 200, 2, 2, 3, 300, 4, 400})
7392
t.Assert(array.Clear().Len(), 0)
93+
err = array.InsertBefore(99, 9900)
94+
t.AssertNE(err, nil)
95+
err = array.InsertAfter(99, 9900)
96+
t.AssertNE(err, nil)
97+
98+
t.Assert(array4.String(), "[1,2,3,4,5]")
7499
})
75100
}
76101

@@ -99,6 +124,11 @@ func TestArray_Unique(t *testing.T) {
99124
array := garray.NewArrayFrom(expect)
100125
t.Assert(array.Unique().Slice(), []interface{}{1, 2, 3, 4, 5})
101126
})
127+
gtest.C(t, func(t *gtest.T) {
128+
expect := []interface{}{}
129+
array := garray.NewArrayFrom(expect)
130+
t.Assert(array.Unique().Slice(), []interface{}{})
131+
})
102132
}
103133

104134
func TestArray_PushAndPop(t *testing.T) {
@@ -382,6 +412,21 @@ func TestArray_Rand(t *testing.T) {
382412
t.Assert(a1.Contains(i1), true)
383413
t.Assert(a1.Len(), 4)
384414
})
415+
416+
gtest.C(t, func(t *gtest.T) {
417+
a1 := []interface{}{}
418+
array1 := garray.NewArrayFrom(a1)
419+
rand, found := array1.Rand()
420+
t.AssertNil(rand)
421+
t.Assert(found, false)
422+
})
423+
424+
gtest.C(t, func(t *gtest.T) {
425+
a1 := []interface{}{}
426+
array1 := garray.NewArrayFrom(a1)
427+
rand := array1.Rands(1)
428+
t.AssertNil(rand)
429+
})
385430
}
386431

387432
func TestArray_Shuffle(t *testing.T) {
@@ -412,13 +457,21 @@ func TestArray_Join(t *testing.T) {
412457
array1 := garray.NewArrayFrom(a1)
413458
t.Assert(array1.Join("."), `0.1."a".\a`)
414459
})
460+
461+
gtest.C(t, func(t *gtest.T) {
462+
a1 := []interface{}{}
463+
array1 := garray.NewArrayFrom(a1)
464+
t.Assert(len(array1.Join(".")), 0)
465+
})
415466
}
416467

417468
func TestArray_String(t *testing.T) {
418469
gtest.C(t, func(t *gtest.T) {
419470
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
420471
array1 := garray.NewArrayFrom(a1)
421472
t.Assert(array1.String(), `[0,1,2,3,4,5,6]`)
473+
array1 = nil
474+
t.Assert(array1.String(), "")
422475
})
423476
}
424477

container/garray/garray_z_unit_normal_int_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ func Test_IntArray_Basic(t *testing.T) {
6363
array.InsertAfter(6, 400)
6464
t.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400})
6565
t.Assert(array.Clear().Len(), 0)
66+
err := array.InsertBefore(99, 300)
67+
t.AssertNE(err, nil)
68+
err = array.InsertAfter(99, 400)
69+
t.AssertNE(err, nil)
70+
})
71+
72+
gtest.C(t, func(t *gtest.T) {
73+
array := garray.NewIntArrayFrom([]int{0, 1, 2, 3})
74+
copyArray := array.DeepCopy().(*garray.IntArray)
75+
copyArray.Set(0, 1)
76+
cval, _ := copyArray.Get(0)
77+
val, _ := array.Get(0)
78+
t.AssertNE(cval, val)
6679
})
6780
}
6881

@@ -89,6 +102,8 @@ func TestIntArray_Unique(t *testing.T) {
89102
expect := []int{1, 2, 3, 4, 5, 3, 2, 2, 3, 5, 5}
90103
array := garray.NewIntArrayFrom(expect)
91104
t.Assert(array.Unique().Slice(), []int{1, 2, 3, 4, 5})
105+
array2 := garray.NewIntArrayFrom([]int{})
106+
t.Assert(array2.Unique().Slice(), []int{})
92107
})
93108
}
94109

@@ -374,6 +389,14 @@ func TestIntArray_Rand(t *testing.T) {
374389
v, ok := array1.Rand()
375390
t.AssertIN(v, a1)
376391
t.Assert(ok, true)
392+
393+
array2 := garray.NewIntArrayFrom([]int{})
394+
v, ok = array2.Rand()
395+
t.Assert(v, 0)
396+
t.Assert(ok, false)
397+
398+
intSlices := array2.Rands(1)
399+
t.Assert(intSlices, nil)
377400
})
378401
}
379402

@@ -420,6 +443,8 @@ func TestIntArray_String(t *testing.T) {
420443
a1 := []int{0, 1, 2, 3, 4, 5, 6}
421444
array1 := garray.NewIntArrayFrom(a1)
422445
t.Assert(array1.String(), "[0,1,2,3,4,5,6]")
446+
array1 = nil
447+
t.Assert(array1.String(), "")
423448
})
424449
}
425450

container/garray/garray_z_unit_normal_str_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func Test_StrArray_Basic(t *testing.T) {
3434
t.Assert(v, 100)
3535
t.Assert(ok, true)
3636

37+
v, ok = array3.Get(0)
38+
t.Assert(v, "")
39+
t.Assert(ok, false)
40+
3741
t.Assert(array.Search("100"), 0)
3842
t.Assert(array.Contains("100"), true)
3943

@@ -61,12 +65,28 @@ func Test_StrArray_Basic(t *testing.T) {
6165
t.Assert(array.Clear().Len(), 0)
6266
t.Assert(array2.Slice(), expect)
6367
t.Assert(array3.Search("100"), -1)
68+
err := array.InsertBefore(99, "300")
69+
t.AssertNE(err, nil)
70+
array.InsertAfter(99, "400")
71+
t.AssertNE(err, nil)
72+
73+
})
74+
75+
gtest.C(t, func(t *gtest.T) {
76+
array := garray.NewStrArrayFrom([]string{"0", "1", "2", "3"})
77+
78+
copyArray := array.DeepCopy().(*garray.StrArray)
79+
copyArray.Set(0, "1")
80+
cval, _ := copyArray.Get(0)
81+
val, _ := array.Get(0)
82+
t.AssertNE(cval, val)
6483
})
6584
}
6685

6786
func TestStrArray_ContainsI(t *testing.T) {
6887
gtest.C(t, func(t *gtest.T) {
6988
s := garray.NewStrArray()
89+
t.Assert(s.Contains("A"), false)
7090
s.Append("a", "b", "C")
7191
t.Assert(s.Contains("A"), false)
7292
t.Assert(s.Contains("a"), true)
@@ -94,6 +114,8 @@ func TestStrArray_Unique(t *testing.T) {
94114
expect := []string{"1", "1", "2", "2", "3", "3", "2", "2"}
95115
array := garray.NewStrArrayFrom(expect)
96116
t.Assert(array.Unique().Slice(), []string{"1", "2", "3"})
117+
array1 := garray.NewStrArrayFrom([]string{})
118+
t.Assert(array1.Unique().Slice(), []string{})
97119
})
98120
}
99121

@@ -364,6 +386,13 @@ func TestStrArray_Rand(t *testing.T) {
364386
v, ok := array1.Rand()
365387
t.Assert(ok, true)
366388
t.AssertIN(v, a1)
389+
390+
array2 := garray.NewStrArrayFrom([]string{})
391+
v, ok = array2.Rand()
392+
t.Assert(ok, false)
393+
t.Assert(v, "")
394+
strArray := array2.Rands(1)
395+
t.AssertNil(strArray)
367396
})
368397
}
369398

@@ -406,13 +435,21 @@ func TestStrArray_Join(t *testing.T) {
406435
array1 := garray.NewStrArrayFrom(a1)
407436
t.Assert(array1.Join("."), `0.1."a".\a`)
408437
})
438+
gtest.C(t, func(t *gtest.T) {
439+
a1 := []string{}
440+
array1 := garray.NewStrArrayFrom(a1)
441+
t.Assert(array1.Join("."), "")
442+
})
409443
}
410444

411445
func TestStrArray_String(t *testing.T) {
412446
gtest.C(t, func(t *gtest.T) {
413447
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
414448
array1 := garray.NewStrArrayFrom(a1)
415449
t.Assert(array1.String(), `["0","1","2","3","4","5","6"]`)
450+
451+
array1 = nil
452+
t.Assert(array1.String(), "")
416453
})
417454
}
418455

0 commit comments

Comments
 (0)