srfi-39.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;;; srfi-39.test --- -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2004, 2005, 2006, 2008 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. (define-module (test-srfi-39)
  19. #:use-module (test-suite lib)
  20. #:use-module (srfi srfi-34)
  21. #:use-module (srfi srfi-39)
  22. #:duplicates (last) ;; avoid warning about srfi-34 replacing `raise'
  23. )
  24. (define a (make-parameter 3))
  25. (define b (make-parameter 4))
  26. (define (check a b a-val b-val)
  27. (and (eqv? (a) a-val)) (eqv? (b) b-val))
  28. (define c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
  29. (define d (make-parameter 15 (lambda (x) (if (< x 10) x 10))))
  30. (with-test-prefix "SRFI-39"
  31. (pass-if "test 1"
  32. (check a b 3 4))
  33. (pass-if "test 2"
  34. (parameterize ((a 2) (b 1))
  35. (and (check a b 2 1)
  36. (parameterize ((b 8))
  37. (check a b 2 8)))))
  38. (pass-if "test 3"
  39. (check a b 3 4))
  40. (pass-if "test 4"
  41. (check c d 2 10))
  42. (pass-if "test 5"
  43. (parameterize ((a 0) (b 1) (c 98) (d 9))
  44. (and (check a b 0 1)
  45. (check c d 10 9)
  46. (parameterize ((c (a)) (d (b)))
  47. (and (check a b 0 1)
  48. (check c d 0 1))))))
  49. (pass-if "SRFI-34"
  50. (let ((inside? (make-parameter #f)))
  51. (call/cc (lambda (return)
  52. (with-exception-handler
  53. (lambda (c)
  54. ;; This handler should be called in the dynamic
  55. ;; environment installed by `parameterize'.
  56. (return (inside?)))
  57. (lambda ()
  58. (parameterize ((inside? #t))
  59. (raise 'some-exception)))))))))
  60. (let ()
  61. (define (test-ports param new-port new-port-2)
  62. (let ((old-port (param)))
  63. (pass-if "new value"
  64. (parameterize ((param new-port))
  65. (eq? (param) new-port)))
  66. (pass-if "set value"
  67. (parameterize ((param old-port))
  68. (param new-port)
  69. (eq? (param) new-port)))
  70. (pass-if "old restored"
  71. (parameterize ((param new-port))
  72. #f)
  73. (eq? (param) old-port))
  74. (pass-if "throw exit"
  75. (catch 'bail
  76. (lambda ()
  77. (parameterize ((param new-port))
  78. (throw 'bail)))
  79. (lambda args #f))
  80. (eq? (param) old-port))
  81. (pass-if "call/cc re-enter"
  82. (let ((cont #f)
  83. (count 0)
  84. (port #f)
  85. (good #t))
  86. (parameterize ((param new-port))
  87. (call/cc (lambda (k) (set! cont k)))
  88. (set! count (1+ count))
  89. (set! port (param))
  90. (if (= 1 count) (param new-port-2)))
  91. (set! good (and good (eq? (param) old-port)))
  92. (case count
  93. ((1)
  94. (set! good (and good (eq? port new-port)))
  95. ;; re-entering should give new-port-2 left there last time
  96. (cont))
  97. ((2)
  98. (set! good (and good (eq? port new-port-2)))))
  99. good))
  100. (pass-if "original unchanged"
  101. (eq? (param) old-port))))
  102. (with-test-prefix "current-input-port"
  103. (test-ports current-input-port
  104. (open-input-string "xyz") (open-input-string "xyz")))
  105. (with-test-prefix "current-output-port"
  106. (test-ports current-output-port
  107. (open-output-string) (open-output-string)))
  108. (with-test-prefix "current-error-port"
  109. (test-ports current-error-port
  110. (open-output-string) (open-output-string))))