places.lsp 6.11 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Oct  7 19:20:17 2002
;;;; Contains: Tests of various kinds of places (section 5.1)

6

7 8

;;; Section 5.1.1.1
9 10

(deftest setf.order.1
11
  (let ((x (vector nil nil nil nil))
sds's avatar
sds committed
12
        (i 0))
13 14 15 16
    (setf (aref x (incf i)) (incf i))
    (values x i))
  #(nil 2 nil nil) 2)

17
(deftest setf.order.2
18
  (let ((x (vector nil nil nil nil))
sds's avatar
sds committed
19
        (i 0))
20
    (setf (aref x (incf i)) (incf i)
sds's avatar
sds committed
21
          (aref x (incf i)) (incf i 10))
22 23 24
    (values x i))
  #(nil 2 nil 13) 13)

25
(deftest incf.order.1
26
  (let ((x (copy-seq #(0 0 0 0 0)))
sds's avatar
sds committed
27
        (i 1))
28 29 30 31 32
    (values
     (incf (aref x (incf i)) (incf i))
     x i))
  3 #(0 0 3 0 0) 3)

33
(deftest decf.order.1
34
  (let ((x (copy-seq #(0 0 0 0 0)))
sds's avatar
sds committed
35
        (i 1))
36 37 38 39 40
    (values
     (decf (aref x (incf i)) (incf i))
     x i))
  -3 #(0 0 -3 0 0) 3)

sds's avatar
sds committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
;;; Section 5.1.2.1
(deftest setf-var
  (let ((x nil))
    (setf x 'a)
    x)
  a)

;;; Section 5.1.2.2
;;; See SETF forms at various accessor functions

;;; Section 5.1.2.3
(deftest setf-values.1
  (let ((x nil) (y nil) (z nil))
    (setf (values x y z) (values 1 2 3)))
  1 2 3)

(deftest setf-values.2
  (let ((x nil) (y nil) (z nil))
    (setf (values x y z) (values 1 2 3))
    (values z y x))
62
  3 2 1)
63 64 65 66 67 68 69

(deftest setf-values.3
  (let ((x nil) (y nil) (z nil))
    (setf (values x x x) (values 1 2 3))
    x)
  3)

70 71 72 73 74 75 76 77 78 79 80 81
;;; Test that the subplaces of a VALUES place can be
;;; complex, and that the various places' subforms are
;;; evaluated in the correct (left-to-right) order.

(deftest setf-values.4
  (let ((x (list 'a 'b)))
    (setf (values (car x) (cadr x)) (values 1 2))
    x)
  (1 2))

(deftest setf-values.5
  (let ((a (vector nil nil))
sds's avatar
sds committed
82 83
        (i 0)
        x y z)
84
    (setf (values (aref a (progn (setf x (incf i)) 0))
sds's avatar
sds committed
85 86 87 88
                  (aref a (progn (setf y (incf i)) 1)))
          (progn
            (setf z (incf i))
            (values 'foo 'bar)))
89 90 91
    (values a i x y z))
  #(foo bar) 3 1 2 3)

92 93 94
(deftest setf-values.6
  (setf (values) (values)))

95 96 97 98 99 100 101
;;; Section 5.1.2.4
(deftest setf-the.1
  (let ((x 1))
    (setf (the integer x) 2)
    x)
  2)

102 103 104 105 106 107 108
(deftest setf-the.2
  (let ((x (list 'a)))
    (values
     (setf (the symbol (car x)) 'b)
     x))
  b (b))

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
;;; Section 5.1.2.5
(deftest setf-apply.1
  (let ((x (vector 0 1 2 3 4 5)))
    (setf (apply #'aref x '(0)) 10)
    x)
  #(10 1 2 3 4 5))

(deftest setf-apply.2
  (let ((a (make-array '(2 2) :initial-contents '((0 0)(0 0)))))
    (setf (apply #'aref a 1 1 nil) 'a)
    (equalp a (make-array '(2 2) :initial-contents '((0 0)(0 a)))))
  t)

(deftest setf-apply.3
  (let ((bv (copy-seq #*0000000000)))
    (setf (apply #'bit bv 4 nil) 1)
    bv)
  #*0000100000)

(deftest setf-apply.4
  (let ((bv (copy-seq #*0000000000)))
    (setf (apply #'sbit bv 4 nil) 1)
    bv)
  #*0000100000)

;;; Section 5.1.2.6
(defun accessor-5-1-2-6-update-fn (x y)
  (setf (car x) y)
  y)

(defsetf accessor-5-1-2-6 accessor-5-1-2-6-update-fn)

(deftest setf-expander.1
  (let ((x (list 1)))
    (values (setf (accessor-5-1-2-6 x) 2)
sds's avatar
sds committed
144
            (1+ (car x))))
145 146 147 148 149 150 151 152
  2 3)

;;; Section 5.1.2.7

(defmacro accessor-5-1-2-7 (x) `(car ,x))
(deftest setf-macro.1
  (let ((x (list 1)))
    (values (setf (accessor-5-1-2-7 x) 2)
sds's avatar
sds committed
153
            (1+ (car x))))
154 155 156 157 158 159 160 161 162 163 164 165 166
  2 3)

(defun accessor-5-1-2-7a-update-fn (x y)
  (declare (special *x*))
  (setf (car x) y)
  (setf *x* 'boo)
  y)

(defmacro accessor-5-1-2-7a (x) `(car ,x))
(defsetf accessor-5-1-2-7a accessor-5-1-2-7a-update-fn)
;; Test that the defsetf override the macro expansion
(deftest setf-macro.2
  (let ((x (list 1))
sds's avatar
sds committed
167
        (*x* nil))
168 169
     (declare (special *x*))
    (values (setf (accessor-5-1-2-7a x) 2)
sds's avatar
sds committed
170 171
            *x*
            (1+ (car x))))
172 173 174 175 176 177 178
  2 boo 3)

(defmacro accessor-5-1-2-7b (x) `(accessor-5-1-2-7 ,x))
;; Test that the macroexpansion occurs more than once
(deftest setf-macro.3
  (let ((x (list 1)))
    (values (setf (accessor-5-1-2-7b x) 2)
sds's avatar
sds committed
179
            (1+ (car x))))
180 181 182 183 184 185 186
  2 3)

;; Macroexpansion from a macrolet
(deftest setf-macro.4
  (macrolet ((%m (y) `(car ,y)))
    (let ((x (list 1)))
      (values (setf (%m x) 2)
sds's avatar
sds committed
187
              (1+ (car x)))))
188
  2 3)
189 190 191 192 193 194 195 196 197 198 199 200 201 202

;;; section 5.1.2.8 -- symbol macros
(deftest setf-symbol-macro.1
  (symbol-macrolet ((x y))
    (let ((y nil))
      (values (setf x 1) x y)))
  1 1 1)

;;; Symbol macros in SETQs are treated as if the form were a SETF
(deftest setf-symbol-macro.2
  (symbol-macrolet ((x y))
    (let ((y nil))
      (values (setq x 1) x y)))
  1 1 1)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

;;; Tests that, being treated like SETF, this causes multiple values
;;; to be assigned to (values y z)
(deftest setf-symbol-macro.3
  (symbol-macrolet ((x (values y z)))
    (let ((y nil) (z nil))
      (values (setq x (values 1 2)) x y z)))
  1 1 1 2)

(deftest setq.1
  (setq)
  nil)

(deftest setq.2
  (let ((x 0) (y 0))
    (values (setq x 1 y 2) x y))
  2 1 2)

(deftest setq.3
  (let ((x 0) (y 0))
    (values (setq x (values 1 3) y (values 2 4)) x y))
  2 1 2)

(deftest setq.4
  (let (x) (setq x (values 1 2)))
  1)

230 231 232 233 234 235 236 237 238 239 240
(deftest setq.5
  (let ((*x* 0))
    (declare (special *x*))
    (values *x* (setq *x* 1) *x*))
  0 1 1)

(deftest setq.6
  (let ((*x* 0))
    (declare (special *x*))
    (setq *x* 1))
  1)
241 242 243 244 245 246 247 248 249

;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest setq.7
  (macrolet
   ((%m (z) z))
   (let ((x nil))
     (values (setq x (expand-in-current-env (%m :good)))
sds's avatar
sds committed
250
             x)))
251 252
  :good :good)

sds's avatar
sds committed
253
;;; Tests of SETF
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
(deftest setf.1
  (setf)
  nil)

(deftest setf.2
  (let ((x 0) (y 0))
    (values (setf x 1 y 2) x y))
  2 1 2)

(deftest setf.3
  (let ((x 0) (y 0))
    (values (setf x (values 1 3) y (values 2 4)) x y))
  2 1 2)

(deftest setf.4
  (let (x) (setf x (values 1 2)))
  1)
272 273 274 275 276 277 278 279 280 281 282 283

(deftest setf.5
  (let ((*x* 0))
    (declare (special *x*))
    (values *x* (setf *x* 1) *x*))
  0 1 1)

(deftest setf.6
  (let ((*x* 0))
    (declare (special *x*))
    (setf *x* 1))
  1)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest setf.7
  (macrolet
   ((%m (z) z))
   (let ((x nil))
     (values x (setf (expand-in-current-env (%m x)) t) x)))
  nil t t)

(deftest setf.8
  (macrolet
   ((%m (z) z))
   (let ((x nil))
     (values x (setf x (expand-in-current-env (%m t))) x)))
  nil t t)