From 570085ad0c4066bcaca15fa180ff0abd2bf99815 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 24 Mar 2025 00:56:14 +0100 Subject: [PATCH] Add basic and low-level tests for hash tables * test/src/fns-tests.el (test-hash-table) (test-hash-table-wrong-keywords, test-remhash, test-clrhash) (test-hash-table-p, test-hash-table-count, test-maphash) (test-copy-hash-table): New tests. (cherry picked from commit 5830d1fa6552ad584daa574135e704d1f1e0220a) --- test/src/fns-tests.el | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index e4bb42ff8ae..8e540d982dd 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el @@ -1133,6 +1133,77 @@ (should (not (proper-list-p (make-bool-vector 0 nil)))) (should (not (proper-list-p (make-symbol "a"))))) +(ert-deftest test-hash-table () + (let ((h (make-hash-table)) + (val "anything")) + (puthash 123 val h) + (should (eq (gethash 123 h) val))) + (let ((h (make-hash-table :test 'equal)) + (val "anything")) + (puthash '("hello" 123) val h) + (should (eq (gethash '("hello" 123) h) val)))) + +(ert-deftest test-hash-table-wrong-keywords () + (should (make-hash-table :purecopy t)) ; obsolete and ignored + (should (make-hash-table :rehash-size 123)) ; obsolete and ignored + (should (make-hash-table :rehash-threshold 123)) ; obsolete and ignored + (should-error (make-hash-table :some-random-keyword 123))) + +(ert-deftest test-remhash () + (let ((h (make-hash-table)) + (val "anything")) + (puthash 'foo val h) + (remhash 'foo h) + (should-not (gethash 'foo h)))) + +(ert-deftest test-clrhash () + (let ((h (make-hash-table))) + (puthash 'foo1 'bar1 h) + (puthash 'foo2 'bar2 h) + (puthash 'foo3 'bar3 h) + (puthash 'foo4 'bar4 h) + (clrhash h) + (should-not (gethash 'foo h)))) + +(ert-deftest test-hash-table-p () + (let ((h (make-hash-table))) + (should (hash-table-p h))) + (should-not (hash-table-p 123)) + (should-not (hash-table-p "foo")) + (should-not (hash-table-p [foo])) + (should-not (hash-table-p (list 'foo)))) + +(ert-deftest test-hash-table-count () + (let ((h (make-hash-table))) + (puthash 'foo1 'bar1 h) + (should (= (hash-table-count h) 1)) + (puthash 'foo2 'bar2 h) + (should (= (hash-table-count h) 2)) + (puthash 'foo3 'bar3 h) + (should (= (hash-table-count h) 3)) + (puthash 'foo4 'bar4 h) + (should (= (hash-table-count h) 4)) + (clrhash h) + (should (= (hash-table-count h) 0)))) + +(ert-deftest test-maphash () + (let ((h (make-hash-table)) + (sum 0)) + (puthash 'foo1 1 h) + (puthash 'foo2 22 h) + (puthash 'foo3 333 h) + (puthash 'foo4 4444 h) + (maphash (lambda (_key value) (incf sum value)) h) + (should (= sum 4800)))) + +(ert-deftest test-copy-hash-table () + (let* ((h1 (make-hash-table)) + h2) + (puthash 'foo '(bar baz) h1) + (setq h2 (copy-hash-table h1)) + (should-not (eq h1 h2)) + (should (equal (gethash 'foo h2) '(bar baz))))) + (ert-deftest test-hash-function-that-mutates-hash-table () (define-hash-table-test 'badeq 'eq 'bad-hash) (let ((h (make-hash-table :test 'badeq :size 1 :rehash-size 1))) -- 2.39.5