test-hash-tables.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; Copyright (C) 2023, 2024 Robin Templeton
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Hash table tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-hash-tables")
  22. (with-additional-imports
  23. ((hoot hashtables))
  24. (test-call "#t" (lambda () (hashtable? (make-eq-hashtable))))
  25. ;; Unimplemented:
  26. ;; - make-eqv-hashtable
  27. ;; - make-hashtable
  28. (test-call "#f" (lambda () (hashtable? 42)))
  29. (test-call "b" (lambda ()
  30. (let ((ht (make-eq-hashtable)))
  31. (hashtable-ref ht 'a 'b))))
  32. (test-call "" ; zero values
  33. (lambda ()
  34. (let ((ht (make-eq-hashtable)))
  35. (hashtable-set! ht 'a 'b))))
  36. (test-call "b" (lambda ()
  37. (let ((ht (make-eq-hashtable)))
  38. (hashtable-set! ht 'a 'b)
  39. (hashtable-ref ht 'a #f))))
  40. (test-call "#f" (lambda ()
  41. (let ((ht (make-eq-hashtable)))
  42. (hashtable-set! ht 'x 'y)
  43. (hashtable-ref ht 'a #f))))
  44. (test-call "b" (lambda ()
  45. (let ((ht (make-eq-hashtable)))
  46. (hashtable-set! ht 'a 'b)
  47. (hashtable-ref ht 'a 'b))))
  48. (test-call "0" (lambda ()
  49. (hashtable-size (make-eq-hashtable))))
  50. (test-call "1" (lambda ()
  51. (let ((ht (make-eq-hashtable)))
  52. (hashtable-set! ht 'a 'b)
  53. (hashtable-size ht))))
  54. (test-call "2" (lambda ()
  55. (let ((ht (make-eq-hashtable)))
  56. (hashtable-set! ht 'a 'b)
  57. (hashtable-set! ht 'c 'd)
  58. (hashtable-size ht))))
  59. (test-call "(#f 0)" (lambda ()
  60. (let ((ht (make-eq-hashtable)))
  61. (hashtable-set! ht 'a 'b)
  62. (hashtable-delete! ht 'a)
  63. (list (hashtable-ref ht 'a #f)
  64. (hashtable-size ht)))))
  65. (test-call "(b 1)" (lambda ()
  66. (let ((ht (make-eq-hashtable)))
  67. (hashtable-set! ht 'a 'b)
  68. (hashtable-delete! ht 'c)
  69. (list (hashtable-ref ht 'a #f)
  70. (hashtable-size ht)))))
  71. (test-call "#f" (lambda ()
  72. (let ((ht (make-eq-hashtable)))
  73. (hashtable-contains? ht 'a))))
  74. (test-call "#t" (lambda ()
  75. (let ((ht (make-eq-hashtable)))
  76. (hashtable-set! ht 'a 'b)
  77. (hashtable-contains? ht 'a))))
  78. (test-call "1" (lambda ()
  79. (let ((ht (make-eq-hashtable)))
  80. (hashtable-update! ht 'a 1+ 0)
  81. (hashtable-ref ht 'a #f))))
  82. (test-call "2" (lambda ()
  83. (let ((ht (make-eq-hashtable)))
  84. (hashtable-set! ht 'a 1)
  85. (hashtable-update! ht 'a 1+ 0)
  86. (hashtable-ref ht 'a #f))))
  87. (test-call "(2 b d)"
  88. (lambda ()
  89. (let ((ht (make-eq-hashtable)))
  90. (hashtable-set! ht 'a 'b)
  91. (hashtable-set! ht 'c 'd)
  92. (let ((ht* (hashtable-copy ht)))
  93. (list (hashtable-size ht*)
  94. (hashtable-ref ht* 'a #f)
  95. (hashtable-ref ht* 'c #f))))))
  96. (test-call "0" (lambda ()
  97. (let ((ht (make-eq-hashtable)))
  98. (hashtable-set! ht 'a 'b)
  99. (hashtable-clear! ht)
  100. (hashtable-size ht))))
  101. (test-call "#()" (lambda ()
  102. (hashtable-keys (make-eq-hashtable))))
  103. (test-call "#(a)" (lambda ()
  104. (let ((ht (make-eq-hashtable)))
  105. (hashtable-set! ht 'a 'b)
  106. (hashtable-keys ht))))
  107. (test-call "#()" (lambda ()
  108. (hashtable-entries (make-eq-hashtable))))
  109. (test-call "#(b)" (lambda ()
  110. (let ((ht (make-eq-hashtable)))
  111. (hashtable-set! ht 'a 'b)
  112. (hashtable-entries ht))))
  113. ;; Unimplemented:
  114. ;; - equal-hash
  115. ;; - string-hash
  116. ;; - string-ci-hash
  117. ;; - symbol-hash
  118. ;; Extensions:
  119. (test-call "(a b)"
  120. (lambda ()
  121. (let ((ht (make-eq-hashtable))
  122. (lst '()))
  123. (hashtable-set! ht 'a 'b)
  124. (hashtable-for-each
  125. (lambda (k v)
  126. (set! lst (cons k (cons v lst))))
  127. ht)
  128. lst)))
  129. ;; Weak key hashtables
  130. (test-call "42"
  131. (lambda ()
  132. (let ((table (make-weak-key-hashtable)))
  133. (weak-key-hashtable-set! table 'foo 42)
  134. (weak-key-hashtable-ref table 'foo))))
  135. (test-call "uh-oh"
  136. (lambda ()
  137. (let ((table (make-weak-key-hashtable)))
  138. (weak-key-hashtable-ref table 'foo 'uh-oh))))
  139. (test-call "#f"
  140. (lambda ()
  141. (let ((table (make-weak-key-hashtable)))
  142. (weak-key-hashtable-set! table 'foo 42)
  143. (weak-key-hashtable-delete! table 'foo)
  144. (weak-key-hashtable-ref table 'foo)))))
  145. (test-end* "test-hash-tables")