test-hash-tables.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;;; Copyright (C) 2023 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. (test-call "#t" (lambda () (hashtable? (make-eq-hashtable))))
  23. (test-call "#t" (lambda () (hashtable? (make-eq-hashtable 42))))
  24. ;; Unimplemented:
  25. ;; - make-eqv-hashtable
  26. ;; - make-hashtable
  27. (test-call "#f" (lambda () (hashtable? 42)))
  28. (test-call "b" (lambda ()
  29. (let ((ht (make-eq-hashtable)))
  30. (hashtable-ref ht 'a 'b))))
  31. (test-call "" ; zero values
  32. (lambda ()
  33. (let ((ht (make-eq-hashtable)))
  34. (hashtable-set! ht 'a 'b))))
  35. (test-call "b" (lambda ()
  36. (let ((ht (make-eq-hashtable)))
  37. (hashtable-set! ht 'a 'b)
  38. (hashtable-ref ht 'a #f))))
  39. (test-call "#f" (lambda ()
  40. (let ((ht (make-eq-hashtable)))
  41. (hashtable-set! ht 'x 'y)
  42. (hashtable-ref ht 'a #f))))
  43. (test-call "b" (lambda ()
  44. (let ((ht (make-eq-hashtable)))
  45. (hashtable-set! ht 'a 'b)
  46. (hashtable-ref ht 'a 'b))))
  47. (test-call "0" (lambda ()
  48. (hashtable-size (make-eq-hashtable))))
  49. (test-call "1" (lambda ()
  50. (let ((ht (make-eq-hashtable)))
  51. (hashtable-set! ht 'a 'b)
  52. (hashtable-size ht))))
  53. (test-call "2" (lambda ()
  54. (let ((ht (make-eq-hashtable)))
  55. (hashtable-set! ht 'a 'b)
  56. (hashtable-set! ht 'c 'd)
  57. (hashtable-size ht))))
  58. (test-call "(#f 0)" (lambda ()
  59. (let ((ht (make-eq-hashtable)))
  60. (hashtable-set! ht 'a 'b)
  61. (hashtable-delete! ht 'a)
  62. (list (hashtable-ref ht 'a #f)
  63. (hashtable-size ht)))))
  64. (test-call "(b 1)" (lambda ()
  65. (let ((ht (make-eq-hashtable)))
  66. (hashtable-set! ht 'a 'b)
  67. (hashtable-delete! ht 'c)
  68. (list (hashtable-ref ht 'a #f)
  69. (hashtable-size ht)))))
  70. (test-call "#f" (lambda ()
  71. (let ((ht (make-eq-hashtable)))
  72. (hashtable-contains? ht 'a))))
  73. (test-call "#t" (lambda ()
  74. (let ((ht (make-eq-hashtable)))
  75. (hashtable-set! ht 'a 'b)
  76. (hashtable-contains? ht 'a))))
  77. (test-call "1" (lambda ()
  78. (let ((ht (make-eq-hashtable)))
  79. (hashtable-update! ht 'a 1+ 0)
  80. (hashtable-ref ht 'a #f))))
  81. (test-call "2" (lambda ()
  82. (let ((ht (make-eq-hashtable)))
  83. (hashtable-set! ht 'a 1)
  84. (hashtable-update! ht 'a 1+ 0)
  85. (hashtable-ref ht 'a #f))))
  86. (test-call "(2 b d)"
  87. (lambda ()
  88. (let ((ht (make-eq-hashtable)))
  89. (hashtable-set! ht 'a 'b)
  90. (hashtable-set! ht 'c 'd)
  91. (let ((ht* (hashtable-copy ht)))
  92. (list (hashtable-size ht*)
  93. (hashtable-ref ht* 'a #f)
  94. (hashtable-ref ht* 'c #f))))))
  95. (test-call "0" (lambda ()
  96. (let ((ht (make-eq-hashtable)))
  97. (hashtable-set! ht 'a 'b)
  98. (hashtable-clear! ht)
  99. (hashtable-size ht))))
  100. (test-call "#()" (lambda ()
  101. (hashtable-keys (make-eq-hashtable))))
  102. (test-call "#(a)" (lambda ()
  103. (let ((ht (make-eq-hashtable)))
  104. (hashtable-set! ht 'a 'b)
  105. (hashtable-keys ht))))
  106. (test-call "#()" (lambda ()
  107. (hashtable-entries (make-eq-hashtable))))
  108. (test-call "#(b)" (lambda ()
  109. (let ((ht (make-eq-hashtable)))
  110. (hashtable-set! ht 'a 'b)
  111. (hashtable-entries ht))))
  112. (test-call "#t" (lambda ()
  113. (eq? eq? (hashtable-equivalence-function
  114. (make-eq-hashtable)))))
  115. (test-call "#t" (lambda ()
  116. (eq? %hashq (hashtable-hash-function
  117. (make-eq-hashtable)))))
  118. (test-call "#t" (lambda ()
  119. (hashtable-mutable? (make-eq-hashtable))))
  120. ;; Unimplemented:
  121. ;; - equal-hash
  122. ;; - string-hash
  123. ;; - string-ci-hash
  124. ;; - symbol-hash
  125. ;; Extensions:
  126. (test-call "(a b)"
  127. (lambda ()
  128. (let ((ht (make-eq-hashtable))
  129. (lst '()))
  130. (hashtable-set! ht 'a 'b)
  131. (hashtable-for-each
  132. (lambda (k v)
  133. (set! lst (cons k (cons v lst))))
  134. ht)
  135. lst)))
  136. ;; Weak key hashtables
  137. (test-call "42"
  138. (lambda ()
  139. (let ((table (make-weak-key-hashtable)))
  140. (weak-key-hashtable-set! table 'foo 42)
  141. (weak-key-hashtable-ref table 'foo))))
  142. (test-call "uh-oh"
  143. (lambda ()
  144. (let ((table (make-weak-key-hashtable)))
  145. (weak-key-hashtable-ref table 'foo 'uh-oh))))
  146. (test-call "#f"
  147. (lambda ()
  148. (let ((table (make-weak-key-hashtable)))
  149. (weak-key-hashtable-set! table 'foo 42)
  150. (weak-key-hashtable-delete! table 'foo)
  151. (weak-key-hashtable-ref table 'foo))))
  152. (test-end* "test-hash-tables")