fixing bugs - no longer panics when iteration < 0 or MaxIteration >= len(TheFibonacciSequence)-1
This commit is contained in:
parent
69ca00b29d
commit
b4edee4292
@ -71,6 +71,14 @@ func main() {
|
||||
fmt.Fprintf(os.Stderr, "iteration %d would overflow int64 - using %d\n", iteration, maxIteration)
|
||||
iteration = maxIteration
|
||||
}
|
||||
} else if iteration < 0 {
|
||||
switch doPanic {
|
||||
case true:
|
||||
panic(fmt.Sprintf("error: input %d would cause index out of range exception\n", iteration))
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "error: input %d would cause index out of range exception - using 0\n", iteration)
|
||||
iteration = 0
|
||||
}
|
||||
}
|
||||
|
||||
backoff := &fibonacci.FibonacciBackoff{
|
||||
|
||||
@ -50,8 +50,8 @@ func (f *FibonacciBackoff) Next() time.Duration {
|
||||
}
|
||||
|
||||
// increment f.Iteration if appropriate
|
||||
maxIteration := int64(len(TheFibonacciSequence) + 1)
|
||||
if f.MaxIteration > 0 {
|
||||
maxIteration := int64(len(TheFibonacciSequence) - 1)
|
||||
if f.MaxIteration > 0 && f.MaxIteration <= maxIteration {
|
||||
maxIteration = f.MaxIteration
|
||||
}
|
||||
if f.foundMax == 0 {
|
||||
|
||||
@ -77,6 +77,20 @@ func TestFibonacciBackoffNextDoesNotOverflow(t *testing.T) {
|
||||
}
|
||||
previous = got
|
||||
}
|
||||
backoff.Reset()
|
||||
backoff.PauseMultiplier = time.Nanosecond
|
||||
for i := range len(TheFibonacciSequence) + 100 {
|
||||
var want time.Duration
|
||||
if i >= len(TheFibonacciSequence) {
|
||||
want = time.Duration(TheFibonacciSequence[len(TheFibonacciSequence)-1])
|
||||
} else {
|
||||
want = time.Duration(TheFibonacciSequence[i])
|
||||
}
|
||||
got := backoff.Next()
|
||||
if want != got {
|
||||
t.Fatalf("error: wanted %v; got %v\n", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFibonacciBackoffIterationLimit(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user