summaryrefslogtreecommitdiff
path: root/test-suite/tests/srfi-39.test
blob: 2c09d138ddbbc9a9aee83e860d6ad8da7235feec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;;; srfi-39.test --- -*- scheme -*-
;;;;
;;;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING.  If not, write to
;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;;;; Boston, MA 02110-1301 USA

(define-module (test-srfi-39)
  #:use-module (test-suite lib)
  #:use-module (srfi srfi-39))

(define a (make-parameter 3))
(define b (make-parameter 4))

(define (check a b a-val b-val)
  (and (eqv? (a) a-val)) (eqv? (b) b-val))

(define c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
(define d (make-parameter 15 (lambda (x) (if (< x 10) x 10))))

(with-test-prefix "SRFI-39"

  (pass-if "test 1"
    (check a b 3 4))

  (pass-if "test 2"
    (parameterize ((a 2) (b 1))
      (and (check a b 2 1)
	   (parameterize ((b 8))
	     (check a b 2 8)))))

  (pass-if "test 3"
    (check a b 3 4))

  (pass-if "test 4"
    (check c d 2 10))

  (pass-if "test 5"
    (parameterize ((a 0) (b 1) (c 98) (d 9))
      (and (check a b 0 1)
           (check c d 10 9)
           (parameterize ((c (a)) (d (b)))
	     (and (check a b 0 1)
		  (check c d 0 1)))))))

(let ()
  (define (test-ports param new-port new-port-2)
    (let ((old-port (param)))

      (pass-if "new value"
	(parameterize ((param new-port))
	  (eq? (param) new-port)))

      (pass-if "set value"
	(parameterize ((param old-port))
	  (param new-port)
	  (eq? (param) new-port)))

      (pass-if "old restored"
	(parameterize ((param new-port))
	  #f)
	(eq? (param) old-port))

      (pass-if "throw exit"
	(catch 'bail
	  (lambda ()
	    (parameterize ((param new-port))
	      (throw 'bail)))
	  (lambda args #f))
	(eq? (param) old-port))

      (pass-if "call/cc re-enter"
	(let ((cont  #f)
	      (count 0)
	      (port  #f)
	      (good  #t))
	  (parameterize ((param new-port))
	    (call/cc (lambda (k) (set! cont k)))
	    (set! count (1+ count))
	    (set! port (param))
	    (if (= 1 count) (param new-port-2)))
	  (set! good (and good (eq? (param) old-port)))
	  (case count
	    ((1)
	     (set! good (and good (eq? port new-port)))
	     ;; re-entering should give new-port-2 left there last time
	     (cont))
	    ((2)
	     (set! good (and good (eq? port new-port-2)))))
	  good))

      (pass-if "original unchanged"
	(eq? (param) old-port))))

  (with-test-prefix "current-input-port"
    (test-ports current-input-port
		(open-input-string "xyz") (open-input-string "xyz")))

  (with-test-prefix "current-output-port"
    (test-ports current-output-port
		(open-output-string) (open-output-string)))

  (with-test-prefix "current-error-port"
    (test-ports current-error-port
		(open-output-string) (open-output-string))))