home *** CD-ROM | disk | FTP | other *** search
- ; test.l - Copyright (c) 1984 by David Morein
- ; this file contains miscellaneous test functions.
- ; every expression should return T as the file is LOADV'ed
- ;
- ; arithmetic functions:
- ;
-
- "testing arithmetic functions:"
-
- (equal 4 (setq a 4))
- (equal t (atom 'a))
- (equal nil (atom '(a b)))
- (equal t (numberp 432))
- (equal 'a (setq b 'a))
- (equal nil (numberp b))
- (equal 4 (setq a 4))
- (equal 5 (setq b 5))
- (equal 0 (div a b))
- (equal 1 (div b a))
- (equal 1 (mod b a))
- (equal 4 (mod a b))
- (equal 20 (* a b))
- (equal 1 (- b a))
- ;
- ; logic functions:
- ;
-
- "testing logic functions:"
-
- (equal t (and t t))
- (equal nil (and t nil))
- (equal nil (and nil t))
- (equal t (or t t))
- (equal t (or t nil))
- (equal t (or nil t))
- (equal nil (or nil nil))
- (equal t (not nil))
- (equal nil (not t))
- ;
- ; string functions
-
- "testing string functions"
-
- (streq "abcde" "abcde")
- (eql 0 (strcmp "abcde" "abcde"))
- (eql 5 (strlen "abcde"))
- (streq "abcdef" (strcat "abc" "def"))
- ;
- ; set functions:
- ;
-
- "testing set functions"
-
- (equal '(b c) (member 'b '(a b c)))
- (equal '(b c) (membq 'b '(a b c)))
- ;
- ; apply & funcall:
- ;
- "testing apply & funcall"
- (equal '((+ 2 3) . 5) (apply 'cons '((+ 2 3) 5)))
- (equal '(1 . 2) (funcall 'cons 1 2))
- ;
- ; mapping functions:
- ;
- "testing mapping functions"
- ;
- ; from Hasemer [1984], p244
- (equal '((a b) (c d) (e f)) (setq q '((a b) (c d) (e f))))
- (equal '(a c e) (mapcar 'car q))
- ;
- ; from Steele [1984], p128, without functional closure:
- (equal '((a . 1) (b . 2) (c . 3)) (mapcar 'cons '(a b c) '(1 2 3)))
- ;
- ; from Steele [1984], p128
- (equal 'foo (defun foo (x) (and (numberp x) (list x))))
- (equal '(1 2 3 4) (mapcan 'foo '(a 1 b c 2 3 d 4)))
- ;
- ; from ???
- ;
- (equal 'square (defun square (n) (* n n)))
- (equal 'square-list (defun square-list (l) (mapcar 'square l)))
- (equal '(4 9) (square-list '(2 3)))
- ;
- ; define consfoo:
- (equal 'consfoo (defun consfoo (x) (cons 'foo x)))
- ;
- ; apply consfoo through maplist:
- (equal '((foo a b c d) (foo b c d) (foo c d) (foo d))
- (maplist 'consfoo '(a b c d)))
- ;
- ; see if we get the same result with an anonymous form:
- (equal '((foo a b c d) (foo b c d) (foo c d) (foo d))
- (maplist '(lambda (x) (cons 'foo x)) '(a b c d)))
- ;
- ; miscellaneous tests:
- (equal '((a . x) (b . y)) (mapcar 'cons '(a b) '(x y)))
- (equal '(((a b) x y) ((b) y)) (maplist 'cons '(a b) '(x y)))
- ;
- ; do:
- ;
-
- "testing do"
-
- (equal 'our-expt
- (defun our-expt (m n)
- (do ((result 1) ;initialize result to 1
- (exponent n)) ;initialize exponent to n
- ((zerop exponent) result) ;termination test
- ;
- ; body of do:
- ;
- (setq result (* m result))
- (setq exponent (- exponent 1)))))
- ;
- (equal 16 (our-expt 2 4))
- (equal 32 (our-expt 2 5))
- ;
- ; misc predicates:
- ;
- (eql t (consp '(a . b)))
- (eql nil (consp nil))
- (eql t (listp '(a b)))
- (eql t (listp nil))
- (eql nil (listp 'a))
- ;
- ; macros:
- ;
- "testing macros"
- ;
- (eql 'mactest
- (def mactest (macro (arg1 arg2)
- (list 'cons arg1 arg2))))
- ;
- (eql 'mac1
- (def mac1 (lambda (a b)
- (mactest a b))))
- ;
- ; the following should return (1 . 2)
- ;
- (equal '(1 . 2) (mac1 1 2))
- ;
- ;
- "testing backquote"
- (eq 'bqtest (def bqtest (macro (carpart cdrpart)
- `(cons ,carpart ,cdrpart))))
- ;
- (equal '(1 . 2) (bqtest 1 2))
- ;
- (equal '(silvery moon) (setq planetoid '(silvery moon)))
- ;
- (equal '(by the light of the silvery moon)
- `(by the light of the ,@planetoid))
- ;
- ;
- ; miscellaneous tests of catch, throw, err, and errset:
- ;
- "testing catch and throw"
- ;
- ; ctest1 just tests a simple THROW to an enclosing CATCH
- ;
- (eql 'ctest1 (
- (def ctest1 (lambda (x)
- (catch 'cflag (throw 'cflag x))))))
- ;
- (eql 4 (ctest1 4))
- ;
- ;
- ; ctest2 tests a throw to a catch which has a list of tags
- ; which it is supposed to intercept
- ;
- (eql 'ctest2 (
- (def ctest2 (lambda (x)
- (catch '(cflag1 cflag2 cflag3) (throw 'cflag2 x))))))
- ;
- (eql 5 (ctest2 5))
- ;
- ; ctest3 tests a throw to a catch which has a tag of NIL
- ;
- (eql 'ctest3 (
- (def ctest3 (lambda (x)
- (catch '() (throw nil x))))))
- ;
- (eql 7 (ctest3 7))
- ;
- ;
-
- "loading examples" ;herald examples
-
- (load "examples.l") ;load examples
-
- "testing examples" ;run examples
-
- ;
- (eql 233 (rfib 12))
- (eql 610 (rfib 14))
- (eql 40320 (ifact 8))
- (eql 479001600 (ifact 12))
- (equal '(a b c d e f) (app '(a b c) '(d e f)))
- ;
- ;
- ;end of file