gc.test 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;;; gc.test --- test guile's garbage collection -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2004, 2006, 2007, 2008, 2009 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 3 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* ((guard (make-guardian))
  51. (total 1000))
  52. (for-each (lambda (x) (guard (make-module))) (iota total))
  53. ;; Avoid false references to the modules on the stack.
  54. (let cleanup ((i 20))
  55. (and (> i 0)
  56. (begin (cleanup (1- i)) i)))
  57. (gc)
  58. (gc) ;; twice: have to kill the weak vectors.
  59. (gc) ;; thrice: because the test doesn't succeed with only
  60. ;; one gc round. not sure why.
  61. (= (let lp ((i 0))
  62. (if (guard)
  63. (lp (1+ i))
  64. i))
  65. total))))