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