parameters.scm 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;; Fibers: cooperative, event-driven user-space threads.
  2. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (tests parameters)
  19. #:use-module (fibers)
  20. #:use-module (fibers channels))
  21. (define failed? #f)
  22. (define-syntax-rule (assert-equal expected actual)
  23. (let ((x expected))
  24. (format #t "assert ~s equal to ~s: " 'actual x)
  25. (force-output)
  26. (let ((y actual))
  27. (cond
  28. ((equal? x y) (format #t "ok\n"))
  29. (else
  30. (format #t "no (got ~s)\n" y)
  31. (set! failed? #t))))))
  32. (define-syntax-rule (assert-run-fibers-terminates exp)
  33. (begin
  34. (format #t "assert run-fibers on ~s terminates: " 'exp)
  35. (force-output)
  36. (let ((start (get-internal-real-time)))
  37. (call-with-values (lambda () (run-fibers (lambda () exp)))
  38. (lambda vals
  39. (format #t "ok (~a s)\n" (/ (- (get-internal-real-time) start)
  40. 1.0 internal-time-units-per-second))
  41. (apply values vals))))))
  42. (define-syntax-rule (assert-run-fibers-returns (expected ...) exp)
  43. (begin
  44. (call-with-values (lambda () (assert-run-fibers-terminates exp))
  45. (lambda run-fiber-return-vals
  46. (assert-equal '(expected ...) run-fiber-return-vals)))))
  47. (define-syntax-rule (rpc exp)
  48. (let ((ch (make-channel)))
  49. (spawn-fiber (lambda () (put-message ch exp)))
  50. (get-message ch)))
  51. (define my-param (make-parameter #f))
  52. (assert-run-fibers-returns (#f) (my-param))
  53. (assert-run-fibers-returns (#f) (rpc (my-param)))
  54. (assert-run-fibers-returns (42) (rpc (begin (my-param 42) (my-param))))
  55. (assert-run-fibers-returns (#f) (my-param))
  56. (assert-run-fibers-returns (100) (begin (my-param 100) (rpc (my-param))))
  57. (assert-run-fibers-returns (#f) (my-param))
  58. (assert-equal #f (my-param))
  59. (assert-equal 'foo (begin (my-param 'foo) (my-param)))
  60. (assert-run-fibers-returns (foo) (my-param))
  61. (assert-run-fibers-returns (foo) (rpc (my-param)))
  62. (exit (if failed? 1 0))