Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions arraydeque/arraydeque.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ type ArrayDeque[T any] struct {
size int
}

func (d *ArrayDeque[T]) AddAll(c collections.Collection[T]) {
//TODO implement me
panic("implement me")
}

// Config holds the values for configuring a ArrayDeque.
type Config struct {
Capacity int
Expand Down Expand Up @@ -161,6 +156,12 @@ func (d *ArrayDeque[T]) RemoveBack() T {
return t
}

func (d *ArrayDeque[T]) AddAll(c collections.Collection[T]) {
for t := range c.All() {
d.Add(t)
}
}

// Size returns the number of elements in this deque.
func (d *ArrayDeque[T]) Size() int {
return d.size
Expand Down
59 changes: 59 additions & 0 deletions arraydeque/arraydeque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arraydeque

import (
"github.com/lock14/collections"
"iter"
"slices"
"testing"

Expand Down Expand Up @@ -250,4 +251,62 @@ func TestType(t *testing.T) {
testType[int](l)
}

func TestArrayDeque_AddAll(t *testing.T) {
t.Parallel()
cases := []struct {
name string
items []int
want []int
}{
{
name: "add_none",
items: []int{},
want: nil,
},
{
name: "add_one",
items: []int{1},
want: []int{1},
},
{
name: "add_up_to_default_capacity",
items: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
want: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
{
name: "add_double_capacity",
items: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
want: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
d := New[int]()
d.AddAll(&sliceCollection[int]{tc.items})
got := slices.Collect(d.All())
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("wrong string value, -got,+want: %s", diff)
}
})
}
}

func testType[T any](deque collections.Deque[T]) {}

type sliceCollection[T any] struct {
s []T
}

func (sc *sliceCollection[T]) All() iter.Seq[T] {
return slices.Values(sc.s)
}

func (sc *sliceCollection[T]) Size() int {
return len(sc.s)
}

func (sc *sliceCollection[T]) Empty() bool {
return sc.Size() == 0
}
Loading