gc.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;;; gc.test --- test guile's garbage collection -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2004, 2006, 2007, 2008, 2009,
  3. ;;;; 2011, 2012, 2013 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (tests gc)
  19. #:use-module (ice-9 documentation)
  20. #:use-module (test-suite lib)
  21. #:use-module ((system base compile) #:select (compile)))
  22. ;; Some of these tests verify that things are collectable. As we use a
  23. ;; third-party conservative collector, we really can't guarantee that --
  24. ;; we can try, but on some platforms, on some versions (possibly), the
  25. ;; test might fail. But we don't want that to stop the build. So,
  26. ;; instead of failing, throw 'unresolved.
  27. ;;
  28. (define (maybe-gc-flakiness result)
  29. (or result
  30. (throw 'unresolved)))
  31. ;;;
  32. ;;; miscellaneous
  33. ;;;
  34. (define (documented? object)
  35. (not (not (object-documentation object))))
  36. ;; In guile 1.6.4 this test bombed, due to the record in h being collected
  37. ;; by the gc, but not removed from h, leaving "x" as a freed cell.
  38. ;; The usual correct result here is for x to be #f, but there's always a
  39. ;; chance gc will mark something used when it isn't, so we allow x to be a
  40. ;; record too.
  41. (pass-if "weak-values versus records"
  42. (let ((rec-type (make-record-type 'foo '()))
  43. (h (make-weak-value-hash-table 61)))
  44. (hash-set! h "foo" ((record-constructor rec-type)))
  45. (gc)
  46. (let ((x (hash-ref h "foo")))
  47. (or (not x)
  48. ((record-predicate rec-type) x)))))
  49. ;;;
  50. ;;;
  51. ;;;
  52. (with-test-prefix "gc"
  53. (pass-if "after-gc-hook gets called"
  54. (let* ((foo #f)
  55. (thunk (lambda () (set! foo #t))))
  56. (add-hook! after-gc-hook thunk)
  57. (gc)
  58. (remove-hook! after-gc-hook thunk)
  59. foo))
  60. (pass-if "Unused modules are removed"
  61. (let* ((guard (make-guardian))
  62. (total 1000))
  63. (for-each (lambda (x) (guard (make-module))) (iota total))
  64. ;; Avoid false references to the modules on the stack.
  65. (clear-stale-stack-references)
  66. (gc)
  67. (gc) ;; twice: have to kill the weak vectors.
  68. (gc) ;; thrice: because the test doesn't succeed with only
  69. ;; one gc round. not sure why.
  70. (maybe-gc-flakiness
  71. (= (let lp ((i 0))
  72. (if (guard)
  73. (lp (1+ i))
  74. i))
  75. total))))
  76. (pass-if "Lexical vars are collectable"
  77. (let ((l (compile
  78. '(begin
  79. (define guardian (make-guardian))
  80. (let ((f (list 'foo)))
  81. (guardian f))
  82. ((@ (test-suite lib) clear-stale-stack-references))
  83. (gc)(gc)(gc)
  84. (guardian))
  85. ;; Prevent the optimizer from propagating f.
  86. #:opts '(#:partial-eval? #f))))
  87. (maybe-gc-flakiness (equal? l '(foo))))))