From f6c32b3dba7df04fc03244edede0bc5f706c75ef Mon Sep 17 00:00:00 2001 From: Helmut Eller Date: Thu, 1 Jan 2026 18:51:39 +0100 Subject: [PATCH] Add test for weak hash tables The current implementation doesn't pass this test. The weak hash table pcase--memoize is a real world example where this matters. * test/src/fns-tests.el (ft-ephemeron-table): New test. (ft--test-ephemeron-table): New helper. --- test/src/fns-tests.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index 6079e404a5c..951b60d145a 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el @@ -1389,6 +1389,22 @@ (dolist (test '(eq eql equal)) (ft--test-weak-fixnums w test)))) +;; Emacs's weak hash tables work like ephemerons: if an object is only +;; reachable through a weak hash table, then the object should be +;; collected. +(defun ft--test-ephemeron-table (weakness) + (let* ((h (make-hash-table :weakness weakness :test 'eq)) + (n 1000)) + (dotimes (i n) + (let* ((obj (cons 'a i))) + (puthash obj obj h))) + (ft--gc weakness) + (should (< (hash-table-count h) n)))) + +(ert-deftest ft-ephemeron-table () + (dolist (w '(key value key-and-value key-or-value)) + (ft--test-ephemeron-table w))) + (ert-deftest test-hash-function-that-mutates-hash-table ()