rotatef.lsp 8.29 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Apr 20 15:44:38 2003
;;;; Contains: Tests for ROTATEF

(in-package :cl-test)

(deftest rotatef-order.1
  (let ((x (vector 'a 'b 'c 'd 'e 'f))
sds's avatar
sds committed
10
        (i 2))
11 12 13 14 15 16 17 18 19
    (values
     (rotatef (aref x (incf i)) (aref x (incf i)))
     x i))
  nil
  #(a b c e d f)
  4)

(deftest rotatef-order.2
  (let ((x (vector 'a 'b 'c 'd 'e 'f))
sds's avatar
sds committed
20
        (i 2))
21 22 23 24 25 26 27
    (values
     (rotatef (aref x (incf i)) (aref x (incf i)) (aref x (incf i)))
     x i))
  nil
  #(a b c e f d)
  5)

pfdietz's avatar
pfdietz committed
28 29 30 31 32 33 34 35 36 37 38 39
(deftest rotatef.1
  (let ((x (vector 0 1 2)))
    (values
     (rotatef (aref x (aref x 0)) (aref x (aref x 1)) (aref x (aref x 2)))
     x))
  nil
  #(1 2 0))

(deftest rotatef.2
  (let ((x (vector 0 1 2 3 4 5 6 7 8 9)))
    (values
     (rotatef (aref x (aref x 0))
sds's avatar
sds committed
40 41 42 43 44 45 46 47 48
              (aref x (aref x 1))
              (aref x (aref x 2))
              (aref x (aref x 3))
              (aref x (aref x 4))
              (aref x (aref x 5))
              (aref x (aref x 6))
              (aref x (aref x 7))
              (aref x (aref x 8))
              (aref x (aref x 9)))
pfdietz's avatar
pfdietz committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
     x))
  nil
  #(1 2 3 4 5 6 7 8 9 0))

(deftest rotatef.3
  (rotatef)
  nil)

(deftest rotatef.4
  (let ((x 10))
    (values
     x
     (rotatef x)
     x))
  10 nil 10)

(deftest rotatef.5
  (let ((x 'a) (y 'b))
    (values x y (rotatef x y) x y))
  a b nil b a)
sds's avatar
sds committed
69

pfdietz's avatar
pfdietz committed
70

71 72 73 74 75
;;; ROTATEF is a good testbed for finding conflicts in setf expansions
;;; These tests apply rotatef to various accessors

(deftest rotatef.6
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
76 77
         (y (list 'c 'd))
         (z 'e))
78 79 80 81 82 83
    (rotatef (car x) (car y) z)
    (values x y z))
  (c b) (e d) a)

(deftest rotatef.7
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
84 85
         (y (list 'c 'd))
         (z 'e))
86 87 88 89 90 91
    (rotatef (first x) (first y) z)
    (values x y z))
  (c b) (e d) a)

(deftest rotatef.8
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
92 93
         (y (list 'c 'd))
         (z '(e)))
94 95 96 97 98 99
    (rotatef (cdr x) (cdr y) z)
    (values x y z))
  (a d) (c e) (b))

(deftest rotatef.9
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
100 101
         (y (list 'c 'd))
         (z '(e)))
102 103 104 105 106 107
    (rotatef (rest x) (rest y) z)
    (values x y z))
  (a d) (c e) (b))

(deftest rotatef.10
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
108 109
         (y (list 'c 'd))
         (z 'e))
110 111 112 113 114 115
    (rotatef (cadr x) (cadr y) z)
    (values x y z))
  (a d) (c e) b)

(deftest rotatef.11
  (let* ((x (list 'a 'b))
sds's avatar
sds committed
116 117
         (y (list 'c 'd))
         (z 'e))
118 119 120 121 122 123
    (rotatef (second x) (second y) z)
    (values x y z))
  (a d) (c e) b)

(deftest rotatef.12
  (let* ((x (list 'a 'b 'c))
sds's avatar
sds committed
124 125
         (y (list 'd 'e 'f))
         (z (list 'g)))
126 127 128 129 130 131
    (rotatef (cddr x) (cddr y) z)
    (values x y z))
  (a b f) (d e g) (c))

(deftest rotatef.13
  (let* ((x (list (list 'a)))
sds's avatar
sds committed
132 133
         (y (list (list 'c)))
         (z 'e))
134 135 136 137 138 139
    (rotatef (caar x) (caar y) z)
    (values x y z))
  ((c)) ((e)) a)

(deftest rotatef.14
  (let* ((x (list (list 'a 'b)))
sds's avatar
sds committed
140 141
         (y (list (list 'c 'd)))
         (z (list 'e)))
142 143 144 145 146 147 148 149 150
    (rotatef (cdar x) (cdar y) z)
    (values x y z))
  ((a d)) ((c e)) (b))

;;; TODO: c*r accessors with > 2 a/d
;;; TODO: third,...,tenth

(deftest rotatef.15
  (let* ((x (vector 'a 'b))
sds's avatar
sds committed
151 152
         (y (vector 'c 'd))
         (z 'e))
153 154 155 156 157 158
    (rotatef (aref x 0) (aref y 0) z)
    (values x y z))
  #(c b) #(e d) a)

(deftest rotatef.16
  (let* ((x (vector 'a 'b))
sds's avatar
sds committed
159 160
         (y (vector 'c 'd))
         (z 'e))
161 162 163 164 165 166
    (rotatef (svref x 0) (svref y 0) z)
    (values x y z))
  #(c b) #(e d) a)

(deftest rotatef.17
  (let* ((x (copy-seq #*11000))
sds's avatar
sds committed
167 168
         (y (copy-seq #*11100))
         (z 1))
169 170 171 172 173 174
    (rotatef (bit x 1) (bit y 3) z)
    (values x y z))
  #*10000 #*11110 1)

(deftest rotatef.18
  (let* ((x (copy-seq "abcde"))
sds's avatar
sds committed
175 176
         (y (copy-seq "fghij"))
         (z #\X))
177 178 179 180 181 182
    (rotatef (char x 1) (char y 2) z)
    (values x y z))
  "ahcde" "fgXij" #\b)

(deftest rotatef.21
  (let* ((x (copy-seq #*11000))
sds's avatar
sds committed
183 184
         (y (copy-seq #*11100))
         (z 1))
185 186
    (rotatef (bit x 1) (bit y 3) z)
    (values x y z))
sds's avatar
sds committed
187
  #*10000 #*11110 1)
188 189 190

(deftest rotatef.22
  (let* ((x (copy-seq "abcde"))
sds's avatar
sds committed
191 192
         (y (copy-seq "fghij"))
         (z #\X))
193 194 195 196 197 198
    (rotatef (char x 1) (char y 2) z)
    (values x y z))
  "ahcde" "fgXij" #\b)

(deftest rotatef.23
  (let* ((x (copy-seq '(a b c d e)))
sds's avatar
sds committed
199 200
         (y (copy-seq '(f g h i j)))
         (z 'k))
201 202 203 204 205 206
    (rotatef (elt x 1) (elt y 2) z)
    (values x y z))
  (a h c d e) (f g k i j) b)

(deftest rotatef.24
  (let ((x #b01010101)
sds's avatar
sds committed
207 208
        (y #b1111)
        (z 0))
209
    (rotatef (ldb (byte 4 2) x)
sds's avatar
sds committed
210 211
             (ldb (byte 4 1) y)
             z)
212 213 214 215 216 217 218
    (values x y z))
  #b01011101
  1
  #b0101)

(deftest rotatef.25
  (let* ((f1 (gensym))
sds's avatar
sds committed
219 220 221 222
         (f2 (gensym))
         (fn1 (constantly :foo))
         (fn2 (constantly :bar))
         (fn3 (constantly :zzz)))
223
    (setf (fdefinition f1) fn1
sds's avatar
sds committed
224
          (fdefinition f2) fn2)
225
    (rotatef (fdefinition f1)
sds's avatar
sds committed
226 227
             (fdefinition f2)
             fn3)
228 229 230 231 232
    (values (funcall f1) (funcall f2) (funcall fn3)))
  :bar :zzz :foo)

(deftest rotatef.26
  (let* ((a1 (make-array '(10) :fill-pointer 5))
sds's avatar
sds committed
233 234
         (a2 (make-array '(20) :fill-pointer 7))
         (z 3))
235 236 237 238 239 240
    (rotatef (fill-pointer a1) (fill-pointer a2) z)
    (values (fill-pointer a1) (fill-pointer a2) z))
  7 3 5)

(deftest rotatef.27
  (let* ((x (list 'a 'b 'c 'd))
sds's avatar
sds committed
241 242 243
         (y (list 'd 'e 'f 'g))
         (n1 1) (n2 2)
         (z 'h))
244 245 246 247 248 249 250 251
    (rotatef (nth n1 x) (nth n2 y) z)
    (values x y z))
  (a f c d)
  (d e h g)
  b)

(deftest rotatef.28
  (let* ((f1 (gensym))
sds's avatar
sds committed
252 253 254 255
         (f2 (gensym))
         (fn1 (constantly :foo))
         (fn2 (constantly :bar))
         (fn3 (constantly :zzz)))
256
    (setf (symbol-function f1) fn1
sds's avatar
sds committed
257
          (symbol-function f2) fn2)
258 259 260 261 262 263
    (rotatef (symbol-function f1) (symbol-function f2) fn3)
    (values (funcall f1) (funcall f2) (funcall fn3)))
  :bar :zzz :foo)

(deftest rotatef.29
  (let* ((s1 (gensym))
sds's avatar
sds committed
264 265
         (s2 (gensym))
         (z 1))
266
    (setf (symbol-value s1) :foo
sds's avatar
sds committed
267
          (symbol-value s2) :bar)
268
    (rotatef (symbol-value s1)
sds's avatar
sds committed
269 270
             (symbol-value s2)
             z)
271 272 273 274 275
    (values (symbol-value s1) (symbol-value s2) z))
  :bar 1 :foo)

(deftest rotatef.30
  (let* ((s1 (gensym))
sds's avatar
sds committed
276 277 278 279
         (s2 (gensym))
         (v1 (list :foo 1))
         (v2 (list :bar 2))
         (z nil))
280
    (setf (symbol-plist s1) v1
sds's avatar
sds committed
281
          (symbol-plist s2) v2)
282 283 284 285 286 287
    (rotatef (symbol-plist s1) (symbol-plist s2) z)
    (values (symbol-plist s1) (symbol-plist s2) z))
  (:bar 2) nil (:foo 1))

(deftest rotatef.31
  (let* ((x (list 'a 'b 'c 'd 'e))
sds's avatar
sds committed
288 289 290
         (y (list 'f 'g 'h 'i 'j))
         (p1 1) (p2 2) (len 3)
         (z '(10 11 12)))
291
    (rotatef (subseq x p1 (+ p1 len))
sds's avatar
sds committed
292 293
             (subseq y p2 (+ p2 len))
             z)
294 295 296 297 298 299 300
    (values x y z))
  (a h i j e)
  (f g 10 11 12)
  (b c d))

(deftest rotatef.32
  (let* ((x (gensym))
sds's avatar
sds committed
301 302 303 304 305 306
         (y (gensym))
         (k1 :foo)
         (k2 :bar)
         (v1 1)
         (v2 2)
         (z 17))
307 308 309 310 311 312 313
    (setf (get x k1) v1 (get y k2) v2)
    (rotatef (get x k1) (get y k2) z)
    (values (symbol-plist x) (symbol-plist y) z))
  (:foo 2) (:bar 17) 1)

(deftest rotatef.33
  (let* ((x nil)
sds's avatar
sds committed
314 315 316 317 318 319
         (y nil)
         (k1 :foo)
         (k2 :bar)
         (v1 1)
         (v2 2)
         (z 21))
320 321 322 323 324 325 326
    (setf (getf x k1) v1 (getf y k2) v2)
    (rotatef (getf x k1) (getf y k2) z)
    (values x y z))
  (:foo 2) (:bar 21) 1)

(deftest rotatef.34
  (let* ((ht1 (make-hash-table))
sds's avatar
sds committed
327 328 329 330
         (ht2 (make-hash-table))
         (k1 :foo) (v1 1)
         (k2 :bar) (v2 2)
         (z 3))
331
    (setf (gethash k1 ht1) v1
sds's avatar
sds committed
332
          (gethash k2 ht2) v2)
333 334 335 336 337 338
    (rotatef z (gethash k1 ht1) (gethash k2 ht2))
    (values z (gethash k1 ht1) (gethash k2 ht2)))
  1 2 3)

(deftest rotatef.35
  (let ((n1 (gensym))
sds's avatar
sds committed
339 340 341
        (n2 (gensym))
        (n3 (gensym))
        (n4 (gensym)))
342 343 344
    (eval `(defclass ,n1 () ()))
    (eval `(defclass ,n2 () ()))
    (setf (find-class n3) (find-class n1)
sds's avatar
sds committed
345
          (find-class n4) (find-class n2))
346 347
    (rotatef (find-class n3) (find-class n4))
    (values (eqlt (find-class n1) (find-class n4))
sds's avatar
sds committed
348
            (eqlt (find-class n2) (find-class n3))))
349 350
  t t)

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest rotatef.36
  (macrolet
   ((%m (z) z))
   (let ((x 1) (y 2))
     (rotatef (expand-in-current-env (%m x)) y)
     (values x y)))
  2 1)

(deftest rotatef.37
  (macrolet
   ((%m (z) z))
   (let ((x 1) (y 2))
     (rotatef x (expand-in-current-env (%m y)))
     (values x y)))
  2 1)

370 371
;;; TODO: macro-function, mask-field, row-major-aref,
;;;   logical-pathname-translations, readtable-case