diff --git a/list/arraylist/arraylist.go b/list/arraylist/arraylist.go index 859ddda..8b2aa7f 100644 --- a/list/arraylist/arraylist.go +++ b/list/arraylist/arraylist.go @@ -70,10 +70,11 @@ func (l *ArrayList) growth() { log.Panic("list size is over listMaxLimit", listMaxLimit) } - nSize := l.size << 1 + lcap := uint(len(l.data)) + nSize := lcap << 1 temp := make([]interface{}, nSize, nSize) - ghidx := l.size / 2 + ghidx := lcap / 2 gtidx := ghidx + l.size + 1 copy(temp[ghidx+1:], l.data[l.hidx+1:l.tidx]) l.data = temp @@ -98,7 +99,7 @@ func (l *ArrayList) PushFront(values ...interface{}) { func (l *ArrayList) PushBack(values ...interface{}) { psize := uint(len(values)) - for l.tidx+psize > uint(len(l.data)) { + for l.tidx+psize > uint(len(l.data)) { // [0 1 2 3 4 5 6] l.growth() } diff --git a/list/arraylist/arraylist_test.go b/list/arraylist/arraylist_test.go index b8c7ef5..1054b60 100644 --- a/list/arraylist/arraylist_test.go +++ b/list/arraylist/arraylist_test.go @@ -1,6 +1,10 @@ package arraylist import ( + "bytes" + "encoding/gob" + "io/ioutil" + "log" "testing" "github.com/davecgh/go-spew/spew" @@ -8,6 +12,7 @@ import ( func TestPush(t *testing.T) { l := New() + for i := 0; i < 2; i++ { l.PushFront(1) } @@ -24,6 +29,7 @@ func TestPush(t *testing.T) { if result != "[1 1 2 2]" { t.Error(result) } + } func TestGrowth(t *testing.T) { @@ -141,3 +147,25 @@ func TestRemove(t *testing.T) { } } + +func loadTestData() []int { + data, err := ioutil.ReadFile("../../l.log") + if err != nil { + log.Println(err) + } + var l []int + decoder := gob.NewDecoder(bytes.NewReader(data)) + decoder.Decode(&l) + return l +} + +func BenchmarkPush(b *testing.B) { + l := loadTestData() + b.N = len(l) + + arr := New() + + for i := 0; i < b.N; i++ { + arr.PushBack(l[i]) + } +}