]> git.eshelyaron.com Git - emacs.git/commitdiff
Add basic and low-level tests for hash tables
authorStefan Kangas <stefankangas@gmail.com>
Sun, 23 Mar 2025 23:56:14 +0000 (00:56 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 25 Mar 2025 18:17:44 +0000 (19:17 +0100)
* 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

index e4bb42ff8aea602b478835b83a7667fa2068c285..8e540d982dda203c8dfe1488fe2368cbd5bb31ef 100644 (file)
   (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)))