parameters.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;;; srfi-39.test --- -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;; Testing the parameters implementation in boot-9.
  19. ;;
  20. (define-module (test-parameters)
  21. #:use-module (srfi srfi-34)
  22. #:use-module (test-suite lib))
  23. (define a (make-parameter 3))
  24. (define b (make-parameter 4))
  25. (define (check a b a-val b-val)
  26. (and (eqv? (a) a-val)) (eqv? (b) b-val))
  27. (define c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
  28. (define d (make-parameter 15 (lambda (x) (if (< x 10) x 10))))
  29. (with-test-prefix "parameters"
  30. (pass-if "test 1"
  31. (check a b 3 4))
  32. (pass-if "test 2"
  33. (parameterize ((a 2) (b 1))
  34. (and (check a b 2 1)
  35. (parameterize ((b 8))
  36. (check a b 2 8)))))
  37. (pass-if "test 3"
  38. (check a b 3 4))
  39. (pass-if "test 4"
  40. (check c d 2 10))
  41. (pass-if "test 5"
  42. (parameterize ((a 0) (b 1) (c 98) (d 9))
  43. (and (check a b 0 1)
  44. (check c d 10 9)
  45. (parameterize ((c (a)) (d (b)))
  46. (and (check a b 0 1)
  47. (check c d 0 1))))))
  48. (pass-if "SRFI-34"
  49. (let ((inside? (make-parameter #f)))
  50. (call/cc (lambda (return)
  51. (with-exception-handler
  52. (lambda (c)
  53. ;; This handler should be called in the dynamic
  54. ;; environment installed by `parameterize'.
  55. (return (inside?)))
  56. (lambda ()
  57. (parameterize ((inside? #t))
  58. (raise 'some-exception)))))))))
  59. (let ()
  60. (define (test-ports param new-port new-port-2)
  61. (let ((old-port (param)))
  62. (pass-if "new value"
  63. (parameterize ((param new-port))
  64. (eq? (param) new-port)))
  65. (pass-if "set value"
  66. (parameterize ((param old-port))
  67. (param new-port)
  68. (eq? (param) new-port)))
  69. (pass-if "old restored"
  70. (parameterize ((param new-port))
  71. #f)
  72. (eq? (param) old-port))
  73. (pass-if "throw exit"
  74. (catch 'bail
  75. (lambda ()
  76. (parameterize ((param new-port))
  77. (throw 'bail)))
  78. (lambda args #f))
  79. (eq? (param) old-port))
  80. (pass-if "call/cc re-enter"
  81. (let ((cont #f)
  82. (count 0)
  83. (port #f)
  84. (good #t))
  85. (parameterize ((param new-port))
  86. (call/cc (lambda (k) (set! cont k)))
  87. (set! count (1+ count))
  88. (set! port (param))
  89. (if (= 1 count) (param new-port-2)))
  90. (set! good (and good (eq? (param) old-port)))
  91. (case count
  92. ((1)
  93. (set! good (and good (eq? port new-port)))
  94. ;; re-entering should give new-port-2 left there last time
  95. (cont))
  96. ((2)
  97. (set! good (and good (eq? port new-port-2)))))
  98. good))
  99. (pass-if "original unchanged"
  100. (eq? (param) old-port))))
  101. (with-test-prefix "current-input-port"
  102. (test-ports current-input-port
  103. (open-input-string "xyz") (open-input-string "xyz")))
  104. (with-test-prefix "current-output-port"
  105. (test-ports current-output-port
  106. (open-output-string) (open-output-string)))
  107. (with-test-prefix "current-error-port"
  108. (test-ports current-error-port
  109. (open-output-string) (open-output-string))))