1#! /usr/bin/env sscm -C UTF-8
2;; -*- buffer-file-coding-system: utf-8 -*-
3
4;;  Filename : test-formal-syntax.scm
5;;  About    : unit test for R5RS formal syntax
6;;
7;;  Copyright (C) 2005-2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
8;;  Copyright (c) 2007-2008 SigScheme Project <uim-en AT googlegroups.com>
9;;
10;;  All rights reserved.
11;;
12;;  Redistribution and use in source and binary forms, with or without
13;;  modification, are permitted provided that the following conditions
14;;  are met:
15;;
16;;  1. Redistributions of source code must retain the above copyright
17;;     notice, this list of conditions and the following disclaimer.
18;;  2. Redistributions in binary form must reproduce the above copyright
19;;     notice, this list of conditions and the following disclaimer in the
20;;     documentation and/or other materials provided with the distribution.
21;;  3. Neither the name of authors nor the names of its contributors
22;;     may be used to endorse or promote products derived from this software
23;;     without specific prior written permission.
24;;
25;;  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
26;;  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27;;  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28;;  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
29;;  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30;;  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31;;  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32;;  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33;;  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34;;  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35;;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37(require-extension (unittest))
38
39(define tn test-name)
40(define *test-track-progress* #f)
41
42;; See "7.1 Formal syntax" of R5RS
43;; See also test-number-literal.scm
44
45(tn "invalid boolean")
46(if sigscheme?
47    (begin
48      (assert-parse-error (tn) "#F")
49      (assert-parse-error (tn) "#T"))
50    (begin
51      (assert-true (tn) (boolean? (string-read "#F")))
52      (assert-true (tn) (boolean? (string-read "#T")))))
53
54(tn "boolean")
55(assert-true (tn) (boolean? (string-read "#f")))
56(assert-true (tn) (boolean? (string-read "#t")))
57
58(tn "invalid identifier")
59(assert-parse-error (tn) "#")
60(assert-parse-error (tn) ".")
61(assert-parse-error (tn) "..")
62(assert-parse-error (tn) "....")
63(assert-parse-error (tn) ".a")
64(assert-parse-error (tn) "+a")
65(assert-parse-error (tn) "++")
66(assert-parse-error (tn) "--")
67(assert-parse-error (tn) "-=")
68(assert-parse-error (tn) "-$")
69(assert-parse-error (tn) "-.")
70(assert-parse-error (tn) "-@")
71(assert-parse-error (tn) "@")
72(assert-parse-error (tn) "1a")
73(assert-parse-error (tn) "-a")
74
75(tn "special initial identifier")
76(assert-true (tn) (symbol? (string-read "!")))
77(assert-true (tn) (symbol? (string-read "$")))
78(assert-true (tn) (symbol? (string-read "%")))
79(assert-true (tn) (symbol? (string-read "&")))
80(assert-true (tn) (symbol? (string-read "*")))
81(assert-true (tn) (symbol? (string-read "/")))
82(assert-true (tn) (symbol? (string-read ":")))
83(assert-true (tn) (symbol? (string-read "<")))
84(assert-true (tn) (symbol? (string-read "=")))
85(assert-true (tn) (symbol? (string-read ">")))
86(assert-true (tn) (symbol? (string-read "?")))
87(assert-true (tn) (symbol? (string-read "^")))
88(assert-true (tn) (symbol? (string-read "_")))
89(assert-true (tn) (symbol? (string-read "~")))
90
91(tn "special initial identifier + number")
92(assert-true (tn) (symbol? (string-read "!1")))
93(assert-true (tn) (symbol? (string-read "$1")))
94(assert-true (tn) (symbol? (string-read "%1")))
95(assert-true (tn) (symbol? (string-read "&1")))
96(assert-true (tn) (symbol? (string-read "*1")))
97(assert-true (tn) (symbol? (string-read "/1")))
98(assert-true (tn) (symbol? (string-read ":1")))
99(assert-true (tn) (symbol? (string-read "<1")))
100(assert-true (tn) (symbol? (string-read "=1")))
101(assert-true (tn) (symbol? (string-read ">1")))
102(assert-true (tn) (symbol? (string-read "?1")))
103(assert-true (tn) (symbol? (string-read "^1")))
104(assert-true (tn) (symbol? (string-read "_1")))
105(assert-true (tn) (symbol? (string-read "~1")))
106
107(tn "special initial identifier + letter")
108(assert-true (tn) (symbol? (string-read "!a")))
109(assert-true (tn) (symbol? (string-read "$a")))
110(assert-true (tn) (symbol? (string-read "%a")))
111(assert-true (tn) (symbol? (string-read "&a")))
112(assert-true (tn) (symbol? (string-read "*a")))
113(assert-true (tn) (symbol? (string-read "/a")))
114(assert-true (tn) (symbol? (string-read ":a")))
115(assert-true (tn) (symbol? (string-read "<a")))
116(assert-true (tn) (symbol? (string-read "=a")))
117(assert-true (tn) (symbol? (string-read ">a")))
118(assert-true (tn) (symbol? (string-read "?a")))
119(assert-true (tn) (symbol? (string-read "^a")))
120(assert-true (tn) (symbol? (string-read "_a")))
121(assert-true (tn) (symbol? (string-read "~a")))
122
123(tn "identifier")
124(assert-true (tn) (symbol? (string-read "...")))
125(assert-true (tn) (symbol? (string-read "+")))
126(assert-true (tn) (symbol? (string-read "-")))
127(assert-true (tn) (symbol? (string-read "a.")))
128(assert-true (tn) (symbol? (string-read "a+")))
129(assert-true (tn) (symbol? (string-read "a-")))
130(assert-true (tn) (symbol? (string-read "a@")))
131(assert-true (tn) (symbol? (string-read "a1")))
132;; SigScheme 0.7.0 and later disallows initial hyphen of an identifier.
133(if sigscheme?
134    (begin
135      (assert-error (tn) (lambda () (symbol? (string-read "-a"))))
136      (assert-true  (tn)            (symbol? (string->symbol "-a")))))
137
138(tn "invalid dot pair")
139(assert-parse-error (tn) "( . )")
140(assert-parse-error (tn) "( . \"foo\")")
141(assert-parse-error (tn) "( . \"foo\" \"bar\")")
142(assert-parse-error (tn) "(\"foo\" . )")
143(assert-parse-error (tn) "(\"foo\" \"bar\" . )")
144(assert-parse-error (tn) "(\"foo\" . \"bar\" \"baz\")")
145(assert-parse-error (tn) "(\"foo\" \"bar\" . \"baz\" \"quux\")")
146
147(tn "invalid dot pair without left space")
148(assert-parse-error (tn) "(. )")
149(assert-parse-error (tn) "(. \"foo\")")
150(assert-parse-error (tn) "(. \"foo\" \"bar\")")
151(assert-parse-error (tn) "(\"foo\". )")
152(assert-parse-error (tn) "(\"foo\" \"bar\". )")
153(assert-parse-error (tn) "(\"foo\". \"bar\" \"baz\")")
154(assert-parse-error (tn) "(\"foo\" \"bar\". \"baz\" \"quux\")")
155
156(tn "dot pair")
157(assert-parseable (tn) "(\"foo\" . \"bar\")")
158(assert-parseable (tn) "(\"foo\" \"bar\" . \"baz\")")
159
160(tn "dot pair without left space")
161(assert-parseable (tn) "(\"foo\". \"bar\")")
162(assert-parseable (tn) "(\"foo\" \"bar\". \"baz\")")
163
164(let ((assert (if (and (provided? "sigscheme")
165                       (not (provided? "strict-r5rs")))
166                  assert-parse-error
167                  assert-parseable)))
168  (tn "invalid dot pair without right space")
169  (assert (tn) "( .)")
170  (assert (tn) "( .\"foo\")")
171  (assert (tn) "( .\"foo\" \"bar\")")
172  (assert (tn) "(\"foo\" .)")
173  (assert (tn) "(\"foo\" \"bar\" .)")
174  (assert (tn) "(\"foo\" .\"bar\" \"baz\")")
175  (assert (tn) "(\"foo\" \"bar\" .\"baz\" \"quux\")")
176
177  (tn "invalid dot pair without both space")
178  (assert (tn) "(.)")
179  (assert (tn) "(.\"foo\")")
180  (assert (tn) "(.\"foo\" \"bar\")")
181  (assert (tn) "(\"foo\".)")
182  (assert (tn) "(\"foo\" \"bar\".)")
183  (assert (tn) "(\"foo\".\"bar\" \"baz\")")
184  (assert (tn) "(\"foo\" \"bar\".\"baz\" \"quux\")")
185
186  (tn "dot pair without right space")
187  (assert (tn) "(\"foo\" .\"bar\")")
188  (assert (tn) "(\"foo\" \"bar\" .\"baz\")")
189
190  (tn "dot pair without both space")
191  (assert (tn) "(\"foo\".\"bar\")")
192  (assert (tn) "(\"foo\" \"bar\".\"baz\")"))
193
194(assert-error "invalid function calling: boolean" (lambda () (#t)))
195(assert-error "invalid function calling: integer" (lambda () (1)))
196(assert-error "invalid function calling: null"    (lambda () ('())))
197(assert-error "invalid function calling: pair"    (lambda () ('(1 2))))
198(assert-error "invalid function calling: char"    (lambda () (#\a)))
199(assert-error "invalid function calling: string"  (lambda () ("a")))
200(assert-error "invalid function calling: vector"  (lambda () (#(1))))
201
202(tn "function calling fixed_0")
203(define f (lambda () #t))
204(assert-equal? (tn) #t         (f))
205(assert-error  (tn) (lambda () (f . #t)))
206(assert-error  (tn) (lambda () (f #t)))
207(assert-error  (tn) (lambda () (f #t . #t)))
208(assert-error  (tn) (lambda () (f #t #t)))
209(assert-error  (tn) (lambda () (f #t #t . #t)))
210(assert-error  (tn) (lambda () (f #t #t #t)))
211(assert-error  (tn) (lambda () (f #t #t #t . #t)))
212(tn "function calling variadic_0")
213(define f (lambda args args))
214(assert-equal? (tn) '()         (f))
215(assert-error  (tn) (lambda ()  (f . #t)))
216(assert-equal? (tn) '(#t)       (f #t))
217(assert-error  (tn) (lambda ()  (f #t . #t)))
218(assert-equal? (tn) '(#t #t)    (f #t #t))
219(assert-error  (tn) (lambda ()  (f #t #t . #t)))
220(assert-equal? (tn) '(#t #t #t) (f #t #t #t))
221(assert-error  (tn) (lambda ()  (f #t #t #t . #t)))
222(tn "function calling fixed_1")
223(define f (lambda (x) x))
224(assert-error  (tn) (lambda () (f)))
225(assert-error  (tn) (lambda () (f . #t)))
226(assert-equal? (tn) #t         (f #t))
227(assert-error  (tn) (lambda () (f #t . #t)))
228(assert-error  (tn) (lambda () (f #t #t)))
229(assert-error  (tn) (lambda () (f #t #t . #t)))
230(assert-error  (tn) (lambda () (f #t #t #t)))
231(assert-error  (tn) (lambda () (f #t #t #t . #t)))
232(tn "function calling variadic_1")
233(define f (lambda (x . rest) (list x rest)))
234(assert-error  (tn) (lambda ()    (f)))
235(assert-error  (tn) (lambda ()    (f . #t)))
236(assert-equal? (tn) '(#t ())      (f #t))
237(assert-error  (tn) (lambda ()    (f #t . #t)))
238(assert-equal? (tn) '(#t (#t))    (f #t #t))
239(assert-error  (tn) (lambda ()    (f #t #t . #t)))
240(assert-equal? (tn) '(#t (#t #t)) (f #t #t #t))
241(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
242(tn "function calling fixed_2")
243(define f (lambda (x y) (list x y)))
244(assert-error  (tn) (lambda ()    (f)))
245(assert-error  (tn) (lambda ()    (f . #t)))
246(assert-error  (tn) (lambda ()    (f #t)))
247(assert-error  (tn) (lambda ()    (f #t . #t)))
248(assert-equal? (tn) '(#t #t)      (f #t #t))
249(assert-error  (tn) (lambda ()    (f #t #t . #t)))
250(assert-error  (tn) (lambda ()    (f #t #t #t)))
251(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
252(tn "function calling variadic_2")
253(define f (lambda (x y . rest) (list x y rest)))
254(assert-error  (tn) (lambda ()    (f)))
255(assert-error  (tn) (lambda ()    (f . #t)))
256(assert-error  (tn) (lambda ()    (f #t)))
257(assert-error  (tn) (lambda ()    (f #t . #t)))
258(assert-equal? (tn) '(#t #t ())   (f #t #t))
259(assert-error  (tn) (lambda ()    (f #t #t . #t)))
260(assert-equal? (tn) '(#t #t (#t)) (f #t #t #t))
261(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
262
263(tn "function calling fixed_0 for define-created closure")
264(define (f) #t)
265(assert-equal? (tn) #t         (f))
266(assert-error  (tn) (lambda () (f . #t)))
267(assert-error  (tn) (lambda () (f #t)))
268(assert-error  (tn) (lambda () (f #t . #t)))
269(assert-error  (tn) (lambda () (f #t #t)))
270(assert-error  (tn) (lambda () (f #t #t . #t)))
271(assert-error  (tn) (lambda () (f #t #t #t)))
272(assert-error  (tn) (lambda () (f #t #t #t . #t)))
273(tn "function calling variadic_0 for define-created closure")
274(define (f . args) args)
275(assert-equal? (tn) '()         (f))
276(assert-error  (tn) (lambda ()  (f . #t)))
277(assert-equal? (tn) '(#t)       (f #t))
278(assert-error  (tn) (lambda ()  (f #t . #t)))
279(assert-equal? (tn) '(#t #t)    (f #t #t))
280(assert-error  (tn) (lambda ()  (f #t #t . #t)))
281(assert-equal? (tn) '(#t #t #t) (f #t #t #t))
282(assert-error  (tn) (lambda ()  (f #t #t #t . #t)))
283(tn "function calling fixed_1 for define-created closure")
284(define (f x) x)
285(assert-error  (tn) (lambda () (f)))
286(assert-error  (tn) (lambda () (f . #t)))
287(assert-equal? (tn) #t         (f #t))
288(assert-error  (tn) (lambda () (f #t . #t)))
289(assert-error  (tn) (lambda () (f #t #t)))
290(assert-error  (tn) (lambda () (f #t #t . #t)))
291(assert-error  (tn) (lambda () (f #t #t #t)))
292(assert-error  (tn) (lambda () (f #t #t #t . #t)))
293(tn "function calling variadic_1 for define-created closure")
294(define (f x . rest) (list x rest))
295(assert-error  (tn) (lambda ()    (f)))
296(assert-error  (tn) (lambda ()    (f . #t)))
297(assert-equal? (tn) '(#t ())      (f #t))
298(assert-error  (tn) (lambda ()    (f #t . #t)))
299(assert-equal? (tn) '(#t (#t))    (f #t #t))
300(assert-error  (tn) (lambda ()    (f #t #t . #t)))
301(assert-equal? (tn) '(#t (#t #t)) (f #t #t #t))
302(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
303(tn "function calling fixed_2 for define-created closure")
304(define (f x y) (list x y))
305(assert-error  (tn) (lambda ()    (f)))
306(assert-error  (tn) (lambda ()    (f . #t)))
307(assert-error  (tn) (lambda ()    (f #t)))
308(assert-error  (tn) (lambda ()    (f #t . #t)))
309(assert-equal? (tn) '(#t #t)      (f #t #t))
310(assert-error  (tn) (lambda ()    (f #t #t . #t)))
311(assert-error  (tn) (lambda ()    (f #t #t #t)))
312(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
313(tn "function calling variadic_2 for define-created closure")
314(define (f x y . rest) (list x y rest))
315(assert-error  (tn) (lambda ()    (f)))
316(assert-error  (tn) (lambda ()    (f . #t)))
317(assert-error  (tn) (lambda ()    (f #t)))
318(assert-error  (tn) (lambda ()    (f #t . #t)))
319(assert-equal? (tn) '(#t #t ())   (f #t #t))
320(assert-error  (tn) (lambda ()    (f #t #t . #t)))
321(assert-equal? (tn) '(#t #t (#t)) (f #t #t #t))
322(assert-error  (tn) (lambda ()    (f #t #t #t . #t)))
323
324;; Although SigScheme's eval facility itself does not ensure properness of
325;; syntax args, each syntax implementation must check it. These tests only
326;; indicate what should be done.
327(tn "syntax application fixed_0")
328;; FIXME: no syntax with syntax_fixed_0
329(assert-equal? (tn) #t         ((lambda () #t)))
330(assert-error  (tn) (lambda () ((lambda () #t) . #t)))
331(assert-error  (tn) (lambda () ((lambda () #t) #t)))
332(assert-error  (tn) (lambda () ((lambda () #t) #t . #t)))
333(assert-error  (tn) (lambda () ((lambda () #t) #t #t)))
334(assert-error  (tn) (lambda () ((lambda () #t) #t #t . #t)))
335(assert-error  (tn) (lambda () ((lambda () #t) #t #t #t)))
336(assert-error  (tn) (lambda () ((lambda () #t) #t #t #t . #t)))
337(tn "syntax application variadic_0")
338(assert-equal? (tn) #t          (and))
339(assert-error  (tn) (lambda ()  (and . #t)))
340(assert-equal? (tn) #t          (and #t))
341(assert-error  (tn) (lambda ()  (and #t . #t)))
342(assert-equal? (tn) #t          (and #t #t))
343(assert-error  (tn) (lambda ()  (and #t #t . #t)))
344(assert-equal? (tn) #t          (and #t #t #t))
345(assert-error  (tn) (lambda ()  (and #t #t #t . #t)))
346(tn "syntax application fixed_1")
347(assert-error  (tn) (lambda () (quote)))
348(assert-error  (tn) (lambda () (quote . #t)))
349(assert-equal? (tn) #t         (quote #t))
350(assert-error  (tn) (lambda () (quote #t . #t)))
351(assert-error  (tn) (lambda () (quote #t #t)))
352(assert-error  (tn) (lambda () (quote #t #t . #t)))
353(assert-error  (tn) (lambda () (quote #t #t #t)))
354(assert-error  (tn) (lambda () (quote #t #t #t . #t)))
355(tn "syntax application variadic_1")
356(assert-error  (tn) (lambda ()    (let*)))
357(assert-error  (tn) (lambda ()    (let* . #t)))
358(assert-error  (tn) (lambda ()    (let* ())))
359(assert-error  (tn) (lambda ()    (let* #t . #t)))
360(assert-equal? (tn) #t            (let* () #t))
361(assert-error  (tn) (lambda ()    (let* #t #t . #t)))
362(assert-equal? (tn) #t            (let* () #t #t))
363(assert-error  (tn) (lambda ()    (let* #t #t #t . #t)))
364(tn "syntax application fixed_2")
365(define foo #f)
366(assert-error  (tn) (lambda ()    (set!)))
367(assert-error  (tn) (lambda ()    (set! . #t)))
368(assert-error  (tn) (lambda ()    (set! #t)))
369(assert-error  (tn) (lambda ()    (set! #t . #t)))
370(if (and (provided? "sigscheme")
371         (provided? "strict-r5rs"))
372    (assert-equal? (tn) (undef)   (set! foo #t))
373    (assert-equal? (tn) #t        (set! foo #t)))
374(assert-error  (tn) (lambda ()    (set! #t #t . #t)))
375(assert-error  (tn) (lambda ()    (set! #t #t #t)))
376(assert-error  (tn) (lambda ()    (set! #t #t #t . #t)))
377(tn "syntax application variadic_2")
378(assert-error  (tn) (lambda ()    (if)))
379(assert-error  (tn) (lambda ()    (if . #t)))
380(assert-error  (tn) (lambda ()    (if #t)))
381(assert-error  (tn) (lambda ()    (if #t . #t)))
382(assert-equal? (tn) #t            (if #t #t))
383(assert-error  (tn) (lambda ()    (if #t #t . #t)))
384(assert-equal? (tn) #t            (if #t #t #t))
385(assert-error  (tn) (lambda ()    (if #t #t #t . #t)))
386
387(tn "EOF immediately after quoter")
388;; (quote #<eof>) is invalid
389(assert-error  (tn) (lambda () (string-read "'")))
390(assert-error  (tn) (lambda () (string-read "`")))
391(assert-error  (tn) (lambda () (string-read ",")))
392(assert-error  (tn) (lambda () (string-read ",@")))
393
394(total-report)
395