ecase.lsp 3.36 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Oct 18 20:17:30 2002
;;;; Contains: Tests for ECASE

6

pfdietz's avatar
pfdietz committed
7 8 9 10 11 12

(deftest ecase.1
  (ecase 'b (a 1) (b 2) (c 3))
  2)

(deftest ecase.2
13
  (signals-type-error x 1 (ecase x))
14
  t)
pfdietz's avatar
pfdietz committed
15 16

(deftest ecase.3
17
  (signals-type-error x 1 (ecase x (a 1) (b 2) (c 3)))
18
  t)
pfdietz's avatar
pfdietz committed
19 20 21 22 23

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

(deftest ecase.4
24
  (signals-type-error x 1 (ecase x (t nil)))
25
  t)
pfdietz's avatar
pfdietz committed
26 27

(deftest ecase.5
28
  (signals-type-error x 1 (ecase x (otherwise nil)))
29
  t)
pfdietz's avatar
pfdietz committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

(deftest ecase.6
  (ecase 'b ((a z) 1) ((y b w) 2) ((b c) 3))
  2)

(deftest ecase.7
  (ecase 'z
    ((a b c) 1)
    ((d e) 2)
    ((f z g) 3))
  3)

(deftest ecase.8
  (ecase (1+ most-positive-fixnum)
    (#.(1+ most-positive-fixnum) 'a))
  a)

(deftest ecase.9
48
  (signals-type-error x nil (ecase x (nil 'a)))
49
  t)
pfdietz's avatar
pfdietz committed
50 51 52 53 54 55 56 57 58 59

(deftest ecase.10
  (ecase nil ((nil) 'a))
  a)

(deftest ecase.11
  (ecase 'a (b 0) (a (values 1 2 3)) (c nil))
  1 2 3)

(deftest ecase.12
60
  (signals-type-error x t (ecase x (a 10)))
61
  t)
pfdietz's avatar
pfdietz committed
62 63 64 65 66 67 68 69 70 71 72

(deftest ecase.13
  (ecase t ((t) 10) (t 20))
  10)

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

(deftest ecase.15
73
  (signals-type-error x 'otherwise (ecase x ((t) 10)))
74
  t)
pfdietz's avatar
pfdietz committed
75 76

(deftest ecase.16
77
  (signals-type-error x t (ecase x ((otherwise) 10)))
78
  t)
pfdietz's avatar
pfdietz committed
79 80

(deftest ecase.17
81
  (signals-type-error x 'a (ecase x (b 0) (c 1) (otherwise 2)))
82
  t)
pfdietz's avatar
pfdietz committed
83 84

(deftest ecase.18
85
  (signals-type-error x 'a (ecase x (b 0) (c 1) ((otherwise) 2)))
86
  t)
pfdietz's avatar
pfdietz committed
87 88

(deftest ecase.19
89
  (signals-type-error x 'a (ecase x (b 0) (c 1) ((t) 2)))
90
  t)
pfdietz's avatar
pfdietz committed
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

(deftest ecase.20
  (ecase #\a
    ((#\b #\c) 10)
    ((#\d #\e #\A) 20)
    (() 30)
    ((#\z #\a #\y) 40))
  40)

(deftest ecase.21 (ecase 1 (1 (values)) (2 'a)))

(deftest ecase.23 (ecase 1 (1 (values 'a 'b 'c)))
  a b c)

;;; Show that the key expression is evaluated only once.
(deftest ecase.25
  (let ((x 0))
    (values
     (ecase (progn (incf x) 'c)
       (a 1)
       (b 2)
       (c 3)
       (d 4))
     x))
  3 1)

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

(deftest ecase.26
  (ecase 'b ((a b c) 10) (b 20))
  10)

(deftest ecase.27
  (ecase 'b (b 20) ((a b c) 10))
  20)

(deftest ecase.28
  (ecase 'b (b 20) (b 10) (d 0))
  20)

;;; There are implicit progns

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

(deftest ecase.31
  (ecase (values 'b 'c) (c 0) ((a b) 10) (d 20))
  10)
146 147 148 149

(deftest ecase.32
  (ecase 'a (a) (b 'b))
  nil)
150 151 152 153 154 155

;;; No implicit tagbody
(deftest ecase.33
  (block done
    (tagbody
     (ecase 'a (a (go 10)
sds's avatar
sds committed
156 157
                  10
                  (return-from done 'bad)))
158 159 160
     10
     (return-from done 'good)))
  good)
161

162 163 164 165 166 167 168
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest ecase.34
  (macrolet
   ((%m (z) z))
   (ecase (expand-in-current-env (%m :b))
sds's avatar
sds committed
169 170 171
          (:a :bad1)
          (:b :good)
          (:c :bad2)))
172 173
  :good)

174
(deftest ecase.error.1
175 176
  (signals-error (funcall (macro-function 'ecase)) program-error)
  t)
177 178

(deftest ecase.error.2
179
  (signals-error (funcall (macro-function 'ecase) '(ecase t))
sds's avatar
sds committed
180
                 program-error)
181
  t)
182 183

(deftest ecase.error.3
184
  (signals-error (funcall (macro-function 'ecase) '(ecase t) nil nil)
sds's avatar
sds committed
185
                 program-error)
186
  t)