gc.test 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;;; gc.test --- test guile's garbage collection -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2004, 2006, 2008 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (use-modules (ice-9 documentation)
  18. (test-suite lib))
  19. ;;;
  20. ;;; miscellaneous
  21. ;;;
  22. (define (documented? object)
  23. (not (not (object-documentation object))))
  24. ;; In guile 1.6.4 this test bombed, due to the record in h being collected
  25. ;; by the gc, but not removed from h, leaving "x" as a freed cell.
  26. ;; The usual correct result here is for x to be #f, but there's always a
  27. ;; chance gc will mark something used when it isn't, so we allow x to be a
  28. ;; record too.
  29. (pass-if "weak-values versus records"
  30. (let ((rec-type (make-record-type "foo" '()))
  31. (h (make-weak-value-hash-table 61)))
  32. (hash-set! h "foo" ((record-constructor rec-type)))
  33. (gc)
  34. (let ((x (hash-ref h "foo")))
  35. (or (not x)
  36. ((record-predicate rec-type) x)))))
  37. ;;;
  38. ;;;
  39. ;;;
  40. (with-test-prefix "gc"
  41. (pass-if "after-gc-hook gets called"
  42. (let* ((foo #f)
  43. (thunk (lambda () (set! foo #t))))
  44. (add-hook! after-gc-hook thunk)
  45. (gc)
  46. (remove-hook! after-gc-hook thunk)
  47. foo)))
  48. (with-test-prefix "gc"
  49. (pass-if "Unused modules are removed"
  50. (let*
  51. ((dummy (gc))
  52. (last-count (cdr (assoc
  53. "eval-closure" (gc-live-object-stats)))))
  54. (for-each (lambda (x) (make-module)) (iota 1000))
  55. ;; XXX: This hack aims to clean up the stack to make sure we
  56. ;; don't leave a reference to one of the modules we created. It
  57. ;; proved to be useful on SPARC:
  58. ;; http://lists.gnu.org/archive/html/guile-devel/2008-02/msg00006.html .
  59. (let cleanup ((i 10))
  60. (and (> i 0)
  61. (begin (cleanup (1- i)) i)))
  62. (gc)
  63. (gc) ;; twice: have to kill the weak vectors.
  64. (= last-count (cdr (assoc "eval-closure" (gc-live-object-stats)))))
  65. ))