001: /*
002: * Tests.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: Tests.java,v 1.6 2003/11/15 11:03:35 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import junit.framework.Test;
027: import junit.textui.TestRunner;
028:
029: public class Tests extends TestCase {
030: private static final String ERROR = Interpreter.ERROR;
031:
032: public void testAll() {
033: verify("foo", ERROR);
034: verify("(symbol-value 'foo)", ERROR);
035: verify("'foo", "foo");
036: verify("'(quote foo)", "'foo");
037:
038: // symbolp
039: verify("(symbolp 'elephant)", "T");
040: verify("(symbolp 12)", "NIL");
041: verify("(symbolp nil)", "T");
042: verify("(symbolp 'nil)", "T");
043: verify("(symbolp ())", "T");
044: verify("(symbolp '())", "T");
045: verify("(symbolp t)", "T");
046: verify("(symbolp 't)", "T");
047: verify("(symbolp :test)", "T");
048: verify("(symbolp \"hello\")", "NIL");
049:
050: // defvar
051: // verify("foo", ERROR);
052: // verify("(defvar foo)", "foo");
053: // verify("foo", ERROR);
054: // verify("(defvar foo 1234)", "foo");
055: // verify("foo", "1234");
056: // verify("(defvar foo 'bar)", "foo");
057: // verify("foo", "1234");
058:
059: verify("(setq)", "NIL");
060:
061: // setq sequential assignment
062: verify("(setq a 1 b 2 c 3)", "3");
063: verify("a", "1");
064: verify("b", "2");
065: verify("c", "3");
066: verify("(setq a (1+ b) b (1+ a) c (+ a b))", "7");
067: verify("a", "3");
068: verify("b", "4");
069: verify("c", "7");
070:
071: // setq, let
072: // verify("(defvar *x* 1234)", "*x*");
073: // verify("*x*", "1234");
074: // verify("(let ((*x* 10)) (setq *x* 23))", "23");
075: // verify("*x*", "1234");
076:
077: verify("(setq foo 'bar)", "bar");
078: verify("(symbol-value 'foo)", "bar");
079: verify("foo", "bar");
080: verify("(eval 'foo)", "bar");
081: verify("(eval foo)", ERROR);
082: verify("(setq foo 'baz)", "baz");
083: verify("foo", "baz");
084: verify("(eval 'foo)", "baz");
085:
086: verify("(setq foo 12)", "12");
087: verify("foo", "12");
088: verify("(eval foo)", "12");
089: verify("(setq foo '12)", "12");
090: verify("foo", "12");
091: verify("(eval foo)", "12");
092:
093: verify("(+ 1 2 3)", "6");
094: verify("(- 1)", "-1");
095: verify("(- 6 4)", "2");
096: verify("(- 10 6 3)", "1");
097: verify("(*)", "1");
098: verify("(* 2 4 6)", "48");
099: verify("(/)", ERROR);
100: verify("(/ 1234)", "1/1234");
101: verify("(/ 81 3)", "27");
102: verify("(/ 48 2 4 6)", "1");
103:
104: verify("(setq foo 12)", "12");
105: verify("(+ 23 foo)", "35");
106: verify("(setq bar 300)", "300");
107: verify("(+ foo bar)", "312");
108: verify("(setq foo (+ foo bar))", "312");
109: verify("foo", "312");
110:
111: verify("(setq a 12 b 13)", "13");
112: verify("a", "12");
113: verify("b", "13");
114: verify("(+ a b)", "25");
115:
116: verify("t", "T");
117: verify("nil", "NIL");
118: verify("()", "NIL");
119:
120: // listp
121: verify("(listp ())", "T");
122: verify("(listp '())", "T");
123: verify("(listp nil)", "T");
124: verify("(listp 123)", "NIL");
125: verify("(listp '(a b c))", "T");
126: verify("(setq foo '(1 2 3))", "(1 2 3)");
127: verify("(listp foo)", "T");
128:
129: verify("(length)", ERROR);
130: verify("(length 12)", ERROR);
131: verify("(length ())", "0");
132: verify("(length '(a b c))", "3");
133: verify("(length '(a . b))", ERROR);
134:
135: verify("(numberp 12)", "T");
136: verify("(setq foo 12)", "12");
137: verify("(numberp foo)", "T");
138: verify("(numberp 'foo)", "NIL");
139:
140: // atom
141: verify("(atom 'sss)", "T");
142: verify("(atom (cons 1 2))", "NIL");
143: verify("(atom nil)", "T");
144: verify("(atom '())", "T");
145: verify("(atom 3)", "T");
146:
147: // consp
148: verify("(consp nil)", "NIL");
149: verify("(consp (cons 1 2))", "T");
150: verify("(consp '())", "NIL");
151: verify("(consp 'nil)", "NIL");
152:
153: // car, cdr
154: verify("(car 'a)", ERROR);
155: verify("(car '(a))", "a");
156: verify("(cdr '(a))", "NIL");
157: verify("(car '(a b c))", "a");
158: verify("(cdr '(a b c))", "(b c)");
159: verify("(setq foo '(a b c))", "(a b c)");
160: verify("(car foo)", "a");
161: verify("(cdr foo)", "(b c)");
162: verify("(car nil)", "NIL");
163: verify("(cdr '(1 . 2))", "2");
164: verify("(cdr '(1 2))", "(2)");
165: verify("(cadr '(1 2))", "2");
166: verify("(car '(a b c))", "a");
167: verify("(cdr '(a b c))", "(b c)");
168:
169: verify("(third '(a b))", "NIL");
170: verify("(third '(a b c))", "C");
171: verify("(fourth '(a b c))", "NIL");
172: verify("(fourth '(a b c d))", "D");
173:
174: // cons
175: verify("(cons 1 2)", "(1 . 2)");
176: verify("(cons 1 nil)", "(1)");
177: verify("(cons nil 2)", "(NIL . 2)");
178: verify("(cons nil nil)", "(NIL)");
179: verify("(cons 1 (cons 2 (cons 3 (cons 4 nil))))", "(1 2 3 4)");
180: verify("(cons 'a 'b)", "(a . b)");
181: verify("(cons 'a (cons 'b (cons 'c '())))", "(a b c)");
182: verify("(cons 'a '(b c d))", "(a b c d)");
183: verify("(cons 'a (cons 'b (cons 'c 'd)))", "(A B C . D)");
184:
185: // list
186: verify("(list)", "NIL");
187: verify("(list '(+ 2 1) (+ 2 1))", "((+ 2 1) 3)");
188: verify("(list 'a 'b 'c)", "(a b c)");
189: verify("(list 1)", "(1)");
190: verify("(setq a 1)", "1");
191: verify("(list a 2)", "(1 2)");
192:
193: // list*
194: verify("(list*)", ERROR);
195: verify("(list* 1)", "1");
196: verify("(list* 1 2)", "(1 . 2)");
197: verify("(setq a '(1 2))", "(1 2)");
198: verify("(eq a (list* a))", "T");
199: verify("(list* 'a 'b 'c 'd)", "(A B C . D)");
200: verify("(list* 'a 'b 'c '(d e f))", "(A B C D E F)");
201:
202: // append
203: verify("(append '(a b c) '(d e f) '() '(g))", "(a b c d e f g)");
204: verify("(append '(a b c) 'd)", "(a b c . d)");
205: verify("(setq lst '(a b c))", "(a b c)");
206: verify("(append lst '(d))", "(a b c d)");
207: verify("lst", "(a b c)");
208: verify("(append)", "NIL");
209: verify("(append 'a)", "a");
210: verify("(append () () '(a b))", "(A B)");
211:
212: // nconc
213: verify("(nconc)", "NIL");
214: verify("(setq x '(a b c))", "(A B C)");
215: verify("(setq y '(d e f))", "(D E F)");
216: verify("(nconc x y)", "(A B C D E F)");
217: verify("x", "(A B C D E F)");
218: verify(
219: "(setq foo (list 'a 'b 'c 'd 'e) bar (list 'f 'g 'h 'i 'j) baz (list 'k 'l 'm))",
220: "(K L M)");
221: verify("(setq foo (nconc foo bar baz))",
222: "(A B C D E F G H I J K L M)");
223: verify("foo", "(A B C D E F G H I J K L M)");
224: verify("bar", "(F G H I J K L M)");
225: verify("baz", "(K L M)");
226: verify(
227: "(setq foo (list 'a 'b 'c 'd 'e) bar (list 'f 'g 'h 'i 'j) baz (list 'k 'l 'm))",
228: "(K L M)");
229: verify("(setq foo (nconc nil foo bar nil baz))",
230: "(A B C D E F G H I J K L M)");
231: verify("foo", "(A B C D E F G H I J K L M)");
232: verify("bar", "(F G H I J K L M)");
233: verify("baz", "(K L M)");
234: verify("(setq x '(a b c))", "(A B C)");
235: verify("(setq y '(d e f))", "(D E F)");
236: verify("(nconc x y)", "(A B C D E F)");
237: verify("x", "(A B C D E F)");
238:
239: // remove
240: // verify("(remove 4 '(1 3 4 5 9))", "(1 3 5 9)");
241: // verify("(remove 4 '(1 2 4 1 3 4 5))", "(1 2 1 3 5)");
242:
243: // strings
244: verify("\"foo\"", "\"foo\"");
245: verify("(setq foo \"bar\")", "\"bar\"");
246: verify("foo", "\"bar\"");
247:
248: // if
249: verify("(if t 123)", "123");
250: verify("(if () t)", "NIL");
251: verify("(if t 123 456)", "123");
252: verify("(if nil 123 456)", "456");
253: verify("(if (listp '(a b c)) 123 456)", "123");
254: verify("(if (listp '(a b c)) (+ 1 2) (+ 3 4))", "3");
255: verify("(if (numberp '(a b c)) (+ 1 2) (+ 3 4))", "7");
256:
257: // when
258: // verify("(when t 'hello)", "hello");
259: // verify("(when nil 'hello)", "NIL");
260: // verify("(when t)", "NIL");
261: // verify("(when ())", "NIL");
262:
263: // and
264: verify("(and)", "T");
265:
266: // or
267: verify("(or)", "NIL");
268: verify("(setq temp0 nil temp1 10 temp2 20 temp3 30)", "30");
269: verify("(or temp0 temp1 (setq temp2 37))", "10");
270: verify("temp2", "20");
271:
272: // unless
273: // verify("(unless t 'hello)", "NIL");
274: // verify("(unless nil 'hello)", "hello");
275: // verify("(unless t)", "NIL");
276: // verify("(unless ())", "NIL");
277:
278: // progn
279: verify("(progn (+ 1 2) (+ 3 4) (+ 5 6))", "11");
280: verify("(progn (setq foo 12) (setq foo 27))", "27");
281: verify("foo", "27");
282: verify("(progn)", "NIL");
283: verify("(progn (+ 2 1) 2)", "2");
284: verify("(progn 1 2 3)", "3");
285: verify("(setq a 1)", "1");
286: verify(
287: "(if a (progn (setq a nil) 'here) (progn (setq a t) 'there))",
288: "here");
289: verify("a", "NIL");
290:
291: // block
292: verify("(block empty)", "NIL");
293: verify("(block test (+ 1 2) (+ 3 4) (+ 5 6))", "11");
294: verify("(block nil (return) 1)", "NIL");
295: verify("(block nil (return 1) 2)", "1");
296: verify("(block nil (block alpha (return 1) 2))", "1");
297: verify("(block alpha (block nil (return 1)) 2)", "2");
298: verify("(block nil (block nil (return 1) 2))", "1");
299: verify("(block nil (return (values 1 2)) 3)", "1, 2");
300: verify("(block alpha (return-from alpha) 1)", "NIL");
301: verify("(block alpha (return-from alpha 1) 2)", "1");
302: verify(
303: "(let ((x 1)) (block stop (setq x 2) (return-from stop) (setq x 3)) x)",
304: "2");
305: verify("(block outer (block inner (return-from outer 1)) 2)",
306: "1");
307: verify("(block twin (block twin (return-from twin 1)) 2)", "2");
308:
309: // let
310: verify("(let () 1234)", "1234");
311: verify("(let ((x 1) (y 2)) (+ x y))", "3");
312: verify("z", ERROR);
313: verify("(setq z 1234)", "1234");
314: verify("z", "1234");
315: verify("(let ((x 1) (y 2) (z 3)) (+ x y z))", "6");
316: verify("z", "1234");
317: verify("(let ((x 1) (y 2) (z 3)) (setq z 4) (+ x y z))", "7");
318: verify("z", "1234");
319: verify("(let ((x 1) (y 2)) (setq z 4) (+ x y z))", "7");
320: verify("z", "4");
321:
322: // let*
323: // verify("(setq a 'top)", "TOP");
324: // verify("(defun dummy-function () a)", "DUMMY-FUNCTION");
325: // verify("(let ((a 'inside) (b a)) (format nil \"~A ~A ~A\" a b (dummy-function)))",
326: // "\"INSIDE TOP TOP\"");
327: // verify("(let* ((a 'inside) (b a)) (format nil \"~A ~A ~A\" a b (dummy-function)))",
328: // "\"INSIDE INSIDE TOP\"");
329: verify("(let* ((x 1) (y (+ x 1))) (+ x y))", "3");
330:
331: // =
332: verify("(= 0 0)", "T");
333: verify("(= 1 0)", "NIL");
334: verify("(= 0 1)", "NIL");
335: verify("(= 3 3)", "T");
336: verify("(= 3 5)", "NIL");
337: verify("(= 3 3 3 3)", "T");
338: verify("(= 3 3 5 3)", "NIL");
339: verify("(= 3 6 5 2)", "NIL");
340: verify("(= 3 2 3)", "NIL");
341: verify("(= 3)", "T");
342: verify("(= 3 4 'foo)", "NIL");
343: verify("(= 23 (+ 11 12))", "T");
344:
345: // /=
346: verify("(/= 3 3)", "NIL");
347: verify("(/= 3 5)", "T");
348: verify("(/= 3 3 3 3)", "NIL");
349: verify("(/= 3 3 5 3)", "NIL");
350: verify("(/= 3 6 5 2)", "T");
351: verify("(/= 3 2 3)", "NIL");
352: verify("(/= 3)", "T");
353: verify("(/= 3 3 'foo)", "NIL");
354:
355: // <=
356: verify("(<= 14 14)", "T");
357: verify("(<= 14 15)", "T");
358: verify("(<= 15 14)", "NIL");
359:
360: // evenp, oddp
361: verify("(evenp 2)", "T");
362: verify("(evenp 3)", "NIL");
363: verify("(oddp 2)", "NIL");
364: verify("(oddp 3)", "T");
365: verify("(evenp 0)", "T");
366: verify("(oddp -1)", "T");
367:
368: // zerop
369: verify("(zerop 0)", "T");
370: verify("(zerop 1234)", "NIL");
371: verify("(zerop '(a b c))", ERROR);
372: verify("(zerop (- 3 3))", "T");
373:
374: // min, max
375: verify("(max 3)", "3");
376: verify("(min 3)", "3");
377: verify("(max 6 12)", "12");
378: verify("(min 6 12)", "6");
379: verify("(max -6 -12)", "-6");
380: verify("(min -6 -12)", "-12");
381: verify("(max 1 3 2 -7)", "3");
382: verify("(min 1 3 2 -7)", "-7");
383: verify("(max -2 3 0 7)", "7");
384: verify("(min -2 3 0 7)", "-2");
385:
386: // defun
387: // verify("(setq x 1234)", "1234");
388: // verify("(defun foo (x) (+ x x))", "foo");
389: // verify("(foo 10)", "20");
390: // verify("x", "1234");
391:
392: // function
393: verify("(function xxx)", ERROR);
394:
395: // functionp
396: verify("(functionp 'append)", "NIL");
397: verify("(functionp #'append)", "T");
398: verify("(functionp (symbol-function 'append))", "T");
399: verify("(functionp nil)", "NIL");
400: verify("(functionp 12)", "NIL");
401: verify("(functionp '(lambda (x) (* x x)))", "NIL");
402: verify("(functionp #'(lambda (x) (* x x)))", "T");
403:
404: // special-operator-p
405: verify("(special-operator-p 'if)", "T");
406: verify("(special-operator-p 'car)", "NIL");
407: verify("(special-operator-p 'one)", "NIL");
408:
409: // funcall
410: verify("(funcall #'+ 1 2 3)", "6");
411: verify("(funcall #'+ (+ 1 2) 3)", "6");
412: verify("(funcall 'car '(1 2 3))", "1");
413: verify("(cons 1 2)", "(1 . 2)");
414: verify(
415: "(progn (setq cons (symbol-function '+)) (funcall cons 1 2 3))",
416: "6");
417: verify("(cons 1 2)", "(1 . 2)");
418:
419: // apply
420: verify("(apply (function +) '(1 2 3))", "6");
421: verify("(apply '+ '(1 2 3))", "6");
422: verify("(apply '+ 1 2 '(3 4 5))", "15");
423: verify("(apply '+ 1 2 3)", ERROR); // Last argument must be a list.
424: verify("(apply 'cons '((+ 2 3) 4))", "((+ 2 3) . 4)");
425: // verify("(defun add (x y) (+ x y))", "add");
426: // verify("(add 1 2)", "3");
427: // verify("(apply #'add '(1 2))", "3");
428: // verify("(apply #'add '((+ 1 2) 3))", ERROR);
429: // verify("(apply #'+ ())", "0");
430: // verify("(apply #'concatenate 'string '(\"foo\" \"bar\"))", "\"foobar\"");
431:
432: // lambda
433: verify("((lambda (x) (+ x x)) 23)", "46");
434: // verify("(apply (lambda (x y z) (+ x y z)) '(1 2 3))", "6");
435: // verify("(funcall (lambda (x) (+ x 3)) 4)", "7");
436:
437: // mapcar
438: verify("(mapcar #'(lambda (x) (+ x 10)) '(1 2 3))",
439: "(11 12 13)");
440: verify("(mapcar #'list '(a b c) '(1 2 3 4))",
441: "((a 1) (b 2) (c 3))");
442: verify("(mapcar #'car '((1 a) (2 b) (3 c)))", "(1 2 3)");
443: verify("(mapcar #'abs '(3 -4 2 -5 -6))", "(3 4 2 5 6)");
444: verify("(mapcar #'cons '(a b c) '(1 2 3))",
445: "((a . 1) (b . 2) (c . 3))");
446:
447: // eq
448: verify("(eq 'a 'a)", "T");
449: verify("(eq 'a 'b)", "NIL");
450: verify("(eq (cons 'a 'b) (cons 'a 'c))", "NIL");
451: verify("(eq (cons 'a 'b) (cons 'a 'b))", "NIL");
452: verify("(progn (setq x (cons 'a 'b)) (eq x x))", "T");
453: verify("(progn (setq x '(a . b)) (eq x x))", "T");
454: verify("(let ((x \"Foo\")) (eq x x))", "T");
455: verify("(eq \"FOO\" \"foo\")", "NIL");
456:
457: // eql
458: verify("(eql 'a 'b)", "NIL");
459: verify("(eql 'a 'a)", "T");
460: verify("(eql 3 3)", "T");
461: verify("(eql (cons 'a 'b) (cons 'a 'c))", "NIL");
462: verify("(eql (cons 'a 'b) (cons 'a 'b))", "NIL");
463: verify("(progn (setq x (cons 'a 'b)) (eql x x))", "T");
464: verify("(progn (setq x '(a . b)) (eql x x))", "T");
465: verify("(eql #\\a #\\a)", "T");
466: verify("(eql \"FOO\" \"foo\")", "NIL");
467:
468: // equal
469: verify("(equal 'a 'b)", "NIL");
470: verify("(equal 'a 'a)", "T");
471: verify("(equal 3 3)", "T");
472: verify("(equal (cons 'a 'b) (cons 'a 'c))", "NIL");
473: verify("(equal (cons 'a 'b) (cons 'a 'b))", "T");
474: verify("(equal #\\A #\\A)", "T");
475: verify("(equal #\\A #\\a)", "NIL");
476: verify("(equal \"Foo\" \"Foo\")", "T");
477: verify("(equal \"FOO\" \"foo\")", "NIL");
478: verify("(equal \"This-string\" \"This-string\")", "T");
479: verify("(equal \"This-string\" \"this-string\")", "NIL");
480:
481: // equalp
482: verify("(equalp 'a 'b)", "NIL");
483: verify("(equalp 'a 'a)", "T");
484: verify("(equalp 3 3)", "T");
485: verify("(equalp (cons 'a 'b) (cons 'a 'c))", "NIL");
486: verify("(equalp (cons 'a 'b) (cons 'a 'b))", "T");
487: verify("(equalp #\\A #\\A)", "T");
488: verify("(equalp #\\A #\\a)", "T");
489: verify("(equalp \"Foo\" \"Foo\")", "T");
490: verify("(equalp \"FOO\" \"foo\")", "T");
491:
492: // string
493: verify("(string \"already a string\")", "\"already a string\"");
494: verify("(string 'elm)", "\"ELM\"");
495: verify("(string #\\c)", "\"c\"");
496:
497: // string= (case sensitive)
498: // verify("(string= \"foo\" \"foo\")", "T");
499: // verify("(string= \"foo\" \"Foo\")", "NIL");
500: // verify("(string= \"foo\" \"bar\")", "NIL");
501:
502: // string-equal (case insensitive)
503: // verify("(string-equal \"foo\" \"foo\")", "T");
504: // verify("(string-equal \"foo\" \"Foo\")", "T");
505: // verify("(string-equal \"foo\" \"bar\")", "NIL");
506:
507: // make-string
508: // verify("(make-string 10 :initial-element #\\5)", "\"5555555555\"");
509: // verify("(length (make-string 10))", "10");
510:
511: // char
512: verify("(char \"test\" 0)", "#\\t");
513:
514: // boundp
515: verify("(setq x 1)", "1");
516: verify("(boundp 'x)", "T");
517: verify("(makunbound 'x)", "X");
518: verify("(boundp 'x)", "NIL");
519: verify("(let ((x 2)) (boundp 'x))", "NIL");
520:
521: // fboundp
522: verify("(fboundp 'car)", "T");
523: // verify("(defun my-function (x) x)", "MY-FUNCTION");
524: // verify("(fboundp 'my-function)", "T");
525: // verify("(fmakunbound 'my-function)", "MY-FUNCTION");
526: // verify("(fboundp 'my-function)", "NIL");
527:
528: // dotimes
529: verify("(dotimes (temp-one 10 temp-one))", "10");
530: verify("(setq temp-two 0)", "0");
531:
532: // member
533: verify("(member 2 '(1 2 3))", "(2 3)");
534: verify("(member 'e '(a b c d))", "NIL");
535:
536: // nth
537: verify("(nth 0 '(foo bar baz))", "FOO");
538: verify("(nth 1 '(foo bar baz))", "BAR");
539: verify("(nth 3 '(foo bar baz))", "NIL");
540:
541: // nthcdr
542: verify("(nthcdr 0 '())", "NIL");
543: verify("(nthcdr 3 '())", "NIL");
544: verify("(nthcdr 0 '(a b c))", "(A B C)");
545: verify("(nthcdr 2 '(a b c))", "(C)");
546: verify("(nthcdr 4 '(a b c))", "NIL");
547: verify("(nthcdr 1 '(0 . 1))", "1");
548: verify("(nthcdr 3 '(0 . 1)))", "ERROR");
549:
550: // rplaca, rplacd
551: verify("(setq g '(a b c))", "(A B C)");
552: verify("(rplaca (cdr g) 'd)", "(D C)");
553: verify("g", "(A D C)");
554: verify("(setq x '(a b c))", "(A B C)");
555: verify("(rplacd x 'd)", "(A . D)");
556: verify("x", "(A . D)");
557:
558: // endp
559: verify("(endp nil)", "T");
560: verify("(endp '(1 2))", "NIL");
561: verify("(endp (cddr '(1 2)))", "T");
562:
563: // reverse
564: // verify("(reverse '(a b c d e))", "(e d c b a)");
565:
566: // subst
567: // verify("(setq tree1 '(1 (1 2) (1 2 3) (1 2 3 4)))",
568: // "(1 (1 2) (1 2 3) (1 2 3 4))");
569: // verify("(subst \"two\" 2 tree1)",
570: // "(1 (1 \"two\") (1 \"two\" 3) (1 \"two\" 3 4))");
571: // verify("(subst \"five\" 5 tree1)",
572: // "(1 (1 2) (1 2 3) (1 2 3 4))");
573: // verify("(subst 'tempest 'hurricane '(shakespeare wrote (the hurricane)))",
574: // "(SHAKESPEARE WROTE (THE TEMPEST))");
575: // verify("(subst 'foo 'nil '(shakespeare wrote (twelfth night)))",
576: // "(SHAKESPEARE WROTE (TWELFTH NIGHT . FOO) . FOO)");
577:
578: // last
579: // verify("(last nil)", "NIL");
580: // verify("(last '(1 2 3))", "(3)");
581: // verify("(last '(1 2 . 3))", "(2 . 3)");
582: // verify("(setq x (list 'a 'b 'c 'd))", "(A B C D)");
583: // verify("(last x)", "(D)");
584: // verify("(rplacd (last x) (list 'e 'f))", "(D E F)");
585: // verify("x", "(A B C D E F)");
586: // verify("(last x)", "(F)");
587: // verify("(last '(a b c))", "(C)");
588: // verify("(last '(a b c) 0)", "NIL");
589: // verify("(last '(a b c) 1)", "(C)");
590: // verify("(last '(a b c) 2)", "(B C)");
591: // verify("(last '(a b c) 3)", "(A B C)");
592: // verify("(last '(a b c) 4)", "(A B C)");
593: // verify("(last '(a . b) 0)", "B");
594: // verify("(last '(a . b) 1)", "(A . B)");
595: // verify("(last '(a . b) 2)", "(A . B)");
596:
597: // subseq
598: // verify("(setq str \"012345\")", "\"012345\"");
599: // verify("(subseq str 2)", "\"2345\"");
600: // verify("(subseq str 3 5)", "\"34\"");
601:
602: // values
603: verify("(values)", "");
604: verify("(values 1)", "1");
605: verify("(values 1 2)", "1, 2");
606: verify("(values 1 2 3)", "1, 2, 3");
607: verify("(values (values 1 2 3) 4 5)", "1, 4, 5");
608: verify("(let ((x (values 1 2 3))) x)", "1");
609: verify("(let ((x (values))) x)", "NIL");
610: verify("(+ 1 (values 1 2 3))", "2");
611: verify("(1+ (values 1 2 3))", "2");
612:
613: // multiple-value-bind
614: verify(
615: "(multiple-value-bind (x y z) (values 1 2 3) (list x y z))",
616: "(1 2 3)");
617: verify(
618: "(multiple-value-bind (x y z) (values 1 2) (list x y z))",
619: "(1 2 NIL)");
620:
621: // multiple-value-list
622: verify("(multiple-value-list (car '(a b c)))", "(A)");
623: verify("(multiple-value-list (values 1 2 3))", "(1 2 3)");
624:
625: // macroexpand-1
626: verify("(defmacro nil! (x) (list 'setq x nil))", "NIL!");
627: verify("(macroexpand-1 '(nil! x))", "(SETQ X NIL), T");
628:
629: // vector
630: verify("(arrayp (setq v (vector 1 2 'sirens)))", "T");
631: verify("(vectorp v)", "T");
632: verify("(simple-vector-p v)", "T");
633: verify("(length v)", "3");
634: verify("(vector)", "#()");
635: verify("(vector ())", "#(NIL)");
636: verify("(vector 'a 'b 'c)", "#(A B C)");
637: verify("#()", "#()");
638: verify("#(a b c)", "#(A B C)");
639:
640: // arrayp
641: verify("(arrayp \"aaaa\")", "T");
642: verify("(vectorp \"aaaa\")", "T");
643: verify("(simple-vector-p \"aaaa\")", "NIL");
644:
645: // unwind-protect
646: verify("(block nil (unwind-protect (return 1) (return 2)))",
647: "2");
648: verify(
649: "(catch nil (unwind-protect (throw nil 1) (throw nil 2)))",
650: "2");
651: verify(
652: "(catch 'a (catch 'b (unwind-protect (1+ (catch 'a (throw 'b 1))) (throw 'a 10))))",
653: "10");
654: verify(
655: "(catch 'bar (catch 'foo (unwind-protect (throw 'foo 3) (throw 'bar 4) (print 'xxx))))",
656: "4");
657:
658: // flet
659: verify("(flet ((double (x) (+ x x))) (double 42))", "84");
660: verify(
661: "(flet ((plus (a b) (+ a b)) (minus (a b) (- a b))) (list (plus 1 2) (minus 1 2)))",
662: "(3 -1)");
663: verify("(list (flet ((+ (a b) (- a b))) (+ 3 2)) (+ 3 2))",
664: "(1 5)");
665: verify("(flet ((+ (a b) (+ (+ a b a) b))) (+ 3 2))", "10");
666:
667: // labels
668: verify(
669: "(labels ((queue (l) (if (car l) (queue (cdr l)) 'end))) (queue '(1 2 3)))",
670: "END");
671: verify("(labels ((+ (a b) (* a (+ a a b)))) (+ 1 2 3))", ERROR);
672: }
673:
674: private void verify(String input, String expected) {
675: assertTrue(input != null);
676: assertTrue(expected != null);
677: String output = Interpreter.evalString(input);
678: if (!expected.equalsIgnoreCase(output)) {
679: System.out.println("input = |" + input + "|");
680: System.out.println("output = |" + output + "|");
681: assertTrue(false);
682: }
683: }
684:
685: public static Test suite() {
686: return new TestSuite(Tests.class);
687: }
688:
689: public static void main(String[] args) {
690: TestRunner.run(suite());
691: }
692: }
|