Update Fib function to return time.Duration in seconds instead of int

This commit is contained in:
ionutboangiu
2022-05-23 16:25:52 +03:00
committed by Dan Christian Bogos
parent 89bcb7a474
commit d53d785c34
2 changed files with 15 additions and 15 deletions

View File

@@ -479,18 +479,18 @@ func copyFile(rc io.ReadCloser, path string, fm os.FileMode) (err error) {
return
}
// successive Fibonacci numbers.
func Fib() func() int {
// Fib returns successive Fibonacci numbers converted to seconds.
func Fib() func() time.Duration {
a, b := 0, 1
return func() int {
return func() time.Duration {
a, b = b, a+b
return a
return time.Duration(a) * time.Second
}
}
func FibDuration(mult time.Duration) func() time.Duration {
fib := Fib()
return func() time.Duration {
return time.Duration(fib()) * mult
return fib() * mult
}
}

View File

@@ -1081,20 +1081,20 @@ func TestClone(t *testing.T) {
func TestFib(t *testing.T) {
fib := Fib()
if tmp := fib(); tmp != 1 {
t.Error("Expecting: 1, received ", tmp)
if tmp := fib(); tmp != 1*time.Second {
t.Error("Expecting: 1s, received ", tmp)
}
if tmp := fib(); tmp != 1 {
t.Error("Expecting: 1, received ", tmp)
if tmp := fib(); tmp != 1*time.Second {
t.Error("Expecting: 1s, received ", tmp)
}
if tmp := fib(); tmp != 2 {
t.Error("Expecting: 2, received ", tmp)
if tmp := fib(); tmp != 2*time.Second {
t.Error("Expecting: 2s, received ", tmp)
}
if tmp := fib(); tmp != 3 {
t.Error("Expecting: 3, received ", tmp)
if tmp := fib(); tmp != 3*time.Second {
t.Error("Expecting: 3s, received ", tmp)
}
if tmp := fib(); tmp != 5 {
t.Error("Expecting: 5, received ", tmp)
if tmp := fib(); tmp != 5*time.Second {
t.Error("Expecting: 5s, received ", tmp)
}
}