srfi-39.test 3.8 KB

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