defclass-01.lsp 20.2 KB
Newer Older
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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Apr 20 20:58:54 2003
;;;; Contains: Tests for DEFCLASS, part 01


(in-package :cl-test)

;;; I've decided to write some 'manual' tests, then refactor these back
;;; to the automatic mechanisms I'll put into defclass-aux.lsp after
;;; I have a better understanding of the object system

(defclass class-01 () (s1 s2 s3))

(deftest class-01.1
  (notnot-mv (typep (make-instance 'class-01) 'class-01))
  t)

(deftest class-01.2
  (notnot-mv (typep (make-instance (find-class 'class-01)) 'class-01))
  t)

(deftest class-01.3
  (let ((c (make-instance 'class-01)))
    (values
     (setf (slot-value c 's1) 12)
     (setf (slot-value c 's2) 18)
     (setf (slot-value c 's3) 27)
     (loop for s in '(s1 s2 s3) collect (slot-value c s))))
  12 18 27
  (12 18 27))

;;;;

(defclass class-02 () ((s1) (s2) (s3)))
sds's avatar
sds committed
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
(deftest class-02.1
  (notnot-mv (typep (make-instance 'class-02) 'class-02))
  t)

(deftest class-02.2
  (notnot-mv (typep (make-instance (find-class 'class-02)) 'class-02))
  t)

(deftest class-02.3
  (let ((c (make-instance 'class-02)))
    (values
     (setf (slot-value c 's1) 12)
     (setf (slot-value c 's2) 18)
     (setf (slot-value c 's3) 27)
     (loop for s in '(s1 s2 s3) collect (slot-value c s))))
  12 18 27
  (12 18 27))

;;;;

(defclass class-03 () ((s1 :type integer) (s2 :type t) (s3 :type fixnum)))
sds's avatar
sds committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
(deftest class-03.1
  (notnot-mv (typep (make-instance 'class-03) 'class-03))
  t)

(deftest class-03.2
  (notnot-mv (typep (make-instance (find-class 'class-03)) 'class-03))
  t)

(deftest class-03.3
  (let ((c (make-instance 'class-03)))
    (values
     (setf (slot-value c 's1) 12)
     (setf (slot-value c 's2) 'a)
     (setf (slot-value c 's3) 27)
     (loop for s in '(s1 s2 s3) collect (slot-value c s))))
  12 a 27
  (12 a 27))

;;;;

79 80
(defclass class-04 ()
  ((s1 :reader s1-r) (s2 :writer s2-w) (s3 :accessor s3-a)))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

;;; Readers, writers, and accessors
(deftest class-04.1
  (let ((c (make-instance 'class-04)))
    (values
     (setf (slot-value c 's1) 'a)
     (setf (slot-value c 's2) 'b)
     (setf (slot-value c 's3) 'c)
     (s1-r c)
     (slot-value c 's2)
     (s2-w 'd c)
     (slot-value c 's2)
     (s3-a c)
     (setf (s3-a c) 'e)
     (slot-value c 's3)
     (s3-a c)))
  a b c a b d d c e e e)

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
(deftest class-04.2
  (notnot-mv (typep #'s1-r 'generic-function))
  t)

(deftest class-04.3
  (notnot-mv (typep #'s2-w 'generic-function))
  t)

(deftest class-04.4
  (notnot-mv (typep #'s3-a 'generic-function))
  t)

(deftest class-04.5
  (notnot-mv (typep #'(setf s3-a) 'generic-function))
  t)

115 116 117
;;;;

(defclass class-05 () (s1 (s2 :allocation :instance) (s3 :allocation :class)))
sds's avatar
sds committed
118

119 120
(deftest class-05.1
  (let ((c1 (make-instance 'class-05))
sds's avatar
sds committed
121
        (c2 (make-instance 'class-05)))
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 164 165 166 167 168 169 170 171 172 173 174
    (values
     (not (eql c1 c2))
     (list
      (setf (slot-value c1 's1) 12)
      (setf (slot-value c2 's1) 17)
      (slot-value c1 's1)
      (slot-value c2 's1))
     (list
      (setf (slot-value c1 's2) 'a)
      (setf (slot-value c2 's2) 'b)
      (slot-value c1 's2)
      (slot-value c2 's2))
     (list
      (setf (slot-value c1 's3) 'x)
      (slot-value c1 's3)
      (slot-value c2 's3)
      (setf (slot-value c2 's3) 'y)
      (slot-value c1 's3)
      (slot-value c2 's3)
      (setf (slot-value c1 's3) 'z)
      (slot-value c1 's3)
      (slot-value c2 's3))
     (slot-value (make-instance 'class-05) 's3)))
  t
  (12 17 12 17)
  (a b a b)
  (x x x y y y z z z)
  z)

;;;;

(defclass class-06 () ((s1 :reader s1-r1 :reader s1-r2 :writer s1-w1 :writer s1-w2)))
(defclass class-06a () ((s1 :reader s1-r1) s3))

(deftest class-06.1
  (let ((c (make-instance 'class-06)))
    (values
     (setf (slot-value c 's1) 'x)
     (slot-value c 's1)
     (s1-r1 c)
     (s1-r2 c)
     (s1-w1 'y c)
     (slot-value c 's1)
     (s1-r1 c)
     (s1-r2 c)
     (s1-w2 'z c)
     (slot-value c 's1)
     (s1-r1 c)
     (s1-r2 c)))
  x x x x y y y y z z z z)

(deftest class-06.2
  (let ((c1 (make-instance 'class-06))
sds's avatar
sds committed
175
        (c2 (make-instance 'class-06a)))
176 177 178 179 180 181 182 183
    (values
     (setf (slot-value c1 's1) 'x)
     (setf (slot-value c2 's1) 'y)
     (mapcar #'s1-r1 (list c1 c2))))
  x y (x y))

;;;;

pfdietz's avatar
pfdietz committed
184
(defclass class-07 () ((s1 :initarg :s1a :initarg :s1b :reader s1)
sds's avatar
sds committed
185
                       (s2 :initarg :s2 :reader s2)))
186

pfdietz's avatar
pfdietz committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
(deftest class-07.1
  (let ((c (make-instance 'class-07)))
    (values
     (slot-boundp c 's1)
     (slot-boundp c 's2)))
  nil nil)

(deftest class-07.2
  (let ((c (make-instance 'class-07 :s1a 'x)))
    (values
     (notnot (slot-boundp c 's1))
     (s1 c)
     (slot-boundp c 's2)))
  t x nil)

(deftest class-07.3
  (let ((c (make-instance 'class-07 :s1b 'x)))
    (values
     (notnot (slot-boundp c 's1))
     (s1 c)
     (slot-boundp c 's2)))
  t x nil)

(deftest class-07.4
  (let ((c (make-instance 'class-07 :s1a 'y :s1b 'x)))
    (values
     (notnot (slot-boundp c 's1))
     (s1 c)
     (slot-boundp c 's2)))
  t y nil)


(deftest class-07.5
  (let ((c (make-instance 'class-07 :s1b 'y :s1a 'x)))
    (values
     (notnot (slot-boundp c 's1))
     (s1 c)
     (slot-boundp c 's2)))
  t y nil)

(deftest class-07.6
  (let ((c (make-instance 'class-07 :s1a 'y :s1a 'x)))
    (values
     (notnot (slot-boundp c 's1))
     (s1 c)
     (slot-boundp c 's2)))
  t y nil)

(deftest class-07.7
  (let ((c (make-instance 'class-07 :s2 'a :s1a 'b)))
    (values
     (notnot (slot-boundp c 's1))
     (notnot (slot-boundp c 's2))
     (s1 c)
     (s2 c)))
  t t b a)

(deftest class-07.8
  (let ((c (make-instance 'class-07 :s2 'a :s1a 'b :s2 'x :s1a 'y :s1b 'z)))
    (values
     (notnot (slot-boundp c 's1))
     (notnot (slot-boundp c 's2))
     (s1 c)
     (s2 c)))
  t t b a)

253 254 255 256 257 258 259 260
(deftest class-07.9
  (let ((c (make-instance 'class-07 :s1b 'x :s1a 'y)))
    (values
     (notnot (slot-boundp c 's1))
     (slot-boundp c 's2)
     (s1 c)))
  t nil x)

261 262 263 264 265 266 267
(deftest class-07.10
  (let ((c (make-instance 'class-07 :s1a 'x :s2 'y :allow-other-keys nil)))
    (values (s1 c) (s2 c)))
  x y)

(deftest class-07.11
  (let ((c (make-instance 'class-07 :s1a 'a :s2 'b :garbage 'z
sds's avatar
sds committed
268
                          :allow-other-keys t)))
269 270 271
    (values (s1 c) (s2 c)))
  a b)

pfdietz's avatar
pfdietz committed
272 273
(deftest class-07.12
  (let ((c (make-instance 'class-07 :s1a 'd :s2 'c :garbage 'z
sds's avatar
sds committed
274 275
                          :allow-other-keys t
                          :allow-other-keys nil)))
pfdietz's avatar
pfdietz committed
276 277 278
    (values (s1 c) (s2 c)))
  d c)

279

pfdietz's avatar
pfdietz committed
280 281 282 283 284 285 286 287 288
;;;;

(declaim (special *class-08-s2-initvar*))

(defclass class-08 ()
  ((s1 :initform 0) (s2 :initform *class-08-s2-initvar*)))

(deftest class-08.1
  (let* ((*class-08-s2-initvar* 'x)
sds's avatar
sds committed
289
         (c (make-instance 'class-08)))
pfdietz's avatar
pfdietz committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    (values
     (slot-value c 's1)
     (slot-value c 's2)))
  0 x)

;;;;

(declaim (special *class-09-s2-initvar*))

(defclass class-09 ()
  ((s1 :initform 0 :initarg :s1)
   (s2 :initform *class-09-s2-initvar* :initarg :s2)))

(deftest class-09.1
  (let* ((*class-09-s2-initvar* 'x)
sds's avatar
sds committed
305
         (c (make-instance 'class-09)))
pfdietz's avatar
pfdietz committed
306 307 308 309 310 311 312
    (values
     (slot-value c 's1)
     (slot-value c 's2)))
  0 x)

(deftest class-09.2
  (let* ((*class-09-s2-initvar* 'x)
sds's avatar
sds committed
313
         (c (make-instance 'class-09 :s1 1)))
pfdietz's avatar
pfdietz committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    (values
     (slot-value c 's1)
     (slot-value c 's2)))
  1 x)

(deftest class-09.3
  (let* ((c (make-instance 'class-09 :s2 'a)))
    (values
     (slot-value c 's1)
     (slot-value c 's2)))
  0 a)

(deftest class-09.4
  (let* ((c (make-instance 'class-09 :s2 'a :s1 10 :s1 'bad :s2 'bad)))
    (values
     (slot-value c 's1)
     (slot-value c 's2)))
  10 a)

;;;;

(declaim (special *class-10-s1-initvar*))

(defclass class-10 ()
  ((s1 :initform (incf *class-10-s1-initvar*) :initarg :s1)))

(deftest class-10.1
  (let* ((*class-10-s1-initvar* 0)
sds's avatar
sds committed
342
         (c (make-instance 'class-10)))
pfdietz's avatar
pfdietz committed
343 344 345 346 347 348 349
    (values
     *class-10-s1-initvar*
     (slot-value c 's1)))
  1 1)

(deftest class-10.2
  (let* ((*class-10-s1-initvar* 0)
sds's avatar
sds committed
350
         (c (make-instance 'class-10 :s1 10)))
pfdietz's avatar
pfdietz committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    (values
     *class-10-s1-initvar*
     (slot-value c 's1)))
  0 10)

;;;;

(let ((x 7))
  (defclass class-11 ()
    ((s1 :initform x :initarg :s1))))

(deftest class-11.1
  (slot-value (make-instance 'class-11) 's1)
  7)

(deftest class-11.2
  (slot-value (make-instance 'class-11 :s1 100) 's1)
  100)

;;;

(flet ((%f () 'x))
  (defclass class-12 ()
    ((s1 :initform (%f) :initarg :s1))))

(deftest class-12.1
  (slot-value (make-instance 'class-12) 's1)
  x)

(deftest class-12.2
  (slot-value (make-instance 'class-12 :s1 'y) 's1)
  y)
383

384 385 386 387 388 389 390
;;;

(defclass class-13 ()
  ((s1 :allocation :class :initarg :s1)))

(deftest class-13.1
  (let ((c1 (make-instance 'class-13))
sds's avatar
sds committed
391
        (c2 (make-instance 'class-13 :s1 'foo)))
392 393
    (values
     (slot-value c1 's1)
pfdietz's avatar
pfdietz committed
394
     (slot-value c2 's1)))
395 396 397 398 399 400 401 402 403 404 405
  foo foo)

;;;

(defclass class-14 ()
  ((s1 :initarg nil :reader s1)))

(deftest class-14.1
  (let ((c (make-instance 'class-14 nil 'x)))
    (s1 c))
  x)
406

407 408 409 410 411
;;;

(defclass class-15 ()
  ((s1 :initarg :allow-other-keys :reader s1)))

412 413 414 415 416 417 418 419 420
;;; Dicussion on comp.lang.lisp convinced me this test was bogus.
;;; The default value of :allow-other-keys specified in 7.1.2 is not
;;; the same as the default value forms, specified by :default-initargs,
;;; that are used to produce the defaulted initialization argument list.

;;; (deftest class-15.1
;;;  (let ((c (make-instance 'class-15)))
;;;    (s1 c))
;;;  nil)
421 422 423 424 425 426 427 428 429 430 431 432 433

(deftest class-15.2
  (let ((c (make-instance 'class-15 :allow-other-keys nil)))
    (s1 c))
  nil)

(deftest class-15.3
  (let ((c (make-instance 'class-15 :allow-other-keys t)))
    (s1 c))
  t)

(deftest class-15.4
  (let ((c (make-instance 'class-15 :allow-other-keys t
sds's avatar
sds committed
434
                          :allow-other-keys nil)))
435 436 437 438 439
    (s1 c))
  t)

(deftest class-15.5
  (let ((c (make-instance 'class-15 :allow-other-keys nil
sds's avatar
sds committed
440
                          :allow-other-keys t)))
441 442 443 444 445
    (s1 c))
  nil)

(deftest class-15.6
  (let ((c (make-instance 'class-15 :allow-other-keys t
sds's avatar
sds committed
446
                          :foo 'bar)))
447 448 449 450 451
    (s1 c))
  t)

(deftest class-15.7
  (let ((c (make-instance 'class-15 :allow-other-keys t
sds's avatar
sds committed
452 453
                          :allow-other-keys nil
                          :foo 'bar)))
454 455 456
    (s1 c))
  t)

457
;;; Tests of :default-initargs
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
(defclass class-16 ()
  ((s1 :initarg :s1))
  (:default-initargs :s1 'x))

(deftest class-16.1
  (let ((c (make-instance 'class-16)))
    (slot-value c 's1))
  x)

(deftest class-16.2
  (let ((c (make-instance 'class-16 :s1 'y)))
    (slot-value c 's1))
  y)

(deftest class-16.3
  (let ((c (make-instance 'class-16 :s1 nil)))
    (slot-value c 's1))
  nil)

;;;

(defclass class-17 ()
  ((s1 :initarg :s1 :initform 'foo))
  (:default-initargs :s1 'bar))

(deftest class-17.1
  (let ((c (make-instance 'class-17)))
    (slot-value c 's1))
  bar)

(deftest class-17.2
  (let ((c (make-instance 'class-17 :s1 'z)))
    (slot-value c 's1))
  z)

(deftest class-17.3
  (let ((c (make-instance 'class-17 :s1 nil)))
    (slot-value c 's1))
  nil)

;;;

(defclass class-18 ()
  ((s1 :initarg :s1 :initarg :s1b))
  (:default-initargs :s1 'x :s1b 'y))

(deftest class-18.1
  (let ((c (make-instance 'class-18)))
    (slot-value c 's1))
  x)

(deftest class-18.2
  (let ((c (make-instance 'class-18 :s1 'z)))
    (slot-value c 's1))
  z)

(deftest class-18.3
  (let ((c (make-instance 'class-18 :s1 nil)))
    (slot-value c 's1))
  nil)

(deftest class-18.4
  (let ((c (make-instance 'class-18 :s1b 'z)))
    (slot-value c 's1))
  z)

(deftest class-18.5
  (let ((c (make-instance 'class-18 :s1b nil)))
    (slot-value c 's1))
  nil)

;;;

(declaim (special *class-19-s1-initvar*))

(defclass class-19 ()
  ((s1 :initarg :s1))
  (:default-initargs :s1 (setf *class-19-s1-initvar* 'a)))

(deftest class-19.1
  (let* ((*class-19-s1-initvar* nil)
sds's avatar
sds committed
540
         (c (make-instance 'class-19)))
541 542 543 544 545 546 547 548
    (declare (special *class-19-s1-initvar*))
    (values
     (slot-value c 's1)
     *class-19-s1-initvar*))
  a a)

(deftest class-19.2
  (let* ((*class-19-s1-initvar* nil)
sds's avatar
sds committed
549
         (c (make-instance 'class-19 :s1 nil)))
550 551 552 553 554 555 556 557
    (declare (special *class-19-s1-initvar*))
    (values
     (slot-value c 's1)
     *class-19-s1-initvar*))
  nil nil)

(deftest class-19.3
  (let* ((*class-19-s1-initvar* nil)
sds's avatar
sds committed
558
         (c (make-instance 'class-19 :s1 'x)))
559 560 561 562 563 564 565 566 567 568 569 570 571
    (declare (special *class-19-s1-initvar*))
    (values
     (slot-value c 's1)
     *class-19-s1-initvar*))
  x nil)

;;;

(declaim (special *class-20-s1-initvar-1* *class-20-s1-initvar-2*))

(defclass class-20 ()
  ((s1 :initarg :s1 :initarg :s1b))
  (:default-initargs :s1 (setf *class-20-s1-initvar-1* 'a)
sds's avatar
sds committed
572
                     :s1b (setf *class-20-s1-initvar-2* 'b)))
573 574 575

(deftest class-20.1
  (let* (*class-20-s1-initvar-1*
sds's avatar
sds committed
576 577
         *class-20-s1-initvar-2*
         (c (make-instance 'class-20)))
578
    (declare (special *class-20-s1-initvar-1*
sds's avatar
sds committed
579
                      *class-20-s1-initvar-2*))
580 581 582 583 584 585 586 587
    (values
     (slot-value c 's1)
     *class-20-s1-initvar-1*
     *class-20-s1-initvar-2*))
  a a b)

(deftest class-20.2
  (let* (*class-20-s1-initvar-1*
sds's avatar
sds committed
588 589
         *class-20-s1-initvar-2*
         (c (make-instance 'class-20 :s1 'x)))
590
    (declare (special *class-20-s1-initvar-1*
sds's avatar
sds committed
591
                      *class-20-s1-initvar-2*))
592 593 594 595 596 597 598 599
    (values
     (slot-value c 's1)
     *class-20-s1-initvar-1*
     *class-20-s1-initvar-2*))
  x nil b)

(deftest class-20.3
  (let* (*class-20-s1-initvar-1*
sds's avatar
sds committed
600 601
         *class-20-s1-initvar-2*
         (c (make-instance 'class-20 :s1b 'y)))
602
    (declare (special *class-20-s1-initvar-1*
sds's avatar
sds committed
603
                      *class-20-s1-initvar-2*))
604 605 606 607 608 609 610 611 612 613
    (values
     (slot-value c 's1)
     *class-20-s1-initvar-1*
     *class-20-s1-initvar-2*))
  y a nil)

;;;

(declaim (special *class-21-s1-initvar-1* *class-21-s1-initvar-2*))

pfdietz's avatar
pfdietz committed
614 615 616 617 618 619
(let ((*class-21-s1-initvar-1* 0)
      (*class-21-s1-initvar-2* 0))
  (defclass class-21 ()
    ((s1 :initarg :s1  :initarg :s1b)
     (s2 :initarg :s1b :initarg :s2))
    (:default-initargs :s1  (incf *class-21-s1-initvar-1*)
sds's avatar
sds committed
620
                       :s1b (incf *class-21-s1-initvar-2*))))
621 622 623

(deftest class-21.1
  (let* ((*class-21-s1-initvar-1* 10)
sds's avatar
sds committed
624 625
         (*class-21-s1-initvar-2* 20)
         (c (make-instance 'class-21)))
626
    (declare (special *class-21-s1-initvar-1*
sds's avatar
sds committed
627
                      *class-21-s1-initvar-2*))
628 629 630 631 632 633 634
    (values
     (slot-value c 's1)
     (slot-value c 's2)
     *class-21-s1-initvar-1*
     *class-21-s1-initvar-2*))
  11 21 11 21)

pfdietz's avatar
pfdietz committed
635 636
(deftest class-21.2
  (let* ((*class-21-s1-initvar-1* 10)
sds's avatar
sds committed
637 638
         (*class-21-s1-initvar-2* 20)
         (c (make-instance 'class-21 :s1 'x)))
pfdietz's avatar
pfdietz committed
639
    (declare (special *class-21-s1-initvar-1*
sds's avatar
sds committed
640
                      *class-21-s1-initvar-2*))
pfdietz's avatar
pfdietz committed
641 642 643 644 645 646
    (values
     (slot-value c 's1)
     (slot-value c 's2)
     *class-21-s1-initvar-1*
     *class-21-s1-initvar-2*))
  x 21 10 21)
647

pfdietz's avatar
pfdietz committed
648 649
(deftest class-21.3
  (let* ((*class-21-s1-initvar-1* 10)
sds's avatar
sds committed
650 651
         (*class-21-s1-initvar-2* 20)
         (c (make-instance 'class-21 :s1 'x :s1b 'y)))
pfdietz's avatar
pfdietz committed
652
    (declare (special *class-21-s1-initvar-1*
sds's avatar
sds committed
653
                      *class-21-s1-initvar-2*))
pfdietz's avatar
pfdietz committed
654 655 656 657 658 659
    (values
     (slot-value c 's1)
     (slot-value c 's2)
     *class-21-s1-initvar-1*
     *class-21-s1-initvar-2*))
  x y 10 20)
660

pfdietz's avatar
pfdietz committed
661 662
(deftest class-21.4
  (let* ((*class-21-s1-initvar-1* 10)
sds's avatar
sds committed
663 664
         (*class-21-s1-initvar-2* 20)
         (c (make-instance 'class-21 :s1b 'y)))
pfdietz's avatar
pfdietz committed
665
    (declare (special *class-21-s1-initvar-1*
sds's avatar
sds committed
666
                      *class-21-s1-initvar-2*))
pfdietz's avatar
pfdietz committed
667 668 669 670 671 672
    (values
     (slot-value c 's1)
     (slot-value c 's2)
     *class-21-s1-initvar-1*
     *class-21-s1-initvar-2*))
  y y 11 20)
673

pfdietz's avatar
pfdietz committed
674 675
(deftest class-21.5
  (let* ((*class-21-s1-initvar-1* 10)
sds's avatar
sds committed
676 677
         (*class-21-s1-initvar-2* 20)
         (c (make-instance 'class-21 :s2 'y)))
pfdietz's avatar
pfdietz committed
678
    (declare (special *class-21-s1-initvar-1*
sds's avatar
sds committed
679
                      *class-21-s1-initvar-2*))
pfdietz's avatar
pfdietz committed
680 681 682 683 684 685
    (values
     (slot-value c 's1)
     (slot-value c 's2)
     *class-21-s1-initvar-1*
     *class-21-s1-initvar-2*))
  11 y 11 21)
pfdietz's avatar
pfdietz committed
686 687 688 689 690 691 692 693 694 695 696 697

;;; Documentation strings

(defclass class-22 ()
  ((s1 :documentation "This is slot s1 in class class-22")))

(deftest class-22.1
  (notnot-mv (typep (make-instance 'class-22) 'class-22))
  t)

;;; We can't portably get at the docstring of slots

698 699
;;;

pfdietz's avatar
pfdietz committed
700 701 702 703 704 705 706 707 708 709 710
(defclass class-23 ()
  (s1 s2 s3)
  (:documentation "This is class-23 in ansi-tests"))

(deftest class-23.1
  (notnot-mv (typep (make-instance 'class-23) 'class-23))
  t)

(deftest class-23.2
  (let ((doc (documentation 'class-23 'type)))
    (or (null doc)
sds's avatar
sds committed
711
        (equalt doc "This is class-23 in ansi-tests")))
pfdietz's avatar
pfdietz committed
712 713 714 715 716
  t)

(deftest class-23.3
  (let ((doc (documentation (find-class 'class-23) 'type)))
    (or (null doc)
sds's avatar
sds committed
717
        (equalt doc "This is class-23 in ansi-tests")))
pfdietz's avatar
pfdietz committed
718 719 720 721 722
  t)

(deftest class-23.4
  (let ((doc (documentation (find-class 'class-23) t)))
    (or (null doc)
sds's avatar
sds committed
723
        (equalt doc "This is class-23 in ansi-tests")))
pfdietz's avatar
pfdietz committed
724
  t)
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

;;;

(defclass class-24 ()
  ((s1 :initarg :allow-other-keys :reader s1))
  (:default-initargs :allow-other-keys t))

(deftest class-24.1
  (s1 (make-instance 'class-24))
  t)

(deftest class-24.2
  (s1 (make-instance 'class-24 :nonsense t))
  t)

(deftest class-24.3
  (s1 (make-instance 'class-24 :allow-other-keys nil))
  nil)

(deftest class-24.4
  (s1 (make-instance 'class-24 :allow-other-keys 'a :foo t))
  a)

;;;

(defclass class-25 ()
  ((s1 :initarg :allow-other-keys :reader s1))
  (:default-initargs :allow-other-keys nil))

(deftest class-25.1
  (s1 (make-instance 'class-25))
  nil)

(deftest class-25.2
  (s1 (make-instance 'class-25 :allow-other-keys t))
  t)

(deftest class-25.3
  (s1 (make-instance 'class-25 :allow-other-keys t :foo nil))
  t)

(deftest class-25.4
  (s1 (make-instance 'class-25 :allow-other-keys t :allow-other-keys nil))
  t)

(deftest class-25.5
  (s1 (make-instance 'class-25 :allow-other-keys t :allow-other-keys nil
sds's avatar
sds committed
772
                     :foo t))
773 774 775 776 777 778
  t)

(deftest class-25.6
  (s1 (make-instance 'class-25 :allow-other-keys 'foo :allow-other-keys 'bar))
  foo)

779 780 781 782 783 784 785 786 787 788 789 790 791
;;;

(defclass class-26 ()
  ((s1-26 :writer (setf s1-26))))

(deftest class-26.1
  (let ((c (make-instance 'class-26)))
    (values
     (slot-boundp c 's1-26)
     (setf (s1-26 c) 'x)
     (slot-value c 's1-26)
     (typep* #'(setf s1-26) 'generic-function)))
  nil x x t)
792

793
;;;
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
(defclass class-27 ()
  (a (b :initform 10) (c :initarg :c) (d :initarg :d))
  (:metaclass standard-class)
  (:default-initargs :d 17))

(deftest class-27.1
  (let ((class (find-class 'class-27)))
    (values
     (subtypep* 'class-27 'standard-object)
     (subtypep* 'class-27 t)
     (subtypep* 'class-27 (find-class 'standard-object))
     (subtypep* 'class-27 (find-class t))
     (subtypep* class 'standard-object)
     (subtypep* class t)
     (subtypep* class (find-class 'standard-object))
     (subtypep* class (find-class t))))
  t t t t t t t t)

(deftest class-27.2
  (let ((c (make-instance 'class-27)))
    (values
     (slot-boundp* c 'a)
     (slot-value c 'b)
     (slot-boundp* c 'c)
     (slot-value c 'd)))
  nil 10 nil 17)

(deftest class-27.3
  (let ((c (make-instance 'class-27 :c 26 :d 43)))
    (values
     (slot-boundp* c 'a)
     (slot-value c 'b)
     (slot-value c 'c)
     (slot-value c 'd)))
  nil 10 26 43)
830 831 832 833

;;;

(declaim (special *class-28-reset-fn*
sds's avatar
sds committed
834
                  *class-28-query-fn*))
835

836 837
(declaim (type function *class-28-reset-fn* *class-28-query-fn*))

838 839
(let ((x 0) (y 0))
  (flet ((%reset (a b) (setf x a y b))
sds's avatar
sds committed
840
         (%query () (list x y)))
841
    (setf *class-28-reset-fn* #'%reset
sds's avatar
sds committed
842
          *class-28-query-fn* #'%query)
843 844 845 846 847 848 849 850 851 852 853 854
    (defclass class-28 ()
      ((s1 :initform (incf x) :initarg :s1)
       (s2 :initarg :s2))
      (:default-initargs :s2 (incf y)))))

(deftest class-28.1
  (let ((class (find-class 'class-28)))
    (funcall *class-28-reset-fn* 5 10)
    (list
     (funcall *class-28-query-fn*)
     (let ((obj (make-instance 'class-28)))
       (list
sds's avatar
sds committed
855 856 857 858 859
        (typep* obj 'class-28)
        (typep* obj class)
        (eqt (class-of obj) class)
        (map-slot-value obj '(s1 s2))
        (funcall *class-28-query-fn*)))))
860 861 862 863 864 865 866 867 868 869
  ((5 10)
   (t t t (6 11) (6 11))))

(deftest class-28.2
  (let ((class (find-class 'class-28)))
    (funcall *class-28-reset-fn* 5 10)
    (list
     (funcall *class-28-query-fn*)
     (let ((obj (make-instance 'class-28 :s1 17)))
       (list
sds's avatar
sds committed
870 871 872 873 874
        (typep* obj 'class-28)
        (typep* obj class)
        (eqt (class-of obj) class)
        (map-slot-value obj '(s1 s2))
        (funcall *class-28-query-fn*)))))
875 876 877 878 879 880 881 882 883 884 885
  ((5 10)
   (t t t (17 11) (5 11))))


(deftest class-28.3
  (let ((class (find-class 'class-28)))
    (funcall *class-28-reset-fn* 5 10)
    (list
     (funcall *class-28-query-fn*)
     (let ((obj (make-instance 'class-28 :s2 17)))
       (list
sds's avatar
sds committed
886 887 888 889 890
        (typep* obj 'class-28)
        (typep* obj class)
        (eqt (class-of obj) class)
        (map-slot-value obj '(s1 s2))
        (funcall *class-28-query-fn*)))))
891 892 893 894
  ((5 10)
   (t t t (6 17) (6 10))))


sds's avatar
sds committed
895 896