]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow equal user-defined hash table tests with different names
authorMattias EngdegÄrd <mattiase@acm.org>
Wed, 31 Jan 2024 11:21:12 +0000 (12:21 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 31 Jan 2024 20:17:01 +0000 (21:17 +0100)
Hash tables using different user-defined tests defined identically
sometimes ended up using the wrong test (bug#68668).

* src/fns.c (get_hash_table_user_test): Take test name into account
when matching the test object.
* test/src/fns-tests.el (fns--define-hash-table-test): New.

(cherry picked from commit 7e85311a9113a4720ec9d7b06188646fc7bdae0b)

src/fns.c
test/src/fns-tests.el

index e4fa8157000bd00b378086d32cbf3d500ff8edc9..1262e3e749ecd323e51b7eee95d5b1ceb857a3ef 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -5374,6 +5374,8 @@ mark_fns (void)
     }
 }
 
+/* Find the hash_table_test object correponding to the (bare) symbol TEST,
+   creating one if none existed.  */
 static struct hash_table_test *
 get_hash_table_user_test (Lisp_Object test)
 {
@@ -5384,7 +5386,8 @@ get_hash_table_user_test (Lisp_Object test)
   Lisp_Object equal_fn = XCAR (prop);
   Lisp_Object hash_fn = XCAR (XCDR (prop));
   struct hash_table_user_test *ut = hash_table_user_tests;
-  while (ut && !(EQ (equal_fn, ut->test.user_cmp_function)
+  while (ut && !(BASE_EQ (test, ut->test.name)
+                && EQ (equal_fn, ut->test.user_cmp_function)
                 && EQ (hash_fn, ut->test.user_hash_function)))
     ut = ut->next;
   if (!ut)
index 3893b8b03207b17e26e24cb3dfe119b1d3972f53..7437c07f1561f142b72685e73f62099ad2fc4681 100644 (file)
   (should (= (sxhash-equal (record 'a (make-string 10 ?a)))
             (sxhash-equal (record 'a (make-string 10 ?a))))))
 
+(ert-deftest fns--define-hash-table-test ()
+  ;; Check that we can have two differently-named tests using the
+  ;; same functions (bug#68668).
+  (define-hash-table-test 'fns-tests--1 'my-cmp 'my-hash)
+  (define-hash-table-test 'fns-tests--2 'my-cmp 'my-hash)
+  (let ((h1 (make-hash-table :test 'fns-tests--1))
+        (h2 (make-hash-table :test 'fns-tests--2)))
+    (should (eq (hash-table-test h1) 'fns-tests--1))
+    (should (eq (hash-table-test h2) 'fns-tests--2))))
+
 (ert-deftest test-secure-hash ()
   (should (equal (secure-hash 'md5    "foobar")
                  "3858f62230ac3c915f300c664312c63f"))