]> code.delx.au - gnu-emacs/blob - test/automated/finalizer-tests.el
142152e3fb0d4865ef7ed857b49668150ba7a28a
[gnu-emacs] / test / automated / finalizer-tests.el
1 ;;; finalizer-tests.el --- Finalizer tests -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Daniel Colascione <dancol@dancol.org>
6 ;; Keywords:
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;
26
27 ;;; Code:
28
29 (require 'ert)
30 (require 'cl-lib)
31
32 (ert-deftest finalizer-basic ()
33 "Test that finalizers run at all."
34 (skip-unless gc-precise)
35 (let* ((finalized nil)
36 (finalizer (make-finalizer (lambda () (setf finalized t)))))
37 (garbage-collect)
38 (should (equal finalized nil))
39 (setf finalizer nil)
40 (garbage-collect)
41 (should (equal finalized t))))
42
43 (ert-deftest finalizer-circular-reference ()
44 "Test references from a callback to a finalizer."
45 (skip-unless gc-precise)
46 (let ((finalized nil))
47 (let* ((value nil)
48 (finalizer (make-finalizer (lambda () (setf finalized value)))))
49 (setf value finalizer)
50 (setf finalizer nil))
51 (garbage-collect)
52 (should finalized)))
53
54 (ert-deftest finalizer-cross-reference ()
55 "Test that between-finalizer references do not prevent collection."
56 (skip-unless gc-precise)
57 (let ((d nil) (fc 0))
58 (let* ((f1-data (cons nil nil))
59 (f2-data (cons nil nil))
60 (f1 (make-finalizer
61 (lambda () (cl-incf fc) (setf d f1-data))))
62 (f2 (make-finalizer
63 (lambda () (cl-incf fc) (setf d f2-data)))))
64 (setcar f1-data f2)
65 (setcar f2-data f1))
66 (garbage-collect)
67 (should (equal fc 2))))
68
69 (ert-deftest finalizer-error ()
70 "Test that finalizer errors are suppressed"
71 (skip-unless gc-precise)
72 (make-finalizer (lambda () (error "ABCDEF")))
73 (garbage-collect)
74 (with-current-buffer "*Messages*"
75 (save-excursion
76 (goto-char (point-max))
77 (forward-line -1)
78 (should (equal
79 (buffer-substring (point) (point-at-eol))
80 "finalizer failed: (error \"ABCDEF\")")))))
81
82 (ert-deftest finalizer-object-type ()
83 (should (equal (type-of (make-finalizer nil)) 'finalizer)))