151 lines
4.0 KiB
Plaintext
151 lines
4.0 KiB
Plaintext
;; Function To Be Tested: if
|
|
;;
|
|
;; Source: Steele's book
|
|
;; Section 7.6: Conditionals
|
|
;; Page: 115
|
|
;;
|
|
;; Created By: Bob Bane
|
|
;; Henry Cate III
|
|
;;
|
|
;; Creation Date: June 26,1986
|
|
;;
|
|
;; Last Update: June 26,1986
|
|
;; October 9,1986 HC3/ documented, and added
|
|
;; several more test cases
|
|
;; October 24,1986 HC3/ broke into several tests
|
|
;;
|
|
;; Filed As: {ERIS}<LISPCORE>CML>TEST>7-6-IF.TEST
|
|
;;
|
|
;;
|
|
;; Syntax: (IF TEST THEN [ELSE])
|
|
;;
|
|
;; which is exactly equal to:
|
|
;; (cond (test then) (t else))
|
|
;;
|
|
;;
|
|
;; Function Description:
|
|
;; The if special form corresponds to the if-then-else
|
|
;; construct common to other languages. First TEST is
|
|
;; evaluated. If the result is not nil, THEN is selected;
|
|
;; otherwise, ELSE is selected. Whatever is slected is
|
|
;; evaluated, and if returns whatever evaluation of the
|
|
;; selected form returns.
|
|
;;
|
|
;;
|
|
;;
|
|
;; Argument(s): TEST - a form which returns nil or non-nil
|
|
;; THEN - a lisp data object meant to be evaluated
|
|
;; to produce one or more values
|
|
;; ELSE - an optional lisp data object meant to be
|
|
;; evaluated to produce one or more values
|
|
;;
|
|
;; Returns: value(s) of the last evaluated form of
|
|
;; the selected clause
|
|
;;
|
|
|
|
|
|
|
|
(do-test "test the simple cases"
|
|
(and
|
|
; Does if evaluate and return the appropriate things?
|
|
; Check just boolean values
|
|
(if T T)
|
|
(if nil nil T)
|
|
(eq (if nil t) nil)
|
|
))
|
|
|
|
|
|
(do-test "test when do some work in test"
|
|
(and
|
|
(if T (> 3 0))
|
|
(if (> 3 0) (> 3 0))
|
|
(if (> 3 0) (> 3 0) nil)
|
|
))
|
|
|
|
|
|
(do-test "test when call a locally defined function"
|
|
; Define two functions, tee returning T,
|
|
; and nill returning nil
|
|
(flet ((tee nil t) (nill nil nil))
|
|
(and
|
|
(eq (if (tee) 'foo 'bar) 'foo)
|
|
(eq (if (tee) 'foo) 'foo)
|
|
(eq (if (nill) 'foo 'bar) 'bar)
|
|
(eq (if (nill) 'foo) nil)
|
|
)))
|
|
|
|
|
|
(do-test "test able to return numbers"
|
|
(flet ((tee nil t) (nill nil nil))
|
|
(and
|
|
(eq (if (tee) (* 3 4) (* 5 6)) 12)
|
|
(eq (if (tee) (* 2 3)) 6)
|
|
(eq (if (nill) (* 1 2) (* 5 5)) 25)
|
|
(eq (if (nill) (* 9 9)) nil)
|
|
)))
|
|
|
|
|
|
(do-test "test numbers and symbols are true"
|
|
(and
|
|
(if 5 T)
|
|
(if 5 T nil)
|
|
(if 'hi T)
|
|
(if 'hi T nil)
|
|
))
|
|
|
|
|
|
(do-test "test able to return several values"
|
|
(flet ((tee nil t) (nill nil nil))
|
|
(and
|
|
; Does if pass multiple values?
|
|
(equal (multiple-value-list
|
|
(if (tee) (values 'foo 'bar)
|
|
(values 'baz 'bletch)))
|
|
'(foo bar))
|
|
(equal (multiple-value-list
|
|
(if (nill) (values 'foo 'bar)
|
|
(values 'baz 'bletch)))
|
|
'(baz bletch))
|
|
)))
|
|
|
|
|
|
(do-test "test values set in IF are still set outside"
|
|
; Check values set withinside of IF
|
|
; are still set outside of IF
|
|
; Define two functions, tee returning T,
|
|
; and nill returning nil
|
|
(flet ((tee nil t) (nill nil nil))
|
|
(let ((sideffect nil))
|
|
(and
|
|
(eq (if (tee) (setq sideffect T)
|
|
nil) T)
|
|
(eq sideffect T)
|
|
(eq (if (tee) (setq sideffect 'foo)
|
|
'bar) 'foo)
|
|
(eq sideffect 'foo)
|
|
(eq (if (tee) (setq sideffect 5)
|
|
23) 5)
|
|
(eq sideffect 5)
|
|
))))
|
|
|
|
(do-test "test path not taken was in fact not taken"
|
|
; Check values set withinside of IF
|
|
; are still set outside of IF
|
|
; Define two functions, tee returning T,
|
|
; and nill returning nil
|
|
(flet ((tee nil t) (nill nil nil))
|
|
(let ((sideffect nil))
|
|
(and
|
|
(eq (if (nill) (setq sideffect T)
|
|
nil) nil)
|
|
(eq sideffect nil)
|
|
(eq (if (tee) (setq sideffect 'foo)
|
|
(setq sideffect 'bar)) 'foo)
|
|
(eq sideffect 'foo)
|
|
(eq (if (nill) (setq sideffect 5)
|
|
(setq sideffect 23)) 23)
|
|
(eq sideffect 23)
|
|
))))
|
|
|
|
STOP
|