weakptr.tst 2.38 KB
Newer Older
Pascal Bourguignon's avatar
Pascal Bourguignon committed
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
;; -*- Lisp -*-

#+ALLEGRO
(progn
  (defun make-weak-pointer (v)
    (make-array 1 :weak t :initial-contents (list v)))
  (defun weak-pointer-value (wv)
    (let ((v (aref wv 0))) (values v (not (null v)))))
  (defun set-weak-pointer-value (wv v) (setf (aref wv 0) v))
  (defsetf weak-pointer-value set-weak-pointer-value)
  t)
#+ALLEGRO
T

#+LISPWORKS
(progn
  (defun gc () (mark-and-sweep 3))
  (defun make-weak-pointer (v)
    (let ((wp (make-array 1)))
      (setf (aref wp 0) v)
      (set-array-weak wp t)
      wp))
  (defun weak-pointer-value (wv)
    (let ((v (aref wv 0))) (values v (not (null v)))))
  (defun set-weak-pointer-value (wv v) (setf (aref wv 0) v))
  (defsetf weak-pointer-value set-weak-pointer-value)
  t)
#+LISPWORKS
T

(defmacro weakptr-test (&body body)
  `(progn (make-list 100) ,@body (make-array 200)
          (list (eq co (weak-pointer-value wp))
                (multiple-value-list (weak-pointer-value wp))
                (multiple-value-bind (v p) (weak-pointer-value wpp)
                  (list #+(or ALLEGRO LISPWORKS) (if (arrayp v) 'WEAK-POINTER (type-of v))
                        #-(or ALLEGRO LISPWORKS) (type-of v)
                        p)))))
weakptr-test

(weakptr-test (setq co (cons 1 2) wp (make-weak-pointer co)
                    wpp (make-weak-pointer wp)))
(T ((1 . 2) T) (WEAK-POINTER T))

(weakptr-test (gc))
(T ((1 . 2) T) (WEAK-POINTER T))

(weakptr-test (setq co nil) (gc))
(T (NIL NIL) (WEAK-POINTER T))

(weakptr-test (setq co (cons 1 2) wp (make-weak-pointer 1)))
(NIL (1 T) (WEAK-POINTER T))

(weakptr-test (setf (weak-pointer-value wp) co) (gc))
(T ((1 . 2) T) (NULL NIL))

(weakptr-test (setf (weak-pointer-value wp) 2 (weak-pointer-value wpp) co)
              (gc))
(NIL (2 T) (CONS T))

(weakptr-test (setf (weak-pointer-value wp) co (weak-pointer-value wpp) wp)
              (gc))
(T ((1 . 2) T) (WEAK-POINTER T))

(weakptr-test (setf (weak-pointer-value wp) 3 co nil) (gc))
(NIL (3 T) (WEAK-POINTER T))

(weakptr-test (setf (weak-pointer-value wp) (cons 1 2)) (gc))
(T (NIL NIL) (WEAK-POINTER T))

(let ((*print-circle* t))
  (setf (weak-pointer-value wp) wpp)
  (prin1-to-string wp))
#+CLISP "#1=#<WEAK-POINTER #<WEAK-POINTER #1#>>"
#+CMU "#1=#<Weak Pointer: #<Weak Pointer: #1#>>"
#+LISPWORKS "#1=#(#(#1#))"
#-(or CLISP CMU LISPWORKS) UNKNOWN

(progn (makunbound 'co) (makunbound 'wp) (makunbound 'wpp) (gc)
       (fmakunbound 'weakptr-test))
weakptr-test