fdes-finalizers.test 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. (define-module (test-suite test-fdes-finalizers)
  17. #:use-module (test-suite lib)
  18. #:use-module (test-suite guile-test)
  19. #:use-module (ice-9 fdes-finalizers))
  20. (define (test-file suffix)
  21. (data-file-name (string-append "ports-test.tmp" suffix)))
  22. (close-port (open-output-file (test-file ".1")))
  23. (close-port (open-output-file (test-file ".2")))
  24. (with-test-prefix "simple"
  25. (let* ((call-count 0)
  26. (f (lambda (fdes) (set! call-count (1+ call-count))))
  27. (p (open-input-file (test-file ".1")))
  28. (q (open-input-file (test-file ".2"))))
  29. (pass-if-equal 0 call-count)
  30. (add-fdes-finalizer! (fileno p) f)
  31. (pass-if-equal 0 call-count)
  32. (close-port q)
  33. (pass-if-equal 0 call-count)
  34. (close-port p)
  35. (pass-if-equal 1 call-count)))
  36. (with-test-prefix "multiple"
  37. (let* ((call-count 0)
  38. (f (lambda (fdes) (set! call-count (1+ call-count))))
  39. (p (open-input-file (test-file ".1"))))
  40. (pass-if-equal 0 call-count)
  41. (add-fdes-finalizer! (fileno p) f)
  42. (add-fdes-finalizer! (fileno p) f)
  43. (pass-if-equal 0 call-count)
  44. (close-port p)
  45. (pass-if-equal 2 call-count)))
  46. (with-test-prefix "with removal"
  47. (let* ((call-count 0)
  48. (f (lambda (fdes) (set! call-count (1+ call-count))))
  49. (p (open-input-file (test-file ".1"))))
  50. (pass-if-equal 0 call-count)
  51. (add-fdes-finalizer! (fileno p) f)
  52. (add-fdes-finalizer! (fileno p) f)
  53. (remove-fdes-finalizer! (fileno p) f)
  54. (pass-if-equal 0 call-count)
  55. (close-port p)
  56. (pass-if-equal 1 call-count)))
  57. (delete-file (test-file ".1"))
  58. (delete-file (test-file ".2"))