* tests/guardians.test: New test file.
[bpt/guile.git] / test-suite / tests / guardians.test
1 ;;;; guardians.test --- test suite for Guile Guardians -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- July 1999
3 ;;;;
4 ;;;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;;; any later version.
10 ;;;;
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 ;;;; Boston, MA 02111-1307 USA
20
21 ;;; These tests make some questionable assumptions.
22 ;;; - They assume that a GC will find all dead objects, so they
23 ;;; will become flaky if we have a generational GC.
24 ;;; - They assume that objects won't be saved by the guardian until
25 ;;; they explicitly invoke GC --- in other words, they assume that GC
26 ;;; won't happen too often.
27
28 (gc)
29
30 (define g1 (make-guardian))
31 (define not-g1-garbage (list 'not-g1-garbage))
32 (g1 not-g1-garbage)
33 (g1 (list 'g1-garbage))
34 (pass-if "g1-garbage not collected yet" (equal? (g1) #f))
35 (gc)
36 (pass-if "g1-garbage saved" (equal? (g1) '(g1-garbage)))
37
38 ;;; Who guards the guardian?
39 (gc)
40 (define g2 (make-guardian))
41 (g2 (list 'g2-garbage))
42 (define g3 (make-guardian))
43 (g3 (list 'g3-garbage))
44 (g3 g2)
45 (pass-if "g2-garbage not collected yet" (equal? (g2) #f))
46 (pass-if "g3-garbage not collected yet" (equal? (g3) #f))
47 (set! g2 #f)
48 (gc)
49 (let ((seen-g3-garbage #f)
50 (seen-g2 #f)
51 (seen-something-else #f))
52 (let loop ()
53 (let ((saved (g3)))
54 (if saved
55 (begin
56 (cond
57 ((equal? saved '(g3-garbage)) (set! seen-g3-garbage #t))
58 ((procedure? saved) (set! seen-g2 saved))
59 (else (set! seen-something-else #t)))
60 (loop)))))
61 (pass-if "g3-garbage saved" seen-g3-garbage)
62 (pass-if "g2-saved" seen-g2)
63 (pass-if "nothing else saved" (not seen-something-else))
64 (pass-if "g2-garbage saved" (and (procedure? seen-g2)
65 (equal? (seen-g2) '(g2-garbage)))))