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
17(define-module (test-suite test-fdes-finalizers)
18  #:use-module (test-suite lib)
19  #:use-module (test-suite guile-test)
20  #:use-module (ice-9 fdes-finalizers))
21
22(define (test-file suffix)
23  (data-file-name (string-append "ports-test.tmp" suffix)))
24
25(close-port (open-output-file (test-file ".1")))
26(close-port (open-output-file (test-file ".2")))
27
28(with-test-prefix "simple"
29  (let* ((call-count 0)
30         (f (lambda (fdes) (set! call-count (1+ call-count))))
31         (p (open-input-file (test-file ".1")))
32         (q (open-input-file (test-file ".2"))))
33    (pass-if-equal 0 call-count)
34    (add-fdes-finalizer! (fileno p) f)
35    (pass-if-equal 0 call-count)
36    (close-port q)
37    (pass-if-equal 0 call-count)
38    (close-port p)
39    (pass-if-equal 1 call-count)))
40
41(with-test-prefix "multiple"
42  (let* ((call-count 0)
43         (f (lambda (fdes) (set! call-count (1+ call-count))))
44         (p (open-input-file (test-file ".1"))))
45    (pass-if-equal 0 call-count)
46    (add-fdes-finalizer! (fileno p) f)
47    (add-fdes-finalizer! (fileno p) f)
48    (pass-if-equal 0 call-count)
49    (close-port p)
50    (pass-if-equal 2 call-count)))
51
52(with-test-prefix "with removal"
53  (let* ((call-count 0)
54         (f (lambda (fdes) (set! call-count (1+ call-count))))
55         (p (open-input-file (test-file ".1"))))
56    (pass-if-equal 0 call-count)
57    (add-fdes-finalizer! (fileno p) f)
58    (add-fdes-finalizer! (fileno p) f)
59    (remove-fdes-finalizer! (fileno p) f)
60    (pass-if-equal 0 call-count)
61    (close-port p)
62    (pass-if-equal 1 call-count)))
63
64(delete-file (test-file ".1"))
65(delete-file (test-file ".2"))
66