GOOPS cosmetics
[bpt/guile.git] / test-suite / tests / guardians.test
index e6a4203..fc4c9d8 100644 (file)
@@ -1,35 +1,45 @@
 ;;;; guardians.test --- test suite for Guile Guardians     -*- scheme -*-
 ;;;; Jim Blandy <jimb@red-bean.com> --- July 1999
 ;;;;
-;;;;   Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc.
+;;;;   Copyright (C) 1999, 2001, 2006, 2014 Free Software Foundation, Inc.
 ;;;; 
-;;;; This program is free software; you can redistribute it and/or modify
-;;;; it under the terms of the GNU General Public License as published by
-;;;; the Free Software Foundation; either version 2, or (at your option)
-;;;; any later version.
+;;;; This library is free software; you can redistribute it and/or
+;;;; modify it under the terms of the GNU Lesser General Public
+;;;; License as published by the Free Software Foundation; either
+;;;; version 3 of the License, or (at your option) any later version.
 ;;;; 
-;;;; This program is distributed in the hope that it will be useful,
+;;;; This library is distributed in the hope that it will be useful,
 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;;;; GNU General Public License for more details.
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;;; Lesser General Public License for more details.
 ;;;; 
-;;;; You should have received a copy of the GNU General Public License
-;;;; along with this software; see the file COPYING.  If not, write to
-;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;;;; Boston, MA 02110-1301 USA
+;;;; You should have received a copy of the GNU Lesser General Public
+;;;; License along with this library; if not, write to the Free Software
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 ;;; These tests make some questionable assumptions.
+;;;
 ;;; - They assume that a GC will find all dead objects, so they
 ;;;   will become flaky if we have a generational GC.
+;;;
+;;; - More generally, when a weakly referenced object doesn't disappear as
+;;;   expected, it's hard to tell whether that's because of a guardian bug of
+;;;   because a reference to it is being held somewhere, e.g., one some part
+;;;   of the stack that hasn't been overwritten.  Thus, most tests cannot
+;;;   fail, they can just throw `unresolved'.  We try hard to clear
+;;;   references that may have been left on the stacks (see "clear refs left
+;;;   on the stack" lines).
+;;;
 ;;; - They assume that objects won't be saved by the guardian until
 ;;;   they explicitly invoke GC --- in other words, they assume that GC
 ;;;   won't happen too often.
 
-(use-modules (test-suite lib)
-            (ice-9 documentation)
-             (ice-9 weak-vector))
-
+(define-module (test-guardians)
+  :use-module (test-suite lib)
+  :use-module (ice-9 documentation)
+  :use-module (ice-9 weak-vector))
 
+\f
 ;;;
 ;;; miscellaneous
 ;;;
   (pass-if "g3-garbage saved" (or seen-g3-garbage (throw 'unresolved)))
   (pass-if "g2-saved" (or (procedure? seen-g2) (throw 'unresolved)))
   (pass-if "nothing else saved" (not seen-something-else))
+
+  ;; FIXME: The following test fails because the guardian for `g2-garbage'
+  ;; disappared from the weak-car guardian list of `g2-garbage' right before
+  ;; `g2-garbage' was finalized (in `finalize_guarded ()').  Sample session
+  ;; (compiled with `-DDEBUG_GUARDIANS'):
+  ;;
+  ;; guile> (define g (make-guardian))
+  ;; guile> (let ((g2 (make-guardian)))
+  ;;          (format #t "g2 = ~x~%" (object-address g2))
+  ;;          (g2 (string-copy "foo"))
+  ;;          (g g2))
+  ;; g2 = 81fde18
+  ;; guile> (gc)
+  ;; finalizing guarded 0x827f6a0 (1 guardians)
+  ;;   guardian for 0x827f6a0 vanished
+  ;; end of finalize (0x827f6a0)
+  ;; finalizing guarded 0x81fde18 (1 guardians)
+  ;; end of finalize (0x81fde18)
+
   (pass-if "g2-garbage saved" (or (and (procedure? seen-g2)
                                       (equal? (seen-g2)
                                               (list (string-copy
       (let ((g (make-guardian)))
        (gc)
        (g (cons #f #f))
+        (cons 'clear 'stack)  ;; clear refs left on the stack
        (if (not (eq? (g) #f))
            (throw 'unresolved)
            (begin
        (gc)
        (g (cons #f #f))
        (g (cons #t #t))
+        (cons 'clear 'stack)  ;; clear refs left on the stack
        (if (not (eq? (g) #f))
            (throw 'unresolved)
            (begin
       (let ((g (make-guardian)))
        (gc)
        (g (cons #f #f))
+        (cons 'clear 'stack)  ;; clear refs left on the stack
        (if (not (eq? (g) #f))
            (throw 'unresolved)
            (begin
        (gc)
        (let ((p (cons #f #f)))
          (g p)
-         (vector-set! v 0 p))
+         (weak-vector-set! v 0 p)
+          (set! p #f))           ;; clear refs left on the stack
        (if (not (eq? (g) #f))
            (throw 'unresolved)
            (begin
        (gc)
        (let ((p (cons #f #f)))
          (g p)
-         (vector-set! v 0 p))
+         (weak-vector-set! v 0 p)
+          (set! p #f))        ;; clear refs left on the stack
        (begin
          (gc)
          (if (not (equal? (g) (cons #f #f)))
              (throw 'unresolved)
              (begin
                (gc)
-               (or (not (vector-ref v 0))
+               (or (not (weak-vector-ref v 0))
                    (throw 'unresolved))))))))
 
   (with-test-prefix "guarding weak containers"
 
     (pass-if "element of guarded weak vector gets collected"
       (let ((g (make-guardian))
-           (v (weak-vector (cons #f #f))))
+           (v (weak-vector #f)))
+        ;; Note: We don't pass `(cons #f #f)' as an argument to `weak-vector'
+        ;; otherwise references to it are likely to be left on the stack.
+        (weak-vector-set! v 0 (cons #f #f))
+
        (g v)
        (gc)
-       (if (equal? (vector-ref v 0) (cons #f #f))
+       (if (equal? (weak-vector-ref v 0) (cons #f #f))
            (throw 'unresolved)
            #t))))
 
         (gc)
         (let ((p (cons #f #f)))
           (g p)
-          (g p))
+          (g p)
+           (set! p #f))       ;; clear refs left on the stack
         (if (not (eq? (g) #f))
             (throw 'unresolved)
             (begin
         (gc)
         (let ((p (cons #f #f)))
           (g p)
-          (h p))
+          (h p)
+           (set! p #f))         ;; clear refs left on the stack
         (if (not (eq? (g) #f))
             (throw 'unresolved)
             (begin