comparison tests/automated/hash-table-tests.el @ 5191:71ee43b8a74d

Add #'equalp as a hash test by default; add #'define-hash-table-test, GNU API tests/ChangeLog addition: 2010-04-05 Aidan Kehoe <kehoea@parhasard.net> * automated/hash-table-tests.el: Test the new built-in #'equalp hash table test. Test #'define-hash-table-test. * automated/lisp-tests.el: When asserting that two objects are #'equalp, also assert that their #'equalp-hash is identical. man/ChangeLog addition: 2010-04-03 Aidan Kehoe <kehoea@parhasard.net> * lispref/hash-tables.texi (Introduction to Hash Tables): Document that we now support #'equalp as a hash table test by default, and mention #'define-hash-table-test. (Working With Hash Tables): Document #'define-hash-table-test. src/ChangeLog addition: 2010-04-05 Aidan Kehoe <kehoea@parhasard.net> * elhash.h: * elhash.c (struct Hash_Table_Test, lisp_object_eql_equal) (lisp_object_eql_hash, lisp_object_equal_equal) (lisp_object_equal_hash, lisp_object_equalp_hash) (lisp_object_equalp_equal, lisp_object_general_hash) (lisp_object_general_equal, Feq_hash, Feql_hash, Fequal_hash) (Fequalp_hash, define_hash_table_test, Fdefine_hash_table_test) (init_elhash_once_early, mark_hash_table_tests, string_equalp_hash): * glyphs.c (vars_of_glyphs): Add a new hash table test in C, #'equalp. Make it possible to specify new hash table tests with functions define_hash_table_test, #'define-hash-table-test. Use define_hash_table_test() in glyphs.c. Expose the hash functions (besides that used for #'equal) to Lisp, for people writing functions to be used with #'define-hash-table-test. Call define_hash_table_test() very early in temacs, to create the built-in hash table tests. * ui-gtk.c (emacs_gtk_boxed_hash): * specifier.h (struct specifier_methods): * specifier.c (specifier_hash): * rangetab.c (range_table_entry_hash, range_table_hash): * number.c (bignum_hash, ratio_hash, bigfloat_hash): * marker.c (marker_hash): * lrecord.h (struct lrecord_implementation): * keymap.c (keymap_hash): * gui.c (gui_item_id_hash, gui_item_hash): * glyphs.c (image_instance_hash, glyph_hash): * glyphs-x.c (x_image_instance_hash): * glyphs-msw.c (mswindows_image_instance_hash): * glyphs-gtk.c (gtk_image_instance_hash): * frame-msw.c (mswindows_set_title_from_ibyte): * fontcolor.c (color_instance_hash, font_instance_hash): * fontcolor-x.c (x_color_instance_hash): * fontcolor-tty.c (tty_color_instance_hash): * fontcolor-msw.c (mswindows_color_instance_hash): * fontcolor-gtk.c (gtk_color_instance_hash): * fns.c (bit_vector_hash): * floatfns.c (float_hash): * faces.c (face_hash): * extents.c (extent_hash): * events.c (event_hash): * data.c (weak_list_hash, weak_box_hash): * chartab.c (char_table_entry_hash, char_table_hash): * bytecode.c (compiled_function_hash): * alloc.c (vector_hash): Change the various object hash methods to take a new EQUALP parameter, hashing appropriately for #'equalp if it is true.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 05 Apr 2010 13:03:35 +0100
parents 0f66906b6e37
children 308d34e9f07d
comparison
equal deleted inserted replaced
5190:1c1d8843de5e 5191:71ee43b8a74d
35 (when (and (boundp 'load-file-name) (stringp load-file-name)) 35 (when (and (boundp 'load-file-name) (stringp load-file-name))
36 (push (file-name-directory load-file-name) load-path) 36 (push (file-name-directory load-file-name) load-path)
37 (require 'test-harness)))) 37 (require 'test-harness))))
38 38
39 ;; Test all combinations of make-hash-table keywords 39 ;; Test all combinations of make-hash-table keywords
40 (dolist (test '(eq eql equal)) 40 (dolist (test '(eq eql equal equalp))
41 (dolist (size '(0 1 100)) 41 (dolist (size '(0 1 100))
42 (dolist (rehash-size '(1.1 9.9)) 42 (dolist (rehash-size '(1.1 9.9))
43 (dolist (rehash-threshold '(0.2 .9)) 43 (dolist (rehash-threshold '(0.2 .9))
44 (dolist (weakness '(nil key value key-or-value key-and-value)) 44 (dolist (weakness '(nil key value key-or-value key-and-value))
45 (dolist (data '(() (1 2) (1 2 3 4))) 45 (dolist (data '(() (1 2) (1 2 3 4)))
192 (puthash (+ two 0.0) t ht) 192 (puthash (+ two 0.0) t ht)
193 (puthash (cons 1 2) t ht) 193 (puthash (cons 1 2) t ht)
194 (puthash (cons 3 4) t ht)) 194 (puthash (cons 3 4) t ht))
195 (Assert (eq (hashtable-test-function ht) 'equal)) 195 (Assert (eq (hashtable-test-function ht) 'equal))
196 (Assert (eq (hash-table-test ht) 'equal)) 196 (Assert (eq (hash-table-test ht) 'equal))
197 (Assert (= 4 (hash-table-count ht)))
198 (Assert (eq t (gethash 1.0 ht)))
199 (Assert (eq t (gethash '(1 . 2) ht)))
200 (check-copy ht)
201 )
202
203 (let ((ht (make-hash-table :size 100 :rehash-threshold .6 :test 'equalp)))
204 (dotimes (j iterations)
205 (puthash (+ one 0.0) t ht)
206 (puthash 1 t ht)
207 (puthash (+ two 0.0) t ht)
208 (puthash 2 t ht)
209 (puthash (cons 1.0 2.0) (gensym) ht)
210 ;; Override the previous entry.
211 (puthash (cons 1 2) t ht)
212 (puthash (cons 3.0 4.0) (gensym) ht)
213 (puthash (cons 3 4) t ht))
214 (Assert (eq (hashtable-test-function ht) 'equalp))
215 (Assert (eq (hash-table-test ht) 'equalp))
197 (Assert (= 4 (hash-table-count ht))) 216 (Assert (= 4 (hash-table-count ht)))
198 (Assert (eq t (gethash 1.0 ht))) 217 (Assert (eq t (gethash 1.0 ht)))
199 (Assert (eq t (gethash '(1 . 2) ht))) 218 (Assert (eq t (gethash '(1 . 2) ht)))
200 (check-copy ht) 219 (check-copy ht)
201 ) 220 )
246 (maphash #'(lambda (k v) (incf k-sum k) (incf v-sum v)) ht) 265 (maphash #'(lambda (k v) (incf k-sum k) (incf v-sum v)) ht)
247 (Assert (= (* 50 49) k-sum)) 266 (Assert (= (* 50 49) k-sum))
248 (Assert (= v-sum k-sum)))) 267 (Assert (= v-sum k-sum))))
249 268
250 ;;; Test reading and printing of hash-table objects 269 ;;; Test reading and printing of hash-table objects
251 (let ((h1 #s(hashtable weakness t rehash-size 3.0 rehash-threshold .2 test eq data (1 2 3 4))) 270 (let ((h1 #s(hashtable :weakness t :rehash-size 3.0 :rehash-threshold .2 :test eq :data (1 2 3 4)))
252 (h2 #s(hash-table weakness t rehash-size 3.0 rehash-threshold .2 test eq data (1 2 3 4))) 271 (h2 #s(hash-table :weakness t :rehash-size 3.0 :rehash-threshold .2 :test eq :data (1 2 3 4)))
253 (h3 (make-hash-table :weakness t :rehash-size 3.0 :rehash-threshold .2 :test 'eq))) 272 (h3 (make-hash-table :weakness t :rehash-size 3.0 :rehash-threshold .2 :test 'eq)))
254 (Assert (equal h1 h2)) 273 (Assert (equal h1 h2))
255 (Assert (not (equal h1 h3))) 274 (Assert (not (equal h1 h3)))
256 (puthash 1 2 h3) 275 (puthash 1 2 h3)
257 (puthash 3 4 h3) 276 (puthash 3 4 h3)
280 299
281 ;;; Test sxhash 300 ;;; Test sxhash
282 (Assert (= (sxhash "foo") (sxhash "foo"))) 301 (Assert (= (sxhash "foo") (sxhash "foo")))
283 (Assert (= (sxhash '(1 2 3)) (sxhash '(1 2 3)))) 302 (Assert (= (sxhash '(1 2 3)) (sxhash '(1 2 3))))
284 (Assert (/= (sxhash '(1 2 3)) (sxhash '(3 2 1)))) 303 (Assert (/= (sxhash '(1 2 3)) (sxhash '(3 2 1))))
304
305 ;; Test #'define-hash-table-test.
306
307 (defstruct hash-table-test-structure
308 number-identifier padding-zero padding-one padding-two)
309
310 (macrolet
311 ((good-hash () 65599)
312 (hash-modulo-figure ()
313 (if (featurep 'bignum)
314 (1+ (* most-positive-fixnum 2))
315 most-positive-fixnum))
316 (hash-table-test-structure-first-hash-figure ()
317 (rem* (* 65599 (eq-hash 'hash-table-test-structure))
318 (if (featurep 'bignum)
319 (1+ (* most-positive-fixnum 2))
320 most-positive-fixnum))))
321 (let ((hash-table-test (gensym))
322 (no-entry-found (gensym))
323 (two 2.0)
324 (equal-function
325 #'(lambda (object-one object-two)
326 (or (equal object-one object-two)
327 (and (hash-table-test-structure-p object-one)
328 (hash-table-test-structure-p object-two)
329 (= (hash-table-test-structure-number-identifier
330 object-one)
331 (hash-table-test-structure-number-identifier
332 object-two))))))
333 (hash-function
334 #'(lambda (object)
335 (if (hash-table-test-structure-p object)
336 (rem* (+ (hash-table-test-structure-first-hash-figure)
337 (equalp-hash
338 (hash-table-test-structure-number-identifier
339 object)))
340 (hash-modulo-figure))
341 (equal-hash object))))
342 hash-table-test-hash equal-hash)
343 (Check-Error wrong-type-argument (define-hash-table-test
344 "hi there everyone"
345 equal-function hash-function))
346 (Check-Error wrong-number-of-arguments (define-hash-table-test
347 (gensym)
348 hash-function hash-function))
349 (Check-Error wrong-number-of-arguments (define-hash-table-test
350 (gensym)
351 equal-function equal-function))
352 (define-hash-table-test hash-table-test equal-function hash-function)
353 (Assert (valid-hash-table-test-p hash-table-test))
354 (setq equal-hash (make-hash-table :test #'equal)
355 hash-table-test-hash (make-hash-table :test hash-table-test))
356 (Assert (hash-table-p equal-hash))
357 (Assert (hash-table-p hash-table-test-hash))
358 (Assert (eq hash-table-test (hash-table-test hash-table-test-hash)))
359 (loop
360 for ii from 200 below 300
361 with structure = nil
362 do
363 (setf structure (make-hash-table-test-structure
364 :number-identifier (if (oddp ii) (float (% ii 10))
365 (% ii 10))
366 :padding-zero (random)
367 :padding-one (random)
368 :padding-two (random))
369 (gethash structure hash-table-test-hash) t
370 (gethash structure equal-hash) t))
371 (Assert (= (hash-table-count hash-table-test-hash) 10))
372 (Assert (= (hash-table-count equal-hash) 100))
373 (Assert (eq t (gethash (make-hash-table-test-structure
374 :number-identifier 1
375 :padding-zero (random)
376 :padding-one (random)
377 :padding-two (random))
378 hash-table-test-hash)))
379 (Assert (eq t (gethash (make-hash-table-test-structure
380 :number-identifier 2.0
381 :padding-zero (random)
382 :padding-one (random)
383 :padding-two (random))
384 hash-table-test-hash)))
385 (Assert (eq no-entry-found (gethash (make-hash-table-test-structure
386 :number-identifier (+ two 0.0)
387 :padding-zero (random)
388 :padding-one (random)
389 :padding-two (random))
390 equal-hash
391 no-entry-found)))))