Skip to content

Commit

Permalink
#102 Change Repeat raises panic if count < 0
Browse files Browse the repository at this point in the history
  • Loading branch information
meian committed Feb 8, 2022
1 parent 1fa3457 commit 8322eaa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
16 changes: 12 additions & 4 deletions repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ type repeatIterator[T any] struct {
//
// itb = gcf.Repeat(1, 3)
//
// If count is 0 or negative, return Iterable with no element.
// If count is 0, returns empty Iterable.
// If count is negative, raises panic.
func Repeat[T any](v T, count int) Iterable[T] {
if count < 1 {
if count < 0 {
panic("count for Repeat must not be negative.")
}
if count == 0 {
return empty[T]()
}
if count == 1 {
Expand Down Expand Up @@ -70,12 +74,16 @@ type repeatIterableIterator[T any] struct {
// itb := gcf.FromSlice(s)
// itb = gcf.RepeatIterable(itb, 3)
//
// If count is 0 or negative, return Iterable with no element.
// If count is 0, returns empty Iterable.
// If count is negative, raises panic.
func RepeatIterable[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for RepeatIterable must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return empty[T]()
}
if count == 1 {
Expand Down
60 changes: 37 additions & 23 deletions repeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

func TestRepeat(t *testing.T) {
tests := []struct {
name string
v int
count int
want []int
name string
v int
count int
want []int
wantPanic bool
}{
{
name: "3 times",
Expand All @@ -34,15 +35,21 @@ func TestRepeat(t *testing.T) {
want: []int{},
},
{
name: "negative times",
v: 1,
count: -1,
want: []int{},
name: "negative times",
v: 1,
count: -1,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
if tt.wantPanic {
assert.Panics(func() {
_ = gcf.Repeat(tt.v, tt.count)
})
return
}
itb := gcf.Repeat(tt.v, tt.count)
s := gcf.ToSlice(itb)
assert.Equal(tt.want, s)
Expand All @@ -55,9 +62,6 @@ func TestRepeat(t *testing.T) {
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Repeat(1, 0)
})
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.Repeat(1, -1)
})
}

func ExampleRepeat() {
Expand All @@ -69,10 +73,11 @@ func ExampleRepeat() {

func TestRepeatIterable(t *testing.T) {
tests := []struct {
name string
itb gcf.Iterable[int]
count int
want []int
name string
itb gcf.Iterable[int]
count int
wantPanic bool
want []int
}{
{
name: "3 times",
Expand Down Expand Up @@ -105,15 +110,27 @@ func TestRepeatIterable(t *testing.T) {
want: []int{},
},
{
name: "negative times",
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
want: []int{},
name: "negative times",
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
wantPanic: true,
},
{
name: "nil and negative",
itb: nil,
count: -1,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
if tt.wantPanic {
assert.Panics(func() {
_ = gcf.RepeatIterable(tt.itb, tt.count)
})
return
}
itb := gcf.RepeatIterable(tt.itb, tt.count)
s := gcf.ToSlice(itb)
assert.Equal(tt.want, s)
Expand All @@ -125,10 +142,7 @@ func TestRepeatIterable(t *testing.T) {
testBeforeAndAfter(t, itb)

testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.RepeatIterable(itb, 0)
})
testEmpties(t, func(itb gcf.Iterable[int]) gcf.Iterable[int] {
return gcf.RepeatIterable(itb, -1)
return gcf.RepeatIterable(itb, 2)
})
}

Expand Down

0 comments on commit 8322eaa

Please sign in to comment.