From c41e8356bcdac2e1ee0c657d4eb58dc5ec6f2af6 Mon Sep 17 00:00:00 2001 From: Sungyu Kang Date: Sun, 18 Oct 2020 16:42:56 +0900 Subject: [PATCH 1/2] Fix: Append => Clean => Append Out of range I fixed the index out of range error when I did Append, Clean, and Append. ------------ Issue Code -------------- package main import ( "github.com/sheerun/queue" ) func main() { q := queue.New() for i := 0; i < 50; i++ { q.Append(i) } q.Clean() for i := 0; i < 50; i++ { q.Append(i) // Out of Range! } } --- queue.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/queue.go b/queue.go index 46822f7..f4cf5d4 100644 --- a/queue.go +++ b/queue.go @@ -40,6 +40,9 @@ func (q *Queue) Clean() { q.items = make(map[int64]interface{}) q.ids = make(map[interface{}]int64) q.buf = make([]int64, minQueueLen) + q.tail = 0 + q.head = 0 + q.count = 0 } // Returns the number of elements in queue From 696cf80023b5448ce427735a4a1e576e75b5646d Mon Sep 17 00:00:00 2001 From: gron1gh1 Date: Sun, 18 Oct 2020 20:12:41 +0900 Subject: [PATCH 2/2] Update: queue_test.go --- queue_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/queue_test.go b/queue_test.go index de429e1..2c71e52 100644 --- a/queue_test.go +++ b/queue_test.go @@ -347,6 +347,20 @@ func TestTestQueueClean(t *testing.T) { } } +func TestTestQueueClean2(t *testing.T) { + q := New() + + for i := 0; i < 50; i++ { + q.Append(i) + } + + q.Clean() + + for i := 0; i < 50; i++ { + q.Append(i) + } +} + // General warning: Go's benchmark utility (go test -bench .) increases the number of // iterations until the benchmarks take a reasonable amount of time to run; memory usage // is *NOT* considered. On my machine, these benchmarks hit around ~1GB before they've had