Skip to content

Commit

Permalink
#102 Change Range then raises panic if step == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
meian committed Feb 8, 2022
1 parent 1fa3457 commit a2d913c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bench/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func BenchmarkRange(b *testing.B) {
},
}
for _, tt := range tests {
itb, _ := gcf.Range(tt.start, tt.end, tt.step)
itb := gcf.Range(tt.start, tt.end, tt.step)
c := 0
it := itb.Iterator()
for it.MoveNext() {
Expand Down
7 changes: 0 additions & 7 deletions error.go

This file was deleted.

14 changes: 7 additions & 7 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ type rangeDecrementIterator[T constraints.Integer] struct {
//
// If step is positive, elements is enumerated from start to end with increment by step.
// If step is negative, elements is enumerated from start to end with decrement by step.
// If step is zero, returns error.
// If step is zero, raises panic.
// If start equals to end, makes Iterable with one element.
// If direction of step is opposite to start and end, returns empty Iterable.
func Range[T constraints.Integer](start, end, step T) (Iterable[T], error) {
func Range[T constraints.Integer](start, end, step T) Iterable[T] {
if step == 0 {
return nil, errStep0
panic("step for Range must not be zero.")
}
if start == end {
return FromSlice([]T{start}), nil
return FromSlice([]T{start})
}
if step > 0 && start > end {
return empty[T](), nil
return empty[T]()
}
if step < 0 && start < end {
return empty[T](), nil
return empty[T]()
}
return &rangeIterable[T]{start, end, step}, nil
return &rangeIterable[T]{start, end, step}
}

func (itb *rangeIterable[T]) Iterator() Iterator[T] {
Expand Down
32 changes: 16 additions & 16 deletions range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func TestRange(t *testing.T) {
step int
}
tests := []struct {
name string
args args
wantErr bool
want []int
name string
args args
wantPanic bool
want []int
}{
{
name: "positive step",
Expand Down Expand Up @@ -117,43 +117,43 @@ func TestRange(t *testing.T) {
end: 2,
step: 0,
},
wantErr: true,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
itb, err := gcf.Range(tt.args.start, tt.args.end, tt.args.step)
if tt.wantErr {
assert.Error(err)
assert.Empty(itb)
if tt.wantPanic {
assert.Panics(func() {
_ = gcf.Range(tt.args.start, tt.args.end, tt.args.step)
})
return
}
assert.NoError(err)
itb := gcf.Range(tt.args.start, tt.args.end, tt.args.step)
s := gcf.ToSlice(itb)
assert.Equal(tt.want, s)
})
}

itb, _ := gcf.Range(1, 3, 1)
itb := gcf.Range(1, 3, 1)
testBeforeAndAfter(t, itb)
itb, _ = gcf.Range(3, 1, -1)
itb = gcf.Range(3, 1, -1)
testBeforeAndAfter(t, itb)

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

func ExampleRange() {
itb, _ := gcf.Range(2, 10, 2)
itb := gcf.Range(2, 10, 2)
fmt.Println(gcf.ToSlice(itb))
itb, _ = gcf.Range(10, 1, -2)
itb = gcf.Range(10, 1, -2)
fmt.Println(gcf.ToSlice(itb))
// Output:
// [2 4 6 8 10]
Expand Down

0 comments on commit a2d913c

Please sign in to comment.