time.scm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
  2. ;; Copyright (C) 2021 GNUnet e.V.
  3. ;;
  4. ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
  5. ;; under the terms of the GNU Affero General Public License as published
  6. ;; by the Free Software Foundation, either version 3 of the License,
  7. ;; or (at your option) any later version.
  8. ;;
  9. ;; scheme-GNUnet is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Affero General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Affero General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;
  17. ;; SPDX-License-Identifier: AGPL-3.0-or-later
  18. (import (gnu gnunet util time)
  19. (srfi srfi-26)
  20. (srfi srfi-43))
  21. (test-begin "time")
  22. (test-equal "back-off factor correct"
  23. 2
  24. (/ (standard-back-off (* 2 time-unit:second))
  25. (standard-back-off time-unit:second)))
  26. (test-assert "not stuck at zero"
  27. (> (standard-back-off 0)))
  28. (define %small-relative-times
  29. (list time-unit:millisecond time-unit:second
  30. time-unit:minute))
  31. (define %very-small-relative-times
  32. (map (cut * <> time-unit:microsecond)
  33. '(0 1 2 3 4 #e1e1 #e1e2 #e1e3)))
  34. (test-equal "minimum is 1 milliseconds"
  35. (map standard-back-off %very-small-relative-times)
  36. (map (const (standard-back-off time-unit:millisecond))
  37. %very-small-relative-times))
  38. (test-equal "back-off is (at first) exponential (factor: 2)"
  39. (map (cut * 2 <>) %small-relative-times)
  40. (map standard-back-off %small-relative-times))
  41. (test-assert "15 minutes is a fixed point"
  42. (standard-back-off (* 15 time-unit:minute)))
  43. (define deltas '(1 2 3 #e1e1 #e1e3 #e1e4 #e1e5 #e1e9 #e1e20))
  44. (test-equal "bounded to 15 minutes"
  45. (map (const (* 15 time-unit:minute)) deltas)
  46. (map (compose standard-back-off (cut + (* 15 time-unit:minute) <>))
  47. deltas))
  48. (define %fractions>=1/2 '(1/2 3/4 5/8 9/16 17/32))
  49. (test-equal "bounded to 15 minutes (slightly smaller start time)"
  50. (map (compose standard-back-off
  51. (cut * <> (* 15 time-unit:minute))) %fractions>=1/2)
  52. (map (const (* 15 time-unit:minute)) %fractions>=1/2))
  53. (test-error "no inexact numbers"
  54. (standard-back-off 0.0))
  55. (test-error "no integers rationals"
  56. (standard-back-off 0.0))
  57. (test-error "no strictly negative numbers"
  58. (standard-back-off -1))
  59. (test-end "time")