From 8291eabc2c1746b14b6adba92ebb40559b2fe56b Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 15 Oct 2020 14:25:10 +0800 Subject: [PATCH] assert len > 0 --- core/collection/ring.go | 4 ++++ core/collection/ring_test.go | 6 ++++++ core/fx/fn.go | 5 +++++ core/fx/fn_test.go | 8 ++++++++ 4 files changed, 23 insertions(+) diff --git a/core/collection/ring.go b/core/collection/ring.go index 17060c06..0690fd8f 100644 --- a/core/collection/ring.go +++ b/core/collection/ring.go @@ -6,6 +6,10 @@ type Ring struct { } func NewRing(n int) *Ring { + if n < 1 { + panic("n should be greater than 0") + } + return &Ring{ elements: make([]interface{}, n), } diff --git a/core/collection/ring_test.go b/core/collection/ring_test.go index 182b67f8..110170e0 100644 --- a/core/collection/ring_test.go +++ b/core/collection/ring_test.go @@ -6,6 +6,12 @@ import ( "github.com/stretchr/testify/assert" ) +func TestNewRing(t *testing.T) { + assert.Panics(t, func() { + NewRing(0) + }) +} + func TestRingLess(t *testing.T) { ring := NewRing(5) for i := 0; i < 3; i++ { diff --git a/core/fx/fn.go b/core/fx/fn.go index a38e3c59..197bcfea 100644 --- a/core/fx/fn.go +++ b/core/fx/fn.go @@ -162,6 +162,7 @@ func (p Stream) Head(n int64) Stream { if n < 1 { panic("n must be greater than 0") } + source := make(chan interface{}) go func() { @@ -247,6 +248,10 @@ func (p Stream) Sort(less LessFunc) Stream { } func (p Stream) Tail(n int64) Stream { + if n < 1 { + panic("n should be greater than 0") + } + source := make(chan interface{}) go func() { diff --git a/core/fx/fn_test.go b/core/fx/fn_test.go index f3240b5e..7033b2af 100644 --- a/core/fx/fn_test.go +++ b/core/fx/fn_test.go @@ -294,6 +294,14 @@ func TestTail(t *testing.T) { assert.Equal(t, 7, result) } +func TestTailZero(t *testing.T) { + assert.Panics(t, func() { + Just(1, 2, 3, 4).Tail(0).Reduce(func(pipe <-chan interface{}) (interface{}, error) { + return nil, nil + }) + }) +} + func TestWalk(t *testing.T) { var result int Just(1, 2, 3, 4, 5).Walk(func(item interface{}, pipe chan<- interface{}) {