mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add tests for fib function
This commit is contained in:
committed by
Dan Christian Bogos
parent
7003a2457d
commit
89d320f2e1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user