test-hash.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;; Copyright (C) 2025 Igalia, S.L.
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  3. ;;; Copyright (C) 2023, 2024 Robin Templeton
  4. ;;;
  5. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  6. ;;; you may not use this file except in compliance with the License.
  7. ;;; You may obtain a copy of the License at
  8. ;;;
  9. ;;; http://www.apache.org/licenses/LICENSE-2.0
  10. ;;;
  11. ;;; Unless required by applicable law or agreed to in writing, software
  12. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  13. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ;;; See the License for the specific language governing permissions and
  15. ;;; limitations under the License.
  16. ;;; Commentary:
  17. ;;;
  18. ;;; Hashtable tests.
  19. ;;;
  20. ;;; Code:
  21. (use-modules (srfi srfi-64)
  22. (test utils))
  23. (test-begin "test-hash")
  24. (with-additional-imports ((hoot hashtables))
  25. ;; Hashing numbers
  26. (test-call "6" hashq 42 37)
  27. ;; Hashing pairs and lists.
  28. (test-call "228" hash '(a . b) 389)
  29. (test-call "94" hash '(a b) 389)
  30. ;; Deeply nested list.
  31. (test-call "69" hash '(a (b (c (d (e (f (g (h (i))))))))) 389)
  32. ;; Circular list!
  33. (test-call "65" hash
  34. (let ((x (list 'a 'b 'c)))
  35. (set-cdr! (cdr (cdr x)) x)
  36. x)
  37. 389)
  38. ;; Hash composition should not be commutative.
  39. (test-call "#f" (lambda () (= (hash '(a . b) 389) (hash '(b . a) 389))))
  40. ;; Hashing vectors of different length.
  41. (test-call "200" hash #() 389)
  42. (test-call "222" hash #(1 2 3) 389)
  43. ;; Hashing bytevectors of different length.
  44. (test-call "51" hash #vu8() 389)
  45. (test-call "155" hash #vu8(1) 389)
  46. (test-call "224" hash #vu8(1 2) 389)
  47. (test-call "294" hash #vu8(1 2 3) 389)
  48. (test-call "206" hash #vu8(1 2 3 4) 389)
  49. ;; Hashing bitvectors of different length.
  50. (test-call "173" hash #* 389)
  51. (test-call "195" hash #*1010 389)
  52. (test-call "119" hash #*01010 389)
  53. ;; Empty bytevector should have different hash than empty bitvector.
  54. (test-call "#f" (lambda () (= (hash #vu8() 389) (hash #* 389))))
  55. ;; Hashing records.
  56. (test-call "222" hash
  57. (let ()
  58. (define-record-type q (make-q a) q? (a q-a))
  59. (make-q 42))
  60. 389))
  61. (test-end* "test-hash")