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