Skip to content

Commit

Permalink
Merge pull request #103 from meian/102-handling-zero-or-negative
Browse files Browse the repository at this point in the history
#102 Handling zero or negative.
  • Loading branch information
meian authored Feb 8, 2022
2 parents 1fa3457 + 89ec5c0 commit 082c567
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 51 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
want []int
wantPanic bool
}{
{
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
8 changes: 6 additions & 2 deletions skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type skipIterator[T any] struct {
// itb := gcf.FromSlice([]{1, 2, 3})
// itb = gcf.Skip(itb, 2)
//
// If count is 0 or negative, returns original Iterable.
// If count is 0, returns original Iterable.
// If count is negative, raises panic.
func Skip[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for Skip must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return itb
}
return &skipIterable[T]{itb, count}
Expand Down
8 changes: 6 additions & 2 deletions skip_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ type skipLastIterator[T any] struct {
// itb := gcf.FromSlice([]{1, 2, 3})
// itb = gcf.SkipLast(itb, 2)
//
// If count is 0 or negative, returns original Iterable.
// If count is 0, returns original Iterable.
// If count is negative, raises panic.
func SkipLast[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for SkipLast must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return itb
}
return &skipLastIterable[T]{
Expand Down
23 changes: 19 additions & 4 deletions skip_last_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func TestSkipLast(t *testing.T) {
count int
}
tests := []struct {
name string
args args
want []int
name string
args args
want []int
wantPanic bool
}{
{
name: "skip last 1 from slice 3",
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestSkipLast(t *testing.T) {
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
},
want: []int{1, 2, 3},
wantPanic: true,
},
{
name: "nil Iterable",
Expand All @@ -74,9 +75,23 @@ func TestSkipLast(t *testing.T) {
},
want: []int{},
},
{
name: "nil and negative",
args: args{
itb: nil,
count: -1,
},
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
assert.Panics(t, func() {
_ = gcf.SkipLast(tt.args.itb, tt.args.count)
})
return
}
itb := gcf.SkipLast(tt.args.itb, tt.args.count)
s := gcf.ToSlice(itb)
assert.Equal(t, tt.want, s)
Expand Down
23 changes: 19 additions & 4 deletions skip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func TestSkip(t *testing.T) {
count int
}
tests := []struct {
name string
args args
want []int
name string
args args
want []int
wantPanic bool
}{
{
name: "skip 1 from slice 3",
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestSkip(t *testing.T) {
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
},
want: []int{1, 2, 3},
wantPanic: true,
},
{
name: "nil Iterable",
Expand All @@ -74,9 +75,23 @@ func TestSkip(t *testing.T) {
},
want: []int{},
},
{
name: "nil and negative",
args: args{
itb: nil,
count: -1,
},
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
assert.Panics(t, func() {
_ = gcf.Skip(tt.args.itb, tt.args.count)
})
return
}
itb := gcf.Skip(tt.args.itb, tt.args.count)
s := gcf.ToSlice(itb)
assert.Equal(t, tt.want, s)
Expand Down
8 changes: 6 additions & 2 deletions take.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type takeIterator[T any] struct {
// itb := gcf.FromSlice([]{1, 2, 3})
// itb = gcf.Take(itb, 2)
//
// If count is 0 or negative, returns empty Iterable.
// If count is 0, returns empty Iterable.
// If count is negative, raises panic.
func Take[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for Take must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return empty[T]()
}
return &takeIterable[T]{itb, count}
Expand Down
8 changes: 6 additions & 2 deletions take_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ type takeLastIterator[T any] struct {
// itb := gcf.FromSlice([]{1, 2, 3})
// itb = gcf.TakeLast(itb, 2)
//
// If count is 0 or negative, returns empty Iterable.
// If count is 0, returns empty Iterable.
// If count is negative, raises panic.
func TakeLast[T any](itb Iterable[T], count int) Iterable[T] {
if count < 0 {
panic("count for TakeLast must not be negative.")
}
if isEmpty(itb) {
return orEmpty(itb)
}
if count < 1 {
if count == 0 {
return empty[T]()
}
return &takeLastIterable[T]{itb, count}
Expand Down
23 changes: 19 additions & 4 deletions take_last_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func TestTakeLast(t *testing.T) {
count int
}
tests := []struct {
name string
args args
want []int
name string
args args
want []int
wantPanic bool
}{
{
name: "take last 1 from slice 3",
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestTakeLast(t *testing.T) {
itb: gcf.FromSlice([]int{1, 2, 3}),
count: -1,
},
want: []int{},
wantPanic: true,
},
{
name: "nil Iterable",
Expand All @@ -74,9 +75,23 @@ func TestTakeLast(t *testing.T) {
},
want: []int{},
},
{
name: "nil and negative",
args: args{
itb: nil,
count: -1,
},
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
assert.Panics(t, func() {
_ = gcf.TakeLast(tt.args.itb, tt.args.count)
})
return
}
itb := gcf.TakeLast(tt.args.itb, tt.args.count)
s := gcf.ToSlice(itb)
assert.Equal(t, tt.want, s)
Expand Down
Loading

0 comments on commit 082c567

Please sign in to comment.