1
;; Filename : test-values.scm
2
;; About : unit tests for multiple values
4
;; Copyright (C) 2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
5
;; Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
7
;; All rights reserved.
9
;; Redistribution and use in source and binary forms, with or without
10
;; modification, are permitted provided that the following conditions
13
;; 1. Redistributions of source code must retain the above copyright
14
;; notice, this list of conditions and the following disclaimer.
15
;; 2. Redistributions in binary form must reproduce the above copyright
16
;; notice, this list of conditions and the following disclaimer in the
17
;; documentation and/or other materials provided with the distribution.
18
;; 3. Neither the name of authors nor the names of its contributors
19
;; may be used to endorse or promote products derived from this software
20
;; without specific prior written permission.
22
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
23
;; IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
;; THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26
;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
(load "test/unittest.scm")
43
;; These tests use explicit equivalence predicates instead of assert-equal?, to
44
;; avoid being affected by multiple-values -specific behavior.
46
(tn "values invalid forms")
47
;; Normal continuations accept exactly one value only.
48
(assert-error (tn) (lambda () (eq? '() (values))))
49
(assert-error (tn) (lambda () (eq? '() (apply values '()))))
50
(assert-error (tn) (lambda () (eq? '() (values . 1))))
51
(assert-error (tn) (lambda () (eq? '() (values 1 2))))
52
(assert-error (tn) (lambda () (eq? '() (apply values '(1 2)))))
53
(assert-error (tn) (lambda () (eq? '() (values 1 . 2))))
55
(tn "values disallowed places")
57
(assert-error (tn) (lambda () (eval '(define foo (values 1 2 3))
58
(interaction-environment))))
60
(assert-error (tn) (lambda () (eval '(set! foo (values 1 2 3))
61
(interaction-environment))))
63
(assert-error (tn) (lambda () (define bar (values 1 2 3))))
65
(assert-error (tn) (lambda () (let ((bar (values 1 2 3))) #t)))
66
(assert-error (tn) (lambda () (let* ((bar (values 1 2 3))) #t)))
67
(assert-error (tn) (lambda () (letrec ((bar (values 1 2 3))) #t)))
68
(assert-error (tn) (lambda () (if (values 1 2 3) #t)))
69
(assert-error (tn) (lambda () (and (values 1 2 3) #t)))
70
(assert-error (tn) (lambda () (or (values 1 2 3) #t)))
71
(assert-error (tn) (lambda () (cond ((values 1 2 3) #t) (else #t))))
72
(assert-error (tn) (lambda () (case (values 1 2 3) (else #t))))
73
(assert-error (tn) (lambda () (begin (values 1 2 3) #t)))
74
(assert-error (tn) (lambda () ((lambda () (values 1 2 3) #t))))
78
(assert-true (tn) (eqv? 1 (values 1)))
79
(assert-true (tn) (eqv? 1 (apply values '(1))))
80
(assert-true (tn) (eq? '() (values '())))
81
(assert-true (tn) (eq? '() (apply values '(()))))
82
(assert-true (tn) (eq? #f (values #f)))
83
(assert-true (tn) (eq? #f (apply values '(#f))))
85
;; Returning multiple values in top-level is allowed (SigScheme-specific).
86
;; These forms test whether evaluations are passed without blowing up.
90
(apply values '(1 2 3))
98
(apply values '(1 2 3)))
104
(tn "call-with-values invalid forms")
105
(assert-error (tn) (lambda ()
107
(assert-error (tn) (lambda ()
108
(call-with-values even?)))
109
(assert-error (tn) (lambda ()
110
(call-with-values even? #t)))
111
(assert-error (tn) (lambda ()
112
(call-with-values #t even?)))
114
(tn "call-with-values")
117
(call-with-values * -))
132
(lambda () (apply values '()))
137
(lambda () (apply values '()))
143
(lambda () (values 1))
148
(lambda () (values 1))
153
(lambda () (apply values '(1)))
158
(lambda () (apply values '(1)))
164
(lambda () (values 1 2))
165
(lambda (x y) (list x y))))
169
(lambda () (values 1 2))
174
(lambda () (apply values '(1 2)))
175
(lambda (x y) (list x y))))
179
(lambda () (apply values '(1 2)))
182
(tn "call-with-values by apply")
185
(apply call-with-values (list * -)))
189
(apply call-with-values
190
(list (lambda () (values))
194
(apply call-with-values
195
(list (lambda () (values))
196
(lambda args args))))
199
(apply call-with-values
200
(list (lambda () (apply values '()))
204
(apply call-with-values
205
(list (lambda () (apply values '()))
206
(lambda args args))))
210
(apply call-with-values
211
(list (lambda () (values 1))
215
(apply call-with-values
216
(list (lambda () (values 1))
217
(lambda args args))))
220
(apply call-with-values
221
(list (lambda () (apply values '(1)))
225
(apply call-with-values
226
(list (lambda () (apply values '(1)))
227
(lambda args args))))
231
(apply call-with-values
232
(list (lambda () (values 1 2))
233
(lambda (x y) (list x y)))))
236
(apply call-with-values
237
(list (lambda () (values 1 2))
238
(lambda args args))))
241
(apply call-with-values
242
(list (lambda () (apply values '(1 2)))
243
(lambda (x y) (list x y)))))
246
(apply call-with-values
247
(list (lambda () (apply values '(1 2)))
248
(lambda args args))))
250
(tn "call-with-values misc")
251
;; test whether the variable is properly bound
256
(lambda () (values 2 3 n))
257
(lambda (dummy1 dummy2 n2)