test-hashtables-guile.scm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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-hashtables-guile")
  24. ;; Guile legacy API
  25. (with-imports ((guile))
  26. (test-call "42"
  27. (lambda ()
  28. (let ((table (make-hash-table)))
  29. (hashq-set! table 'foo 42)
  30. (hashq-ref table 'foo))))
  31. (test-call "#f"
  32. (lambda ()
  33. (let ((table (make-hash-table)))
  34. (hash-set! table "foo" 42)
  35. (hash-remove! table "foo")
  36. (hash-ref table "foo"))))
  37. (test-call "42"
  38. (lambda ()
  39. (let ((table (make-weak-key-hash-table)))
  40. (hashq-set! table 'foo 42)
  41. (hashq-ref table 'foo))))
  42. (test-call "((baz . 3) (bar . 2) (foo . 1))"
  43. (lambda ()
  44. (let ((table (make-hash-table)))
  45. (hashq-set! table 'foo 1)
  46. (hashq-set! table 'bar 2)
  47. (hashq-set! table 'baz 3)
  48. (hash-map->list cons table))))
  49. (test-call "3"
  50. (lambda ()
  51. (let ((table (make-hash-table)))
  52. (hash-set! table "foo" 1)
  53. (hash-set! table "bar" 2)
  54. (hash-set! table "baz" 3)
  55. (hash-count (lambda (key val) #t) table))))
  56. ;; clear, fold, and for-each on an empty table should no-op because
  57. ;; we don't yet know the concrete table type.
  58. (test-call "#t"
  59. (lambda ()
  60. (let ((table (make-hash-table)))
  61. (hash-clear! table)
  62. #t)))
  63. (test-call "0"
  64. (lambda ()
  65. (let ((table (make-hash-table)))
  66. (hash-fold (lambda (key val sum)
  67. (+ sum val))
  68. 0 table))))
  69. (test-call "0"
  70. (lambda ()
  71. (let ((count 0)
  72. (table (make-hash-table)))
  73. (hash-for-each (lambda (key val)
  74. (set! count (1+ count)))
  75. table)
  76. count))))
  77. (test-end* "test-hashtables-guile")