1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Fri Oct 18 22:51:25 2002
;;;; Contains: Tests for TYPECASE
(in-package :cl-test)
(deftest typecase.1
(typecase 1 (integer 'a) (t 'b))
a)
(deftest typecase.2
(typecase 1 (symbol 'a))
nil)
(deftest typecase.3
(typecase 1 (symbol 'a) (t 'b))
b)
(deftest typecase.4
(typecase 1 (t (values))))
(deftest typecase.5
(typecase 1 (integer (values)) (t 'a)))
(deftest typecase.6
(typecase 1 (bit 'a) (integer 'b))
a)
(deftest typecase.7
(typecase 1 (otherwise 'a))
a)
(deftest typecase.8
(typecase 1 (t (values 'a 'b 'c)))
a b c)
(deftest typecase.9
(typecase 1 (integer (values 'a 'b 'c)) (t nil))
a b c)
(deftest typecase.10
(let ((x 0))
(values
(typecase 1
(bit (incf x) 'a)
(integer (incf x 2) 'b)
(t (incf x 4) 'c))
x))
a 1)
(deftest typecase.11
(typecase 1 (otherwise 'a))
a)
(deftest typecase.12
(typecase 1 (integer) (t 'a))
nil)
(deftest typecase.13
(typecase 1 (symbol 'a) (t))
nil)
(deftest typecase.14
(typecase 1 (symbol 'a) (otherwise))
nil)
(deftest typecase.15
(typecase 'a
(number 'bad)
(#.(find-class 'symbol nil) 'good))
good)
(deftest typecase.16
(block done
(tagbody
(typecase 'a (symbol (go 10)
10
(return-from done 'bad)))
10
(return-from done 'good)))
good)
(deftest typecase.17
(block done
(tagbody
(typecase 'a
(integer 'bad)
(t (go 10)
10
(return-from done 'bad)))
10
(return-from done 'good)))
good)
(deftest typecase.18
(loop for x in '(a 1 1.4 "c")
collect (typecase x
(t :good)
(otherwise :bad)))
(:good :good :good :good))
;;; A randomized test
(deftest typecase.19
(let* ((u (coerce *universe* 'vector))
(len1 (length u))
(types (coerce *cl-all-type-symbols* 'vector))
(len2 (length types)))
(loop
for n = (random 10)
for my-types = (loop repeat n collect (elt types (random len2)))
for val = (elt u (random len1))
for i = (position val my-types :test #'typep)
for form = `(typecase ',val
,@(loop for i from 0 for type in my-types collect `(,type ,i))
(otherwise nil))
for j = (eval form)
repeat 1000
unless (eql i j)
collect (list n my-types val i form j)))
nil)
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment
(deftest typecase.20
(macrolet
((%m (z) z))
(typecase (expand-in-current-env (%m 2))
((integer 0 1) :bad1)
((integer 2 10) :good)
(t :bad2)))
:good)
(deftest typecase.21
(macrolet
((%m (z) z))
(typecase 2
((integer 0 1) (expand-in-current-env (%m :bad1)))
((integer 2 10) (expand-in-current-env (%m :good)))
(t (expand-in-current-env (%m :bad2)))))
:good)
;;; Error cases
(deftest typecase.error.1
(signals-error (funcall (macro-function 'typecase)) program-error)
t)
(deftest typecase.error.2
(signals-error (funcall (macro-function 'typecase)
'(typecase t)) program-error)
t)
(deftest typecase.error.3
(signals-error (funcall (macro-function 'typecase)
'(typecase t)
nil nil) program-error)
t)
|