aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--deque.go9
-rw-r--r--deque_test.go36
2 files changed, 27 insertions, 18 deletions
diff --git a/deque.go b/deque.go
index 1208019..d4ef4be 100644
--- a/deque.go
+++ b/deque.go
@@ -182,15 +182,10 @@ func (q *Deque[T]) PopAll() func(func(T) bool) {
n := len(q.buf)
q.buf = q.buf[:0]
return func(yield func(T) bool) {
- endIdx := q.toPhysicalIdx(n)
- for i := q.head; ; {
- if !yield(q.buf[:cap(q.buf)][i]) {
+ for i := range n {
+ if !yield(q.buf[:cap(q.buf)][q.toPhysicalIdx(i)]) {
return
}
- i = q.wrapAdd(i, 1)
- if i == endIdx {
- break
- }
}
}
}
diff --git a/deque_test.go b/deque_test.go
index 4f298e1..fede96b 100644
--- a/deque_test.go
+++ b/deque_test.go
@@ -260,17 +260,31 @@ func TestReset(t *testing.T) {
func TestPopAll(t *testing.T) {
t.Parallel()
- q := From([]int{1, 2, 3})
- got := make([]int, 0, q.Len())
- for x := range q.PopAll() {
- got = append(got, x)
- }
- if got, want := q.Len(), 0; got != want {
- t.Errorf("Len() = %d, want %d", got, want)
- }
- want := []int{1, 2, 3}
- if !slices.Equal(got, want) {
- t.Errorf("PopAll() returned values %d, want %d", got, want)
+ for _, tc := range []struct {
+ desc string
+ content []int
+ }{{
+ desc: "PopAll",
+ content: []int{1, 2, 3},
+ }, {
+ desc: "PopAllEmpty",
+ content: make([]int, 0, 3),
+ }} {
+ t.Run(tc.desc, func(t *testing.T) {
+ t.Parallel()
+
+ q := From(tc.content)
+ got := make([]int, 0, q.Len())
+ for x := range q.PopAll() {
+ got = append(got, x)
+ }
+ if got, want := q.Len(), 0; got != want {
+ t.Errorf("Len() = %d, want %d", got, want)
+ }
+ if !slices.Equal(got, tc.content) {
+ t.Errorf("PopAll() returned values %d, want %d", got, tc.content)
+ }
+ })
}
}