ccase.lsp 3.84 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Oct 18 21:06:45 2002
;;;; Contains: Tests of CCASE

6

7 8 9 10 11 12 13

(deftest ccase.1
  (let ((x 'b))
    (ccase x (a 1) (b 2) (c 3)))
  2)

(deftest ccase.2
14
  (signals-type-error x 1 (ccase x))
15
  t)
16 17

(deftest ccase.3
18
  (signals-type-error x 1 (ccase x (a 1) (b 2) (c 3)))
19
  t)
20 21 22 23 24

;;; It is legal to use T or OTHERWISE as key designators
;;; in CCASE forms.  They have no special meaning here.

(deftest ccase.4
25
  (signals-type-error x 1 (ccase x (t nil)))
26
  t)
27 28

(deftest ccase.5
29
  (signals-type-error x 1 (ccase x (otherwise nil)))
30
  t)
31 32 33 34 35 36 37 38 39

(deftest ccase.6
  (let ((x 'b))
    (ccase x ((a z) 1) ((y b w) 2) ((b c) 3)))
  2)

(deftest ccase.7
  (let ((x 'z))
    (ccase x
sds's avatar
sds committed
40 41 42
           ((a b c) 1)
           ((d e) 2)
           ((f z g) 3)))
43 44 45 46 47 48 49 50
  3)

(deftest ccase.8
  (let ((x (1+ most-positive-fixnum)))
    (ccase x (#.(1+ most-positive-fixnum) 'a)))
  a)

(deftest ccase.9
51
  (signals-type-error x nil (ccase x (nil 'a)))
52
  t)
53 54 55 56 57 58 59 60 61 62 63 64

(deftest ccase.10
  (let (x)
    (ccase x ((nil) 'a)))
  a)

(deftest ccase.11
  (let ((x 'a))
    (ccase x (b 0) (a (values 1 2 3)) (c nil)))
  1 2 3)

(deftest ccase.12
65
  (signals-type-error x t (ccase x (a 10)))
66
  t)
67 68 69 70 71 72 73 74 75 76 77 78

(deftest ccase.13
  (let ((x t))
    (ccase x ((t) 10) (t 20)))
  10)

(deftest ccase.14
  (let ((x (list 'a 'b)))
    (eval `(let ((y (quote ,x))) (ccase y ((,x) 1) (a 2)))))
  1)

(deftest ccase.15
79
  (signals-type-error x 'otherwise (ccase x ((t) 10)))
80
  t)
81 82

(deftest ccase.16
83
  (signals-type-error x t (ccase x ((otherwise) 10)))
84
  t)
85 86

(deftest ccase.17
87
  (signals-type-error x 'a (ccase x (b 0) (c 1) (otherwise 2)))
88
  t)
89 90

(deftest ccase.19
91
  (signals-type-error x 'a (ccase x (b 0) (c 1) ((t) 2)))
92
  t)
93 94 95 96

(deftest ccase.20
  (let ((x #\a))
    (ccase x
sds's avatar
sds committed
97 98 99 100
           ((#\b #\c) 10)
           ((#\d #\e #\A) 20)
           (() 30)
           ((#\z #\a #\y) 40)))
101 102 103 104 105 106 107 108 109 110
  40)

(deftest ccase.21 (let ((x 1)) (ccase x (1 (values)) (2 'a))))

(deftest ccase.23 (let ((x 1)) (ccase x (1 (values 'a 'b 'c))))
  a b c)

;;; Show that the key expression is evaluated only once.
(deftest ccase.25
  (let ((a (vector 'a 'b 'c 'd 'e))
sds's avatar
sds committed
111
        (i 1))
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
    (values
     (ccase (aref a (incf i))
       (a 1)
       (b 2)
       (c 3)
       (d 4))
     i))
  3 2)

;;; Repeated keys are allowed (all but the first are ignored)

(deftest ccase.26
  (let ((x 'b))
    (ccase x ((a b c) 10) (b 20)))
  10)

(deftest ccase.27
  (let ((x 'b))
    (ccase x (b 20) ((a b c) 10)))
  20)

(deftest ccase.28
  (let ((x 'b))
    (ccase x (b 20) (b 10) (d 0)))
  20)

;;; There are implicit progns

(deftest ccase.29
  (let ((x nil) (y 2))
    (values
     (ccase y
       (1 (setq x 'a) 'w)
       (2 (setq x 'b) 'y)
       (3 (setq x 'c) 'z))
     x))
  y b)

150
(deftest ccase.30
151 152
  (let ((x 'a))
    (ccase x (a)))
153 154
  nil)

155 156 157 158 159 160 161 162 163 164
(deftest ccase.31
  (handler-bind
   ((type-error #'(lambda (c) (store-value 7 c))))
   (let ((x 0))
     (ccase x
      (1 :bad)
      (7 :good)
      (2 nil))))
  :good)

165 166 167 168 169 170
;;; No implicit tagbody
(deftest ccase.32
  (block done
    (tagbody
     (let ((x 'a))
       (ccase x (a (go 10)
sds's avatar
sds committed
171 172
                   10
                   (return-from done 'bad))))
173 174 175 176
     10
     (return-from done 'good)))
  good)

177 178 179 180 181 182 183 184
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest ccase.33
  (let ((x :b))
    (macrolet
     ((%m (z) z))
     (ccase (expand-in-current-env (%m x))
sds's avatar
sds committed
185 186 187
            (:a :bad1)
            (:b :good)
            (:c :bad2))))
188 189 190
  :good)


191

192
;;; (deftest ccase.error.1
193 194
;;;  (signals-error (ccase) program-error)
;;;  t)
195 196

(deftest ccase.error.1
197
  (signals-error (funcall (macro-function 'ccase))
sds's avatar
sds committed
198
                 program-error)
199
  t)
200 201

(deftest ccase.error.2
202
  (signals-error (funcall (macro-function 'ccase) '(ccase t))
sds's avatar
sds committed
203
                 program-error)
204
  t)
205 206

(deftest ccase.error.3
207
  (signals-error (funcall (macro-function 'ccase) '(ccase t) nil nil)
sds's avatar
sds committed
208
                 program-error)
209
  t)