From 0876e00eb8c8eb5e23a3cf952c3fb423a36623d2 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 26 Dec 2022 19:28:01 +0800 Subject: [PATCH] fix issue in NewIntArrayRange function for package garray (#2374) --- container/garray/garray_normal_any.go | 4 ++-- container/garray/garray_normal_int.go | 4 ++-- container/garray/garray_sorted_any.go | 4 ++-- container/garray/garray_sorted_int.go | 4 ++-- container/garray/garray_z_example_normal_int_test.go | 2 +- container/garray/garray_z_unit_normal_int_test.go | 11 +++++++++++ 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/container/garray/garray_normal_any.go b/container/garray/garray_normal_any.go index 52fb3b246..3062d0ab8 100644 --- a/container/garray/garray_normal_any.go +++ b/container/garray/garray_normal_any.go @@ -59,10 +59,10 @@ func NewArrayRange(start, end, step int, safe ...bool) *Array { if step == 0 { panic(fmt.Sprintf(`invalid step value: %d`, step)) } - slice := make([]interface{}, (end-start+1)/step) + slice := make([]interface{}, 0) index := 0 for i := start; i <= end; i += step { - slice[index] = i + slice = append(slice, i) index++ } return NewArrayFrom(slice, safe...) diff --git a/container/garray/garray_normal_int.go b/container/garray/garray_normal_int.go index d8b5e2f34..da14a81a2 100644 --- a/container/garray/garray_normal_int.go +++ b/container/garray/garray_normal_int.go @@ -51,10 +51,10 @@ func NewIntArrayRange(start, end, step int, safe ...bool) *IntArray { if step == 0 { panic(fmt.Sprintf(`invalid step value: %d`, step)) } - slice := make([]int, (end-start+1)/step) + slice := make([]int, 0) index := 0 for i := start; i <= end; i += step { - slice[index] = i + slice = append(slice, i) index++ } return NewIntArrayFrom(slice, safe...) diff --git a/container/garray/garray_sorted_any.go b/container/garray/garray_sorted_any.go index 73a772a55..2469ac449 100644 --- a/container/garray/garray_sorted_any.go +++ b/container/garray/garray_sorted_any.go @@ -61,10 +61,10 @@ func NewSortedArrayRange(start, end, step int, comparator func(a, b interface{}) if step == 0 { panic(fmt.Sprintf(`invalid step value: %d`, step)) } - slice := make([]interface{}, (end-start+1)/step) + slice := make([]interface{}, 0) index := 0 for i := start; i <= end; i += step { - slice[index] = i + slice = append(slice, i) index++ } return NewSortedArrayFrom(slice, comparator, safe...) diff --git a/container/garray/garray_sorted_int.go b/container/garray/garray_sorted_int.go index 32ab672f0..b2df6e9b9 100644 --- a/container/garray/garray_sorted_int.go +++ b/container/garray/garray_sorted_int.go @@ -62,10 +62,10 @@ func NewSortedIntArrayRange(start, end, step int, safe ...bool) *SortedIntArray if step == 0 { panic(fmt.Sprintf(`invalid step value: %d`, step)) } - slice := make([]int, (end-start+1)/step) + slice := make([]int, 0) index := 0 for i := start; i <= end; i += step { - slice[index] = i + slice = append(slice, i) index++ } return NewSortedIntArrayFrom(slice, safe...) diff --git a/container/garray/garray_z_example_normal_int_test.go b/container/garray/garray_z_example_normal_int_test.go index ceaf278ae..308dc9ec1 100644 --- a/container/garray/garray_z_example_normal_int_test.go +++ b/container/garray/garray_z_example_normal_int_test.go @@ -59,7 +59,7 @@ func ExampleNewIntArrayRange() { fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) // Output: - // [1 2 3 4 5] 5 5 + // [1 2 3 4 5] 5 8 } func ExampleNewIntArrayFrom() { diff --git a/container/garray/garray_z_unit_normal_int_test.go b/container/garray/garray_z_unit_normal_int_test.go index 57300ede8..90dcc0461 100644 --- a/container/garray/garray_z_unit_normal_int_test.go +++ b/container/garray/garray_z_unit_normal_int_test.go @@ -813,3 +813,14 @@ func TestIntArray_Walk(t *testing.T) { }), g.Slice{11, 12}) }) } + +func TestIntArray_NewIntArrayRange(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayRange(0, 128, 4) + t.Assert(array.String(), `[0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124,128]`) + }) + gtest.C(t, func(t *gtest.T) { + array := garray.NewIntArrayRange(1, 128, 4) + t.Assert(array.String(), `[1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125]`) + }) +}