# @return a list of sleep delays
#
function get_timeout_delays() {
- local -i timeout=$1
- local -i first_step=1
+ local timeout=$1
+ local first_step=${2:-1}
- local -i i
- local -i total=0
- for (( i = $first_step ; $total + $i <= $timeout ; i *= 2 )) ; do
+ local i
+ local total="0"
+ i=$first_step
+ while test "$(echo $total + $i \<= $timeout | bc -l)" = "1"; do
echo -n "$i "
- total+=$i
+ total=$(echo $total + $i | bc -l)
+ i=$(echo $i \* 2 | bc -l)
done
- if (( $total < $timeout )) ; then
- echo -n $(( $timeout - $total ))
+ if test "$(echo $total \< $timeout | bc -l)" = "1"; then
+ echo -n $(echo $timeout - $total | bc -l)
fi
}
test "$(get_timeout_delays 6)" = "1 2 3" || return 1
test "$(get_timeout_delays 7)" = "1 2 4 " || return 1
test "$(get_timeout_delays 8)" = "1 2 4 1" || return 1
+ test "$(get_timeout_delays 1 .1)" = ".1 .2 .4 .3" || return 1
+ test "$(get_timeout_delays 1.5 .1)" = ".1 .2 .4 .8 " || return 1
+ test "$(get_timeout_delays 5 .1)" = ".1 .2 .4 .8 1.6 1.9" || return 1
+ test "$(get_timeout_delays 6 .1)" = ".1 .2 .4 .8 1.6 2.9" || return 1
+ test "$(get_timeout_delays 6.3 .1)" = ".1 .2 .4 .8 1.6 3.2 " || return 1
+ test "$(get_timeout_delays 20 .1)" = ".1 .2 .4 .8 1.6 3.2 6.4 7.3" || return 1
}
#######################################################################