Add tests for fib function

This commit is contained in:
ionutboangiu
2022-08-01 17:43:03 +03:00
committed by Dan Christian Bogos
parent 7003a2457d
commit 89d320f2e1
2 changed files with 27 additions and 2 deletions

View File

@@ -479,11 +479,11 @@ func copyFile(rc io.ReadCloser, path string, fm os.FileMode) (err error) {
return
}
// Fib returns successive Fibonacci numbers
// Fib returns successive Fibonacci numbers.
func Fib() func() int {
a, b := 0, 1
return func() int {
if b > 0 {
if b > 0 { // only increment Fibonacci numbers while b doesn't overflow
a, b = b, a+b
}
return a

View File

@@ -1860,3 +1860,28 @@ func TestCoreUtilsSplitPath(t *testing.T) {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
func TestCoreUtilsFibSeqNrOverflow(t *testing.T) {
fib := Fib()
for i := 0; i < 92; i++ { // the 93rd fibonacci number in the sequence would normally overflow
fib()
}
exp := fib()
for i := 0; i < 100; i++ {
if rcv := fib(); rcv != exp {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
}
func TestCoreUtilsFibDurationSeqNrOverflow(t *testing.T) {
fib := FibDuration(time.Second, 0)
for i := 0; i < 49; i++ { // the 50th fibonacci number in the sequence would normally overflow when multiplied with time.Second
fib()
}
for i := 0; i < 100; i++ {
if rcv := fib(); rcv != AbsoluteMaxDuration {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", AbsoluteMaxDuration, rcv)
}
}
}